guild/backend/modules/project/models/Project.php
2020-08-04 11:14:55 +03:00

98 lines
2.8 KiB
PHP
Executable File

<?php
namespace backend\modules\project\models;
use common\classes\Debug;
use common\models\FieldsValue;
use common\models\FieldsValueNew;
use common\models\ProjectUser;
use yii\helpers\ArrayHelper;
use Yii;
class Project extends \common\models\Project
{
public $fields;
public $user;
public function init()
{
parent::init();
$fieldValue = FieldsValueNew::find()
->where(
[
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_PROJECT,
])
->all();
$array = [];
if (!empty($fieldValue)) {
foreach ($fieldValue as $item) {
array_push($array, [
'field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
'type_file' => $item->type_file,
'field_name' => $item->field->name]);
}
$this->fields = $array;
} else {
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
'type_file' => null,
'field_name' => 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');
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROJECT]);
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();
}
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
}
}