/**
  * Create a tree or category
  *
  * @param Request $request
  * @param int     $parent
  *
  * @AclAncestor("pim_enrich_category_create")
  *
  * @return Response|RedirectResponse
  */
 public function createAction(Request $request, $parent = null)
 {
     if ($parent) {
         $parent = $this->findCategory($parent);
         $category = $this->categoryManager->getCategoryInstance();
         $category->setParent($parent);
     } else {
         $category = $this->categoryManager->getTreeInstance();
     }
     $category->setCode($request->get('label'));
     $this->eventDispatcher->dispatch(CategoryEvents::PRE_CREATE, new GenericEvent($category));
     $form = $this->createForm('pim_category', $category, $this->getFormOptions($category));
     if ($request->isMethod('POST')) {
         $form->bind($request);
         if ($form->isValid()) {
             $this->categorySaver->save($category);
             $this->addFlash('success', sprintf('flash.%s.created', $category->getParent() ? 'category' : 'tree'));
             $this->eventDispatcher->dispatch(CategoryEvents::POST_CREATE, new GenericEvent($category));
             return $this->redirectToRoute('pim_enrich_categorytree_edit', ['id' => $category->getId()]);
         }
     }
     return $this->render(sprintf('PimEnrichBundle:CategoryTree:%s.html.twig', $request->get('content', 'edit')), ['form' => $form->createView()]);
 }