function it_updates_a_not_translatable_category($categoryRepository, CategoryInterface $category, CategoryInterface $categoryMaster, CategoryTranslation $translatable)
 {
     $categoryRepository->findOneByIdentifier('master')->willReturn($categoryMaster);
     $category->setCode('mycode')->shouldBeCalled();
     $category->setParent($categoryMaster)->shouldBeCalled();
     $category->getId()->willReturn(null);
     $values = ['code' => 'mycode', 'parent' => 'master'];
     $this->update($category, $values, []);
 }
 /**
  * {@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)
 {
     if ('labels' === $field) {
         foreach ($data as $localeCode => $label) {
             $category->setLocale($localeCode);
         }
     } 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);
     }
 }
 /**
  * @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, []);
 }
 /**
  * Provides a tree filled up to the categories provided, with all their ancestors
  * and ancestors sibligns are filled too, in order to be able to display the tree
  * directly without loading other data.
  *
  * @param CategoryInterface $root       Tree root category
  * @param Collection        $categories categories
  *
  * @return array Multi-dimensional array representing the tree
  *
  * TODO: To MOVE in the Repository. Try it in SQL.
  */
 public function getFilledTree(CategoryInterface $root, Collection $categories)
 {
     $parentsIds = [];
     foreach ($categories as $category) {
         $categoryParentsIds = [];
         $path = $this->getEntityRepository()->getPath($category);
         if ($path[0]->getId() === $root->getId()) {
             foreach ($path as $pathItem) {
                 $categoryParentsIds[] = $pathItem->getId();
             }
         }
         $parentsIds = array_merge($parentsIds, $categoryParentsIds);
     }
     $parentsIds = array_unique($parentsIds);
     return $this->getEntityRepository()->getTreeFromParents($parentsIds);
 }
 /**
  * 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;
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public function addChild(CategoryInterface $child)
 {
     $child->setParent($this);
     $this->children[] = $child;
     return $this;
 }
 /**
  * Create a query builder with just a link to the category passed in parameter
  *
  * @param CategoryInterface $category
  *
  * @return QueryBuilder
  */
 protected function getNodeQueryBuilder(CategoryInterface $category)
 {
     $qb = $this->createQueryBuilder('ps');
     $qb->where('ps.id = :nodeId')->setParameter('nodeId', $category->getId());
     return $qb;
 }
 /**
  * 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;
 }
 /**
  * {@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;
 }