Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *<?php
- /**
- * Created by PhpStorm.
- * User: hlogeon
- * Date: 13.08.14
- * Time: 12:33
- *
- * @author Degtyaruk Andrey <hlogeon1@gmail.com>
- *
- */
- /**
- *
- * Created by PhpStorm.
- * User: hlogeon
- * Date: 21.06.14
- * Time: 14:29
- */
- abstract class BaseAction extends CAction{
- /**
- * @var string model class for action
- */
- public $modelClass;
- /**
- * @var string view for render
- */
- public $view = 'index';
- /**
- * @var array
- */
- public $renderParams = array();
- /**
- * If we have form on page that action renders - provide it here
- * @var string
- */
- public $formName = '';
- /**
- * Render with specified params
- */
- public function render()
- {
- if(Yii::app()->request->isAjaxRequest){
- $this->controller->renderPartial($this->view, $this->renderParams);
- }
- else{
- $this->controller->render($this->view, $this->renderParams);
- }
- }
- /**
- * Returns model by its ID
- *
- * @param $id
- *
- * @return EMongoDocument
- * @throws Exception
- *
- * @author Degtyaruk Andrey <hlogeon1@gmail.com>
- */
- public function getModelById($id)
- {
- if(is_string($id))
- $id = new MongoId($id);
- if(!$id instanceof MongoId)
- throw new Exception("ID is not instance of MongoId");
- return EMongoDocument::model($this->modelClass)->findByPk($id);
- }
- /**
- * Return modelClass
- *
- * @return EMongoDocument
- *
- * @author Degtyaruk Andrey <hlogeon1@gmail.com>
- */
- public function getModelClass()
- {
- return EMongoDocument::model($this->modelClass);
- }
- /**
- * @param $model
- *
- * @author Degtyaruk Andrey <hlogeon1@gmail.com>
- */
- protected function performAjaxValidation($model)
- {
- if(isset($_POST['ajax']))
- {
- echo CActiveForm::validate($model);
- Yii::app()->end();
- }
- }
- }
- /**
- *
- * Created by PhpStorm.
- * User: hlogeon
- * Date: 21.06.14
- * Time: 14:57
- */
- class BaseListAction extends BaseAction{
- /**
- * @var string
- */
- public $view = 'list';
- /**
- * @var array|EMongoCriteria predefined criteria
- */
- public $criteria = array();
- /**
- * @var string scenario for models
- */
- public $modelScenario = 'list';
- /**
- * @var array dataProvider config
- */
- public $dataProviderConfig = array();
- /**
- * @var string dataProvider class
- */
- public $dataProviderClass = 'EMongoDocumentDataProvider';
- /**
- * @var string filter class
- */
- public $filterClass;
- /**
- * @var string filter scenario
- */
- public $filterScenario = 'search';
- public function run()
- {
- /** @var $filter EMongoDocument */
- $filterClass = $this->filterClass ? $this->filterClass : $this->modelClass;
- $filter = new $filterClass($this->filterScenario);
- $filter->unsetAttributes();
- if($data = $this->getFilterData($filter))
- $filter->setAttributes($data);
- $filter->search();
- $filter->getDbCriteria()->mergeWith($this->criteria);
- /** @var $dataProvider EMongoDocumentDataProvider */
- $dataProviderClass = $this->dataProviderClass;
- $dataProvider = new $dataProviderClass($filter, $this->dataProviderConfig);
- self::setScenario($dataProvider->getData(), $this->modelScenario);
- $this->renderParams = array(
- 'dataProvider' => $dataProvider,
- 'filter' => $filter
- );
- $this->render();
- }
- public function getFilterData($filter)
- {
- $filterParams = Yii::app()->request->getParam(get_class($filter));
- return $filterParams;
- }
- /**
- * Set scenario to each model found
- * @param $data
- * @param $scenario
- */
- public static function setScenario($data, $scenario)
- {
- /**
- * @var EMongoDocument $model
- */
- foreach($data as $model){
- $model->setScenario($scenario);
- }
- }
- }
- /**
- *
- * Created by PhpStorm.
- * User: hlogeon
- * Date: 30.07.14
- * Time: 3:12
- */
- class CompanyController extends BackendController{
- public function actions()
- {
- return array(
- 'index' => array(
- 'class' => 'BaseListAction',
- 'modelClass' => 'Company',
- 'view' => 'index',
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement