FieldEditor.php 1 KB
<?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()];
        }
    }
}