FieldEditor.php 815 Bytes
<?php
namespace backend\components;
use common\models\Fields;
use yii\base\Widget;


class FieldEditor extends Widget
{
    public $item_id;
    public $model;
    public $template;

    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'=>$widgetData]);
    }

    protected function findModel()
    {
        if (($model = Fields::find()->where(['table_id'=>$this->item_id, 'table_name'=>$this->model, 'field_type'=>$this->template])->all()) !== null) {

            return $model;

        } else {
            return [new Fields()];
        }
    }
}