/** * Find or create the category * * @param array $convertedItem * * @return CategoryInterface */ protected function findOrCreateCategory(array $convertedItem) { $category = $this->findObject($this->repository, $convertedItem); if (null === $category) { $category = $this->categoryFactory->create(); } return $category; }
/** * Create a tree or category * * @param Request $request * @param int $parent * * @throws AccessDeniedException * * @return Response|RedirectResponse */ public function createAction(Request $request, $parent = null) { if (false === $this->securityFacade->isGranted($this->buildAclName('category_create'))) { throw new AccessDeniedException(); } $category = $this->categoryFactory->create(); if ($parent) { $parent = $this->findCategory($parent); $category->setParent($parent); } $category->setCode($request->get('label')); $this->eventDispatcher->dispatch(CategoryEvents::PRE_CREATE, new GenericEvent($category)); $form = $this->createForm($this->rawConfiguration['form_type'], $category, $this->getFormOptions($category)); if ($request->isMethod('POST')) { $form->submit($request); if ($form->isValid()) { $this->categorySaver->save($category); $message = new Message(sprintf('flash.%s.created', $category->getParent() ? 'category' : 'tree')); $this->addFlash('success', $message); $this->eventDispatcher->dispatch(CategoryEvents::POST_CREATE, new GenericEvent($category)); return $this->redirectToRoute($this->buildRouteName('categorytree_edit'), ['id' => $category->getId()]); } } return $this->render(sprintf('PimEnrichBundle:CategoryTree:%s.html.twig', $request->get('content', 'edit')), ['form' => $form->createView(), 'related_entity' => $this->rawConfiguration['related_entity'], 'acl' => $this->rawConfiguration['acl'], 'route' => $this->rawConfiguration['route']]); }
/** * 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->findOr404($this->categoryFactory->getCategoryClass(), $categoryId); $categories = null; $includeParent = $request->get('include_parent', false); $includeParent = $includeParent === 'true'; $selectedCategoryIds = $request->get('selected', null); if (null !== $selectedCategoryIds) { $categories = $this->categoryManager->getCategoriesByIds($selectedCategoryIds); } elseif (null !== $product) { $categories = $product->getCategories(); } $trees = $this->getFilledTree($parent, $categories); return ['trees' => $trees, 'categories' => $categories]; }
/** * Get a new tree instance * * @return CategoryInterface * * @deprecated Please use CategoryFactory::create() instead */ public function getTreeInstance() { return $this->categoryFactory->create(); }
/** * 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->categoryFactory->getCategoryClass())); } $categories = null; $includeParent = $request->get('include_parent', false); $includeParent = $includeParent === 'true'; $selectedCategoryIds = $request->get('selected', null); if (null !== $selectedCategoryIds) { $categories = $this->categoryManager->getCategoriesByIds($selectedCategoryIds); } elseif (null !== $product) { $categories = $product->getCategories(); } $trees = $this->getFilledTree($parent, $categories); return ['trees' => $trees, 'categories' => $categories]; }