/** * @inheritdoc */ public function init() { /** * @var $activeWidgets \common\models\Widget */ $activeWidgets = Widget::find()->where(['widget_location' => $this->location])->orderBy(['widget_order' => SORT_ASC])->all(); if ($activeWidgets) { foreach ($activeWidgets as $activeWidget) { $this->_widget[] = ArrayHelper::merge($this->defaultConfig, $this->config, $activeWidget->getConfig(), ['id' => $activeWidget->id]); } } }
/** * Activated widget via ajax * * @param $id string * * @return null|string */ public function actionAjaxActivate($id) { $model = new Widget(['scenario' => 'activate']); if ($model->load(Yii::$app->request->post())) { $count = Widget::find()->where(['widget_location' => $model->widget_location])->count(); // Require widget config $configPath = $this->_widgetDir . $id . '/config/main.php'; $widgetConfig = (require $configPath); // Set attribute of model $model->setAttributes($widgetConfig); $model->widget_dir = $id; $model->widget_config = Json::encode($model->widget_config); $model->widget_order = $count; if ($model->save()) { return $this->renderPartial('_activated', ['activatedWidget' => $model, 'availableWidget' => [$model->widget_dir => $widgetConfig]]); } } return null; }