guild/common/models/ProjectUser.php

192 lines
5.6 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace common\models;
use Exception;
use Yii;
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;
2018-10-11 11:15:09 +03:00
/**
* This is the model class for table "project_user".
*
* @property int $id
* @property int $card_id
2018-10-11 11:15:09 +03:00
* @property int $project_id
2021-11-23 14:58:28 +03:00
* @property int $user_id
2023-11-21 11:23:38 +03:00
* @property int $project_role_id
* @property int $status
2018-10-11 11:15:09 +03:00
*
* @property Project $project
* @property UserCard $card
2021-11-23 14:58:28 +03:00
* @property User $user
2023-11-21 11:23:38 +03:00
* @property ProjectRole $projectRole
* @property ProjectTaskUser[] $taskUsers
2018-10-11 11:15:09 +03:00
*/
class ProjectUser extends \yii\db\ActiveRecord
{
2023-11-21 11:23:38 +03:00
public const STATUS_INACTIVE = 0;
public const STATUS_ACTIVE = 1;
public static function statusList() :array
{
return [
self::STATUS_INACTIVE => 'Не активен',
self::STATUS_ACTIVE => 'Активен',
];
}
2018-10-11 11:15:09 +03:00
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'project_user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2021-12-20 17:49:11 +03:00
[['user_id', 'project_id', 'card_id'], 'required'],
['user_id', 'unique', 'targetAttribute' => ['user_id', 'project_id'], 'message'=>'Сотрудник уже назначен на этот проект'],
2021-12-20 17:49:11 +03:00
['card_id', 'unique', 'targetAttribute' => ['card_id', 'project_id'], 'message'=>'Сотрудник уже назначен на этот проект'],
2023-11-21 11:23:38 +03:00
[['card_id', 'project_id', 'user_id', 'project_role_id', 'status'], 'integer'],
[['status'], 'default', 'value'=> self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_INACTIVE, self::STATUS_ACTIVE]],
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::class, 'targetAttribute' => ['project_id' => 'id']],
[['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::class, 'targetAttribute' => ['card_id' => 'id']],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
[['project_role_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectRole::class, 'targetAttribute' => ['project_role_id' => 'id']],
2018-10-11 11:15:09 +03:00
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'card_id' => 'Карточка',
2021-12-17 15:16:45 +03:00
'project_id' => 'Проект',
2021-11-23 14:58:28 +03:00
'user_id' => 'Сотрудник',
2023-11-21 11:23:38 +03:00
'project_role_id' => 'Роль на проекте',
'status' => 'Статус'
2018-10-11 11:15:09 +03:00
];
}
/**
* @return ActiveQuery
2018-10-11 11:15:09 +03:00
*/
public function getProject()
{
2023-11-21 11:23:38 +03:00
return $this->hasOne(Project::class, ['id' => 'project_id']);
2018-10-11 11:15:09 +03:00
}
/**
* @return ActiveQuery
2018-10-11 11:15:09 +03:00
*/
public function getCard()
2018-10-11 11:15:09 +03:00
{
2023-11-21 11:23:38 +03:00
return $this->hasOne(UserCard::class, ['id' => 'card_id']);
2021-11-23 14:58:28 +03:00
}
/**
* @return ActiveQuery
2021-11-23 14:58:28 +03:00
*/
public function getUser()
{
2023-11-21 11:23:38 +03:00
return $this->hasOne(User::class, ['id' => 'user_id']);
}
/**
* @return ActiveQuery
*/
2021-11-23 14:58:28 +03:00
public function getTasks()
{
2023-11-21 11:23:38 +03:00
return $this->hasMany(ProjectTask::class, ['project_user_id' => 'id']);
2021-11-23 14:58:28 +03:00
}
/**
* @return ActiveQuery
*/
public function getTasksByProject()
{
2023-11-21 11:23:38 +03:00
return $this->hasMany(ProjectTask::class, ['project_id' => 'project_id']);
}
/**
* @return ActiveQuery
*/
public function getProjectRole(): ActiveQuery
{
return $this->hasOne(ProjectRole::class, ['id' => 'project_role_id']);
}
/**
* @return ActiveQuery
2021-11-23 14:58:28 +03:00
*/
public function getTaskUsers()
{
2023-11-21 11:23:38 +03:00
return $this->hasMany(ProjectTaskUser::class, ['project_user_id' => 'id']);
2018-10-11 11:15:09 +03:00
}
public static function usersByProjectArr($project_id): array
{
return ArrayHelper::map(
self::find()->joinWith('user')->where(['project_id' => $project_id])->all(), 'id', 'user.username');
}
public static function usersByTaskArr($task_id): array
{
return ArrayHelper::map(
self::find()->joinWith(['tasksByProject', 'user'])->where(['task.id' => $task_id])->all(), 'id', 'user.username');
}
public static function userCardByTaskArr($task_id): array
{
return ArrayHelper::map(
self::find()->joinWith(['tasksByProject', 'card'])->where(['task.id' => $task_id])->all(), 'id', 'card.fio');
}
public static function setUsersByCardId()
{
$projectUserModels = self::findAll(['user_id' => null]);
foreach ($projectUserModels as $projectUser)
{
$projectUser->user_id = UserCard::getUserIdByCardId($projectUser->card_id);
if ($projectUser->user_id !== null) {
$projectUser->save();
}
}
}
public static function setCardsByUsersId()
{
$projectUserModels = self::findAll(['card_id' => null]);
foreach ($projectUserModels as $projectUser)
{
$projectUser->card_id = UserCard::getCardIdByUserId($projectUser->user_id);
if ($projectUser->card_id !== null) {
$projectUser->save();
}
}
}
public static function getUsersNotOnProject($project_id): array
{
$usersIdList = ProjectUser::find()->where(['project_id' => $project_id])->select('card_id')->column();
$userCards = UserCard::find()
->where(['not in', 'id', $usersIdList])
->andWhere(['not', ['id_user' => null]])
->all();
return ArrayHelper::map($userCards, 'id', 'fio');
}
2018-10-11 11:15:09 +03:00
}