guild/backend/modules/project/models/Project.php

89 lines
2.5 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace backend\modules\project\models;
use common\classes\Debug;
use common\models\FieldsValue;
2019-06-27 15:41:58 +03:00
use common\models\FieldsValueNew;
2018-10-11 11:15:09 +03:00
use common\models\ProjectUser;
use yii\helpers\ArrayHelper;
class Project extends \common\models\Project
{
public $fields;
public $user;
public function init()
{
parent::init();
2019-06-27 15:41:58 +03:00
$fieldValue = FieldsValueNew::find()
2018-10-11 11:15:09 +03:00
->where(
[
2019-06-27 15:41:58 +03:00
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_PROJECT,
2018-10-11 11:15:09 +03:00
])
->all();
$array = [];
if (!empty($fieldValue)) {
foreach ($fieldValue as $item) {
2019-06-27 15:41:58 +03:00
array_push($array, [
'field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
'field_name' => $item->field->name]);
2018-10-11 11:15:09 +03:00
}
$this->fields = $array;
} else {
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
2019-06-27 15:41:58 +03:00
'field_name' => null,
2018-10-11 11:15:09 +03:00
],
];
}
$user = ArrayHelper::getColumn(ProjectUser::find()->where(['project_id' => \Yii::$app->request->get('id')])->all(),
'card_id');
if (!empty($user)) {
$this->user = $user;
}
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('Project');
2019-06-27 15:41:58 +03:00
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROJECT]);
2018-10-11 11:15:09 +03:00
foreach ($post['fields'] as $item) {
2019-06-27 15:41:58 +03:00
$fildsValue = new FieldsValueNew();
2018-10-11 11:15:09 +03:00
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->order = $item['order'];
2019-06-27 15:41:58 +03:00
$fildsValue->item_id = $this->id;
$fildsValue->item_type = FieldsValueNew::TYPE_PROJECT;
2018-10-11 11:15:09 +03:00
$fildsValue->save();
}
ProjectUser::deleteAll(['project_id' => $this->id]);
2018-11-23 16:20:49 +03:00
if($post['user']){
foreach ($post['user'] as $item) {
$prUser = new ProjectUser();
$prUser->project_id = $this->id;
$prUser->card_id = $item;
2018-10-11 11:15:09 +03:00
2018-11-23 16:20:49 +03:00
$prUser->save();
}
2018-10-11 11:15:09 +03:00
}
2018-11-23 16:20:49 +03:00
2018-10-11 11:15:09 +03:00
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}