guild/backend/modules/company/models/Company.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2018-10-11 17:24:47 +03:00
<?php
namespace backend\modules\company\models;
use common\models\FieldsValue;
2019-06-26 17:22:10 +03:00
use common\models\FieldsValueNew;
use Yii;
2018-10-11 17:24:47 +03:00
class Company extends \common\models\Company
{
public $fields;
public function init()
{
parent::init();
2019-06-26 17:22:10 +03:00
$fieldValue = FieldsValueNew::find()
->where(
[
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_COMPANY,
])
2018-10-11 17:24:47 +03:00
->all();
$array = [];
if(!empty($fieldValue)){
foreach ($fieldValue as $item){
2019-06-26 17:22:10 +03:00
array_push($array,
['field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
'type_file' => $item->type_file,
2019-06-26 17:22:10 +03:00
'field_name' => $item->field->name]);
2018-10-11 17:24:47 +03:00
}
$this->fields = $array;
}
else{
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
2019-06-26 17:22:10 +03:00
'field_name' => null,
'type_file' => null,
2018-10-11 17:24:47 +03:00
],
];
}
}
2020-08-06 13:19:20 +03:00
public function behaviors()
{
return [
'log' => [
'class' => \common\behaviors\LogBehavior::class,
]
];
}
2018-10-11 17:24:47 +03:00
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('Company');
2019-06-27 15:41:58 +03:00
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_COMPANY]);
2018-10-11 17:24:47 +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_COMPANY;
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
}
2018-10-11 17:24:47 +03:00
$fieldsValue->save();
2018-10-11 17:24:47 +03:00
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}