public function down()
 {
     $template = Template::findByKey('basic-template');
     if ($template !== null) {
         $template->delete();
     }
     $this->dropColumn(Template::tableName(), 'is_deleted');
 }
 public function actionDelete()
 {
     RequestHelper::allowAjaxOnly();
     RequestHelper::allowOnlyJsonRequest();
     $template_id = (int) Yii::$app->request->post('template_id', 0);
     if ($template_id <= 0) {
         throw new BadRequestHttpException();
     }
     $model = Template::findById($template_id);
     return RequestHelper::jsonpFormat($model->delete() !== false);
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTemplate()
 {
     return $this->hasOne(Template::className(), ['id' => 'template_id'])->inverseOf('templateRegions');
 }
 /**
  * @return \DotPlant\Monster\models\Template
  */
 public function getTemplate()
 {
     return $this->template_id > 0 ? Template::findById($this->template_id) : null;
 }
 /**
  * @param ActionData $actionData
  *
  * @throws \yii\base\InvalidConfigException
  * @throws \yii\web\NotFoundHttpException
  * @throws bool
  */
 public function run(&$actionData)
 {
     // first we need to find our entity
     $entity = $this->defineMainEntity($actionData);
     if ($this->defaultTemplateKey === null) {
         throw new yii\base\InvalidConfigException('You must provide default template key');
     }
     $templateId = ArrayHelper::getValue($this->visualBuilderProvided(), 'template.templateId');
     if ($templateId !== null) {
         $entity->setTemplateId($templateId);
     }
     /** @var Template $template */
     $template = null;
     if ($this->forceTemplate !== false) {
         $template = Template::findByKey($this->forceTemplate);
     } elseif ($templateId = $entity->getTemplateId()) {
         $template = Template::findById($templateId);
     }
     if ($template === null) {
         $template = Template::findByKey($this->defaultTemplateKey);
     }
     // now we have template and it can be rendered
     /**
      * @var string $action Action type
      */
     if ($this->action() !== self::ACTION_DEFAULT) {
         $this->providersSupplied($entity, 'entity');
         $this->materialsSupplied($entity, 'entity.materialsByRegionDecl', true);
         $this->providersSupplied($template, 'template');
         $this->templateRegions($template, 'template');
     }
     $this->handleMaterialsProviders($entity, $entity);
     $packedTemplateProviders = [];
     $providedKeysTemplate = [];
     $packedEntityProviders = [];
     $providedKeysEntity = [];
     $dataTemplate = DataProviderProcessor::process($template->getEntityDataProviders(), $actionData, $packedTemplateProviders, $providedKeysTemplate);
     $dataEntity = DataProviderProcessor::process($entity->getEntityDataProviders(), $actionData, $packedEntityProviders, $providedKeysEntity);
     $actionData->result['dataByTemplateRegion'] = ArrayHelper::merge($dataTemplate, $dataEntity);
     $actionData->result['model'] =& $entity;
     if (YII_ENV === 'dev') {
         Yii::$app->params['actionData'] =& $actionData;
         //            Yii::$app->params['providers'] = $packed;
     }
     $layoutData = $this->applyLayout($entity, $actionData, $dataEntity);
     $actionData->viewFile = '@DotPlant/Monster/views/monster-template.php';
     $actionData->result['templateRegions'] = $template->templateRegions;
     /** @var \yii\web\Request|\DotPlant\Monster\behaviors\MonsterRequest $request */
     $request = Yii::$app->request;
     if ($request->isEditMode()) {
         $view = $actionData->controller->view;
         $jsonData = yii\helpers\Json::encode(['template' => ['id' => $template->id, 'key' => $template->key, 'providedKeys' => $providedKeysTemplate, 'regions' => ArrayHelper::map($template->templateRegions, 'id', function (TemplateRegion $item) {
             return ['id' => $item->id, 'name' => $item->name, 'key' => $item->key, 'entity_dependent' => (bool) $item->entity_dependent];
         }), 'providers' => $packedTemplateProviders], 'entity' => ['id' => isset($entity->id) ? $entity->id : null, 'name' => $entity->formName(), 'class' => $entity::className(), 'providedKeys' => $providedKeysEntity, 'providers' => $packedEntityProviders], 'layout' => $layoutData]);
         $js = "window.MONSTER_EDIT_MODE_DATA = {$jsonData};";
         $view->registerJs($js, yii\web\View::POS_BEGIN, 'edit-mode-vars');
     }
 }