/**
  * {@inherit}
  */
 public function getItemsCountInCategory(CategoryInterface $category, QueryBuilder $categoryQb = null)
 {
     $qb = $this->em->createQueryBuilder();
     $qb->select($qb->expr()->count('distinct i'));
     $qb->from($this->entityName, 'i');
     $qb->join('i.categories', 'node');
     if (null === $categoryQb) {
         $qb->where('node.id = :nodeId');
         $qb->setParameter('nodeId', $category->getId());
     } else {
         $qb->where($categoryQb->getDqlPart('where'));
         $qb->setParameters($categoryQb->getParameters());
     }
     return $qb->getQuery()->getSingleScalarResult();
 }
 /**
  * @param CategoryInterface $category
  * @param string            $field
  * @param mixed             $data
  */
 protected function setData(CategoryInterface $category, $field, $data)
 {
     // TODO: Locales should be managed in a dedicated implementation of this updater
     if ('labels' === $field && $category instanceof TranslatableInterface) {
         foreach ($data as $localeCode => $label) {
             $category->setLocale($localeCode);
             $translation = $category->getTranslation();
             $translation->setLabel($label);
         }
     } elseif ('parent' === $field) {
         $categoryParent = $this->findParent($data);
         if (null !== $categoryParent) {
             $category->setParent($categoryParent);
         } else {
             throw new \InvalidArgumentException(sprintf('The parent category "%s" does not exist', $data));
         }
     } else {
         $this->accessor->setValue($category, $field, $data);
     }
 }
 function it_updates_a_category($categoryRepository, CategoryInterface $category, CategoryInterface $categoryMaster, CategoryTranslation $translatable)
 {
     $categoryRepository->findOneByIdentifier('master')->willReturn($categoryMaster);
     $category->getTranslation()->willReturn($translatable);
     $translatable->setLabel('Ma superbe catégorie')->shouldBeCalled();
     $category->setCode('mycode')->shouldBeCalled();
     $category->setParent($categoryMaster)->shouldBeCalled();
     $category->setLocale('fr_FR')->shouldBeCalled();
     $category->getId()->willReturn(null);
     $values = ['code' => 'mycode', 'parent' => 'master', 'labels' => ['fr_FR' => 'Ma superbe catégorie']];
     $this->update($category, $values, []);
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function addChild(CategoryInterface $child)
 {
     $child->setParent($this);
     $this->children[] = $child;
     return $this;
 }
 /**
  * Define the state of a category
  *
  * @param CategoryInterface $category
  * @param bool              $hasChild
  * @param array             $selectedIds
  *
  * @return string
  */
 protected function defineCategoryState(CategoryInterface $category, $hasChild = false, array $selectedIds = [])
 {
     $state = $category->hasChildren() ? 'closed' : 'leaf';
     if ($hasChild === true) {
         $state = 'open';
     }
     if (in_array($category->getId(), $selectedIds)) {
         $state .= ' toselect jstree-checked';
     }
     if ($category->isRoot()) {
         $state .= ' jstree-root';
     }
     return $state;
 }
 /**
  * Return categories ids provided by the categoryQb or by the provided category
  *
  * @param CategoryInterface $category
  * @param OrmQueryBuilder   $categoryQb
  *
  * @return array $categoryIds
  */
 protected function getCategoryIds(CategoryInterface $category, OrmQueryBuilder $categoryQb = null)
 {
     $categoryIds = [];
     if (null !== $categoryQb) {
         $categoryAlias = $categoryQb->getRootAlias();
         $categories = $categoryQb->select('PARTIAL ' . $categoryAlias . '.{id}')->getQuery()->getArrayResult();
     } else {
         $categories = [['id' => $category->getId()]];
     }
     foreach ($categories as $category) {
         $categoryIds[] = $category['id'];
     }
     return $categoryIds;
 }
 /**
  * @param Request                $request
  * @param CategoryInterface|null $selectNode
  *
  * @return array|ArrayCollection
  */
 protected function getChildrenCategories(Request $request, $selectNode)
 {
     $parent = $this->findCategory($request->get('id'));
     if (null !== $selectNode) {
         $categories = $this->categoryRepository->getChildrenTreeByParentId($parent->getId(), $selectNode->getId());
     } else {
         $categories = $this->categoryRepository->getChildrenByParentId($parent->getId());
     }
     return $categories;
 }
 /**
  * {@inheritdoc}
  */
 public function isAncestor(CategoryInterface $parentNode, CategoryInterface $childNode)
 {
     $sameRoot = $parentNode->getRoot() === $childNode->getRoot();
     $isAncestor = $childNode->getLeft() > $parentNode->getLeft() && $childNode->getRight() < $parentNode->getRight();
     return $sameRoot && $isAncestor;
 }