Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE
- * COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY
- * COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
- * AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
- *
- * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
- * BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
- * CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED
- * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
- *
- * @category Bestbuy
- * @package BestBuy_ORM
- * @copyright Copyright (c) 2008-2011 SkyConcepts B.V. (http://www.skyconcepts.nl)
- * @license http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode Attribution-NonCommercial-NoDerivs 3.0 Unported
- *
- */
- namespace BestBuy\Entity\Repository;
- use Doctrine\ORM\EntityRepository,
- Doctrine\ORM\Query;
- /**
- *
- * Repository for categories
- *
- * @category Bestbuy
- * @package BestBuy_ORM
- * @copyright Copyright (c) 2008-2011 SkyConcepts B.V. (http://www.skyconcepts.nl)
- * @license http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode Attribution-NonCommercial-NoDerivs 3.0 Unported
- * @author Kees Schepers <[email protected]>
- */
- class CategoryRepository extends EntityRepository
- {
- /**
- *
- * @param integer $webshopid
- * @return Doctrine\ORM\Query
- */
- public function getCategoriesOverviewByWebshopQuery($webshopid, $parentid = null)
- {
- $qb = $this->_em->createQueryBuilder()
- ->select('c.categoryid', 'c.name', 'c.status')
- ->from('BestBuy\Entity\Category', 'c')
- ->join('c.webshop', 'w')
- ->leftJoin('c.parent', 'p')
- ->where('w.webshopid = :webshopid')
- ->setParameter('webshopid', $webshopid, \PDO::PARAM_INT)
- ->orderBy('c.weight', 'ASC');
- if ($parentid === null) {
- $qb->andWhere('p.categoryid IS NULL');
- } else {
- $qb->andWhere('p.categoryid = :parentid')
- ->setParameter('parentid', $parentid, \PDO::PARAM_NULL);
- }
- return $qb->getQuery();
- }
- /**
- * Builds an recursive array with categories.
- *
- * @param Webshop $webshop
- * @return \RecursiveArrayIterator
- */
- public function getRecursiveCategoriesArray(Webshop $webshop)
- {
- $qb = $this->getAllCategoriesQueryBuilder($webshop);
- $q = $qb->getQuery()->useResultCache(true, 1800);
- $result = $q->getArrayResult();
- $categories = array();
- foreach ($result as $category) {
- $categories[$category['parent']][] = $category;
- }
- $recursion = function &($parent, &$data = array()) use ($categories, &$recursion) {
- if (!isset($categories[$parent]))
- return $data;
- foreach ($categories[$parent] as $item) {
- if (isset($categories[$item['name']])) {
- $item['children'] = array();
- $item['children'] = $recursion($item['name'], $item['children']);
- }
- $data[] = $item;
- }
- return $data;
- };
- return $recursion(null, $data);
- }
- /**
- *
- * Will return a category path as array by child => parent => parent => parent.. etc
- * containing category entities.
- *
- * @param Webshop $webshop
- * @param integer $childCategoryId
- * @return array
- */
- public function getCategoryPathByChild(Webshop $webshop, $childCategoryId)
- {
- $qb = $this->getAllCategoriesQueryBuilder($webshop);
- $q = $qb->getQuery()->useResultCache(true, 1800);
- $categories = array();
- do {
- $qb = $this->getEntityManager()->createQueryBuilder();
- $qb->select('c')
- ->from('BestBuy\Entity\Category', 'c')
- ->andWhere('c.webshop', ':webshop')
- ->setParameter('webshop', $webshop)
- ->andWhere('c.categoryid', ':categoryid')
- ->setParameter('categoryid', $childCategoryId, \PDO::PARAM_INT);
- $q = $qb->getQuery()->useResultCache(true, 1800);
- $category = $q->getOneOrNullResult();
- if(null !== $category) {
- $categories[] = $category;
- $childCategoryId = $category->parent->categoryid;
- }
- } while(null !== $category);
- return $categories;
- }
- /**
- * @param Webshop $webshop
- * @return \Doctrine\ORM\QueryBuilder
- */
- public function getAllCategoriesQueryBuilder(Webshop $webshop) {
- $qb = $this->_em->createQueryBuilder()
- ->select('c.categoryid', 'c.name', 'c.status', 'p.name as parent')
- ->from('BestBuy\Entity\Category', 'c')
- ->join('c.webshop', 'w')
- ->leftJoin('c.parent', 'p')
- ->where('c.webshop = :webshop')
- ->setParameter('webshop', $webshop)
- ->orderBy('c.weight', 'ASC');
- return $qb;
- }
- /**
- *
- * Will search for the final child category and will return either null or the
- * category entity child found
- *
- * @param array $path The category path from parent to child in a numeric array
- * @param Webshop $webshop
- */
- public function getCategoryDataByArrayPath(array $path, Webshop $webshop = null)
- {
- foreach ($path as $categoryName) {
- $qb = $this->getEntityManager()->createQueryBuilder();
- $qb->select('c')
- ->from('BestBuy\Entity\Category', 'c')
- ->where($qb->expr()->eq('c.name', ':category'))
- ->setParameter('category', $categoryName);
- if ($webshop instanceof Webshop) {
- $qb->andWhere('c.webshop', ':webshop')->setParameter('webshop', $webshop);
- }
- if (null !== $category->parent) {
- $qb->andWhere('c.parent', ':parent')->setParameter('parent', $category->parent);
- }
- $q = $qb->getQuery()->useResultCache(true, 1800);
- $category = $q->getOneOrNullResult();
- }
- return $category;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment