2018-10-11 11:15:09 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\models;
|
|
|
|
|
2021-12-16 17:14:33 +03:00
|
|
|
use Exception;
|
|
|
|
use Yii;
|
2021-11-24 13:05:12 +03:00
|
|
|
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
|
2021-12-16 17:14:33 +03:00
|
|
|
* @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
|
2018-10-11 11:15:09 +03:00
|
|
|
*
|
|
|
|
* @property Project $project
|
2021-12-16 17:14:33 +03:00
|
|
|
* @property UserCard $card
|
2021-11-23 14:58:28 +03:00
|
|
|
* @property User $user
|
|
|
|
* @property TaskUser[] $taskUsers
|
2018-10-11 11:15:09 +03:00
|
|
|
*/
|
|
|
|
class ProjectUser extends \yii\db\ActiveRecord
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function tableName()
|
|
|
|
{
|
|
|
|
return 'project_user';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
2021-12-16 17:14:33 +03:00
|
|
|
[['user_id', 'project_id'], 'required'],
|
|
|
|
['user_id', 'unique', 'targetAttribute' => ['user_id', 'project_id'], 'message'=>'Сотрудник уже назначен на этот проект'],
|
|
|
|
[['card_id', 'project_id', 'user_id'], 'integer'],
|
2018-10-11 11:15:09 +03:00
|
|
|
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']],
|
2021-12-16 17:14:33 +03:00
|
|
|
[['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::className(), 'targetAttribute' => ['card_id' => 'id']],
|
2021-11-23 14:58:28 +03:00
|
|
|
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
|
2018-10-11 11:15:09 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
2021-12-16 17:14:33 +03:00
|
|
|
'card_id' => 'Карточка',
|
|
|
|
'project_id' => 'Project ID',
|
2021-11-23 14:58:28 +03:00
|
|
|
'user_id' => 'Сотрудник',
|
2018-10-11 11:15:09 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-24 13:05:12 +03:00
|
|
|
* @return ActiveQuery
|
2018-10-11 11:15:09 +03:00
|
|
|
*/
|
|
|
|
public function getProject()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Project::className(), ['id' => 'project_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-24 13:05:12 +03:00
|
|
|
* @return ActiveQuery
|
2018-10-11 11:15:09 +03:00
|
|
|
*/
|
2021-12-16 17:14:33 +03:00
|
|
|
public function getCard()
|
2018-10-11 11:15:09 +03:00
|
|
|
{
|
2021-12-16 17:14:33 +03:00
|
|
|
return $this->hasOne(UserCard::className(), ['id' => 'card_id']);
|
2021-11-23 14:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-06 15:13:31 +03:00
|
|
|
* @return ActiveQuery
|
2021-11-23 14:58:28 +03:00
|
|
|
*/
|
2021-12-16 17:14:33 +03:00
|
|
|
public function getUser()
|
2021-11-24 13:05:12 +03:00
|
|
|
{
|
2021-12-16 17:14:33 +03:00
|
|
|
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
2021-11-24 13:05:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
2021-11-23 14:58:28 +03:00
|
|
|
public function getTasks()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Task::className(), ['project_user_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-24 13:05:12 +03:00
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getTasksByProject()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Task::className(), ['project_id' => 'project_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
2021-11-23 14:58:28 +03:00
|
|
|
*/
|
|
|
|
public function getTaskUsers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(TaskUser::className(), ['project_user_id' => 'id']);
|
2018-10-11 11:15:09 +03:00
|
|
|
}
|
2021-11-24 13:05:12 +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');
|
|
|
|
}
|
2021-12-16 17:14:33 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 11:15:09 +03:00
|
|
|
}
|