Advertisement
AndreyKlipikov

Catalog Controller (action View)

Oct 10th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.70 KB | None | 0 0
  1.     public function actionView($url)
  2.     {
  3.         $category = Categories::model()->find(array('condition'=>'alias="' . $url . '" AND visible=1', 'limit'=>1));
  4.        
  5.         if ($category == null)
  6.             throw new CHttpException(404, 'Запрашиваемая страница не найдена.');
  7.            
  8.         $subcategories = Categories::model()->count(array('condition'=>'parent_id=' . $category->id));
  9.        
  10.         if ($subcategories == 0)
  11.         {
  12.             $ids = array();
  13.             $filter_values = array();
  14.             $page_size = 10;
  15.            
  16.             if (isset($_POST['Filter']) || isset($_POST['Cost']))
  17.             {
  18.                 $cost = null;
  19.                 if (isset($_POST['Cost']))
  20.                 {
  21.                     $cost = Yii::app()->request->getPost('Cost');
  22.                     $tmp = explode(";", $cost['prices']);
  23.                     $cost['start'] = $tmp[0];
  24.                     $cost['finish'] = $tmp[1];
  25.                    
  26.                     if (!empty($cost['finish']) && !empty($cost['start']) && $cost['start'] > $cost['finish'])
  27.                         $cost['start'] = $cost['finish'];
  28.                 }
  29.                
  30.                 $page_size = 100;
  31.                 $list = array();
  32.                 $goods = array();
  33.                 $goods_list = Goods::model()->findAll(array('condition'=>'visible=1 AND category_id=' . $category->id, 'order'=>((int)$_POST['sort'] == 2 ? 'price9 DESC' : 'price9 ASC')));
  34.                
  35.                 foreach($goods_list as $item)
  36.                 {
  37.                     $goods[$item->id] = array();
  38.                     $goods[$item->id]['id'] = $item->id;
  39.                     $goods[$item->id]['show_price'] = $item->show_price;
  40.                     $goods[$item->id]['price1'] = $item->price1;
  41.                     $goods[$item->id]['price2'] = $item->price2;
  42.                     $goods[$item->id]['price3'] = $item->price3;
  43.                     $goods[$item->id]['price4'] = $item->price4;
  44.                     $goods[$item->id]['price5'] = $item->price5;
  45.                     $goods[$item->id]['price6'] = $item->price6;
  46.                     $goods[$item->id]['price8'] = $item->price8;
  47.                     $goods[$item->id]['price9'] = $item->price9;
  48.                     $list[] = $item->id;
  49.                 }
  50.                
  51.                 $criteria_price = new CDbCriteria();
  52.                 $criteria_price->addCondition('visible', 1);
  53.                 $criteria_price->addInCondition('good_id', $list);
  54.                 $filter_values = FilterGoods::model()->findAll($criteria_price);
  55.                
  56.                 foreach($filter_values as $item)
  57.                 {
  58.                     $goods[$item->good_id][$item->filter_id] = str_replace(",", ".", $item->value_id);
  59.                 }
  60.                
  61.                 $values = Yii::app()->request->getPost('Filter');
  62.                 $posted = array();
  63.                
  64.                 foreach($values as $item => $key)
  65.                 {
  66.                     if (strpos($key, ';') !== false)
  67.                     {
  68.                         $tmp = explode(";", $key);
  69.                         $posted[$item] = array('id'=>$item, 'start'=>$tmp[0], 'finish'=>$tmp[1]);
  70.                     }
  71.                     else if ($key != '- Выберите значение -')
  72.                         $posted[$item] = array('id'=>$item, 'value'=>$key);
  73.                 }
  74.                
  75.                 foreach($goods as $item)
  76.                 {
  77.                     $deleted = false;
  78.                    
  79.                     if (!isset($cost['only']) && $cost != null && $item['show_price'] == 0)
  80.                     {
  81.                         unset($goods[$item['id']]);
  82.                         $deleted = true;
  83.                     }
  84.                    
  85.                     if (!$deleted && $cost != null && $item['show_price'] == 1 && ($cost['start'] > $item['price9'] || $cost['finish'] < $item['price9']))
  86.                     {
  87.                         unset($goods[$item['id']]);
  88.                         $deleted = true;
  89.                     }
  90.                    
  91.                     if (!$deleted)
  92.                     {
  93.                         foreach($posted as $tmp)
  94.                         {
  95.                             if (!$deleted)
  96.                             {
  97.                                 if(isset($tmp['value']))
  98.                                 {
  99.                                     if (!isset($item[$tmp['id']]) || $tmp['value'] != $item[$tmp['id']])
  100.                                     {
  101.                                         unset($goods[$item['id']]);
  102.                                         $deleted = true;
  103.                                     }
  104.                                 }
  105.                                 else
  106.                                 {
  107.                                     if (!isset($item[$tmp['id']]) || $tmp['start'] > $item[$tmp['id']] || $tmp['finish'] < $item[$tmp['id']])
  108.                                     {
  109.                                         unset($goods[$item['id']]);
  110.                                         $deleted = true;
  111.                                     }
  112.                                 }
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.                
  118.                 if (count($goods) == 0 && gettype($goods) == 'array')
  119.                     $ids[] = 0;
  120.                 else
  121.                 {
  122.                     foreach($goods as $item)
  123.                         $ids[] = $item['id'];
  124.                 }
  125.             }
  126.            
  127.             $criteria = new CDbCriteria();
  128.             $criteria->condition = 'category_id=' . $category->id;
  129.            
  130.             if (count($ids) > 0)
  131.                 $criteria->addInCondition('id', $ids);
  132.            
  133.             if (isset($_POST['sort']) && is_numeric($_POST['sort']) && (int)$_POST['sort'] !== 0)
  134.                 $criteria->order = ((int)$_POST['sort'] == '2' ? 'price9 ASC' : 'price9 DESC');
  135.        
  136.             $dataProvider=new CActiveDataProvider('Goods', array(
  137.                 'criteria'=>$criteria,
  138.                 'pagination'=>array(
  139.                     'pageSize'=>$page_size,
  140.                     'pageVar'=>'page',
  141.                 ),
  142.                 'sort'=>array(
  143.                     'defaultOrder'=>array(
  144.                         'name'=>CSort::SORT_ASC,
  145.                     )
  146.                 )
  147.             ));
  148.            
  149.             $content_type = ContentType::getField("category", $category->id, "description-text", 9);
  150.            
  151.             $this->render('view', array(
  152.                 'category'=>$category,
  153.                 'dataProvider'=>$dataProvider,
  154.                 'pages'=>$pages,
  155.                 'breadcrumbs' => Categories::getBreadCrumbs($category->parent_id),
  156.                 'content_text'=>$content_type,
  157.             ));
  158.         }
  159.         else
  160.         {
  161.             $content = '';
  162.             $sub = Categories::model()->findAll(array('condition'=>'parent_id=' . $category->id, 'order'=>'name ASC'));
  163.            
  164.             foreach($sub as $item)
  165.             {
  166.                 $goods = '';
  167.                 $goods_list = GoodsCategory::model()->findAll(array('condition'=>'visible=1 AND category_id=' . $item->id, 'limit'=>4, 'order'=>'sort DESC, RAND()'));
  168.                
  169.                 foreach($goods_list as $tmp)
  170.                 {
  171.                     $good = Goods::model()->findByPk($tmp->good_id);
  172.                     $image = GoodsImages::model()->find(array('condition'=>'good_id=' . $tmp->good_id));
  173.                    
  174.                     $goods .= '<div class="col-md-3 goods text-center"><a href="/good/' . $good->id . '" class="title" title="' . $good->name . '"><img src="' . (strpos($image->image, 'http://') > 0 ? $image->image : '/images/goods/' . $image->image) . '" alt="' . $good->name . '" class="img-responsive" style="display:block;margin:0 auto 9px;height:120px"><div>' . mb_substr($good->name, 0, 32, 'UTF-8') . '...</div></a><div class="price">' . ($good->price9 > 0 ? '<span>' . $good->price9 . '</span> р.' : '<b alt="' . $good->id . '">Узнать цену</b>') . '</div><a href="javascript://" class="buy" rel="' . $good->id . '"><strong>+</strong> В корзину</a></div>' . PHP_EOL;
  175.                 }
  176.                
  177.                 if (empty($goods))
  178.                     $goods = $item->small_description;
  179.                
  180.                 $content .= '<div style="padding-top:0px"><a href="/catalog/' . $item->alias . '" class="pull-right btn btn-primary btn-sm">Перейти в категорию</a><div class="content_title"><a href="/catalog/' . $item->alias . '" style="color:#333">' . $item->name . '</a></div><div class="content_desc"><div class="row best_goods">' . $goods . '</div></div></div>' . PHP_EOL;
  181.             }
  182.        
  183.             $content_type = ContentType::getField("category", $category->id, "description-text", 9);
  184.            
  185.             $this->render('subview', array(
  186.                 'category'=>$category,
  187.                 'content'=>$content,
  188.                 'breadcrumbs' => Categories::getBreadCrumbs($category->parent_id),
  189.                 'content_text'=>$content_type,
  190.             ));
  191.         }
  192.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement