src/Controller/Super/ReviewBatchController.php line 24

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\ReviewBatch;
  7. use App\Util\Exporter\ReviewExporter;
  8. use App\Repository\ReviewRepository;
  9. use App\Form\ReviewBatchType;
  10. /**
  11.  *
  12.  * @Route("/super/review-batch/", name="super_review_batch_")
  13.  * @author "wendell.zheng <wxzheng@ustc.edu.cn>"
  14.  */
  15. class ReviewBatchController extends BaseController
  16. {
  17.     /**
  18.      *
  19.      * @Route("list", name="list")
  20.      */
  21.     public function list(): Response
  22.     {
  23.         return $this->doList();
  24.     }
  25.     /**
  26.      *
  27.      * @Route("new", name="new")
  28.      */
  29.     public function new(Request $request): Response
  30.     {
  31.         return $this->doEdit($request, new ReviewBatch());
  32.     }
  33.     /**
  34.      *
  35.      * @Route("edit/{id}", name="edit", requirements={
  36.      * "id": "\d+"
  37.      * })
  38.      */
  39.     public function edit(Request $requestReviewBatch $reviewBatch): Response
  40.     {
  41.         return $this->doEdit($request$reviewBatch);
  42.     }
  43.     /**
  44.      *
  45.      * @Route("delete/{id}", name="delete", requirements={
  46.      * "id": "\d+"
  47.      * })
  48.      */
  49.     public function delete(ReviewBatch $reviewBatch): Response
  50.     {
  51.         $items = [
  52.             'Reviewers' => '评审专家'
  53.         ];
  54.         foreach ($items as $type => $name) {
  55.             $method "get{$type}";
  56.             if (count($reviewBatch->$method())) {
  57.                 $this->addFlash('notice'"批次下有{$name},需先删除{$name}!");
  58.                 return $this->redirectToRoute($this->getListRoute());
  59.             }
  60.         }
  61.         return $this->doDelete($reviewBatch);
  62.     }
  63.     /**
  64.      *
  65.      * @Route("current/{id}", name="current", requirements={
  66.      * "id": "\d+"
  67.      * })
  68.      */
  69.     public function current(ReviewBatch $reviewBatch): Response
  70.     {
  71.         $reviewBatchs $this->repository->findAll();
  72.         foreach ($reviewBatchs as $item) {
  73.             if ($item->getId() == $reviewBatch->getId()) {
  74.                 $reviewBatch->setCurrent(true);
  75.             } else {
  76.                 $item->setCurrent(false);
  77.             }
  78.         }
  79.         $this->em->flush();
  80.         $this->addFlash('notice''设置成功');
  81.         return $this->redirectToRoute($this->getListRoute());
  82.     }
  83.     /**
  84.      *
  85.      * @Route("export/{id}", name="export", requirements={
  86.      * "id": "\d+"
  87.      * })
  88.      */
  89.     public function export(ReviewBatch $reviewBatchReviewRepository $rr): Response
  90.     {
  91.         $reviews $rr->findBy([
  92.             'reviewBatch' => $reviewBatch
  93.         ], [
  94.             'group' => 'ASC',
  95.             'sort' => 'ASC'
  96.         ]);
  97.         $exporter = new ReviewExporter($reviews);
  98.         return $exporter->download($reviewBatch->getName() . '_评审结果');
  99.     }
  100.     protected function doEdit(Request $request$reviewBatch null): Response
  101.     {
  102.         $form $this->createForm(ReviewBatchType::class, $reviewBatch);
  103.         $form->handleRequest($request);
  104.         if ($form->isSubmitted() && $form->isValid()) {
  105.             $this->em->persist($reviewBatch);
  106.             $this->em->flush();
  107.             if ($reviewBatch->getCurrent()) {
  108.                 $reviewBatchs $this->repository->findAll();
  109.                 foreach ($reviewBatchs as $item) {
  110.                     if ($item->getId() != $reviewBatch->getId()) {
  111.                         $item->setCurrent(false);
  112.                     }
  113.                 }
  114.                 $this->em->flush();
  115.             }
  116.             $this->addFlash('notice''操作成功');
  117.             return $this->redirectToRoute($this->getListRoute());
  118.         }
  119.         $data = [
  120.             'name' => $this->getLowerName(),
  121.             'title' => $this->getEditTitle($reviewBatch->getId()),
  122.             'form' => $form->createView()
  123.         ];
  124.         return $this->render('super/common_edit.html.twig'$data);
  125.     }
  126. }