src/Controller/Super/NoticeController.php line 49

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\Notice;
  8. use App\Form\NoticeType;
  9. /**
  10.  *
  11.  * @Route("/super/notice/", name="super_notice_")
  12.  * @author "wendell.zheng <wxzheng@ustc.edu.cn>"
  13.  */
  14. class NoticeController 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/notice/list.html.twig', [
  23.             'name' => $this->getLowerName(),
  24.             'title' => $batch->getName() . ' - 通知列表',
  25.             'batch' => $batch,
  26.             'entities' => $batch->getNotices()
  27.         ]);
  28.     }
  29.     /**
  30.      *
  31.      * @Route("new/{id}", name="new", requirements={"id"="\d+"})
  32.      */
  33.     public function new(Batch $batchRequest $request): Response
  34.     {
  35.         $notice = new Notice();
  36.         $notice->setBatch($batch);
  37.         return $this->noticeEdit($notice$batch$request);
  38.     }
  39.     /**
  40.      *
  41.      * @Route("edit/{id}", name="edit", requirements={"id"="\d+"})
  42.      */
  43.     public function edit(Notice $noticeRequest $request)
  44.     {
  45.         $batch $notice->getBatch();
  46.         return $this->noticeEdit($notice$batch$request);
  47.     }
  48.     /**
  49.      *
  50.      * @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
  51.      */
  52.     public function delete(Notice $notice): Response
  53.     {
  54.         $attachments $notice->getAttachments();
  55.         if (count($attachments)) {
  56.             foreach ($attachments as $attachment) {
  57.                 $this->em->remove($attachment);
  58.             }
  59.         }
  60.         return $this->doDelete($notice'super_notice_list', [
  61.             'id' => $notice->getBatch()
  62.                 ->getId()
  63.         ]);
  64.     }
  65.     protected function noticeEdit(Notice $noticeBatch $batchRequest $request): Response
  66.     {
  67.         $title $notice->getId() ? '编辑' '新建';
  68.         $form $this->createForm(NoticeType::class, $notice);
  69.         $form->handleRequest($request);
  70.         if ($form->isSubmitted() && $form->isValid()) {
  71.             $this->em->persist($notice);
  72.             $this->em->flush();
  73.             $this->addFlash('notice''操作成功');
  74.             return $this->redirectToRoute('super_notice_list', [
  75.                 'id' => $batch->getId()
  76.             ]);
  77.         }
  78.         return $this->render('super/notice/edit.html.twig', [
  79.             'title' => $batch->getName() . ' - ' $title '通知',
  80.             'batch' => $batch,
  81.             'form' => $form->createView()
  82.         ]);
  83.     }
  84. }