/** * @param null $id * @return string|\yii\web\Response */ public function actionConfig($id = null) { /** @var Block $model */ if ($id === null) { $model = Yii::$app->session->get('new-block'); if (!$model) { $this->redirect('create'); } } else { $model = $this->findModel($id); } /** @var \nullref\cms\components\BlockManager $blockManager */ $blockManager = Yii::$app->getModule('cms')->get('blockManager'); /** @var \nullref\cms\components\Block $block */ $block = $blockManager->getBlock($model->class_name); if (!$model->isNewRecord) { $block->setAttributes($model->getData()); } if ($block->load(Yii::$app->request->post()) && $block->validate()) { $model->setData($block); $model->save(); TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->id); TagDependency::invalidate(Yii::$app->cache, 'cms.block.' . $model->oldAttributes['id']); Yii::$app->session->remove('new-block'); /** Create relation when path page_id parameter */ if ($pageId = Yii::$app->request->get('page_id')) { if ($id === null) { $pageHasBlock = new PageHasBlock(['page_id' => $pageId, 'block_id' => $model->id]); $pageHasBlock->save(false, ['page_id', 'block_id']); } return $this->redirect(['/cms/admin/page/update', 'id' => $pageId]); } if ($redirect = Yii::$app->request->get('redirect_to')) { return $this->redirect($redirect); } return $this->redirect(['view', 'id' => $model->id, 'page_id' => $pageId]); } if (Yii::$app->request->isAjax) { return $this->renderAjax('config', ['block' => $block]); } return $this->render('config', ['block' => $block]); }
/** * @return $this */ public function getPages() { return $this->hasMany(Page::className(), ['id' => 'page_id'])->viaTable(PageHasBlock::tableName(), ['block_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getItems() { return $this->hasMany(PageHasBlock::className(), ['page_id' => 'id'])->orderBy(['order' => SORT_ASC]); }