guild/backend/modules/project/models/Project.php
2018-11-23 16:20:49 +03:00

83 lines
2.2 KiB
PHP

<?php
namespace backend\modules\project\models;
use common\classes\Debug;
use common\models\FieldsValue;
use common\models\ProjectUser;
use yii\helpers\ArrayHelper;
class Project extends \common\models\Project
{
public $fields;
public $user;
public function init()
{
parent::init();
$fieldValue = FieldsValue::find()
->where(
[
'project_id' => \Yii::$app->request->get('id'),
'card_id' => null,
'company_id' => null,
])
->all();
$array = [];
if (!empty($fieldValue)) {
foreach ($fieldValue as $item) {
array_push($array, ['field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order]);
}
$this->fields = $array;
} else {
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
],
];
}
$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');
FieldsValue::deleteAll(['project_id' => $this->id]);
foreach ($post['fields'] as $item) {
$fildsValue = new FieldsValue();
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->order = $item['order'];
$fildsValue->project_id = $this->id;
$fildsValue->save();
}
ProjectUser::deleteAll(['project_id' => $this->id]);
if($post['user']){
foreach ($post['user'] as $item) {
$prUser = new ProjectUser();
$prUser->project_id = $this->id;
$prUser->card_id = $item;
$prUser->save();
}
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}