add import tasks to xlsx

This commit is contained in:
iIronside 2023-11-10 15:55:01 +03:00
parent bc4cb3b54c
commit 05271d5f17
20 changed files with 542 additions and 116 deletions

View File

@ -2,6 +2,8 @@
namespace backend\modules\task\controllers; namespace backend\modules\task\controllers;
use common\models\forms\TasksImportForm;
use common\services\ImportProjectTaskService;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use Yii; use Yii;
use backend\modules\task\models\ProjectTask; use backend\modules\task\models\ProjectTask;
@ -137,4 +139,33 @@ class TaskController extends Controller
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');
} }
public function actionImport()
{
$model = new TasksImportForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$importTaskService = new ImportProjectTaskService();
$query = ProjectTask::genQueryToImport(
(int)$model->companyId,
(int)$model->userId,
(int)$model->projectId,
(int)$model->fromDate,
(int)$model->toDate
);
$tasks = $query->all();
if (!$tasks) {
Yii::$app->session->setFlash('danger', 'Задачи не найдены!');
return Yii::$app->getResponse()->redirect(['/task/task/import']);
} else {
return $importTaskService->importTasks($tasks);
}
}
return $this->render('_form-import', [
'model' => $model,
]);
}
} }

View File

@ -0,0 +1,79 @@
<?php
use backend\modules\project\models\Project;
use common\models\forms\TasksImportForm;
use kartik\date\DatePicker;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model TasksImportForm*/
/* @var $form yii\widgets\ActiveForm */
$this->title = 'Импорт задач';
$this->params['breadcrumbs'][] = ['label' => 'Tasks', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="task-index">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'companyId')->dropDownList(\common\models\Company::find()
->select(['name', 'id'])->indexBy('id')->column(),
[
'prompt' => 'Выберите'
]
);
?>
<?= $form->field($model, 'userId')->widget(
Select2::class,
[
'data' => \common\models\UserCard::getListUserWithUserId(),
'options' => ['placeholder' => '...', 'class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
],
]
); ?>
<?= $form->field($model, 'projectId')->widget(
Select2::class,
[
'data' => Project::find()->select(['name', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...', 'class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
],
]
); ?>
<?= $form->field($model, 'fromDate')->widget(DatePicker::class,[
'type' => DatePicker::TYPE_COMPONENT_APPEND,
'pluginOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);
?>
<?= $form->field($model, 'toDate')->widget(DatePicker::class,[
'type' => DatePicker::TYPE_COMPONENT_APPEND,
'pluginOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);
?>
<div class="form-group">
<?= Html::submitButton('Импорт', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -22,6 +22,7 @@ $this->params['breadcrumbs'][] = $this->title;
<p> <p>
<?= Html::a('Создать задачу', ['create'], ['class' => 'btn btn-success']) ?> <?= Html::a('Создать задачу', ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Импорт задач', ['import'], ['class' => 'btn btn-primary']) ?>
</p> </p>
<?= GridView::widget([ <?= GridView::widget([

View File

@ -83,7 +83,7 @@ YiiAsset::register($this);
[ [
'attribute' => 'project_user_id', 'attribute' => 'project_user_id',
'value' => 'user.email' 'value' => 'user.username'
], ],
[ [

View File

@ -11,6 +11,8 @@ use Yii;
* @property int $mark_id * @property int $mark_id
* @property int $entity_type * @property int $entity_type
* @property int $entity_id * @property int $entity_id
*
* @property Mark $mark
*/ */
class MarkEntity extends \yii\db\ActiveRecord class MarkEntity extends \yii\db\ActiveRecord
{ {

View File

@ -29,6 +29,9 @@ use yii\helpers\ArrayHelper;
* @property User $user * @property User $user
* @property UserCard $card * @property UserCard $card
* @property UserCard $cardIdCreator * @property UserCard $cardIdCreator
* @property Company $company
* @property ProjectColumn $column
* @property User $executor
* @property Mark[] $mark * @property Mark[] $mark
* @property MarkEntity[] $markEntity * @property MarkEntity[] $markEntity
* @property ProjectTaskUser[] $taskUsers * @property ProjectTaskUser[] $taskUsers
@ -42,6 +45,8 @@ class ProjectTask extends ActiveRecord
const PRIORITY_MEDIUM = 1; const PRIORITY_MEDIUM = 1;
const PRIORITY_HIGH = 2; const PRIORITY_HIGH = 2;
const DAY_IN_UNIX_TIME = 86340; // 23:59:59
/** /**
* @return string[] * @return string[]
*/ */
@ -147,7 +152,7 @@ class ProjectTask extends ActiveRecord
'user_id', 'user_id',
'user' => function () { 'user' => function () {
return [ return [
"fio" => $this->user->userCard->fio ?? $this->user->id, "fio" => $this->user->userCard->fio ?? ($this->user->id ?? ''),
"avatar" => $this->user->userCard->photo ?? '', "avatar" => $this->user->userCard->photo ?? '',
]; ];
}, },
@ -164,7 +169,7 @@ class ProjectTask extends ActiveRecord
return null; return null;
}, },
'comment_count' => function () { 'comment_count' => function () {
return Comment::find()->where(['entity_id' => $this->id, 'entity_type' => 2, 'status' => Comment::STATUS_ACTIVE])->count(); return $this->getCommentCount();
}, },
'taskUsers', 'taskUsers',
'mark', 'mark',
@ -172,6 +177,14 @@ class ProjectTask extends ActiveRecord
]; ];
} }
/**
* @return bool|int|string|null
*/
public function getCommentCount()
{
return Comment::find()->where(['entity_id' => $this->id, 'entity_type' => 2, 'status' => Comment::STATUS_ACTIVE])->count();
}
/** /**
* @return string[] * @return string[]
*/ */
@ -215,6 +228,15 @@ class ProjectTask extends ActiveRecord
return $this->hasOne(Project::className(), ['id' => 'project_id']); return $this->hasOne(Project::className(), ['id' => 'project_id']);
} }
/**
* @return ActiveQuery
*/
public function getCompany(): ActiveQuery
{
return $this->hasOne(Company::class,['id' => 'company_id'])
->via('project');
}
/** /**
* @return ActiveQuery * @return ActiveQuery
*/ */
@ -275,4 +297,71 @@ class ProjectTask extends ActiveRecord
return ArrayHelper::map( return ArrayHelper::map(
self::find()->joinWith(['user', 'project'])->where(['project_id' => $task_id])->all(), 'id', 'user.username'); self::find()->joinWith(['user', 'project'])->where(['project_id' => $task_id])->all(), 'id', 'user.username');
} }
/**
* @return string
*/
public function getTaskUsersToStr(): string
{
$participants = '';
foreach ($this->taskUsers as $taskUser) {
$participants .= $taskUser->user->userCard->fio ?? $taskUser->user->username;
$participants .= ', ';
}
return $participants;
}
/**
* @return string
*/
public function getMarkTitleToStr(): string
{
$tags = '';
foreach ($this->markEntity as $markEntity) {
$tags .= $markEntity->mark->title . ', ';
}
return $tags;
}
/**
* @param int|null $companyId
* @param int|null $userId
* @param int|null $projectId
* @param int|null $fromDate
* @param int|null $toDate
* @return ActiveQuery
*/
public static function genQueryToImport(
int $companyId = null,
int $userId = null,
int $projectId = null,
int $fromDate = null,
int $toDate = null
): ActiveQuery
{
$query = ProjectTask::find();
if ($companyId) {
$query->joinWith('company')
->andWhere(['company.id' => $companyId]);
}
if ($userId) {
$query->andWhere(['project_task.user_id' => $userId]);
}
if ($projectId) {
$query->andWhere(['project_task.project_id' => $projectId]);
}
if ($fromDate) {
$query->andFilterWhere(['>=', 'project_task.created_at', date("Y-m-d H:i:s", $fromDate)]);
}
if ($toDate) {
$query->andFilterWhere(['<=', 'project_task.created_at', date("Y-m-d H:i:s", ($toDate + self::DAY_IN_UNIX_TIME))]);
}
return $query;
}
} }

View File

@ -11,8 +11,8 @@ use yii\db\ActiveQuery;
* @property int $task_id * @property int $task_id
* @property int $user_id * @property int $user_id
* *
* @property ProjectUser $projectUser
* @property ProjectTask $task * @property ProjectTask $task
* @property User $user
*/ */
class ProjectTaskUser extends \yii\db\ActiveRecord class ProjectTaskUser extends \yii\db\ActiveRecord
{ {

View File

@ -0,0 +1,44 @@
<?php
namespace common\models\forms;
use yii\base\Model;
class TasksImportForm extends Model
{
public $companyId;
public $userId;
public $projectId;
public $fromDate;
public $toDate;
/**
* @return array
*/
public function rules()
{
return [
[['companyId', 'userId', 'projectId'], 'integer'],
['fromDate', 'date', 'format' => 'php:Y-m-d', 'timestampAttribute' => 'fromDate'],
['toDate', 'date', 'format' => 'php:Y-m-d', 'timestampAttribute' => 'toDate'],
];
}
public function attributeLabels()
{
return [
'companyId' => 'ID компании',
'userId' => 'ID пользователя',
'projectId' => 'ID проекта',
'fromDate' => 'Дата начала поиска',
'toDate' => 'Дата конца поиска',
];
}
/**
* @return string
*/
public function formName(): string
{
return '';
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace common\services;
use common\models\ProjectTask;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use yii\helpers\ArrayHelper;
class ImportProjectTaskService
{
/**
* @param array $tasks
* @return string|void
*/
public function importTasks(array $tasks)
{
try {
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet
->setCellValue('A1', 'ID')
->setCellValue('B1', 'ID проекта')
->setCellValue('C1', 'Название проекта')
->setCellValue('D1', 'Задача')
->setCellValue('E1', 'Дата создания')
->setCellValue('F1', 'Дата обновления')
->setCellValue('G1', 'Дедлайн')
->setCellValue('H1', 'Описание')
->setCellValue('I1', 'Статус')
->setCellValue('J1', 'Колонка')
->setCellValue('G1', 'ID создателя задачи')
->setCellValue('K1', 'Создатель задачи')
->setCellValue('L1', 'ID исполнителя')
->setCellValue('M1', 'Приоритет')
->setCellValue('N1', 'Исполнитель')
->setCellValue('O1', 'Количество коментариев')
->setCellValue('P1', 'Участники')
->setCellValue('Q1', 'Теги')
->setCellValue('R1', 'Приоритет выполнения');
$i = 2;
/** @var ProjectTask $task */
foreach ($tasks as $task) {
$sheet
->setCellValue('A' . $i, $task->id)
->setCellValue('B' . $i, $task->project_id)
->setCellValue('C' . $i, $task->project->name)
->setCellValue('D' . $i, $task->title)
->setCellValue('E' . $i, $task->created_at)
->setCellValue('F' . $i, $task->updated_at)
->setCellValue('G' . $i, $task->dead_line)
->setCellValue('H' . $i, $task->description)
->setCellValue('I' . $i, ArrayHelper::getValue(ProjectTask::getStatus(), $task->status))
->setCellValue('J' . $i, $task->column->title)
->setCellValue('G' . $i, $task->user_id)
->setCellValue('K' . $i, $task->user->userCard->fio ?? ($task->user->username ?? 'kkk'))
->setCellValue('L' . $i, $task->executor_id)
->setCellValue('M' . $i, $task->priority)
->setCellValue('N' . $i, $task->executor->userCard->fio ?? ($task->executor->username ?? 'ggg'))
->setCellValue('O' . $i, $task->getCommentCount())
->setCellValue('P' . $i, $task->getTaskUsersToStr())
->setCellValue('Q' . $i, $task->getMarkTitleToStr())
->setCellValue('R' . $i, $task->execution_priority);
$i++;
}
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="'. urlencode('tasks.xlsx').'"');
$writer->save('php://output');
exit();
} catch (\Exception $ex) {
return $ex->getMessage();
}
}
}

View File

@ -1,57 +0,0 @@
<?php
namespace common\services;
use common\models\ProjectTask;
use common\models\ProjectTaskUser;
class TaskService
{
public static function createTask($taskParams)
{
$task = new ProjectTask();
$task->load($taskParams, '');
$task->save();
return $task;
}
public static function getTask($task_id): ?ProjectTask
{
return ProjectTask::findOne($task_id);
}
public static function getTaskList($task_id): array
{
return ProjectTask::find()->asArray()->all();
}
public static function getTaskListByProject($project_id): array
{
return ProjectTask::find()->where(['project_id' => $project_id])->orderBy('priority DESC')->all();
}
public static function getTaskListByUser($user_id): array
{
$taskIdList = ProjectTaskUser::find()->where(['user_id' => $user_id])->select('task_id')->column();
return ProjectTask::find()->where([ 'IN', 'id', $taskIdList])->orWhere(['user_id' => $user_id])->orderBy('priority DESC')->all();
}
public static function updateTask($task_params): ?ProjectTask
{
$modelTask = ProjectTask::findOne($task_params['task_id']);
if (isset($task_params['executor_id']) && $task_params['executor_id'] == 0){
$task_params['executor_id'] = null;
}
$modelTask->load($task_params, '');
$modelTask->save();
return $modelTask;
}
public static function taskExists($task_id): bool
{
return ProjectTask::find()->where(['id' => $task_id])->exists();
}
}

View File

@ -37,7 +37,9 @@
"kartik-v/yii2-mpdf": "dev-master", "kartik-v/yii2-mpdf": "dev-master",
"mihaildev/yii2-ckeditor": "*", "mihaildev/yii2-ckeditor": "*",
"developeruz/yii2-db-rbac": "*", "developeruz/yii2-db-rbac": "*",
"zircote/swagger-php": "^4.7" "zircote/swagger-php": "^4.7",
"phpoffice/phpspreadsheet": "^1.29",
"kartik-v/yii2-widget-datepicker": "dev-master"
}, },
"require-dev": { "require-dev": {
"yiisoft/yii2-debug": "~2.0.0", "yiisoft/yii2-debug": "~2.0.0",

View File

@ -2,25 +2,14 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\ProjectColumn; use common\models\ProjectColumn;
use frontend\modules\api\models\Project; use frontend\modules\api\models\project\Project;
use yii\db\ActiveRecord;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
class ProjectColumnController extends ApiController class ProjectColumnController extends ApiController
{ {
public function verbs(): array
{
return [
'get-column-list' => ['get'],
'create-column' => ['post'],
'set-priority' => ['post'],
'update-column' => ['put', 'patch'],
];
}
/** /**
* *
* @OA\Get(path="/project-column/get-column-list", * @OA\Get(path="/project-column/get-column-list",
@ -49,17 +38,17 @@ class ProjectColumnController extends ApiController
* ) * )
* *
* @param $project_id * @param $project_id
* @return array|\yii\db\ActiveRecord[] * @return array|ActiveRecord[]
* @throws BadRequestHttpException * @throws BadRequestHttpException
*/ */
public function actionGetColumnList($project_id) public function actionGetColumnList($project_id): array
{ {
$project = Project::findOne($project_id); $project = Project::findOne($project_id);
if (!$project) { if (!$project) {
throw new BadRequestHttpException(json_encode(['Проект не найден'])); throw new BadRequestHttpException(json_encode(['Проект не найден']));
} }
$columns = \frontend\modules\api\models\ProjectColumn::find()->where(['project_id' => $project_id, 'status' => ProjectColumn::STATUS_ACTIVE])->all(); $columns = \frontend\modules\api\models\project\ProjectColumn::find()->where(['project_id' => $project_id, 'status' => ProjectColumn::STATUS_ACTIVE])->all();
return $columns; return $columns;
@ -177,7 +166,7 @@ class ProjectColumnController extends ApiController
* ), * ),
* ) * )
* *
* @return array|ProjectColumn|\yii\db\ActiveRecord|null * @return array|ProjectColumn|ActiveRecord|null
* @throws BadRequestHttpException * @throws BadRequestHttpException
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
*/ */
@ -241,7 +230,7 @@ class ProjectColumnController extends ApiController
* ), * ),
* ) * )
* *
* @return array|Project|\yii\db\ActiveRecord * @return array|Project|ActiveRecord
* @throws BadRequestHttpException * @throws BadRequestHttpException
*/ */
public function actionSetPriority() public function actionSetPriority()

View File

@ -2,15 +2,16 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\ProjectTaskCategory; use common\models\ProjectTaskCategory;
use common\models\Status; use common\models\Status;
use common\models\UseStatus; use common\models\UseStatus;
use frontend\modules\api\models\Manager; use frontend\modules\api\models\Manager;
use frontend\modules\api\models\Project; use frontend\modules\api\models\project\Project;
use frontend\modules\api\models\ProjectUser; use frontend\modules\api\models\project\ProjectUser;
use Yii; use Yii;
use yii\base\InvalidConfigException;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
@ -84,7 +85,7 @@ class ProjectController extends ApiController
* ) * )
* *
* @param $project_id * @param $project_id
* @return array|Project|\yii\db\ActiveRecord|null * @return array|ActiveRecord|null
*/ */
public function actionGetProject($project_id) public function actionGetProject($project_id)
{ {
@ -323,7 +324,7 @@ class ProjectController extends ApiController
* ) * )
* *
* @throws \Throwable * @throws \Throwable
* @throws \yii\base\InvalidConfigException * @throws InvalidConfigException
* @throws \yii\db\StaleObjectException * @throws \yii\db\StaleObjectException
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
@ -366,7 +367,7 @@ class ProjectController extends ApiController
* ), * ),
* ) * )
* *
* @return array|\yii\db\ActiveRecord * @return array|ActiveRecord
* @throws BadRequestHttpException * @throws BadRequestHttpException
*/ */
public function actionMyEmployee() public function actionMyEmployee()
@ -486,7 +487,7 @@ class ProjectController extends ApiController
* ) * )
* *
* @return Project * @return Project
* @throws \yii\base\InvalidConfigException * @throws InvalidConfigException|NotFoundHttpException
*/ */
public function actionDelUser(): Project public function actionDelUser(): Project
{ {

View File

@ -2,31 +2,32 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\ProjectTask; use common\models\ProjectTask;
use common\models\ProjectTaskUser; use common\models\ProjectTaskUser;
use common\models\User; use common\models\User;
use common\services\TaskService; use frontend\modules\api\models\project\ProjectColumn;
use frontend\modules\api\models\ProjectColumn; use frontend\modules\api\services\TaskService;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\data\ActiveDataProvider;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
class TaskController extends ApiController class TaskController extends ApiController
{ {
public function verbs(): array private TaskService $taskService;
/**
* @param $id
* @param $module
* @param TaskService $taskService
* @param array $config
*/
public function __construct($id, $module, TaskService $taskService, $config = [])
{ {
return [ $this->taskService = $taskService;
'get-task' => ['get'], parent::__construct($id, $module, $config);
'get-task-list' => ['get'],
'get-user-tasks' => ['get'],
'create-task' => ['post'],
'update-task' => ['put', 'patch'],
'add-user-to-task' => ['post'],
'del-user' => ['delete'],
];
} }
/** /**
@ -117,7 +118,7 @@ class TaskController extends ApiController
$request['user_id'] = Yii::$app->user->id; $request['user_id'] = Yii::$app->user->id;
} }
$taskModel = TaskService::createTask($request); $taskModel = $this->taskService->createTask($request);
if ($taskModel->errors) { if ($taskModel->errors) {
throw new ServerErrorHttpException(json_encode($taskModel->errors)); throw new ServerErrorHttpException(json_encode($taskModel->errors));
} }
@ -170,9 +171,9 @@ class TaskController extends ApiController
if (empty($project_id) or !is_numeric($project_id)) { if (empty($project_id) or !is_numeric($project_id)) {
throw new NotFoundHttpException('Incorrect project ID'); throw new NotFoundHttpException('Incorrect project ID');
} }
$tasks = TaskService::getTaskListByProject($project_id); $tasks = $this->taskService->getTaskListByProject($project_id);
} else { } else {
$tasks = TaskService::getTaskList($project_id); $tasks = $this->taskService->getTaskList($project_id);
} }
if (empty($tasks)) { if (empty($tasks)) {
@ -228,9 +229,9 @@ class TaskController extends ApiController
if (empty($user_id) or !is_numeric($user_id)) { if (empty($user_id) or !is_numeric($user_id)) {
throw new NotFoundHttpException('Incorrect project ID'); throw new NotFoundHttpException('Incorrect project ID');
} }
$tasks = TaskService::getTaskListByUser($user_id); $tasks = $this->taskService->getTaskListByUser($user_id);
} else { } else {
$tasks = TaskService::getTaskList($user_id); $tasks = $this->taskService->getTaskList($user_id);
} }
if (empty($tasks)) { if (empty($tasks)) {
@ -283,7 +284,7 @@ class TaskController extends ApiController
throw new NotFoundHttpException('Incorrect task ID'); throw new NotFoundHttpException('Incorrect task ID');
} }
$task = TaskService::getTask($task_id); $task = $this->taskService->getTask($task_id);
if (empty($task)) { if (empty($task)) {
throw new NotFoundHttpException('The task does not exist'); throw new NotFoundHttpException('The task does not exist');
} }
@ -372,11 +373,11 @@ class TaskController extends ApiController
public function actionUpdateTask(): ?ProjectTask public function actionUpdateTask(): ?ProjectTask
{ {
$params = array_diff(\Yii::$app->request->getBodyParams(), [null, '']); $params = array_diff(\Yii::$app->request->getBodyParams(), [null, '']);
if (empty ($params['task_id']) or !TaskService::taskExists($params['task_id'])) { if (empty ($params['task_id']) or !$this->taskService->taskExists($params['task_id'])) {
throw new NotFoundHttpException('The task does not exist'); throw new NotFoundHttpException('The task does not exist');
} }
$modelTask = TaskService::updateTask($params); $modelTask = $this->taskService->updateTask($params);
if (!empty($modelTask->hasErrors())) { if (!empty($modelTask->hasErrors())) {
throw new ServerErrorHttpException(json_encode($modelTask->errors)); throw new ServerErrorHttpException(json_encode($modelTask->errors));
} }
@ -435,7 +436,7 @@ class TaskController extends ApiController
throw new NotFoundHttpException('User not found'); throw new NotFoundHttpException('User not found');
} }
if (empty ($request['task_id']) or !TaskService::taskExists($request['task_id'])) { if (empty ($request['task_id']) or !$this->taskService->taskExists($request['task_id'])) {
throw new NotFoundHttpException('The task does not exist'); throw new NotFoundHttpException('The task does not exist');
} }
@ -506,13 +507,13 @@ class TaskController extends ApiController
throw new NotFoundHttpException('User not found'); throw new NotFoundHttpException('User not found');
} }
if (empty ($request['task_id']) or !TaskService::taskExists($request['task_id'])) { if (empty ($request['task_id']) or !$this->taskService->taskExists($request['task_id'])) {
throw new NotFoundHttpException('The task does not exist'); throw new NotFoundHttpException('The task does not exist');
} }
ProjectTaskUser::deleteAll(['task_id' => $request['task_id'], 'user_id' => $request['user_id']]); ProjectTaskUser::deleteAll(['task_id' => $request['task_id'], 'user_id' => $request['user_id']]);
return TaskService::getTask($request['task_id']); return $this->taskService->getTask($request['task_id']);
} }
/** /**
@ -582,4 +583,72 @@ class TaskController extends ApiController
return $column; return $column;
} }
/**
*
* @OA\Get(path="/task/import",
* summary="Экспорт задач",
* description="Метод экспорта задач в xlsx",
* security={
* {"bearerAuth": {}}
* },
* tags={"TaskManager"},
* @OA\Parameter(
* name="companyId",
* in="query",
* description="ID компании",
* required=true,
* @OA\Schema(
* type="integer",
* )
* ),
* @OA\Parameter(
* name="userId",
* in="query",
* description="ID исполнителя задачи",
* required=true,
* @OA\Schema(
* type="integer",
* )
* ),
* @OA\Parameter(
* name="projectId",
* in="query",
* description="ID проекта",
* required=true,
* @OA\Schema(
* type="integer",
* )
* ),
* @OA\Parameter(
* name="fromDate",
* in="query",
* example="2023-11-09",
* description="Поиск задач с указанной даты",
* @OA\Schema(
* type="string",
* )
* ),
* @OA\Parameter(
* name="toDate",
* in="query",
* example="2023-11-09",
* description="Поиск задач до указанной даты",
* @OA\Schema(
* type="string",
* )
* ),
* @OA\Response(
* response=200,
* description="Возвращает задачи в xlsx файле",
* ),
* )
*
* @return string|void
* @throws BadRequestHttpException
*/
public function actionImport()
{
return $this->taskService->importTasks(Yii::$app->request->get());
}
} }

View File

@ -1,7 +1,8 @@
<?php <?php
namespace frontend\modules\api\models; namespace frontend\modules\api\models\project;
use frontend\modules\api\models\Company;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
use yii\helpers\Url; use yii\helpers\Url;
use yii\web\Link; use yii\web\Link;

View File

@ -1,6 +1,6 @@
<?php <?php
namespace frontend\modules\api\models; namespace frontend\modules\api\models\project;
/** /**
* *

View File

@ -1,6 +1,6 @@
<?php <?php
namespace frontend\modules\api\models; namespace frontend\modules\api\models\project;
/** /**
* *

View File

@ -1,6 +1,6 @@
<?php <?php
namespace frontend\modules\api\models; namespace frontend\modules\api\models\project;
/** /**
* *

View File

@ -1,6 +1,6 @@
<?php <?php
namespace frontend\modules\api\models; namespace frontend\modules\api\models\project;
/** /**
* *

View File

@ -0,0 +1,97 @@
<?php
namespace frontend\modules\api\services;
use common\models\forms\TasksImportForm;
use common\services\ImportProjectTaskService;
use frontend\modules\api\models\project\ProjectTask;
use frontend\modules\api\models\project\ProjectTaskUser;
use yii\web\BadRequestHttpException;
class TaskService
{
public ImportProjectTaskService $importProjectTaskService;
public function __construct(ImportProjectTaskService $importProjectTaskService)
{
$this->importProjectTaskService = $importProjectTaskService;
}
public function createTask($taskParams)
{
$task = new ProjectTask();
$task->load($taskParams, '');
$task->save();
return $task;
}
public function getTask($task_id): ?ProjectTask
{
return ProjectTask::findOne($task_id);
}
public function getTaskList($task_id): array
{
return ProjectTask::find()->asArray()->all();
}
public function getTaskListByProject($project_id): array
{
return ProjectTask::find()->where(['project_id' => $project_id])->orderBy('priority DESC')->all();
}
public function getTaskListByUser($user_id): array
{
$taskIdList = ProjectTaskUser::find()->where(['user_id' => $user_id])->select('task_id')->column();
return ProjectTask::find()->where([ 'IN', 'id', $taskIdList])->orWhere(['user_id' => $user_id])->orderBy('priority DESC')->all();
}
public function updateTask($task_params): ?ProjectTask
{
$modelTask = ProjectTask::findOne($task_params['task_id']);
if (isset($task_params['executor_id']) && $task_params['executor_id'] == 0){
$task_params['executor_id'] = null;
}
$modelTask->load($task_params, '');
$modelTask->save();
return $modelTask;
}
public function taskExists($task_id): bool
{
return ProjectTask::find()->where(['id' => $task_id])->exists();
}
/**
* @throws BadRequestHttpException
*/
public function importTasks(array $params)
{
$form = new TasksImportForm();
$form->load($params);
if (!$form->validate()){
$errors = $form->errors;
throw new BadRequestHttpException(array_shift($errors)[0]);
}
$query = ProjectTask::genQueryToImport(
$form->companyId,
$form->userId,
$form->projectId,
$form->fromDate,
$form->toDate
);
$tasks = $query->all();
if (!$tasks) {
return null;
}
return $this->importProjectTaskService->importTasks($tasks);
}
}