Advertisement
Incubus

Untitled

Aug 5th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. class CatalogController extends Controller {
  4.  
  5.     public $defaultAction = 'view';
  6.  
  7.     /**
  8.      * Lists all models.
  9.      */
  10.     public function actionView($id) {
  11.         $cat = Category::model()->findByPK($id);
  12.         if ($cat !== null) {
  13.             $min = (isset($_GET['min']))?(int)$_GET['min']:0;
  14.             $max = (isset($_GET['max']))?(int)$_GET['max']:100000;
  15.  
  16.             $cc = $cat->children()->findAll();
  17.             $childrens = array();
  18.             foreach ($cc as $key=>$child) {
  19.                 if($child->children()) {
  20.                     $child_childrens = $child->getChildrens();
  21.                     if($child_childrens) {
  22.                         foreach ($child_childrens as $child_children) {
  23.                             if($child_children->itemsCount) {
  24.                                 $childrens[] = $child;
  25.                                 break;
  26.                             }
  27.                         }
  28.                     } else {
  29.                         if($child->itemsCount) {
  30.                             $childrens[] = $child;
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             $cc = $childrens;
  37.             $parents = $cat->ancestors()->findAll();
  38.             if($cc) {
  39.                 $dataProvider = new CArrayDataProvider($cc,
  40.                     array(
  41.                         'pagination'=>array(
  42.                             'pageSize'=>20,
  43.                             'pageVar' =>'page',
  44.                         ),
  45.                     )
  46.                 );
  47.  
  48.  
  49.  
  50.                 $this->render('indexCat', array(
  51.                     'dataProvider' => $dataProvider,
  52.                     'cat' => $cat,
  53.                     'parents' => $parents,
  54.                     'id' => $id,
  55.                     'min' => $min,
  56.                     'max' => $max,
  57.                 ));
  58.             } else {
  59.                 $dataProvider = new CActiveDataProvider(Items::model()->active()->category($id)->price($min,$max)->cache(86000),
  60.                     array(
  61.                         'pagination'=>array(
  62.                             'pageSize'=>21,
  63.                             'pageVar' =>'page',
  64.                         ),
  65.                     )
  66.                 );
  67.  
  68.  
  69.  
  70.                 $this->render('index', array(
  71.                     'dataProvider' => $dataProvider,
  72.                     'cat' => $cat,
  73.                     'cat_children' => $cc,
  74.                     'parents' => $parents,
  75.                     'id' => $id,
  76.                     'min' => $min,
  77.                     'max' => $max,
  78.                 ));
  79.             }
  80.         } else {
  81.             $this->redirect('/');
  82.         }
  83.     }
  84.  
  85.     public function actionFilter() {
  86.         $id = (int)$_POST['id'];
  87.         $min = (int)$_POST['min'];
  88.         $max = (int)$_POST['max'];
  89.  
  90.         if($id == 0) $id = (int)$_POST['this_id'];
  91.         if($min < 0) $min = 0;
  92.         if($max == 0) $max = 100000;
  93.  
  94.         $this->redirect(
  95.             $this->createUrl('catalog/view',
  96.                 array(
  97.                     'id'=>$id,
  98.                     'min'=>$min,
  99.                     'max'=>$max,
  100.                 )
  101.             )
  102.         );
  103.     }
  104.  
  105.         /**
  106.      * Returns the data model based on the primary key given in the GET variable.
  107.      * If the data model is not found, an HTTP exception will be raised.
  108.      * @param integer $id the ID of the model to be loaded
  109.      * @return Catalog the loaded model
  110.      * @throws CHttpException
  111.      */
  112.     public function loadModel($id) {
  113.     $model = Catalog::model()->findByPk($id);
  114.     if ($model === null)
  115.         throw new CHttpException(404, 'The requested page does not exist.');
  116.     return $model;
  117.     }
  118.  
  119.     /**
  120.      * Performs the AJAX validation.
  121.      * @param Catalog $model the model to be validated
  122.      */
  123.     protected function performAjaxValidation($model) {
  124.     if (isset($_POST['ajax']) && $_POST['ajax'] === 'catalog-form') {
  125.         echo CActiveForm::validate($model);
  126.         Yii::app()->end();
  127.     }
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement