add multi-assignment to tasks and projects

This commit is contained in:
iIronside 2021-12-16 17:14:33 +03:00
parent 27c0951202
commit 994353d557
17 changed files with 10342 additions and 54 deletions

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,
]); ]);
@ -124,4 +147,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'],
]; ];
} }
@ -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

@ -31,7 +31,7 @@ use yii\widgets\ActiveForm;
'options' => ['placeholder' => '...','class' => 'form-control'], 'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [ 'pluginOptions' => [
'allowClear' => true, 'allowClear' => true,
'multiple' => false,//true, 'multiple' => true,
], ],
] ]
) ?> ) ?>

View File

@ -1,5 +1,6 @@
<?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 yii\helpers\Html; use yii\helpers\Html;
@ -16,6 +17,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([
@ -34,6 +37,11 @@ $this->params['breadcrumbs'][] = $this->title;
'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(), 'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'value' => 'user.username' 'value' => 'user.username'
], ],
[
'attribute' => 'card_id',
'filter' => UserCard::find()->select(['fio', 'id'])->indexBy('id')->column(),
'value' => 'card.fio'
],
['class' => 'yii\grid\ActionColumn'], ['class' => 'yii\grid\ActionColumn'],
], ],

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

@ -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

@ -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

@ -4,6 +4,7 @@ use backend\modules\project\models\Project;
use backend\modules\project\models\ProjectUser; use backend\modules\project\models\ProjectUser;
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,14 +30,34 @@ $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(),
'value' => 'project.name',
'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(),
'value' => 'userIdCreator.username',
'options' => [
'class' => 'form-control',
'placeholder' => 'Выберите значение'
],
])
// 'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
], ],
[ [
'attribute' => 'user_id', 'attribute' => 'user_id',

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,7 +52,8 @@ class ProjectUser extends \yii\db\ActiveRecord
{ {
return [ return [
'id' => 'ID', 'id' => 'ID',
'project_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');
} }
@ -206,6 +209,19 @@ class UserCard extends \yii\db\ActiveRecord
return ArrayHelper::map(Skill::find()->all(), 'id', 'name'); return ArrayHelper::map(Skill::find()->all(), 'id', 'name');
} }
public function getUser(): ActiveQuery
{
return $this->hasOne(User::class, ['id' => 'id_user']);
}
/**
* @throws Exception
*/
public static function getIdByUserId($user_id)
{
return ArrayHelper::getValue(self::find()->where(['id_user' => $user_id])->one(), 'id');
}
public static function getUserList() public static function getUserList()
{ {
return ArrayHelper::map(self::find()->all(), 'id', 'fio'); return ArrayHelper::map(self::find()->all(), 'id', 'fio');
@ -268,4 +284,23 @@ class UserCard extends \yii\db\ActiveRecord
$user_card->id_user = $user_id; $user_card->id_user = $user_id;
$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

@ -12,8 +12,10 @@ class m211123_082634_add_foreign_key_from_project_user_to_user_table extends Mig
*/ */
public function safeUp() public function safeUp()
{ {
$this->addColumn('project_user', 'user_id', $this->integer(11)->notNull()); $this->alterColumn('project_user', 'card_id', $this->integer()->defaultValue(null));
$this->addColumn('project_user', 'user_id', $this->integer(11)->notNull()->defaultValue(null));
$this->addForeignKey('user_project_user', 'project_user', 'user_id', 'user', 'id'); $this->addForeignKey('user_project_user', 'project_user', 'user_id', 'user', 'id');
} }
/** /**
@ -21,6 +23,7 @@ class m211123_082634_add_foreign_key_from_project_user_to_user_table extends Mig
*/ */
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');

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff