keesschepers

Category Repository

May 19th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.60 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE
  5.  * COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY
  6.  * COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
  7.  * AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
  8.  *
  9.  * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
  10.  * BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
  11.  * CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED
  12.  * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
  13.  *
  14.  * @category   Bestbuy
  15.  * @package    BestBuy_ORM
  16.  * @copyright  Copyright (c) 2008-2011 SkyConcepts B.V. (http://www.skyconcepts.nl)
  17.  * @license    http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode Attribution-NonCommercial-NoDerivs 3.0 Unported
  18.  *
  19.  */
  20.  
  21. namespace BestBuy\Entity\Repository;
  22.  
  23. use Doctrine\ORM\EntityRepository,
  24.     Doctrine\ORM\Query;
  25.  
  26. /**
  27.  *
  28.  * Repository for categories
  29.  *
  30.  * @category   Bestbuy
  31.  * @package    BestBuy_ORM
  32.  * @copyright  Copyright (c) 2008-2011 SkyConcepts B.V. (http://www.skyconcepts.nl)
  33.  * @license    http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode Attribution-NonCommercial-NoDerivs 3.0 Unported
  34.  * @author     Kees Schepers <[email protected]>
  35.  */
  36. class CategoryRepository extends EntityRepository
  37. {
  38.  
  39.     /**
  40.      *
  41.      * @param integer $webshopid
  42.      * @return Doctrine\ORM\Query
  43.      */
  44.     public function getCategoriesOverviewByWebshopQuery($webshopid, $parentid = null)
  45.     {
  46.         $qb = $this->_em->createQueryBuilder()
  47.                 ->select('c.categoryid', 'c.name', 'c.status')
  48.                 ->from('BestBuy\Entity\Category', 'c')
  49.                 ->join('c.webshop', 'w')
  50.                 ->leftJoin('c.parent', 'p')
  51.                 ->where('w.webshopid = :webshopid')
  52.                 ->setParameter('webshopid', $webshopid, \PDO::PARAM_INT)
  53.                 ->orderBy('c.weight', 'ASC');
  54.  
  55.         if ($parentid === null) {
  56.             $qb->andWhere('p.categoryid IS NULL');
  57.         } else {
  58.             $qb->andWhere('p.categoryid = :parentid')
  59.                     ->setParameter('parentid', $parentid, \PDO::PARAM_NULL);
  60.         }
  61.  
  62.         return $qb->getQuery();
  63.     }
  64.  
  65.     /**
  66.      * Builds an recursive array with categories.
  67.      *
  68.      * @param Webshop $webshop
  69.      * @return \RecursiveArrayIterator
  70.      */
  71.     public function getRecursiveCategoriesArray(Webshop $webshop)
  72.     {
  73.         $qb = $this->getAllCategoriesQueryBuilder($webshop);
  74.         $q = $qb->getQuery()->useResultCache(true, 1800);
  75.  
  76.         $result = $q->getArrayResult();
  77.         $categories = array();
  78.  
  79.         foreach ($result as $category) {
  80.             $categories[$category['parent']][] = $category;
  81.         }
  82.  
  83.         $recursion = function &($parent, &$data = array()) use ($categories, &$recursion) {
  84.                     if (!isset($categories[$parent]))
  85.                         return $data;
  86.  
  87.                     foreach ($categories[$parent] as $item) {
  88.                         if (isset($categories[$item['name']])) {
  89.                             $item['children'] = array();
  90.                             $item['children'] = $recursion($item['name'], $item['children']);
  91.                         }
  92.  
  93.                         $data[] = $item;
  94.                     }
  95.  
  96.                     return $data;
  97.                 };
  98.  
  99.         return $recursion(null, $data);
  100.     }
  101.  
  102.     /**
  103.      *
  104.      * Will return a category path as array by child => parent => parent => parent.. etc
  105.      * containing category entities.
  106.      *
  107.      * @param Webshop $webshop
  108.      * @param integer $childCategoryId
  109.      * @return array
  110.      */
  111.     public function getCategoryPathByChild(Webshop $webshop, $childCategoryId)
  112.     {
  113.         $qb = $this->getAllCategoriesQueryBuilder($webshop);        
  114.         $q = $qb->getQuery()->useResultCache(true, 1800);
  115.        
  116.         $categories = array();
  117.        
  118.         do {
  119.             $qb = $this->getEntityManager()->createQueryBuilder();
  120.             $qb->select('c')
  121.                     ->from('BestBuy\Entity\Category', 'c')
  122.                     ->andWhere('c.webshop', ':webshop')
  123.                     ->setParameter('webshop', $webshop)
  124.                     ->andWhere('c.categoryid', ':categoryid')
  125.                     ->setParameter('categoryid', $childCategoryId, \PDO::PARAM_INT);
  126.  
  127.             $q = $qb->getQuery()->useResultCache(true, 1800);            
  128.             $category = $q->getOneOrNullResult();
  129.            
  130.             if(null !== $category) {
  131.                 $categories[] = $category;
  132.                 $childCategoryId = $category->parent->categoryid;
  133.             }
  134.            
  135.         } while(null !== $category);    
  136.        
  137.         return $categories;
  138.        
  139.     }
  140.  
  141.     /**
  142.      * @param Webshop $webshop
  143.      * @return \Doctrine\ORM\QueryBuilder
  144.      */
  145.     public function getAllCategoriesQueryBuilder(Webshop $webshop) {
  146.         $qb = $this->_em->createQueryBuilder()
  147.                 ->select('c.categoryid', 'c.name', 'c.status', 'p.name as parent')
  148.                 ->from('BestBuy\Entity\Category', 'c')
  149.                 ->join('c.webshop', 'w')
  150.                 ->leftJoin('c.parent', 'p')
  151.                 ->where('c.webshop = :webshop')
  152.                 ->setParameter('webshop', $webshop)
  153.                 ->orderBy('c.weight', 'ASC');
  154.        
  155.         return $qb;
  156.     }
  157.    
  158.     /**
  159.      *
  160.      * Will search for the final child category and will return either null or the
  161.      * category entity child found
  162.      *
  163.      * @param array $path The category path from parent to child in a numeric array
  164.      * @param Webshop $webshop
  165.      */
  166.     public function getCategoryDataByArrayPath(array $path, Webshop $webshop = null)
  167.     {
  168.         foreach ($path as $categoryName) {
  169.             $qb = $this->getEntityManager()->createQueryBuilder();
  170.             $qb->select('c')
  171.                     ->from('BestBuy\Entity\Category', 'c')
  172.                     ->where($qb->expr()->eq('c.name', ':category'))
  173.                     ->setParameter('category', $categoryName);
  174.  
  175.             if ($webshop instanceof Webshop) {
  176.                 $qb->andWhere('c.webshop', ':webshop')->setParameter('webshop', $webshop);
  177.             }
  178.  
  179.             if (null !== $category->parent) {
  180.                 $qb->andWhere('c.parent', ':parent')->setParameter('parent', $category->parent);
  181.             }
  182.  
  183.             $q = $qb->getQuery()->useResultCache(true, 1800);
  184.             $category = $q->getOneOrNullResult();
  185.         }
  186.  
  187.         return $category;
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment