Advertisement
Guest User

Yii framework 1 Actions THE RIGHT WAY

a guest
Apr 4th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. *<?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: hlogeon
  5.  * Date: 13.08.14
  6.  * Time: 12:33
  7.  *
  8.  * @author Degtyaruk Andrey <hlogeon1@gmail.com>
  9.  *
  10.  */
  11.  
  12.  
  13. /**
  14.  *
  15.  * Created by PhpStorm.
  16.  * User: hlogeon
  17.  * Date: 21.06.14
  18.  * Time: 14:29
  19.  */
  20.  
  21. abstract class BaseAction extends CAction{
  22.  
  23.     /**
  24.      * @var string model class for action
  25.      */
  26.     public $modelClass;
  27.  
  28.     /**
  29.      * @var string view for render
  30.      */
  31.     public $view = 'index';
  32.  
  33.     /**
  34.      * @var array
  35.      */
  36.     public $renderParams = array();
  37.  
  38.     /**
  39.      * If we have form on page that action renders - provide it here
  40.      * @var string
  41.      */
  42.     public $formName = '';
  43.  
  44.  
  45.     /**
  46.      * Render with specified params
  47.      */
  48.     public function render()
  49.     {
  50.         if(Yii::app()->request->isAjaxRequest){
  51.             $this->controller->renderPartial($this->view, $this->renderParams);
  52.         }
  53.         else{
  54.             $this->controller->render($this->view, $this->renderParams);
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Returns model by its ID
  60.      *
  61.      * @param $id
  62.      *
  63.      * @return EMongoDocument
  64.      * @throws Exception
  65.      *
  66.      * @author Degtyaruk Andrey <hlogeon1@gmail.com>
  67.      */
  68.     public function getModelById($id)
  69.     {
  70.         if(is_string($id))
  71.             $id = new MongoId($id);
  72.         if(!$id instanceof MongoId)
  73.             throw new Exception("ID is not instance of MongoId");
  74.         return EMongoDocument::model($this->modelClass)->findByPk($id);
  75.     }
  76.  
  77.     /**
  78.      * Return modelClass
  79.      *
  80.      * @return EMongoDocument
  81.      *
  82.      * @author Degtyaruk Andrey <hlogeon1@gmail.com>
  83.      */
  84.     public function getModelClass()
  85.     {
  86.         return EMongoDocument::model($this->modelClass);
  87.     }
  88.  
  89.     /**
  90.      * @param $model
  91.      *
  92.      * @author Degtyaruk Andrey <hlogeon1@gmail.com>
  93.      */
  94.     protected function performAjaxValidation($model)
  95.     {
  96.         if(isset($_POST['ajax']))
  97.         {
  98.             echo CActiveForm::validate($model);
  99.             Yii::app()->end();
  100.         }
  101.     }
  102.  
  103. }
  104.  
  105.  
  106. /**
  107.  *
  108.  * Created by PhpStorm.
  109.  * User: hlogeon
  110.  * Date: 21.06.14
  111.  * Time: 14:57
  112.  */
  113.  
  114. class BaseListAction extends BaseAction{
  115.  
  116.     /**
  117.      * @var string
  118.      */
  119.     public $view = 'list';
  120.  
  121.     /**
  122.      * @var array|EMongoCriteria predefined criteria
  123.      */
  124.     public $criteria = array();
  125.  
  126.  
  127.     /**
  128.      * @var string scenario for models
  129.      */
  130.     public $modelScenario = 'list';
  131.  
  132.     /**
  133.      * @var array dataProvider config
  134.      */
  135.     public $dataProviderConfig = array();
  136.  
  137.     /**
  138.      * @var string dataProvider class
  139.      */
  140.     public $dataProviderClass = 'EMongoDocumentDataProvider';
  141.  
  142.     /**
  143.      * @var string filter class
  144.      */
  145.     public $filterClass;
  146.  
  147.     /**
  148.      * @var string filter scenario
  149.      */
  150.     public $filterScenario = 'search';
  151.  
  152.  
  153.     public function run()
  154.     {
  155.         /** @var $filter EMongoDocument */
  156.         $filterClass = $this->filterClass ? $this->filterClass : $this->modelClass;
  157.         $filter = new $filterClass($this->filterScenario);
  158.         $filter->unsetAttributes();
  159.         if($data = $this->getFilterData($filter))
  160.             $filter->setAttributes($data);
  161.         $filter->search();
  162.  
  163.         $filter->getDbCriteria()->mergeWith($this->criteria);
  164.         /** @var $dataProvider EMongoDocumentDataProvider */
  165.         $dataProviderClass = $this->dataProviderClass;
  166.         $dataProvider = new $dataProviderClass($filter, $this->dataProviderConfig);
  167.  
  168.         self::setScenario($dataProvider->getData(), $this->modelScenario);
  169.  
  170.         $this->renderParams = array(
  171.             'dataProvider' => $dataProvider,
  172.             'filter' => $filter
  173.         );
  174.         $this->render();
  175.     }
  176.  
  177.     public function getFilterData($filter)
  178.     {
  179.         $filterParams = Yii::app()->request->getParam(get_class($filter));
  180.         return $filterParams;
  181.     }
  182.  
  183.     /**
  184.      * Set scenario to each model found
  185.      * @param $data
  186.      * @param $scenario
  187.      */
  188.     public static function setScenario($data, $scenario)
  189.     {
  190.         /**
  191.          * @var EMongoDocument $model
  192.          */
  193.         foreach($data as $model){
  194.             $model->setScenario($scenario);
  195.         }
  196.     }
  197.  
  198. }
  199.  
  200.  
  201. /**
  202.  *
  203.  * Created by PhpStorm.
  204.  * User: hlogeon
  205.  * Date: 30.07.14
  206.  * Time: 3:12
  207.  */
  208.  
  209. class CompanyController extends BackendController{
  210.  
  211.     public function actions()
  212.     {
  213.         return array(
  214.             'index' => array(
  215.                 'class' => 'BaseListAction',
  216.                 'modelClass' => 'Company',
  217.                 'view' => 'index',
  218.             ),
  219.         );
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement