This commit is contained in:
andrey 2021-12-17 16:40:31 +03:00
commit da9334dcf6
22 changed files with 13837 additions and 81 deletions

View File

@ -75,7 +75,7 @@ return [
'components' => [ 'components' => [
'request' => [ 'request' => [
'csrfParam' => '_csrf-backend', 'csrfParam' => '_csrf-backend',
'baseUrl' => '/secure', // TODO /secure 'baseUrl' => '/secure',
'parsers' => [ 'parsers' => [
'application/json' => 'yii\web\JsonParser', 'application/json' => 'yii\web\JsonParser',
'text/xml' => 'yii/web/XmlParser', 'text/xml' => 'yii/web/XmlParser',

View File

@ -2,12 +2,16 @@
namespace backend\modules\project\controllers; namespace backend\modules\project\controllers;
use common\models\UserCard;
use Exception;
use Yii; use Yii;
use backend\modules\project\models\ProjectUser; use backend\modules\project\models\ProjectUser;
use backend\modules\project\models\ProjectUserSearch; use backend\modules\project\models\ProjectUserSearch;
use yii\helpers\ArrayHelper;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use yii\web\Response;
/** /**
* ProjectUserController implements the CRUD actions for ProjectUser model. * ProjectUserController implements the CRUD actions for ProjectUser model.
@ -60,16 +64,35 @@ class ProjectUserController extends Controller
/** /**
* Creates a new ProjectUser model. * Creates a new ProjectUser model.
* If creation is successful, the browser will be redirected to the 'view' page. * If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed * @return string|Response
* @throws Exception
*/ */
public function actionCreate() public function actionCreate()
{ {
$model = new ProjectUser(); $post = \Yii::$app->request->post('ProjectUser');
if ($model->load(Yii::$app->request->post()) && $model->save()) { if (!empty($post)) {
return $this->redirect(['view', 'id' => $model->id]); $user_id_arr = ArrayHelper::getValue($post, 'user_id');
$project_id = $post['project_id'];
foreach ($user_id_arr as $user_id) {
$emtModel = new ProjectUser();
$emtModel->project_id = $project_id;
$emtModel->user_id = $user_id;
$emtModel->card_id = UserCard::getIdByUserId($user_id);
$emtModel->save();
// if (!$emtModel->save()) {
// return $this->render('create', [
// 'model' => $emtModel,
// ]);
// }
}
return $this->redirect(['index']);
} }
$model = new ProjectUser();
return $this->render('create', [ return $this->render('create', [
'model' => $model, 'model' => $model,
]); ]);
@ -86,10 +109,14 @@ class ProjectUserController extends Controller
{ {
$model = $this->findModel($id); $model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) ) {
$model->card_id = UserCard::getIdByUserId($model->user_id);
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->id]); return $this->redirect(['view', 'id' => $model->id]);
} }
}
return $this->render('update', [ return $this->render('update', [
'model' => $model, 'model' => $model,
]); ]);
@ -124,4 +151,18 @@ class ProjectUserController extends Controller
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');
} }
public function actionSetUserFields(): Response
{
ProjectUser::setUsersByCardId();
return $this->redirect(['index']);
}
public function actionSetCardFields(): Response
{
ProjectUser::setCardsByUsersId();
return $this->redirect(['index']);
}
} }

View File

@ -96,6 +96,7 @@ class Project extends \common\models\Project
$prUser = new ProjectUser(); $prUser = new ProjectUser();
$prUser->project_id = $this->id; $prUser->project_id = $this->id;
$prUser->card_id = $item; $prUser->card_id = $item;
$prUser->user_id = $prUser->card->user->id;
$prUser->save(); $prUser->save();
} }

View File

@ -17,7 +17,7 @@ class ProjectUserSearch extends ProjectUser
public function rules() public function rules()
{ {
return [ return [
[['id', 'project_id', 'user_id'], 'integer'], [['id', 'project_id', 'user_id', 'card_id'], 'integer'],
]; ];
} }
@ -39,7 +39,7 @@ class ProjectUserSearch extends ProjectUser
*/ */
public function search($params) public function search($params)
{ {
$query = ProjectUser::find()->joinWith(['project', 'user']); $query = ProjectUser::find()->joinWith(['project', 'user', 'card']);
// add conditions that should always apply here // add conditions that should always apply here
@ -60,6 +60,7 @@ class ProjectUserSearch extends ProjectUser
'id' => $this->id, 'id' => $this->id,
'project_id' => $this->project_id, 'project_id' => $this->project_id,
'user_id' => $this->user_id, 'user_id' => $this->user_id,
'card_id' => $this->card_id,
]); ]);
return $dataProvider; return $dataProvider;

View File

@ -30,7 +30,8 @@ use yii\widgets\ActiveForm;
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(), 'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'], 'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [ 'pluginOptions' => [
'allowClear' => true 'allowClear' => true,
'multiple' => true,
], ],
] ]
) ?> ) ?>

View File

@ -0,0 +1,45 @@
<?php
use backend\modules\project\models\Project;
use common\models\User;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\project\models\ProjectUser */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="project-user-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'project_id')->widget(Select2::className(),
[
'data' => Project::find()->select(['name', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
],
]
) ?>
<?= $form->field($model, 'user_id')->widget(Select2::className(),
[
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => false,
],
]
) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -1,7 +1,9 @@
<?php <?php
use backend\modules\card\models\UserCard;
use backend\modules\project\models\Project; use backend\modules\project\models\Project;
use common\models\User; use common\models\User;
use kartik\select2\Select2;
use yii\helpers\Html; use yii\helpers\Html;
use yii\grid\GridView; use yii\grid\GridView;
@ -16,6 +18,8 @@ $this->params['breadcrumbs'][] = $this->title;
<p> <p>
<?= Html::a('Назначить сотрудника на проект', ['create'], ['class' => 'btn btn-success']) ?> <?= Html::a('Назначить сотрудника на проект', ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Установить значения поля "Сотрудник"', ['set-user-fields'], ['class' => 'btn btn-secondary']) ?>
<?= Html::a('Установить значения поля "Карточка"', ['set-card-fields'], ['class' => 'btn btn-secondary']) ?>
</p> </p>
<?= GridView::widget([ <?= GridView::widget([
@ -26,13 +30,54 @@ $this->params['breadcrumbs'][] = $this->title;
[ [
'attribute' => 'project_id', 'attribute' => 'project_id',
'filter' => Project::find()->select(['name', 'id'])->indexBy('id')->column(), 'value' => 'project.name',
'value' => 'project.name' 'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'project_id',
'data' => Project::find()->select(['name', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '250px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
[ [
'attribute' => 'user_id', 'attribute' => 'user_id',
'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(), 'value' => 'user.username',
'value' => 'user.username' 'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'user_id',
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '250px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
],
[
'attribute' => 'card_id',
'value' => 'card.fio',
'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'card_id',
'data' => UserCard::find()->select(['fio', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '250px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
['class' => 'yii\grid\ActionColumn'], ['class' => 'yii\grid\ActionColumn'],

View File

@ -12,7 +12,7 @@ $this->params['breadcrumbs'][] = 'Update';
?> ?>
<div class="project-user-update"> <div class="project-user-update">
<?= $this->render('_form', [ <?= $this->render('_form_update', [
'model' => $model, 'model' => $model,
]) ?> ]) ?>

View File

@ -3,6 +3,8 @@
namespace backend\modules\task\controllers; namespace backend\modules\task\controllers;
use backend\modules\project\models\ProjectUser; use backend\modules\project\models\ProjectUser;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\web\Response; use yii\web\Response;
use Yii; use Yii;
use backend\modules\task\models\TaskUser; use backend\modules\task\models\TaskUser;
@ -63,41 +65,39 @@ class TaskUserController extends Controller
* Creates a new TaskUser model. * Creates a new TaskUser model.
* If creation is successful, the browser will be redirected to the 'view' page. * If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed * @return mixed
* @throws \Exception
*/ */
public function actionCreate($task_id = null) public function actionCreate($task_id = null)
{ {
$model = new TaskUser(); $post = \Yii::$app->request->post('TaskUser');
if ($model->load(Yii::$app->request->post()) && $model->save()) { if (!empty($post)) {
$project_user_id_arr = ArrayHelper::getValue($post, 'project_user_id');
foreach ($project_user_id_arr as $project_user_id) {
$emtModel = new TaskUser();
$emtModel->task_id = $post['task_id'];
$emtModel->project_user_id = $project_user_id;
if (!$emtModel->save()) {
return $this->render('create', [
'model' => $emtModel,
'task_id' => $task_id,
]);
}
}
if ($task_id !== null) if ($task_id !== null)
{ {
return $this->redirect(['task/view', 'id' => $task_id]); return $this->redirect(['task/view', 'id' => $task_id]);
} }
return $this->redirect(['view', 'id' => $model->id]); return $this->redirect(['index']);
} }
return $this->render('create', [
'model' => $model,
'task_id' => $task_id,
]);
}
/**
* Creates a new TaskUser model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreateForCurrentTask($task_id = null)
{
$model = new TaskUser(); $model = new TaskUser();
return $this->render('create', [
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['task/view', 'id' => $task_id]);
}
return $this->render('create_for_current_task', [
'model' => $model, 'model' => $model,
'task_id' => $task_id, 'task_id' => $task_id,
]); ]);
@ -185,4 +185,9 @@ class TaskUserController extends Controller
} }
return ['output'=>'', 'selected'=>'']; return ['output'=>'', 'selected'=>''];
} }
public function actionDynamicProjectUser()
{
var_dump('hhh'); die;
}
} }

View File

@ -41,7 +41,7 @@ class TaskUserSearch extends TaskUser
*/ */
public function search($params) public function search($params)
{ {
$query = TaskUser::find()->joinWith(['task', 'projectUser', 'projectUser.project']); $query = TaskUser::find()->joinWith(['task', 'projectUser', 'projectUser.project', 'projectUser.user']);
// add conditions that should always apply here // add conditions that should always apply here

View File

@ -21,19 +21,27 @@ use yii\widgets\ActiveForm;
'data' => Task::find()->select(['title', 'id'])->indexBy('id')->column(), 'data' => Task::find()->select(['title', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => 'Выберите проект', 'value' => $task_id, 'id' => 'task-id',], 'options' => ['placeholder' => 'Выберите проект', 'value' => $task_id, 'id' => 'task-id',],
'pluginOptions' => [ 'pluginOptions' => [
'allowClear' => true, 'allowClear' => false,
], ],
]); ]);
?> ?>
<?= $form->field($model, 'project_user_id')->widget(DepDrop::className(), <?= $form->field($model, 'project_user_id')->widget(DepDrop::className(),
[ [
'options' => ['id' => 'project-user-id'], 'type' => DepDrop::TYPE_SELECT2,
'options' => ['id' => 'project-user-id', 'allowClear' => true, 'multiple' => true], // , 'multiple' => true
'select2Options' => [
'pluginOptions' => [
'allowClear' => true,
'closeOnSelect' => false,
],
'showToggleAll' => false,
],
'pluginOptions' => [ 'pluginOptions' => [
'depends' => ['task-id'], 'depends' => ['task-id'],
'placeholder' => 'Выберите', 'placeholder' => 'Выберите',
'initialize' => true, 'initialize' => true,
'url' => Url::to(['/task/task-user/executor']) 'url' => Url::to(['/task/task-user/executor']),
] ]
] ]
); ?> ); ?>

View File

@ -0,0 +1,47 @@
<?php
use backend\modules\task\models\Task;
use kartik\depdrop\DepDrop;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\task\models\TaskUser */
/* @var $form yii\widgets\ActiveForm */
/* @var $task_id */
?>
<div class="task-user-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'task_id')->widget(Select2::className(),[
'data' => Task::find()->select(['title', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => 'Выберите проект', 'value' => $task_id, 'id' => 'task-id',],
'pluginOptions' => [
'allowClear' => false,
],
]);
?>
<?= $form->field($model, 'project_user_id')->widget(DepDrop::className(),
[
'options' => ['id' => 'project-user-id', 'allowClear' => true],
'pluginOptions' => [
'depends' => ['task-id'],
'placeholder' => 'Выберите',
'initialize' => true,
'url' => Url::to(['/task/task-user/executor']),
]
]
); ?>
<div class="form-group">
<?= Html::submitButton('Назначить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -2,6 +2,7 @@
use backend\modules\project\models\ProjectUser; use backend\modules\project\models\ProjectUser;
use backend\modules\task\models\Task; use backend\modules\task\models\Task;
use kartik\select2\Select2;
use yii\helpers\Html; use yii\helpers\Html;
use yii\grid\GridView; use yii\grid\GridView;
@ -30,14 +31,38 @@ $this->params['breadcrumbs'][] = $this->title;
[ [
'attribute' => 'task_id', 'attribute' => 'task_id',
'filter' => Task::find()->select(['title', 'id'])->indexBy('id')->column(), 'value' => 'task.title',
'value' => 'task.title' 'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'task_id',
'data' => Task::find()->select(['title', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '250px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
[ [
'attribute' => 'project_user_id', 'attribute' => 'project_user_id',
'filter' => ProjectUser::find()->select(['user.username', 'project_user.id']) 'value' => 'projectUser.user.username',
'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'project_user_id',
'data' => ProjectUser::find()->select(['user.username', 'project_user.id'])
->joinWith('user')->indexBy('project_user.id')->column(), ->joinWith('user')->indexBy('project_user.id')->column(),
'value' => 'projectUser.user.username' 'pluginOptions' => [
'allowClear' => true,
'width' => '250px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
['class' => 'yii\grid\ActionColumn'], ['class' => 'yii\grid\ActionColumn'],

View File

@ -12,7 +12,7 @@ $this->params['breadcrumbs'][] = 'Update';
?> ?>
<div class="task-user-update"> <div class="task-user-update">
<?= $this->render('_form', [ <?= $this->render('_form_update', [
'model' => $model, 'model' => $model,
'task_id' => $task_id, 'task_id' => $task_id,
]) ?> ]) ?>

View File

@ -30,7 +30,7 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'user_id_creator')->widget(Select2::class, <?= $form->field($model, 'user_id_creator')->widget(Select2::class,
[ [
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(), 'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'], 'options' => ['placeholder' => '...','class' => 'form-control', 'value' => Yii::$app->user->id],
'pluginOptions' => [ 'pluginOptions' => [
'allowClear' => true, 'allowClear' => true,
'prompt' => 'Выберите' 'prompt' => 'Выберите'

View File

@ -2,8 +2,10 @@
use backend\modules\project\models\Project; use backend\modules\project\models\Project;
use backend\modules\project\models\ProjectUser; use backend\modules\project\models\ProjectUser;
use backend\modules\task\models\Task;
use common\helpers\StatusHelper; use common\helpers\StatusHelper;
use common\models\User; use common\models\User;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html; use yii\helpers\Html;
use yii\grid\GridView; use yii\grid\GridView;
@ -29,19 +31,55 @@ $this->params['breadcrumbs'][] = $this->title;
[ [
'attribute' => 'project_id', 'attribute' => 'project_id',
'filter' => Project::find()->select(['name', 'id'])->indexBy('id')->column(), 'value' => 'project.name',
'value' => 'project.name' 'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'project_id',
'data' => Project::find()->select(['name', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '150px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
'title', 'title',
[ [
'attribute' => 'user_id_creator', 'attribute' => 'user_id_creator',
'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(), 'value' => 'userIdCreator.username',
'value' => 'userIdCreator.username' 'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'user_id_creator',
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '150px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
[ [
'attribute' => 'user_id', 'attribute' => 'user_id',
'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(), 'value' => 'user.username',
'value' => 'user.username' 'filter' => Select2::widget([
'model' => $searchModel,
'attribute' => 'user_id',
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true,
'width' => '150px',
],
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
], ],
'description', 'description',
[ [
@ -58,7 +96,6 @@ $this->params['breadcrumbs'][] = $this->title;
], ],
[ [
'attribute' => 'updated_at', 'attribute' => 'updated_at',
'filter' => User::find()->select(['updated_at', 'updated_at'])->indexBy('updated_at')->column(),
'format' => ['datetime', 'php:d.m.Y H:i'] 'format' => ['datetime', 'php:d.m.Y H:i']
], ],

View File

@ -2,6 +2,8 @@
namespace common\models; namespace common\models;
use Exception;
use Yii;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
@ -9,12 +11,13 @@ use yii\helpers\ArrayHelper;
* This is the model class for table "project_user". * This is the model class for table "project_user".
* *
* @property int $id * @property int $id
* @property int $card_id
* @property int $project_id * @property int $project_id
* @property int $user_id * @property int $user_id
* *
* @property Project $project * @property Project $project
* @property UserCard $card
* @property User $user * @property User $user
* @property Task[] $tasks
* @property TaskUser[] $taskUsers * @property TaskUser[] $taskUsers
*/ */
class ProjectUser extends \yii\db\ActiveRecord class ProjectUser extends \yii\db\ActiveRecord
@ -33,10 +36,11 @@ class ProjectUser extends \yii\db\ActiveRecord
public function rules() public function rules()
{ {
return [ return [
[['project_id', 'user_id'], 'required'], [['user_id', 'project_id'], 'required'],
['user_id', 'unique', 'targetAttribute' => ['user_id', 'project_id'], 'message'=>'Сотрудник уже занят на этом проекте'], ['user_id', 'unique', 'targetAttribute' => ['user_id', 'project_id'], 'message'=>'Сотрудник уже назначен на этот проект'],
[['project_id', 'user_id'], 'integer'], // [['card_id', 'project_id', 'user_id'], 'integer'],
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']], [['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']],
[['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::className(), 'targetAttribute' => ['card_id' => 'id']],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']], [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
]; ];
} }
@ -48,6 +52,7 @@ class ProjectUser extends \yii\db\ActiveRecord
{ {
return [ return [
'id' => 'ID', 'id' => 'ID',
'card_id' => 'Карточка',
'project_id' => 'Проект', 'project_id' => 'Проект',
'user_id' => 'Сотрудник', 'user_id' => 'Сотрудник',
]; ];
@ -64,17 +69,17 @@ class ProjectUser extends \yii\db\ActiveRecord
/** /**
* @return ActiveQuery * @return ActiveQuery
*/ */
public function getUser() public function getCard()
{ {
return $this->hasOne(User::className(), ['id' => 'user_id']); return $this->hasOne(UserCard::className(), ['id' => 'card_id']);
} }
/** /**
* @return ActiveQuery * @return ActiveQuery
*/ */
public function getCard() public function getUser()
{ {
return $this->hasOne(UserCard::className(), ['id_user' => 'user_id']); return $this->hasOne(User::className(), ['id' => 'user_id']);
} }
/** /**
@ -112,4 +117,30 @@ class ProjectUser extends \yii\db\ActiveRecord
return ArrayHelper::map( return ArrayHelper::map(
self::find()->joinWith(['tasksByProject', 'user'])->where(['task.id' => $task_id])->all(), 'id', 'user.username'); self::find()->joinWith(['tasksByProject', 'user'])->where(['task.id' => $task_id])->all(), 'id', 'user.username');
} }
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();
}
}
}
} }

View File

@ -31,8 +31,8 @@ class TaskUser extends \yii\db\ActiveRecord
public function rules() public function rules()
{ {
return [ return [
[['task_id', 'project_user_id'], 'integer'], [['task_id', 'project_user_id'], 'required'],
['project_user_id', 'unique', 'targetAttribute' => ['task_id', 'project_user_id'], 'message'=>'Этот сотрудник уже назначен на эту задачу'], ['project_user_id', 'unique', 'targetAttribute' => ['task_id', 'project_user_id'], 'message'=>'Уже закреплён(ы) за задачей'],
[['project_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectUser::className(), 'targetAttribute' => ['project_user_id' => 'id']], [['project_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectUser::className(), 'targetAttribute' => ['project_user_id' => 'id']],
[['task_id'], 'exist', 'skipOnError' => true, 'targetClass' => Task::className(), 'targetAttribute' => ['task_id' => 'id']], [['task_id'], 'exist', 'skipOnError' => true, 'targetClass' => Task::className(), 'targetAttribute' => ['task_id' => 'id']],
]; ];

View File

@ -3,8 +3,11 @@
namespace common\models; namespace common\models;
use common\classes\Debug; use common\classes\Debug;
use Exception;
use phpDocumentor\Reflection\Types\This;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior; use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\Expression; use yii\db\Expression;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
@ -141,7 +144,7 @@ class UserCard extends \yii\db\ActiveRecord
} }
/** /**
* @return \yii\db\ActiveQuery * @return ActiveQuery
*/ */
public function getFieldsValues() public function getFieldsValues()
{ {
@ -149,7 +152,7 @@ class UserCard extends \yii\db\ActiveRecord
} }
/** /**
* @return \yii\db\ActiveQuery * @return ActiveQuery
*/ */
public function getProjectUsers() public function getProjectUsers()
{ {
@ -157,7 +160,7 @@ class UserCard extends \yii\db\ActiveRecord
} }
/** /**
* @return \yii\db\ActiveQuery * @return ActiveQuery
*/ */
public function getPosition() public function getPosition()
{ {
@ -165,7 +168,7 @@ class UserCard extends \yii\db\ActiveRecord
} }
/** /**
* @return \yii\db\ActiveQuery * @return ActiveQuery
*/ */
public function getStatus0() public function getStatus0()
{ {
@ -173,9 +176,9 @@ class UserCard extends \yii\db\ActiveRecord
} }
/** /**
* @return \yii\db\ActiveQuery * @return ActiveQuery
*/ */
public function getAchievements(): \yii\db\ActiveQuery public function getAchievements(): ActiveQuery
{ {
return $this->hasMany(AchievementUserCard::class, ['user_card_id' => 'id'])->with('achievement'); return $this->hasMany(AchievementUserCard::class, ['user_card_id' => 'id'])->with('achievement');
} }
@ -201,14 +204,22 @@ class UserCard extends \yii\db\ActiveRecord
return $this->hasMany(CardSkill::class, ['card_id' => 'id'])->with('skill'); return $this->hasMany(CardSkill::class, ['card_id' => 'id'])->with('skill');
} }
public function getUser() public static function getNameSkills()
{
return ArrayHelper::map(Skill::find()->all(), 'id', 'name');
}
public function getUser(): ActiveQuery
{ {
return $this->hasOne(User::class, ['id' => 'id_user']); return $this->hasOne(User::class, ['id' => 'id_user']);
} }
public static function getNameSkills() /**
* @throws Exception
*/
public static function getIdByUserId($user_id)
{ {
return ArrayHelper::map(Skill::find()->all(), 'id', 'name'); return ArrayHelper::getValue(self::find()->where(['id_user' => $user_id])->one(), 'id');
} }
public static function getUserList() public static function getUserList()
@ -274,5 +285,23 @@ class UserCard extends \yii\db\ActiveRecord
$user_card->save(); $user_card->save();
} }
public static function getUserIdByCardId ($card_id)
{
$userCard = self::findOne(['id' => $card_id]);
if (empty($userCard)) {
return null;
}
return $userCard['id_user'];
}
public static function getCardIdByUserId ($user_id)
{
$userCard = self::findOne(['id_user' => $user_id]);
if (empty($userCard)) {
return null;
}
return $userCard['id'];
}
} }

View File

@ -3,19 +3,19 @@
use yii\db\Migration; use yii\db\Migration;
/** /**
* Class m211123_082634_change_foreign_key_in_project_user_from_user_card_table_to_user_table * Class m211123_082634_add_foreign_key_from_project_user_to_user_table
*/ */
class m211123_082634_change_foreign_key_in_project_user_from_user_card_table_to_user_table extends Migration class m211123_082634_add_foreign_key_from_project_user_to_user_table extends Migration
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function safeUp() public function safeUp()
{ {
$this->dropForeignKey('project_user_ibfk_user_card', 'project_user'); $this->alterColumn('project_user', 'card_id', $this->integer()->defaultValue(null));
$this->dropColumn('project_user', 'card_id'); $this->addColumn('project_user', 'user_id', $this->integer(11)->notNull()->defaultValue(null));
$this->addColumn('project_user', 'user_id', $this->integer(11)->notNull()); $this->addForeignKey('user_project_user', 'project_user', 'user_id', 'user', 'id');
// $this->addForeignKey('user_project_user', 'project_user', 'user_id', 'user', 'id');
} }
/** /**
@ -23,9 +23,11 @@ class m211123_082634_change_foreign_key_in_project_user_from_user_card_table_to_
*/ */
public function safeDown() public function safeDown()
{ {
$this->alterColumn('project_user', 'card_id', $this->integer()->notNull());
$this->dropForeignKey('user_project_user', 'project_user'); $this->dropForeignKey('user_project_user', 'project_user');
$this->dropColumn('project_user', 'user_id'); $this->dropColumn('project_user', 'user_id');
$this->addColumn('project_user', 'card_id', $this->integer(11)->notNull());
// $this->addColumn('project_user', 'card_id', $this->integer(11)->notNull());
// $this->addForeignKey( // $this->addForeignKey(
// 'project_user_ibfk_user_card', // 'project_user_ibfk_user_card',
// 'project_user', // 'project_user',
@ -46,7 +48,7 @@ class m211123_082634_change_foreign_key_in_project_user_from_user_card_table_to_
public function down() public function down()
{ {
echo "m211123_082634_change_foreign_key_in_project_user_from_user_card_table_to_user_table cannot be reverted.\n"; echo "m211123_082634_add_foreign_key__from_project_user__to_user_table cannot be reverted.\n";
return false; return false;
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff