FieldEditor.php
982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace common\widgets;
use common\models\Fields;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
class FieldEditor extends Widget
{
    public $item_id;
    public $model;
    public $template;
    public $language;
    public function init(){
        parent::init();
    }
    public function run()
    {
        if($this->item_id && $this->model){
            $widgetData = $this->findModel();
        } else {
            $widgetData= [new Fields()];
        }
        return $this->render($this->template.'_field',['model'=>ArrayHelper::toArray($widgetData)]);
    }
    protected function findModel()
    {
        if (($model = Fields::find()->where([
                'table_id'=>$this->item_id,
                'table_name'=>$this->model,
                'field_type'=>$this->template,
                'language'=>$this->language,
            ])->all())) {
            return $model;
        } else {
            return [new Fields()];
        }
    }
}