Пример #1
0
 public function actionCreate($app, $id = null)
 {
     $app = $this->getApp($app);
     if ($id === null || ($model = Items::findOne(["id" => $id])) === null) {
         $model = new Items();
         $model->app_id = $app->id;
         $model->regBehaviors();
     } else {
         if ($this->module->accessRoles === null && !Yii::$app->user->can('admin') && !Yii::$app->user->can('accessStrange') && $model->user_id !== Yii::$app->user->identity->id) {
             Yii::$app->getSession()->setFlash('warning', Yii::t('backend', 'Доступ запрещён'));
             return $this->redirect(['index', 'app' => $app->id]);
         }
     }
     if (Yii::$app->request->post("reload") == 'true') {
         $model->load(Yii::$app->request->post());
         if ($model->validate() || true) {
             $renderedElements = Yii::$app->request->post("renderedElements", []);
             $renderElements = [];
             $removeElements = [];
             foreach ($model->renderedElements as $element) {
                 if (!in_array($element, $renderedElements)) {
                     $renderElements[] = $element;
                 } elseif ($model->elements[$element]->refresh) {
                     $renderElements[] = $element;
                     $removeElements[] = $element;
                 } elseif ($model->elements[$element]->related && $model->isAttributeChanged($model->elements[$element]->related)) {
                     $renderElements[] = $element;
                     $removeElements[] = $element;
                 }
             }
             $renders = [];
             foreach ($renderElements as $element) {
                 $path = '@worstinme/zoo/elements/' . $model->elements[$element]['type'] . '/form.php';
                 $renders[$element] = $this->renderAjax($path, ['model' => $model, 'element' => $model->elements[$element], 'attribute' => $element]);
             }
             $removeElements = array_merge($removeElements, array_diff($renderedElements, $model->renderedElements));
             Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ['renderElements' => $renders, 'removeElements' => $removeElements, 'renderedElements' => $model->renderedElements];
         }
     } elseif (Yii::$app->request->isAjax) {
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             return ['success' => true, 'model' => $model->getAttributes()];
         } else {
             return ['success' => false, 'model' => $model->getAttributes(), 'errors' => $model->errors];
         }
     } elseif ($model->load(Yii::$app->request->post()) && $model->save()) {
         Yii::$app->getSession()->setFlash('success', Yii::t('backend', 'Материал сохранён'));
         if (Yii::$app->request->post('save') == 'close') {
             return $this->redirect(['index', 'app' => $app->id]);
         } else {
             return $this->redirect(['create', 'app' => $app->id, 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Пример #2
0
 public function run()
 {
     if ($this->id !== null && ($item = Items::findOne($this->id)) !== null) {
         $teaser = \worstinme\zoo\helpers\TemplateHelper::render($item, 'teaser');
         return "<div class='item item-teaser'>" . $teaser . "</div>";
     }
     if ($this->ids !== null) {
         $ids = explode(",", $this->ids);
         $items = Items::find()->where(['id' => $ids])->all();
         $html = null;
         if (count($items)) {
             $html .= '<div class="uk-grid uk-grid-width-medium-1-2 uk-grid-large uk-grid-match" data-uk-grid-margin>';
             foreach ($items as $item) {
                 $teaser = \worstinme\zoo\helpers\TemplateHelper::render($item, 'teaser');
                 $html .= "<div><div class='item item-teaser'>" . $teaser . "</div></div>";
             }
             $html .= '</div>';
         }
         return $html;
     }
     return null;
 }