guild/frontend/modules/api/controllers/ReportsController.php

169 lines
4.7 KiB
PHP
Raw Normal View History

2021-07-05 15:30:35 +03:00
<?php
namespace frontend\modules\api\controllers;
2021-07-28 17:08:08 +03:00
use common\models\Reports;
use common\models\ReportsTask;
2021-11-30 16:54:04 +03:00
use common\models\UserCard;
2021-07-28 17:08:08 +03:00
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\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
2021-07-05 15:30:35 +03:00
2021-11-29 18:21:50 +03:00
class ReportsController extends ApiController
2021-07-05 15:30:35 +03:00
{
2021-07-28 17:08:08 +03:00
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}
2021-07-05 15:30:35 +03:00
public function behaviors()
{
2021-11-29 18:21:50 +03:00
$parent = parent::behaviors();
$b = [
2021-07-05 15:30:35 +03:00
[
2021-07-28 17:08:08 +03:00
'class' => ContentNegotiator::className(),
2021-07-05 15:30:35 +03:00
'formats' => [
2021-07-28 17:08:08 +03:00
'application/json' => Response::FORMAT_JSON,
2021-07-05 15:30:35 +03:00
],
],
2021-07-28 17:08:08 +03:00
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
2021-07-05 15:30:35 +03:00
]
];
2021-11-29 18:21:50 +03:00
return array_merge($parent, $b);
2021-07-05 15:30:35 +03:00
}
2021-07-28 17:08:08 +03:00
public function actionIndex(): array
2021-07-05 15:30:35 +03:00
{
2021-07-28 17:08:08 +03:00
$reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get();
2021-11-30 16:54:04 +03:00
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;
}
}
2021-07-28 17:08:08 +03:00
$reportsModel->attributes = $params;
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]);
}
2022-02-15 12:42:56 +03:00
/**
* @throws NotFoundHttpException
*/
public function actionFindByDate(): array
{
$reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get();
if(!isset($params['user_card_id']) or !isset($params['date'])){
throw new NotFoundHttpException('Required parameter are missing!');
}
$reportsModel->attributes = $params;
$reportsModel->byDate = true;
if(!$reportsModel->validate()){
return $reportsModel->errors;
}
return $reportsModel->byParams();
}
2021-07-28 17:08:08 +03:00
public function actionCreate()
{
2021-09-07 16:32:57 +03:00
$params = Yii::$app->request->post();
if (!isset($params['tasks'])){
throw new BadRequestHttpException('Нет параметра tasks');
}
2021-11-30 16:54:04 +03:00
if(!isset($params['user_card_id'])){
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
if($userCard){
2022-12-16 20:18:25 +03:00
$params['user_card_id'] = $userCard->id;
2021-11-30 16:54:04 +03:00
}
}
2022-12-16 20:18:25 +03:00
$reportsModel = new Reports();
$reportsModel->attributes = $params;
if(!$reportsModel->validate() || !$reportsModel->save()){
throw new BadRequestHttpException(json_encode($reportsModel->errors));
}
2021-07-28 17:08:08 +03:00
2022-12-16 20:18:25 +03:00
$tasks = $params['tasks'];
foreach ($tasks as $task) {
$reportsTask = new ReportsTask();
$reportsTask->attributes = $task;
$reportsTask->report_id = $reportsModel->id;
$reportsTask->created_at = $reportsTask->created_at ?? strtotime($reportsModel->created_at);
$reportsTask->status = $reportsTask->status ?? 1;
2021-11-30 16:54:04 +03:00
2022-12-16 20:18:25 +03:00
if(!$reportsTask->validate() || !$reportsTask->save()){
throw new BadRequestHttpException(json_encode($reportsTask->errors));
2022-12-16 18:54:13 +03:00
}
}
2021-07-28 17:08:08 +03:00
2022-12-16 20:18:25 +03:00
return array_merge($reportsModel->toArray());
2021-07-28 17:08:08 +03:00
}
public function actionDelete()
{
$id = Yii::$app->request->get('id');
$report = Reports::findOne($id);
if(null === $report) {
throw new NotFoundHttpException('Report not found');
}
if(false === ($report->delete())) {
throw new JsonException('Report not deleted');
}
return true;
}
public function actionUpdate(): array
{
$params = Yii::$app->request->get();
$reportsModel = Reports::findone($params['id']);
if(!isset($reportsModel)) {
throw new NotFoundHttpException('report not found');
}
if(isset($params['user_card_id'])) {
throw new JsonException('constraint by user_card_id');
}
$reportsModel->attributes = $params;
if(!$reportsModel->validate()){
throw new BadRequestHttpException(json_encode($reportsModel->errors));
}
$reportsModel->save();
2021-07-05 15:30:35 +03:00
2021-07-28 17:08:08 +03:00
return $reportsModel->toArray();
2021-07-05 15:30:35 +03:00
}
}