src/Controller/Super/CategoryController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Super;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Entity\Batch;
  7. use App\Entity\Category;
  8. use App\Form\CategoryType;
  9. /**
  10.  *
  11.  * @Route("/super/category/", name="super_category_")
  12.  * @author "wendell.zheng <wxzheng@ustc.edu.cn>"
  13.  */
  14. class CategoryController extends BaseController
  15. {
  16.     /**
  17.      *
  18.      * @Route("list/{id}", name="list", requirements={"id"="\d+"})
  19.      */
  20.     public function list(Batch $batch): Response
  21.     {
  22.         return $this->render('super/category/list.html.twig', [
  23.             'name' => $this->getLowerName(),
  24.             'title' => $batch->getName() . ' - 项目类别列表',
  25.             'batch' => $batch,
  26.             'entities' => $this->getRepository()
  27.                 ->findby([
  28.                 'batch' => $batch
  29.             ])
  30.         ]);
  31.     }
  32.     /**
  33.      *
  34.      * @Route("new/{id}", name="new", requirements={"id"="\d+"})
  35.      */
  36.     public function new(Batch $batchRequest $request): Response
  37.     {
  38.         $category = new Category();
  39.         $category->setBatch($batch);
  40.         return $this->categoryEdit($category$batch$request);
  41.     }
  42.     /**
  43.      *
  44.      * @Route("edit/{id}", name="edit", requirements={"id"="\d+"})
  45.      */
  46.     public function edit(Category $categoryRequest $request)
  47.     {
  48.         $batch $category->getBatch();
  49.         return $this->categoryEdit($category$batch$request);
  50.     }
  51.     /**
  52.      *
  53.      * @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
  54.      */
  55.     public function delete(Category $category): Response
  56.     {
  57.         $levels $category->getLevels();
  58.         if (count($levels)) {
  59.             foreach ($levels as $level) {
  60.                 $this->em->remove($level);
  61.             }
  62.         }
  63.         return $this->doDelete($category'super_category_list', [
  64.             'id' => $category->getBatch()
  65.                 ->getId()
  66.         ]);
  67.     }
  68.     protected function categoryEdit(Category $categoryBatch $batchRequest $request): Response
  69.     {
  70.         $title $category->getId() ? '编辑' '新建';
  71.         $form $this->createForm(CategoryType::class, $category);
  72.         $form->handleRequest($request);
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             $this->em->persist($category);
  75.             $this->em->flush();
  76.             $this->addFlash('category''操作成功');
  77.             return $this->redirectToRoute('super_category_list', [
  78.                 'id' => $batch->getId()
  79.             ]);
  80.         }
  81.         return $this->render('super/category/edit.html.twig', [
  82.             'title' => $batch->getName() . ' - ' $title '项目类别',
  83.             'batch' => $batch,
  84.             'form' => $form->createView()
  85.         ]);
  86.     }
  87. }