guild/backend/modules/notes/models/Note.php

72 lines
2.0 KiB
PHP
Raw Normal View History

2019-12-05 15:05:33 +03:00
<?php
namespace backend\modules\notes\models;
2019-12-05 17:03:00 +03:00
use Yii;
2019-12-05 15:05:33 +03:00
use common\models\FieldsValueNew;
class Note extends \common\models\Note
{
2019-12-05 17:03:00 +03:00
public $fields;
public function init()
{
parent::init();
$fieldValue = FieldsValueNew::find()
->where(
[
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_NOTE,
])
->all();
$array = [];
if(!empty($fieldValue)){
foreach ($fieldValue as $item){
array_push($array,
['field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
'field_name' => $item->field->name]);
}
$this->fields = $array;
}
else{
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
'field_name' => null,
],
];
}
}
2019-12-05 15:05:33 +03:00
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('Note');
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_NOTE]);
foreach ( $post['fields'] as $item) {
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_NOTE;
$fieldsValue->order = $item['order'];
$fieldsValue->value = $item['value'];
2019-12-05 17:03:00 +03:00
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
}
2019-12-05 15:05:33 +03:00
$fieldsValue->save();
}
parent::afterSave($insert, $changedAttributes);
}
}