/**
  * Creates a new WidgetConfig model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new WidgetConfig();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function run($modal = null)
 {
     if (!($widget_id = Yii::$app->request->getBodyParam('widget_id'))) {
         throw new BadRequestHttpException(Yii::t('gromver.platform', "Widget ID isn't specified"));
     }
     if (!($widget_class = Yii::$app->request->getBodyParam('widget_class'))) {
         throw new BadRequestHttpException(Yii::t('gromver.platform', "Widget Class isn't specified"));
     }
     if (($widget_context = Yii::$app->request->getBodyParam('widget_context')) === null) {
         throw new BadRequestHttpException(Yii::t('gromver.platform', "Widget Context isn't specified"));
     }
     $selected_context = Yii::$app->request->getBodyParam('selected_context', $widget_context);
     $task = Yii::$app->request->getBodyParam('task');
     if (($url = Yii::$app->request->getBodyParam('url')) === null) {
         throw new BadRequestHttpException(Yii::t('gromver.platform', "Widget page url isn't specified"));
     }
     //$url = Yii::$app->request->getBodyParam('url', Yii::$app->request->getReferrer());
     if ($task == 'delete') {
         if (Yii::$app->request->getBodyParam('bulk-method')) {
             foreach (WidgetConfig::find()->where('widget_id=:widget_id AND context>=:context', [':widget_id' => $widget_id, ':context' => $selected_context])->each() as $configModel) {
                 $configModel->delete();
             }
         } elseif ($configModel = WidgetConfig::findOne(['widget_id' => $widget_id, 'context' => $selected_context])) {
             $configModel->delete();
         }
         if ($modal) {
             ModalIFrame::refreshParent();
         }
     }
     $widget_config = Yii::$app->request->getBodyParam('widget_config', '[]');
     $widgetConfig = Json::decode($widget_config);
     $widgetConfig['id'] = $widget_id;
     $widgetConfig['context'] = $selected_context;
     $widgetConfig['skipInit'] = true;
     /** @var \gromver\platform\core\modules\widget\widgets\Widget $widget */
     $widget = new $widget_class($widgetConfig);
     $model = new ObjectModel($widget);
     if (($task == 'save' || $task == 'refresh') && $model->load(Yii::$app->request->post())) {
         if ($model->validate() && $task == 'save') {
             $configModel = WidgetConfig::findOne(['widget_id' => $widget_id, 'context' => $selected_context]) or $configModel = new WidgetConfig();
             $configModel->loadDefaultValues();
             $configModel->widget_id = $widget_id;
             $configModel->widget_class = $widget_class;
             $configModel->context = $selected_context;
             $configModel->url = $url;
             $configModel->setParamsArray($model->toArray());
             $configModel->save();
             if (Yii::$app->request->getBodyParam('bulk-method')) {
                 foreach (WidgetConfig::find()->where('widget_id=:widget_id AND context>:context', [':widget_id' => $widget_id, ':context' => $selected_context])->each() as $configModel) {
                     /** @var $configModel WidgetConfig */
                     $configModel->delete();
                 }
             }
             if ($modal) {
                 ModalIFrame::refreshParent();
             } else {
                 return $this->redirect($url);
             }
         }
     }
     if ($modal) {
         Yii::$app->applyModalLayout();
     }
     return $this->controller->render($this->view, ['model' => $model, 'widget' => $widget, 'widget_id' => $widget_id, 'widget_class' => $widget_class, 'widget_config' => $widget_config, 'widget_context' => $widget_context, 'selected_context' => $selected_context, 'url' => $url]);
 }