2023-01-24 17:32:46 +03:00
< ? php
namespace frontend\modules\api\controllers ;
2023-01-25 11:50:07 +03:00
use common\models\ProjectTaskCategory ;
2023-01-24 17:32:46 +03:00
use common\models\Status ;
use common\models\UseStatus ;
2023-05-04 01:18:24 +03:00
use frontend\modules\api\models\Manager ;
2023-11-10 15:55:01 +03:00
use frontend\modules\api\models\project\Project ;
2023-11-21 11:23:38 +03:00
use frontend\modules\api\models\project\ProjectStatistic ;
2023-11-10 15:55:01 +03:00
use frontend\modules\api\models\project\ProjectUser ;
2023-01-24 17:32:46 +03:00
use Yii ;
2023-11-10 15:55:01 +03:00
use yii\base\InvalidConfigException ;
2023-01-24 17:32:46 +03:00
use yii\data\ActiveDataProvider ;
2023-11-10 15:55:01 +03:00
use yii\db\ActiveRecord ;
2023-01-24 17:32:46 +03:00
use yii\helpers\ArrayHelper ;
2023-04-25 01:32:15 +03:00
use yii\web\BadRequestHttpException ;
2023-01-24 17:32:46 +03:00
use yii\web\NotFoundHttpException ;
class ProjectController extends ApiController
{
public $modelClass = 'frontend\modules\api\models\Project' ;
public $serializer = [
'class' => 'yii\rest\Serializer' ,
'collectionEnvelope' => 'projects' ,
];
public function behaviors () : array
{
return ArrayHelper :: merge ( parent :: behaviors (), [
'verbs' => [
'class' => \yii\filters\VerbFilter :: class ,
'actions' => [
'get-project' => [ 'GET' , 'OPTIONS' ],
'project-list' => [ 'GET' , 'OPTIONS' ],
'status-list' => [ 'GET' , 'OPTIONS' ],
2023-01-25 11:50:07 +03:00
'project-task-category-list' => [ 'GET' , 'OPTIONS' ],
2023-01-24 17:32:46 +03:00
'create' => [ 'POST' , 'OPTIONS' ],
2023-05-11 01:18:40 +03:00
'add-user' => [ 'POST' , 'OPTIONS' ],
2023-04-27 01:53:21 +03:00
'update' => [ 'PUT' , 'OPTIONS' ]
2023-01-24 17:32:46 +03:00
],
]
]);
}
2023-04-25 01:32:15 +03:00
/**
*
* @ OA\Get ( path = " /project/get-project " ,
* summary = " Получить данные проекта " ,
2023-05-04 00:16:30 +03:00
* description = " Метод для получения проета<br>
Статусы :< br >
10 - Закрыт < br >
19 - Работает " ,
2023-04-25 01:32:15 +03:00
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
* @ OA\Parameter (
* name = " project_id " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = null
* )
* ),
* @ OA\Parameter (
* name = " expand " ,
* in = " query " ,
2023-10-12 11:34:47 +03:00
* example = " column,mark " ,
2023-04-25 01:32:15 +03:00
* required = false ,
2023-10-12 11:34:47 +03:00
* description = " В этом параметре по необходимости передаются поля, которые нужно добавить в ответ сервера, сейчас доступно только поле <b>columns</b> и <b>mark</b>" ,
2023-04-25 01:32:15 +03:00
* @ OA\Schema (
* type = " string " ,
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает массив объектов проекта " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/Project " ),
* ),
* ),
* )
*
* @ param $project_id
2023-11-10 15:55:01 +03:00
* @ return array | ActiveRecord | null
2023-04-25 01:32:15 +03:00
*/
public function actionGetProject ( $project_id )
2023-01-24 17:32:46 +03:00
{
2023-05-04 01:18:24 +03:00
return Project :: find () -> with ( 'columns' ) -> where ([ 'id' => $project_id ]) -> one ();
2023-01-24 17:32:46 +03:00
}
2023-04-19 01:22:57 +03:00
/**
*
* @ OA\Get ( path = " /project/project-list " ,
* summary = " Список проектов " ,
2023-05-04 00:16:30 +03:00
* description = " Метод для получения списка проетов, если не передан параметр user_id, то возвращаются проеты текущего пользователя.<br>
2023-05-04 01:18:24 +03:00
Статусы :< br >
10 - Закрыт < br >
19 - Работает " ,
2023-04-19 01:22:57 +03:00
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
* @ OA\Parameter (
* name = " user_id " ,
* in = " query " ,
* required = false ,
* @ OA\Schema (
* type = " integer " ,
* default = null
* )
* ),
2023-04-25 01:32:15 +03:00
* @ OA\Parameter (
* name = " expand " ,
* in = " query " ,
2023-10-12 11:34:47 +03:00
* example = " column,mark " ,
2023-04-25 01:32:15 +03:00
* required = false ,
2023-10-12 11:34:47 +03:00
* description = " В этом параметре по необходимости передаются поля, которые нужно добавить в ответ сервера, сейчас доступно только поле <b>columns</b> и <b>mark</b>" ,
2023-04-25 01:32:15 +03:00
* @ OA\Schema (
* type = " string " ,
* )
* ),
*
2023-04-19 01:22:57 +03:00
* @ OA\Response (
* response = 200 ,
* description = " Возвращает массив объектов проекта " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectExample " ),
* ),
* ),
* )
*
* @ param $user_id
* @ return ActiveDataProvider
*/
public function actionProjectList ( $user_id = null ) : ActiveDataProvider
2023-01-24 17:32:46 +03:00
{
2023-05-04 01:18:24 +03:00
if ( ! $user_id ) {
2023-04-19 01:22:57 +03:00
$user_id = Yii :: $app -> user -> id ;
}
if ( ! empty ( $user_id )) {
$projectIdList = ProjectUser :: find () -> where ([ 'user_id' => $user_id ]) -> select ( 'project_id' ) -> column ();
2023-06-21 17:22:35 +03:00
$query = Project :: find () -> where ([ 'IN' , 'id' , $projectIdList ]) -> orWhere ([ 'owner_id' => $user_id , 'status' => Project :: STATUS_OTHER ]);
2023-01-24 17:32:46 +03:00
} else {
$query = Project :: find ();
}
return new ActiveDataProvider ([
'query' => $query ,
]);
}
public function actionStatusList () : array
{
return Status :: find ()
-> joinWith ( 'useStatuses' )
-> where ([ '`use_status`.`use`' => UseStatus :: USE_PROJECT ]) -> all ();
}
2023-01-25 11:50:07 +03:00
public function actionProjectTaskCategoryList ( $project_id ) : array
{
return ProjectTaskCategory :: find () -> where ([ 'project_id' => $project_id ]) -> all ();
}
public function actionCreateProjectTaskCategory ()
{
$projectTaskCategory = new ProjectTaskCategory ();
2023-05-04 01:18:24 +03:00
$projectTaskCategory -> attributes = \yii :: $app -> request -> post ();
2023-01-25 11:50:07 +03:00
2023-05-04 01:18:24 +03:00
if ( $projectTaskCategory -> validate ()) {
2023-01-25 11:50:07 +03:00
$projectTaskCategory -> save ( false );
return $projectTaskCategory ;
}
return $projectTaskCategory -> errors ;
}
public function actionUpdateProjectTaskCategory ()
{
$projectTaskCategory = ProjectTaskCategory :: find ()
-> where ([ 'project_id' => Yii :: $app -> request -> post ( 'project_id' )])
-> andWhere ([ 'title' => Yii :: $app -> request -> post ( 'title' )])
-> one ();
2023-05-04 01:18:24 +03:00
if ( empty ( $projectTaskCategory )) {
2023-01-25 11:50:07 +03:00
throw new NotFoundHttpException ( 'The project not found' );
}
$projectTaskCategory -> title = Yii :: $app -> request -> post ( 'new_title' );
if ( ! $projectTaskCategory -> update () && $projectTaskCategory -> hasErrors ()) {
return $projectTaskCategory -> errors ;
}
return $projectTaskCategory ;
}
2023-04-25 01:32:15 +03:00
/**
*
* @ OA\Post ( path = " /project/create " ,
* summary = " Добавить проект " ,
2023-05-04 00:16:30 +03:00
* description = " Метод для создания проекта, если не передан параметр <b>user_id</b>, то будет получен текущий пользователь<br>
Статусы :< br >
10 - Закрыт < br >
19 - Работает " ,
2023-04-25 01:32:15 +03:00
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " multipart/form-data " ,
* @ OA\Schema (
* required = { " name " , " status " },
* @ OA\Property (
* property = " name " ,
* type = " string " ,
* description = " Название проекта " ,
* ),
* @ OA\Property (
* property = " description " ,
* type = " string " ,
* description = " Описание проекта " ,
* ),
* @ OA\Property (
* property = " status " ,
* type = " integer " ,
* description = " статус " ,
* ),
* @ OA\Property (
* property = " company_id " ,
* type = " integer " ,
* description = " Компания к которой относится проект " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект Проекта " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/Project " ),
* ),
* ),
* )
*
* @ return array | Project
* @ throws BadRequestHttpException
*/
2023-01-24 17:32:46 +03:00
public function actionCreate ()
{
$project = new Project ();
2023-04-25 01:32:15 +03:00
$user_id = \Yii :: $app -> user -> id ;
if ( ! $user_id ) {
throw new BadRequestHttpException ( json_encode ([ 'Пользователь не найден' ]));
}
$project -> load ( \yii :: $app -> request -> post (), '' );
$project -> owner_id = $user_id ;
2023-01-24 17:32:46 +03:00
2023-05-04 01:18:24 +03:00
if ( $project -> validate ()) {
2023-01-24 17:32:46 +03:00
$project -> save ( false );
return $project ;
}
return $project -> errors ;
}
/**
2023-04-27 01:53:21 +03:00
*
* @ OA\PUT ( path = " /project/update " ,
* summary = " Редактировать проект " ,
2023-05-04 00:18:57 +03:00
* description = " Метод для редактирования проекта<br>
Статусы :< br >
10 - Закрыт < br >
19 - Работает " ,
2023-04-27 01:53:21 +03:00
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " application/x-www-form-urlencoded " ,
* @ OA\Schema (
* required = { " project_id " },
* @ OA\Property (
* property = " project_id " ,
* type = " integer " ,
* description = " Идентификатор проекта " ,
* ),
* @ OA\Property (
* property = " name " ,
* type = " string " ,
* description = " Название проекта " ,
* ),
* @ OA\Property (
* property = " description " ,
* type = " string " ,
* description = " Описание проекта " ,
* ),
* @ OA\Property (
* property = " status " ,
* type = " integer " ,
* description = " статус " ,
* ),
* @ OA\Property (
* property = " company_id " ,
* type = " integer " ,
* description = " Компания к которой относится проект " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект Проекта " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/Project " ),
* ),
* ),
* )
*
2023-01-24 17:32:46 +03:00
* @ throws \Throwable
2023-11-10 15:55:01 +03:00
* @ throws InvalidConfigException
2023-01-24 17:32:46 +03:00
* @ throws \yii\db\StaleObjectException
* @ throws NotFoundHttpException
*/
public function actionUpdate ()
{
2023-04-27 01:53:21 +03:00
$request = Yii :: $app -> request -> getBodyParams ();
2023-05-04 01:18:24 +03:00
if ( ! isset ( $request [ 'project_id' ]) || $request [ 'project_id' ] == null ) {
2023-04-27 01:53:21 +03:00
throw new BadRequestHttpException ( json_encode ([ 'The project ID not found' ]));
}
$project = Project :: findOne ( $request [ 'project_id' ]);
2023-05-04 01:18:24 +03:00
if ( empty ( $project )) {
2023-01-24 17:32:46 +03:00
throw new NotFoundHttpException ( 'The project not found' );
}
2023-04-27 01:53:21 +03:00
$put = array_diff ( $request , [ null , '' ]);
$project -> load ( $put , '' );
2023-01-24 17:32:46 +03:00
if ( ! $project -> update ()) {
return $project -> errors ;
}
return $project ;
}
2023-05-04 01:18:24 +03:00
/**
*
* @ OA\Get ( path = " /project/my-employee " ,
* summary = " Список Сотрудников текущего пользователя " ,
* description = " Метод для получения списка сотрудников " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект Менеджера " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ManagerEmployee " ),
* ),
* ),
* )
*
2023-11-10 15:55:01 +03:00
* @ return array | ActiveRecord
2023-05-04 01:18:24 +03:00
* @ throws BadRequestHttpException
*/
public function actionMyEmployee ()
{
$user_id = \Yii :: $app -> user -> id ;
if ( ! $user_id ) {
throw new BadRequestHttpException ( json_encode ([ 'Пользователь не найден' ]));
}
$model = Manager :: find () -> with ([ 'managerEmployees' ]) -> where ([ 'user_id' => $user_id ]) -> one ();
2023-05-11 01:18:40 +03:00
if ( ! $model ) {
2023-05-17 00:20:51 +03:00
return [];
2023-05-04 01:18:24 +03:00
}
return $model ;
}
2023-05-11 01:18:40 +03:00
/**
*
* @ OA\Post ( path = " /project/add-user " ,
* summary = " Добавить пользователя в проект " ,
* description = " Метод для добавления пользователя в проект " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " multipart/form-data " ,
* @ OA\Schema (
* required = { " user_id " , " project_id " },
* @ OA\Property (
* property = " user_id " ,
* type = " integer " ,
* description = " Идентификатор пользователя " ,
* ),
* @ OA\Property (
* property = " project_id " ,
* type = " integer " ,
* description = " Идентификатор проекта " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectUsers " ),
* ),
* ),
* )
*
* @ return array | ProjectUser
* @ throws NotFoundHttpException
*/
public function actionAddUser ()
{
$request = Yii :: $app -> request -> post ();
$project = Project :: findOne ( $request [ 'project_id' ]);
if ( empty ( $project )) {
throw new NotFoundHttpException ( 'The project not found' );
}
$model = new ProjectUser ();
$model -> load ( $request , '' );
2023-05-11 01:24:04 +03:00
if ( isset ( $model -> user -> userCard )){
2023-05-11 01:18:40 +03:00
$model -> card_id = $model -> user -> userCard -> id ;
}
if ( ! $model -> save ()){
return $model -> errors ;
}
return $model ;
}
/**
*
* @ OA\Delete ( path = " /project/del-user " ,
* summary = " Удаление пользователя из проекта " ,
* description = " Метод для Удаления пользователя из проекта " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " application/x-www-form-urlencoded " ,
* @ OA\Schema (
* required = { " project_id " , " user_id " },
* @ OA\Property (
* property = " project_id " ,
* type = " integer " ,
* description = " Идентификатор проекта " ,
* ),
* @ OA\Property (
* property = " user_id " ,
* type = " integer " ,
* description = " Идентификатор пользователя " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект проекта " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/Project " ),
* ),
* ),
* )
*
* @ return Project
2023-11-10 15:55:01 +03:00
* @ throws InvalidConfigException | NotFoundHttpException
2023-05-11 01:18:40 +03:00
*/
public function actionDelUser () : Project
{
$request = Yii :: $app -> request -> getBodyParams ();
ProjectUser :: deleteAll ([ 'project_id' => $request [ 'project_id' ], 'user_id' => $request [ 'user_id' ]]);
$project = Project :: findOne ( $request [ 'project_id' ]);
if ( empty ( $project )) {
throw new NotFoundHttpException ( 'The project not found' );
}
return $project ;
}
2023-11-21 11:23:38 +03:00
/**
*
* @ OA\Get ( path = " /project/statistic " ,
* summary = " Получить статистику проекта " ,
* description = " Метод для получения статистики проета " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
* @ OA\Parameter (
* name = " project_id " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* default = null
* )
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект статистики проекта " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectStatisticExample " ),
* ),
* ),
* )
*
* @ param $project_id
* @ return array | ActiveRecord | null
*/
public function actionStatistic ( $project_id ) : array | ActiveRecord | null
{
return ProjectStatistic :: find () -> where ([ 'id' => $project_id ]) -> one ();
}
2023-01-24 17:32:46 +03:00
}