/** * Helps to render the category form for add/edit actions * @param Category $category * @param CategoryForm $form * @param $parentCategoryID * @return JsonModel */ protected function renderCategData(Category $category, CategoryForm $form, $parentCategoryID) { $action = $category->getId() ? 'edit' : 'add'; $categoryTree = $this->getServiceLocator()->get('category-tree'); $form->get('parent')->setValueOptions($categoryTree->getSelectOptions($category)); $form->get('parent')->setValue($parentCategoryID); $viewModel = new ViewModel(['action' => $action, 'id' => $category->getId(), 'form' => $form]); $viewModel->setTemplate('admin/category/edit'); $renderer = $this->getServiceLocator()->get('Zend\\View\\Renderer\\RendererInterface'); return new JsonModel(array('title' => $this->translator->translate(ucfirst($action) . ' a category'), 'form' => $renderer->render($viewModel), 'parent' => (int) $parentCategoryID)); }
/** * @depends testGetChildrenCategories * @depends testAddCategoryPost2 * @param Category $childCategory * @param $lastInsertedId */ public function testChangeChild($childCategory, $lastInsertedId) { $childId = $childCategory->getId(); $categoryClassName = get_class(new Category()); $this->mockLogin(); $form = new CategoryForm($this->entityManager); $this->dispatch('/admin/category/' . $childId, Request::METHOD_PUT, ['sort' => 5, 'content[0][title]' => urlencode('Some title'), 'parent' => $lastInsertedId, 'category_csrf' => $form->get('category_csrf')->getValue()]); $this->assertEquals(200, $this->getResponse()->getStatusCode()); //check if the child category has changed it's direct parent $childCategory = $this->entityManager->find($categoryClassName, $childId); $this->assertEquals($lastInsertedId, $childCategory->getParent()); //check all the parents of the child category $parentIDs = []; foreach ($childCategory->getParents() as $parent) { $parentIDs[] = $parent->getId(); } $this->assertTrue(in_array($lastInsertedId, $parentIDs)); //check the child's child parents $childChild = $this->entityManager->getRepository($categoryClassName)->getChildren($childCategory)[0]; $childParentIDs = []; foreach ($childChild->getParents() as $descendants) { $childParentIDs[] = $descendants->getId(); } $this->assertTrue(in_array($lastInsertedId, $childParentIDs)); $this->assertEquals($childCategory->getId(), $childChild->getParent()); //test the direct parent }
public function getChildren(Category $category) { $qb = $this->createQueryBuilder('c'); $qb->select('c')->join('c.parents', 'p')->where('p.id=' . $category->getId()); return $qb->getQuery()->getResult(); }
/** * Get all the categories except the current one and it's children * @param Category $category * @return array Categories array handy for setting form select options [id] => intent+title */ protected function getAllButChildren(Category $category) { $childIds = []; if (!empty($category->getId())) { $categoryChildren = $this->categoryRepo->getChildren($category); foreach ($categoryChildren as $child) { $childIds[] = $child->getId(); } } $allButOwnCategs = []; foreach ($this->categoriesAsOptions as $id => $categs) { if ($id != $category->getId() && !in_array($id, $childIds)) { $allButOwnCategs[$id] = $this->categoriesAsOptions[$id]; } } return $allButOwnCategs; }