Advertisement
Guest User

Untitled

a guest
Mar 21st, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.90 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Catalog
  4.  *
  5.  * @author dandelion <web.dandelion@gmail.com>
  6.  */
  7. class App_Catalog extends Controller
  8. {
  9.     protected $itemsPerPage = 30;
  10.  
  11.     function indexAction(array $params)
  12.     {
  13.         if (empty($params))
  14.         {
  15.             $this->load->view('catalog/main');
  16.             return $this->mainAction($params);
  17.         }
  18.         /**
  19.          * Параметры
  20.          */
  21.         $page = 1;
  22.         if(in_array('page',$params))
  23.         {
  24.             $pageInd = array_search('page',$params);
  25.             if(isset($params[$pageInd+1]))
  26.                 $page = $params[$pageInd+1];
  27.             unset($params[$pageInd]);
  28.             unset($params[$pageInd+1]);
  29.         }
  30.         if (!$page)
  31.             $page = 1;
  32.            
  33.         $key = @$params[0];
  34.         if (empty($key))
  35.         {
  36.             return $this->Url_redirectToHome();
  37.         }
  38.  
  39.         $category = $this->model->product->category->getByKey($key);
  40.         $this->tpl->assignBlockVars('category', array(
  41.             'ID'    => $category->id,
  42.             'KEY'   => $category->key,
  43.             'NAME' => $category->name,
  44.             'SEOTEXT' => $category->seotext_html,
  45.             'DESCRIPTION' => $category->description_html
  46.         ));
  47.         if ($category->seotext_html)
  48.             $this->tpl->assignBlockVars('category.seotext');
  49.         if ($category->block)
  50.             $this->tpl->assignBlockVars('category_block',array(
  51.                 'HTML' => $category->block,
  52.             ));
  53.         if ($category->title)
  54.             $this->page->setTitle($category->title);
  55.         $this->page->setKeywords($category->keywords)
  56.                    ->setDescription($category->description);
  57.         // Subcats
  58.         $subcats = $this->model->product->category->getSub($category->id);
  59.         foreach ($subcats as $subcat)
  60.         {
  61.             $this->tpl->assignBlockVars('subcat', array(
  62.                 'ID'    => $subcat->id,
  63.                 'KEY'   => $subcat->key,
  64.                 'NAME'  => $subcat->name,
  65.                 'COUNT' => $this->model->product->getCount($subcat->id)
  66.             ));
  67.         }
  68.         if (!$subcats->isEmpty())
  69.             $this->tpl->assignBlockVars('if_subcats');
  70.         // Upper cats
  71.         $uppercats = array();
  72.         if ($category->parent_id)
  73.         {
  74.             $uppercat = $this->model->product->category->getById($category->parent_id);
  75.             $uppercats[] = $uppercat;
  76.             if ($uppercat->parent_id)
  77.             {
  78.                 $uppercat = $this->model->product->category->getById($uppercat->parent_id);
  79.                 $uppercats[] = $uppercat;
  80.             }
  81.         }
  82.         $uppercats = array_reverse($uppercats);
  83.         foreach ($uppercats as $uppercat)
  84.         {
  85.             $this->tpl->assignBlockVars('uppercat', array(
  86.                 'KEY'   => $uppercat->key,
  87.                 'NAME'  => $uppercat->name
  88.             ));
  89.         }
  90.         if (!empty($uppercats))
  91.             $this->tpl->assignBlockVars('if_uppercats');
  92.         /**
  93.          * Выборка данных
  94.          */
  95.         if ($this->var->itemsPerPage)
  96.             $this->itemsPerPage = $this->var->itemsPerPage;
  97.         $start = ($page-1)*$this->itemsPerPage;
  98.         $entries = $this->model->product->getByCategory($category->id, $total, $start, $this->itemsPerPage);
  99.         $i=0;
  100.         foreach ($entries as $product)
  101.         {
  102.             $i++;
  103.             $this->tpl->assignBlockVars('product', array(
  104.                 'ID'   => $product->id,
  105.                 'NAME' => $product->name,
  106.                 'BRIEF' => $product->brief,
  107.                 'LABEL' => $product->label,
  108.                 'PRICE' => $product->price,
  109.                 'PRICE_OLD' => $product->price_old
  110.             ));
  111.             if ($product->picture)
  112.             {
  113.                 $this->tpl->assignBlockVars('product.picture', array(
  114.                     'SRC'   => $product->picture
  115.                 ));
  116.                 $photos = explode('|',$product->album);
  117.                 foreach ($photos as $photo)
  118.                 {
  119.                     if ($photo)
  120.                     $this->tpl->assignBlockVars('product.album', array(
  121.                         'SRC'   => $photo
  122.                     ));
  123.                 }
  124.             }
  125.             if ($product->price)
  126.                 $this->tpl->assignBlockVars('product.price');
  127.             if ($product->price_old)
  128.                 $this->tpl->assignBlockVars('product.price_old');
  129.         }
  130.         if (!$entries->isEmpty())
  131.             $this->tpl->assignBlockVars('products');
  132.         /**
  133.          * Навигация
  134.          */
  135.         $paginator = new Paginator(array(
  136.            'countPerPage'  => $this->itemsPerPage,
  137.            'countOfEntries'=> $total,
  138.            'currentPage' => $page,
  139.            'countOfFirstPages'=> 5,
  140.            'countOfLastPages' => 5,
  141.            'countOfMiddlePages'=> 4,
  142.            'template' => $this->tpl
  143.         ));
  144.         $paginator->compile();
  145.  
  146.         $this->setTitle(array('CATEGORY_NAME'=>$category->name));
  147.         $this->tpl->assignVar('CATEGORY_KEY',$category->key);
  148.     }
  149.  
  150.     function itemAction(array $params)
  151.     {
  152.         $id = (int)@$params[0];
  153.         if (empty($id))
  154.         {
  155.             return $this->Url_redirectToHome();
  156.         }
  157.  
  158.         $product = $this->model->product->getById($id);
  159.         $this->tpl->assignBlockVars('product', array(
  160.             'ID'   => $product->id,
  161.             'NAME' => $product->name,
  162.             'DESCRIPTION' => $product->description_html,
  163.             'SEOTEXT' => $product->seotext_html,
  164.             'PRICE' => $product->price,
  165.             'PRICE_OLD' => $product->price_old,
  166.             'LABEL' => $product->label
  167.         ));
  168.         if ($product->picture)
  169.         {
  170.             $this->tpl->assignBlockVars('product.picture', array(
  171.                 'SRC'   => $product->picture
  172.             ));
  173.             $photos = explode('|',$product->album);
  174.             foreach ($photos as $photo)
  175.             {
  176.                 if ($photo)
  177.                 $this->tpl->assignBlockVars('product.album', array(
  178.                     'SRC'   => $photo
  179.                 ));
  180.             }
  181.         }
  182.         if ($product->album)
  183.             $this->tpl->assignBlockVars('product.if_album');
  184.         if ($product->price)
  185.             $this->tpl->assignBlockVars('product.price');
  186.         if ($product->price_old)
  187.             $this->tpl->assignBlockVars('product.price_old');
  188.         if ($product->seotext_html)
  189.             $this->tpl->assignBlockVars('product.seotext');
  190.            
  191.         if ($product->title)
  192.             $this->page->setTitle($product->title);
  193.         $this->page->setKeywords($product->keywords)
  194.                    ->setDescription($product->description);
  195.         // Sizes
  196.         $sizes = (array)json_decode($product->sizes,true);
  197.         foreach ($sizes as $color=>$size)
  198.         {
  199.             $this->tpl->assignBlockVars('product.size', array(
  200.                 'COLOR'   => $color,
  201.                 'SIZE'   => $size,
  202.             ));
  203.         }
  204.         if (!empty($sizes))
  205.             $this->tpl->assignBlockVars('product.sizes');
  206.            
  207.         $flag = '_prev';
  208.         $entries = $this->model->product->getByCategory($product->category_id, $total);
  209.         foreach ($entries as $entry)
  210.         {
  211.             if ($entry->picture && $entry->id!=$product->id)
  212.             $this->tpl->assignBlockVars('products'.$flag, array(
  213.                 'SRC_BIG'   => is_file(DIR_IMAGES.'/product/l/'.$entry->picture) ? 'l/'.$entry->picture : $entry->picture,
  214.                 'NAME' => $entry->name,
  215.             ));
  216.             if ($entry->id==$product->id)
  217.                 $flag = '_next';
  218.         }
  219.  
  220.         $this->setTitle(array('PRODUCT_NAME'=>$product->name));
  221.        
  222.         // Upper cats
  223.  
  224.         $uppercats = array();
  225.         $parent_cats = $product->category->asArray();
  226.         $parent_id = (int)@$parent_cats[0]['id'];
  227.  
  228.         if ($parent_id)
  229.         do
  230.         {
  231.             $uppercat = $this->model->product->category->getById($parent_id);
  232.             $uppercats[] = $uppercat;
  233.             $parent_id = $uppercat->parent_id;
  234.  
  235.         }
  236.         while ($parent_id);
  237.         $uppercats = array_reverse($uppercats);
  238.         foreach ($uppercats as $uppercat)
  239.         {
  240.             $this->tpl->assignBlockVars('uppercat', array(
  241.                 'KEY'   => $uppercat->key,
  242.                 'NAME'  => $uppercat->name
  243.             ));
  244.         }
  245.         if (!empty($uppercats))
  246.             $this->tpl->assignBlockVars('if_uppercats');
  247.            
  248.         $category = @$uppercats[0];
  249.         if ($category && $category->block)
  250.             $this->tpl->assignBlockVars('category_block',array(
  251.                 'HTML' => $category->block,
  252.             ));
  253.        
  254.         /**
  255.          * See also
  256.          */
  257.         if ($product->also)
  258.         {
  259.             $entries = $this->model->product->getByIds(explode('|',$product->also), $total);
  260.             $i=0;
  261.             foreach ($entries as $product)
  262.             {
  263.                 $i++;
  264.                 $this->tpl->assignBlockVars('also', array(
  265.                     'ID'   => $product->id,
  266.                     'NAME' => $product->name,
  267.                     'BRIEF' => $product->brief,
  268.                     'LABEL' => $product->label,
  269.                     'PRICE' => $product->price,
  270.                     'PRICE_OLD' => $product->price_old
  271.                 ));
  272.                 if ($product->picture)
  273.                 {
  274.                     $this->tpl->assignBlockVars('also.picture', array(
  275.                         'SRC'   => $product->picture
  276.                     ));
  277.                 }
  278.                 if ($product->price)
  279.                     $this->tpl->assignBlockVars('also.price');
  280.                 if ($product->price_old)
  281.                     $this->tpl->assignBlockVars('also.price_old');
  282.             }
  283.             if (!$entries->isEmpty())
  284.                 $this->tpl->assignBlockVars('see_also');  
  285.         }
  286.     }
  287.    
  288.     function searchAction(array $params)
  289.     {
  290.         /**
  291.          * Параметры
  292.          */
  293.         $page = 1;
  294.         if(in_array('page',$params))
  295.         {
  296.             $pageInd = array_search('page',$params);
  297.             if(isset($params[$pageInd+1]))
  298.                 $page = $params[$pageInd+1];
  299.             unset($params[$pageInd]);
  300.             unset($params[$pageInd+1]);
  301.         }
  302.         if (!$page)
  303.             $page = 1;
  304.            
  305.         $query = empty($_POST['query']) ? urldecode(@$params[0]) : $_POST['query'];
  306.         $query = strip_tags($query);
  307.         if (empty($query))
  308.         {
  309.             return $this->Url_redirectToHome();
  310.         }
  311.         $this->tpl->assignVar('SEARCH_QUERY',$query);
  312.         $this->tpl->assignVar('SEARCH_QUERY2',urlencode($query));
  313.         /**
  314.          * Выборка данных
  315.          */
  316.         if ($this->var->itemsPerPage)
  317.             $this->itemsPerPage = $this->var->itemsPerPage;
  318.         $start = ($page-1)*$this->itemsPerPage;
  319.         $entries = $this->model->product->search($query, $total, $start, $this->itemsPerPage);
  320.         foreach ($entries as $product)
  321.         {
  322.             $this->tpl->assignBlockVars('product', array(
  323.                 'ID'   => $product->id,
  324.                 'NAME' => $product->name,
  325.                 'BRIEF' => $product->brief,
  326.                 'LABEL' => $product->label,
  327.                 'PRICE' => $product->price,
  328.                 'PRICE_OLD' => $product->price_old
  329.             ));
  330.             if ($product->picture)
  331.             {
  332.                 $this->tpl->assignBlockVars('product.picture', array(
  333.                     'SRC'   => $product->picture
  334.                 ));
  335.                 $photos = explode('|',$product->album);
  336.                 foreach ($photos as $photo)
  337.                 {
  338.                     if ($photo)
  339.                     $this->tpl->assignBlockVars('product.album', array(
  340.                         'SRC'   => $photo
  341.                     ));
  342.                 }
  343.             }
  344.             if ($product->price)
  345.                 $this->tpl->assignBlockVars('product.price');
  346.             if ($product->price_old)
  347.                 $this->tpl->assignBlockVars('product.price_old');
  348.         }
  349.         $this->tpl->assignVar('SEARCH_TOTAL',$total);
  350.         if (!$entries->isEmpty())
  351.             $this->tpl->assignBlockVars('products');
  352.         /**
  353.          * Навигация
  354.          */
  355.         $paginator = new Paginator(array(
  356.            'countPerPage'  => $this->itemsPerPage,
  357.            'countOfEntries'=> $total,
  358.            'currentPage' => $page,
  359.            'countOfFirstPages'=> 5,
  360.            'countOfLastPages' => 5,
  361.            'countOfMiddlePages'=> 4,
  362.            'template' => $this->tpl
  363.         ));
  364.         $paginator->compile();
  365.     }
  366.    
  367.     function mainAction(array $params)
  368.     {
  369.         $entries = $this->model->product->getMain($total);
  370.         foreach ($entries as $product)
  371.         {
  372.             $this->tpl->assignBlockVars('product', array(
  373.                 'ID'   => $product->id,
  374.                 'NAME' => $product->name,
  375.                 'BRIEF' => nl2br($product->brief),
  376.                 'LABEL' => $product->label,
  377.                 'PRICE' => $product->price,
  378.                 'PRICE_OLD' => $product->price_old
  379.             ));
  380.             if ($product->picture)
  381.             {
  382.                 $this->tpl->assignBlockVars('product.picture', array(
  383.                     'SRC'   => $product->picture
  384.                 ));
  385.                 $photos = explode('|',$product->album);
  386.                 foreach ($photos as $photo)
  387.                 {
  388.                     if ($photo)
  389.                     $this->tpl->assignBlockVars('product.album', array(
  390.                         'SRC'   => $photo
  391.                     ));
  392.                 }
  393.             }
  394.             if ($product->price)
  395.                 $this->tpl->assignBlockVars('product.price');
  396.             if ($product->price_old)
  397.                 $this->tpl->assignBlockVars('product.price_old');
  398.         }
  399.     }
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement