Merge branch 'master' into update_projects_and_tasks
This commit is contained in:
@ -7,72 +7,34 @@ use common\models\ReportsTask;
|
||||
use common\models\UserCard;
|
||||
use DateTime;
|
||||
use frontend\modules\api\models\ReportSearchForm;
|
||||
use JsonException;
|
||||
use Yii;
|
||||
use yii\filters\auth\CompositeAuth;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\filters\ContentNegotiator;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\web\Response;
|
||||
|
||||
class ReportsController extends ApiController
|
||||
{
|
||||
public function init()
|
||||
{
|
||||
parent::init(); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
$parent = parent::behaviors();
|
||||
$b = [
|
||||
[
|
||||
'class' => ContentNegotiator::className(),
|
||||
'formats' => [
|
||||
'application/json' => Response::FORMAT_JSON,
|
||||
],
|
||||
],
|
||||
'authenticator' => [
|
||||
'class' => CompositeAuth::class,
|
||||
'authMethods' => [
|
||||
HttpBearerAuth::class,
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
return array_merge($parent, $b);
|
||||
}
|
||||
|
||||
public function actionIndex(): array
|
||||
public function actionIndex($user_card_id)
|
||||
{
|
||||
$reportsModel = new ReportSearchForm();
|
||||
|
||||
$params = Yii::$app->request->get();
|
||||
if(!isset($params['user_card_id'])){
|
||||
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
|
||||
if($userCard){
|
||||
$params['user_card_id'] = $userCard->id;
|
||||
}
|
||||
}
|
||||
$reportsModel->attributes = $params;
|
||||
|
||||
$reportsModel->limit = $params['limit'] ?? $reportsModel->limit;
|
||||
$reportsModel->offset = $params['offset'] ?? $reportsModel->offset;
|
||||
|
||||
if(!$reportsModel->validate()){
|
||||
return $reportsModel->errors;
|
||||
}
|
||||
return $reportsModel->byParams();
|
||||
}
|
||||
|
||||
public function actionView($id): array{
|
||||
$report = Reports::findOne($id);
|
||||
return array_merge($report->toArray(), ['tasks' => $report->_task]);
|
||||
return $reportsModel->byParams();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionFindByDate(): array
|
||||
public function actionFindByDate()//: array
|
||||
{
|
||||
$reportsModel = new ReportSearchForm();
|
||||
|
||||
@ -82,12 +44,17 @@ class ReportsController extends ApiController
|
||||
}
|
||||
|
||||
$reportsModel->attributes = $params;
|
||||
$reportsModel->byDate = true;
|
||||
$reportsModel->date = $params['date'];
|
||||
|
||||
if (!$this->checkDate($reportsModel->date)) {
|
||||
throw new BadRequestHttpException('Wrong date format');
|
||||
}
|
||||
|
||||
if(!$reportsModel->validate()){
|
||||
return $reportsModel->errors;
|
||||
}
|
||||
return $reportsModel->byParams();
|
||||
|
||||
return $reportsModel->findByDate();
|
||||
}
|
||||
|
||||
public function actionCreate()
|
||||
@ -137,7 +104,7 @@ class ReportsController extends ApiController
|
||||
}
|
||||
|
||||
if(false === ($report->delete())) {
|
||||
throw new JsonException('Report not deleted');
|
||||
throw new \RuntimeException('Report not deleted');
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -153,7 +120,7 @@ class ReportsController extends ApiController
|
||||
}
|
||||
|
||||
if(isset($params['user_card_id'])) {
|
||||
throw new JsonException('constraint by user_card_id');
|
||||
throw new \RuntimeException('constraint by user_card_id');
|
||||
}
|
||||
|
||||
$reportsModel->attributes = $params;
|
||||
@ -170,26 +137,19 @@ class ReportsController extends ApiController
|
||||
/**
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionReportsByDate($fromDate, $toDate, $user_id = null)
|
||||
public function actionReportsByDate($fromDate, $toDate, $user_card_id)
|
||||
{
|
||||
if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) {
|
||||
throw new BadRequestHttpException('Wrong date format');
|
||||
}
|
||||
|
||||
$params = Yii::$app->request->get();
|
||||
$userId = $user_id ?? Yii::$app->user->id;
|
||||
/** @var UserCard $userCard */
|
||||
$userCard = UserCard::find()->where(['id_user' => $userId])->one();
|
||||
|
||||
if (!$userCard) {
|
||||
throw new NotFoundHttpException('User not found');
|
||||
}
|
||||
|
||||
$reportsModel = new ReportSearchForm();
|
||||
$reportsModel->attributes = $params;
|
||||
$reportsModel->user_id = $userCard->id;
|
||||
$reportsModel->user_card_id = $user_card_id;
|
||||
|
||||
$reports = $reportsModel->findByDate();
|
||||
$reports = $reportsModel->reportsByDate();
|
||||
return ArrayHelper::toArray($reports , [
|
||||
'common\models\Reports' => [
|
||||
'date' => 'created_at',
|
||||
|
Reference in New Issue
Block a user