diff --git a/README.md b/README.md index 07c710f..4f7adae 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,19 @@ php yii migrate --migrationPath=@vendor/yii2mod/yii2-cms/migrations 'modules' => [ 'admin' => [ 'controllerMap' => [ - 'cms' => 'yii2mod\cms\controllers\CmsController' - // You can set your template files - 'layout' => '@app/modules/backend/views/layouts/main', - 'viewPath' => '@app/modules/backend/views/cms/' + 'cms' => 'yii2mod\cms\controllers\CmsController', + // Also you can override some controller properties. + 'cms' => [ + 'class' => 'yii2mod\cms\controllers\CmsController', + 'searchClass' => [ + 'class' => 'yii2mod\cms\models\search\CmsSearch', + 'pageSize' => 25 + ], + 'modelClass' => 'Your own cms model class', + 'indexView' => 'custom path to index view file', + 'createView' => 'custom path to create view file', + 'updateView' => 'custom path to update view file', + ], ], ], ], diff --git a/controllers/CmsController.php b/controllers/CmsController.php index 818c9b3..64790cb 100644 --- a/controllers/CmsController.php +++ b/controllers/CmsController.php @@ -7,7 +7,6 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; -use yii2mod\cms\models\search\CmsModelSearch; use yii2mod\editable\EditableAction; use yii2mod\toggle\actions\ToggleAction; @@ -18,9 +17,29 @@ class CmsController extends Controller { /** - * @var string view path + * @var string path to index view file, which is used in admin panel */ - public $viewPath = '@vendor/yii2mod/yii2-cms/views/cms/'; + public $indexView = '@vendor/yii2mod/yii2-cms/views/cms/index'; + + /** + * @var string path to create view file, which is used in admin panel + */ + public $createView = '@vendor/yii2mod/yii2-cms/views/cms/create'; + + /** + * @var string path to update view file, which is used in admin panel + */ + public $updateView = '@vendor/yii2mod/yii2-cms/views/cms/update'; + + /** + * @var string search class name for searching + */ + public $searchClass = 'yii2mod\cms\models\search\CmsSearch'; + + /** + * @var string model class name for CRUD operations + */ + public $modelClass = 'yii2mod\cms\models\CmsModel'; /** * @inheritdoc @@ -60,14 +79,15 @@ public function actions() /** * Lists all CmsModel models. + * * @return mixed */ public function actionIndex() { - $searchModel = new CmsModelSearch(); + $searchModel = Yii::createObject($this->searchClass); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - return $this->render($this->viewPath . 'index', [ + return $this->render($this->indexView, [ 'dataProvider' => $dataProvider, 'searchModel' => $searchModel ]); @@ -75,26 +95,29 @@ public function actionIndex() /** * Creates a new CmsModel model. - * If creation is successful, the browser will be redirected to the 'view' page. + * + * If creation is successful, the browser will be redirected to the 'index' page. + * * @return mixed */ public function actionCreate() { - $model = new CmsModel(); + $model = Yii::createObject($this->modelClass); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been created.')); return $this->redirect(['index']); } - return $this->render($this->viewPath . 'create', [ - 'model' => $model, + return $this->render($this->createView, [ + 'model' => $model ]); } /** * Updates an existing CmsModel model. - * If update is successful, the browser will be redirected to the 'view' page. + * + * If update is successful, the browser will be redirected to the 'index' page. * * @param integer $id * @@ -108,13 +131,15 @@ public function actionUpdate($id) Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been updated.')); return $this->redirect(['index']); } - return $this->render($this->viewPath . 'update', [ - 'model' => $model, + + return $this->render($this->updateView, [ + 'model' => $model ]); } /** * Deletes an existing CmsModel model. + * * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id @@ -135,15 +160,17 @@ public function actionDelete($id) * @param integer $id * * @return CmsModel the loaded model + * * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { - if (($model = CmsModel::findOne($id)) !== null) { + $cmsModel = $this->modelClass; + + if (($model = $cmsModel::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException(Yii::t('yii2mod.cms', 'The requested page does not exist.')); } } - -} +} \ No newline at end of file diff --git a/models/CmsModel.php b/models/CmsModel.php index 493ad48..193f35c 100644 --- a/models/CmsModel.php +++ b/models/CmsModel.php @@ -99,6 +99,7 @@ public static function find() * Find page by url * * @param $url + * * @return array|null|ActiveRecord */ public function findPage($url) @@ -124,6 +125,7 @@ public function getContent() * Replaces widget short code on appropriate widget * * @param $data + * * @return string */ private function replace($data) diff --git a/models/search/CmsModelSearch.php b/models/search/CmsSearch.php similarity index 87% rename from models/search/CmsModelSearch.php rename to models/search/CmsSearch.php index 170486c..3a32156 100644 --- a/models/search/CmsModelSearch.php +++ b/models/search/CmsSearch.php @@ -6,11 +6,16 @@ use yii2mod\cms\models\CmsModel; /** - * Class CmsModelSearch + * Class CmsSearch * @package yii2mod\cms\models\search */ -class CmsModelSearch extends CmsModel +class CmsSearch extends CmsModel { + /** + * @var int the default page size. + */ + public $pageSize = 10; + /** * @inheritdoc */ @@ -35,7 +40,7 @@ public function search($params) $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ - 'pageSize' => 10 + 'pageSize' => $this->pageSize ] ]); diff --git a/tests/PageActionTest.php b/tests/PageActionTest.php index 9e28a24..9d0c5c5 100644 --- a/tests/PageActionTest.php +++ b/tests/PageActionTest.php @@ -31,9 +31,11 @@ public function testViewPage() $this->assertEquals('about-us', $response['params']['model']['url']); } + /** + * @expectedException \yii\web\NotFoundHttpException + */ public function testViewNotExistPage() { - $this->setExpectedException('yii\web\NotFoundHttpException'); $this->runAction(['pageId' => 'not exist page']); }