private function checkDuplicity(Category $category) { $duplicate = $this->repository->findOneBy(['path' => $category->getPath()]); if ($duplicate !== null) { throw new EntityDuplicateException(sprintf('Category with path %s already exists.', $category->getPath())); } }
private function addParentCategoryControl() { $parentCategoryItems = [self::ROOT_CATEGORY_KEY => '']; foreach ($this->categoryService->getAll() as $category) { if ($this->editedCategory !== null) { if ($category->isSelfOrSubcategoryOf($this->editedCategory)) { continue; } } $parentCategoryItems[$category->getId()] = ''; } $parentCategoryControl = $this->addRadioList('parentCategory', 'Parent category', $parentCategoryItems); $parentCategoryControl->setRequired(); $parentCategoryControl->setDefaultValue($this->editedCategory !== null && $this->editedCategory->hasParent() ? $this->editedCategory->getParent()->getId() : self::ROOT_CATEGORY_KEY); }
private function createCategory(CategoriesForm $form) { $values = $form->getValues(); $category = new Category($values->name); if ($values->parentCategory !== CategoriesForm::ROOT_CATEGORY_KEY) { $parentCategory = $this->categoryService->getById($values->parentCategory); $category->setParent($parentCategory); } try { if (!$form->hasErrors()) { $this->categoryService->create($category); $this->flashMessage(sprintf('Category %s has been created.', $category->getName())); $this->redirect(':Admin:Category:List:'); } } catch (EntityDuplicateException $e) { $form->addError(sprintf('Category with name %s already exists.', $category->getName())); } }
public function isDirectlyInCategory(Category $category) { foreach ($category->getSubcategories() as $subcategory) { if ($this->belongsIntoCategory($subcategory)) { return false; } } return true; }