src/PN/Bundle/ProductBundle/Controller/Administration/CategoryController.php
/**
     * Creates a new category entity.
     *
     * @Route("/new/{parent}", requirements={"parent" = "\d+"}, name="category_new", methods={"GET", "POST"})
     */
Creates a new category entity.
Creates a new category entity .
public function newAction(Request $request, $parent = null)
    {
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
        $em = $this->getDoctrine()->getManager();
        if ($parent != null) {
            $parent = $em->getRepository(Category::class)->find($parent);
        }
        $category = new Category();
        $form = $this->createForm(CategoryType::class, $category);
        if ($parent) {
            $category->setParent($parent);
        }
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $depth = $this->getDepth($category);
            $category->setDepth($depth);
            $levelOneCategory = $this->getLevelOne($category);
            $category->setLevelOne($levelOneCategory);
            $parentConcat = $this->getParentConcat($parent);
            $category->setParentConcatIds($parentConcat);
            $em->persist($category);
            $em->flush();
            $category->setConcatIds($category->getId());
            $this->updateParentConcatIds($category, $parent);
            $em->persist($category);
            $em->flush();
            if ($request->request->get("action") == "saveAndNext") {
                return $this->redirectToRoute('attribute_index', ["id" => $category->getId()]);
            }
            $uploadImage = $this->uploadImage($request, $form, $category);
            if ($uploadImage != false) {
                $this->addFlash('success', 'Successfully saved');
                return $this->redirectToRoute('category_index');
            }
            if ($category->getParent()) {
                return $this->redirectToRoute('category_index', ['parentCategory' => $category->getParent()->getId()]);
            }
            return $this->redirectToRoute('category_index');
        }
        $categoryParents = $this->get(CategoryService::class)->parentsByChildId($parent);
        return $this->render('product/admin/category/new.html.twig', array(
            'category' => $category,
            'categoryParents' => $categoryParents,
            'form' => $form->createView(),
        ));
    }
public function newAction ( Request $ request , $ parent = null ) { $ this -> denyAccessUnlessGranted ( 'ROLE_ADMIN' ) ; $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; if ( $ parent != null ) { $ parent = $ em -> getRepository ( Category :: class ) -> find ( $ parent ) ; } $ category = new Category ( ) ; $ form = $ this -> createForm ( CategoryType :: class , $ category ) ; if ( $ parent ) { $ category -> setParent ( $ parent ) ; } $ form -> handleRequest ( $ request ) ; if ( $ form -> isSubmitted ( ) && $ form -> isValid ( ) ) { $ depth = $ this -> getDepth ( $ category ) ; $ category -> setDepth ( $ depth ) ; $ levelOneCategory = $ this -> getLevelOne ( $ category ) ; $ category -> setLevelOne ( $ levelOneCategory ) ; $ parentConcat = $ this -> getParentConcat ( $ parent ) ; $ category -> setParentConcatIds ( $ parentConcat ) ; $ em -> persist ( $ category ) ; $ em -> flush ( ) ; $ category -> setConcatIds ( $ category -> getId ( ) ) ; $ this -> updateParentConcatIds ( $ category , $ parent ) ; $ em -> persist ( $ category ) ; $ em -> flush ( ) ; if ( $ request -> request -> get ( " action " ) == " saveAndNext " ) { return $ this -> redirectToRoute ( 'attribute_index' , [ " id " => $ category -> getId ( ) ] ) ; } $ uploadImage = $ this -> uploadImage ( $ request , $ form , $ category ) ; if ( $ uploadImage != false ) { $ this -> addFlash ( 'success' , 'Successfully saved' ) ; return $ this -> redirectToRoute ( 'category_index' ) ; } if ( $ category -> getParent ( ) ) { return $ this -> redirectToRoute ( 'category_index' , [ 'parentCategory' => $ category -> getParent ( ) -> getId ( ) ] ) ; } return $ this -> redirectToRoute ( 'category_index' ) ; } $ categoryParents = $ this -> get ( CategoryService :: class ) -> parentsByChildId ( $ parent ) ; return $ this -> render ( 'product/admin/category/new.html.twig' , array ( 'category' => $ category , 'categoryParents' => $ categoryParents , 'form' => $ form -> createView ( ) , ) ) ; }
Creates a new category entity.
Creates a new category entity .