/**
  * List categories and trees for a product
  *
  * @param string $id
  *
  * @AclAncestor("pim_enrich_product_categories_view")
  *
  * @return JsonResponse
  */
 public function listAction($id)
 {
     $product = $this->findProductOr404($id);
     $trees = $this->productCategoryRepository->getProductCountByTree($product);
     $result['trees'] = $this->buildTrees($trees);
     $result['categories'] = $this->buildCategories($product);
     return new JsonResponse($result);
 }
 /**
  * List categories and trees for a product
  *
  * @param string $id
  *
  * @AclAncestor("pim_enrich_product_categories_view")
  *
  * @return JsonResponse
  */
 public function listAction($id)
 {
     $product = $this->findProductOr404($id);
     $trees = $this->productCategoryRepository->getProductCountByTree($product);
     $result = ['trees' => [], 'categories' => []];
     foreach ($trees as $tree) {
         $result['trees'][] = ['id' => $tree['tree']->getId(), 'code' => $tree['tree']->getCode(), 'label' => $tree['tree']->getLabel(), 'associated' => $tree['itemCount'] > 0];
     }
     foreach ($product->getCategories() as $category) {
         $result['categories'][] = ['id' => $category->getId(), 'code' => $category->getCode(), 'rootId' => $category->getRoot()];
     }
     return new JsonResponse($result);
 }
 /**
  * {@inheritdoc}
  */
 public function addFieldFilter($field, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     $categoryIds = $value;
     if ($operator !== Operators::UNCLASSIFIED) {
         $this->checkValue($field, $value);
         if (FieldFilterHelper::getProperty($field) === FieldFilterHelper::CODE_PROPERTY) {
             $categoryIds = $this->objectIdResolver->getIdsFromCodes('category', $value);
         }
     }
     switch ($operator) {
         case Operators::IN_LIST:
             $this->productRepository->applyFilterByCategoryIds($this->qb, $categoryIds, true);
             break;
         case Operators::NOT_IN_LIST:
             $this->productRepository->applyFilterByCategoryIds($this->qb, $categoryIds, false);
             break;
         case Operators::IN_CHILDREN_LIST:
             $categoryIds = $this->getAllChildrenIds($categoryIds);
             $this->productRepository->applyFilterByCategoryIds($this->qb, $categoryIds, true);
             break;
         case Operators::NOT_IN_CHILDREN_LIST:
             $categoryIds = $this->getAllChildrenIds($categoryIds);
             $this->productRepository->applyFilterByCategoryIds($this->qb, $categoryIds, false);
             break;
         case Operators::UNCLASSIFIED:
             $this->productRepository->applyFilterByUnclassified($this->qb);
             break;
         case Operators::IN_LIST_OR_UNCLASSIFIED:
             $this->productRepository->applyFilterByCategoryIdsOrUnclassified($this->qb, $categoryIds);
             break;
     }
     return $this;
 }
 /**
  * Return the number of times the product is present in each tree
  *
  * @param ProductInterface $product The product to look for in the trees
  *
  * @return array Each row of the array has the format:'tree'=>treeObject, 'productCount'=>integer
  *
  * @deprecated Will be remove in 1.5, please use ProductCategoryRepositoryInterface::getProductCountByTree()
  *             instead.
  */
 public function getProductCountByTree(ProductInterface $product)
 {
     return $this->productRepository->getProductCountByTree($product);
 }