guild/common/models/ProjectTask.php

371 lines
10 KiB
PHP
Raw Normal View History

2021-11-23 13:13:19 +03:00
<?php
namespace common\models;
use yii\behaviors\TimestampBehavior;
2021-11-25 12:33:08 +03:00
use yii\db\ActiveQuery;
2021-12-20 17:49:11 +03:00
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
2021-11-23 13:13:19 +03:00
/**
* This is the model class for table "task".
*
* @property int $id
* @property int $project_id
* @property string $title
* @property int $status
* @property string $created_at
* @property string $updated_at
2023-04-25 01:32:15 +03:00
* @property int $column_id
* @property int $user_id
2023-04-27 01:53:21 +03:00
* @property int $executor_id
2023-05-12 02:12:43 +03:00
* @property int $priority
2023-10-13 17:58:58 +03:00
* @property int $execution_priority
2021-11-23 13:13:19 +03:00
* @property string $description
2023-06-30 18:13:40 +03:00
* @property string $dead_line
2021-11-23 13:13:19 +03:00
*
* @property Project $project
* @property User $user
2021-12-20 17:49:11 +03:00
* @property UserCard $card
* @property UserCard $cardIdCreator
2023-11-10 15:55:01 +03:00
* @property Company $company
* @property ProjectColumn $column
* @property User $executor
* @property Mark[] $mark
* @property MarkEntity[] $markEntity
* @property ProjectTaskUser[] $taskUsers
2021-11-23 13:13:19 +03:00
*/
class ProjectTask extends ActiveRecord
2021-11-23 13:13:19 +03:00
{
2023-04-26 01:22:02 +03:00
const STATUS_DISABLE = 0;
2023-11-20 11:39:44 +03:00
const STATUS_ACTIVE = 1;
const STATUS_ARCHIVE = 2;
2023-11-21 11:23:38 +03:00
const STATUS_AT_WORK = 3;
2023-04-27 01:53:21 +03:00
2023-10-13 17:58:58 +03:00
const PRIORITY_LOW = 0;
const PRIORITY_MEDIUM = 1;
const PRIORITY_HIGH = 2;
2023-11-10 15:55:01 +03:00
const DAY_IN_UNIX_TIME = 86340; // 23:59:59
2023-10-13 17:58:58 +03:00
/**
* @return string[]
*/
public static function priorityList() :array
{
return [
self::PRIORITY_LOW => 'Низкий',
self::PRIORITY_MEDIUM => 'Средний',
self::PRIORITY_HIGH => 'Высокий',
];
}
/**
* @param $priority
* @return string
* @throws \Exception
*/
public static function getPriority($priority): string
{
return ArrayHelper::getValue(self::priorityList(), $priority);
}
2021-11-23 13:13:19 +03:00
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'project_task';
2021-11-23 13:13:19 +03:00
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
2021-11-23 13:13:19 +03:00
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2023-04-25 01:32:15 +03:00
[['project_id', 'status', 'title', 'description',], 'required'],
2023-10-13 17:58:58 +03:00
[['project_id', 'status', 'column_id', 'user_id', 'executor_id', 'priority', 'execution_priority'], 'integer'],
2023-06-30 18:13:40 +03:00
[['created_at', 'updated_at', 'dead_line'], 'safe'],
2023-10-13 17:58:58 +03:00
['execution_priority', 'in', 'range' => [self::PRIORITY_LOW, self::PRIORITY_MEDIUM, self::PRIORITY_HIGH]],
2023-04-25 01:32:15 +03:00
['title', 'unique', 'targetAttribute' => ['title', 'project_id'], 'message' => 'Такая задача уже создана'],
2021-11-23 13:13:19 +03:00
[['title'], 'string', 'max' => 255],
2023-11-21 11:23:38 +03:00
['status', 'in', 'range' => [self::STATUS_DISABLE, self::STATUS_ACTIVE, self::STATUS_ARCHIVE, self::STATUS_AT_WORK]],
2023-05-23 23:11:46 +03:00
[['description'], 'string', 'max' => 1500],
2021-11-23 13:13:19 +03:00
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']],
2023-04-25 01:32:15 +03:00
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
2023-04-27 01:53:21 +03:00
[['executor_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['executor_id' => 'id']],
2023-04-25 01:32:15 +03:00
[['column_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectColumn::className(), 'targetAttribute' => ['column_id' => 'id']],
2021-11-23 13:13:19 +03:00
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'project_id' => 'Проект',
'title' => 'Название задачи',
'status' => 'Статус',
'created_at' => 'Дата создания',
'updated_at' => 'Дата обновления',
'description' => 'Описание',
2023-04-25 01:32:15 +03:00
'user_id' => 'Создатель задачи',
'column_id' => 'Колонка',
2023-04-27 01:53:21 +03:00
'executor_id' => 'Исполнитель',
2023-05-12 02:12:43 +03:00
'priority' => 'Приоритет',
2023-06-30 18:13:40 +03:00
'dead_line' => 'Срок выполнения задачи',
2023-10-13 17:58:58 +03:00
'execution_priority' => 'Приоритет выполнения',
2023-04-25 01:32:15 +03:00
];
}
2023-10-13 14:25:40 +03:00
/**
* @return string[]
*/
public function fields(): array
{
return [
'id',
'project_id',
'project_name' => function () {
return $this->project->name ?? null;
},
'title',
'created_at',
'updated_at',
'dead_line',
'description',
'status',
'column_id',
'user_id',
'user' => function () {
return [
2023-11-10 15:55:01 +03:00
"fio" => $this->user->userCard->fio ?? ($this->user->id ?? ''),
2023-10-13 14:25:40 +03:00
"avatar" => $this->user->userCard->photo ?? '',
];
},
'executor_id',
'priority',
'executor' => function () {
if ($this->executor) {
return [
"fio" => $this->executor->userCard->fio ?? $this->executor->username,
"avatar" => $this->executor->userCard->photo ?? '',
];
}
return null;
},
'comment_count' => function () {
2023-11-10 15:55:01 +03:00
return $this->getCommentCount();
2023-10-13 14:25:40 +03:00
},
'taskUsers',
2023-10-13 17:58:58 +03:00
'mark',
'execution_priority'
2023-10-13 14:25:40 +03:00
];
}
2023-11-10 15:55:01 +03:00
/**
* @return bool|int|string|null
*/
public function getCommentCount()
{
return Comment::find()->where(['entity_id' => $this->id, 'entity_type' => 2, 'status' => Comment::STATUS_ACTIVE])->count();
}
2023-10-13 14:25:40 +03:00
/**
* @return string[]
*/
public function extraFields(): array
{
return [
'timers',
'column' => function () {
return [
'column_title' => $this->column->title ?? null
];
},
'mark'
];
}
2023-04-26 01:22:02 +03:00
/**
* @return string[]
*/
public static function getStatus(): array
{
return [
self::STATUS_ACTIVE => 'Активен',
self::STATUS_DISABLE => 'Выключен'
];
}
public function beforeDelete()
{
2023-04-25 01:32:15 +03:00
foreach ($this->taskUsers as $taskUser) {
$taskUser->delete();
}
return parent::beforeDelete();
}
2021-11-23 13:13:19 +03:00
/**
2021-11-25 12:33:08 +03:00
* @return ActiveQuery
2021-11-23 13:13:19 +03:00
*/
2023-04-27 01:53:21 +03:00
public function getProject(): ActiveQuery
2021-11-23 13:13:19 +03:00
{
return $this->hasOne(Project::className(), ['id' => 'project_id']);
}
2023-11-10 15:55:01 +03:00
/**
* @return ActiveQuery
*/
public function getCompany(): ActiveQuery
{
return $this->hasOne(Company::class,['id' => 'company_id'])
->via('project');
}
2021-11-23 13:13:19 +03:00
/**
2021-11-25 12:33:08 +03:00
* @return ActiveQuery
2021-11-23 13:13:19 +03:00
*/
2023-04-27 01:53:21 +03:00
public function getUser(): ActiveQuery
2021-11-23 13:13:19 +03:00
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
2021-11-23 13:13:19 +03:00
}
2023-04-27 01:53:21 +03:00
/**
* @return ActiveQuery
*/
public function getExecutor(): ActiveQuery
{
return $this->hasOne(User::class, ['id' => 'executor_id']);
}
2023-04-25 01:32:15 +03:00
/**
* @return ActiveQuery
*/
public function getColumn(): ActiveQuery
2021-12-20 17:49:11 +03:00
{
2023-04-25 01:32:15 +03:00
return $this->hasOne(ProjectColumn::class, ['id' => 'column_id']);
2021-11-23 13:13:19 +03:00
}
/**
2021-11-25 12:33:08 +03:00
* @return ActiveQuery
2021-11-23 13:13:19 +03:00
*/
public function getTaskUsers()
{
return $this->hasMany(ProjectTaskUser::className(), ['task_id' => 'id']);
2021-11-23 13:13:19 +03:00
}
public function getTimers()
{
return $this->hasMany(Timer::class, ['entity_id' => 'id'])->where(['status' => Timer::STATUS_ACTIVE]);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getMark()
{
return $this->hasMany(Mark::class, ['id' => 'mark_id'])
->via('markEntity');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getMarkEntity()
{
return $this->hasMany(MarkEntity::class, ['entity_id' => 'id'])
->where(['entity_type' => Entity::ENTITY_TYPE_TASK]);
}
public static function usersByTaskArr($task_id): array
{
return ArrayHelper::map(
self::find()->joinWith(['user', 'project'])->where(['project_id' => $task_id])->all(), 'id', 'user.username');
}
2023-11-10 15:55:01 +03:00
/**
* @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;
}
2021-11-23 13:13:19 +03:00
}