/**
  * Find a category from its id
  *
  * @param int $categoryId
  *
  * @throws NotFoundHttpException
  *
  * @return CategoryInterface
  */
 protected function findCategory($categoryId)
 {
     $category = $this->categoryRepository->find($categoryId);
     if (!$category) {
         throw $this->createNotFoundException('Category not found');
     }
     return $category;
 }
Exemplo n.º 2
0
 /**
  * Get children category ids
  *
  * @param integer[] $categoryIds
  *
  * @return integer[]
  */
 protected function getAllChildrenIds(array $categoryIds)
 {
     $allChildrenIds = [];
     foreach ($categoryIds as $categoryId) {
         $category = $this->categoryRepository->find($categoryId);
         $childrenIds = $this->categoryRepository->getAllChildrenIds($category);
         $childrenIds[] = $category->getId();
         $allChildrenIds = array_merge($allChildrenIds, $childrenIds);
     }
     return $allChildrenIds;
 }
 /**
  * List categories associated with the provided product and descending from the category
  * defined by the parent parameter.
  *
  * @param Request    $request    The request object
  * @param int|string $id         Product id
  * @param int        $categoryId The parent category id
  *
  * httpparam include_category if true, will include the parentCategory in the response
  *
  * @Template
  * @AclAncestor("pim_enrich_product_categories_view")
  *
  * @return array
  */
 public function listCategoriesAction(Request $request, $id, $categoryId)
 {
     $product = $this->findProductOr404($id);
     $parent = $this->categoryRepository->find($categoryId);
     if (null === $parent) {
         throw new NotFoundHttpException(sprintf('%s entity not found', $this->categoryClass));
     }
     $categories = null;
     $selectedCategoryIds = $request->get('selected', null);
     if (null !== $selectedCategoryIds) {
         $categories = $this->categoryRepository->getCategoriesByIds($selectedCategoryIds);
     } elseif (null !== $product) {
         $categories = $product->getCategories();
     }
     $trees = $this->getFilledTree($parent, $categories);
     return ['trees' => $trees, 'categories' => $categories];
 }
 /**
  * Add filter to display categorized products
  *
  * @param FilterDatasourceAdapterInterface $ds
  * @param array                            $data
  *
  * @return bool has been applied
  */
 protected function applyFilterByCategory(FilterDatasourceAdapterInterface $ds, $data)
 {
     $category = $this->categoryRepo->find($data['categoryId']);
     if (!$category) {
         $category = $this->categoryRepo->find($data['treeId']);
     }
     if ($category) {
         if ($data['includeSub']) {
             $categoryIds = $this->getAllChildrenIds($category);
         } else {
             $categoryIds = array();
         }
         $categoryIds[] = $category->getId();
         $this->util->applyFilter($ds, 'categories.id', 'IN', $categoryIds);
         return true;
     }
     return false;
 }