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

108 lines
3.0 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;
use Yii;
2018-10-11 11:15:09 +03:00
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,
'type_file' => $item->type_file,
2019-06-27 15:41:58 +03:00
'field_name' => $item->field->name]);
2018-10-11 11:15:09 +03:00
}
$this->fields = $array;
} else {
$this->fields = [
[
'field_id' => null,
'value' => null,
2018-10-11 11:15:09 +03:00
'order' => null,
'type_file' => 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;
}
}
2020-08-06 13:19:20 +03:00
public function behaviors()
{
return [
'log' => [
'class' => \common\behaviors\LogBehavior::class,
]
];
}
2018-10-11 11:15:09 +03:00
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) {
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->value = $item['value'];
$fieldsValue->order = $item['order'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_PROJECT;
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
}
$fieldsValue->save();
2018-10-11 11:15:09 +03:00
}
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;
$prUser->user_id = $prUser->card->user->id;
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
}
}