Skip to content

Commit

Permalink
add new properties to CmsController, rename cms search class
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-chepurnoi committed Nov 10, 2016
1 parent 5abfdb3 commit 622a9d1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 23 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
],
Expand Down
57 changes: 42 additions & 15 deletions controllers/CmsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -60,41 +79,45 @@ 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
]);
}

/**
* 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
*
Expand All @@ -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
Expand All @@ -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.'));
}
}

}
}
2 changes: 2 additions & 0 deletions models/CmsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public static function find()
* Find page by url
*
* @param $url
*
* @return array|null|ActiveRecord
*/
public function findPage($url)
Expand All @@ -124,6 +125,7 @@ public function getContent()
* Replaces widget short code on appropriate widget
*
* @param $data
*
* @return string
*/
private function replace($data)
Expand Down
11 changes: 8 additions & 3 deletions models/search/CmsModelSearch.php → models/search/CmsSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -35,7 +40,7 @@ public function search($params)
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10
'pageSize' => $this->pageSize
]
]);

Expand Down
4 changes: 3 additions & 1 deletion tests/PageActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down

0 comments on commit 622a9d1

Please sign in to comment.