src/Controller/Admin/BatchCollegeAttachmentController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Vich\UploaderBundle\Handler\DownloadHandler;
  9. use App\Entity\BatchCollegeAttachment;
  10. use App\Entity\Batch;
  11. use App\Security\BatchVoter;
  12. use App\Form\AttachmentType;
  13. use App\Repository\BatchCollegeAttachmentRepository;
  14. /**
  15.  *
  16.  * @Route("/admin/batch-college-attachment/", name="admin_batch_college_attachment_")
  17.  * @author "wendell.zheng <wxzheng@ustc.edu.cn>"
  18.  */
  19. class BatchCollegeAttachmentController extends AbstractController
  20. {
  21.     /**
  22.      *
  23.      * @Route("list/{id}", name="list", requirements={"id"="\d+"})
  24.      */
  25.     public function list(Batch $batchBatchCollegeAttachmentRepository $re): Response
  26.     {
  27.         $user $this->getUser();
  28.         $this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION$batch);
  29.         return $this->render('admin/batch_college_attachment/list.html.twig', [
  30.             'title' => $batch->getName() . ' - 学院附件列表',
  31.             'batch' => $batch,
  32.             'entities' => $re->findBy([
  33.                 'batch' => $batch,
  34.                 'college' => $user->getAdminCollege()
  35.             ])
  36.         ]);
  37.     }
  38.     /**
  39.      *
  40.      * @Route("download/{id}", name="download", requirements={"id"="\d+"})
  41.      */
  42.     public function download(BatchCollegeAttachment $attachmentDownloadHandler $downloadHandler): Response
  43.     {
  44.         $batch $attachment->getBatch();
  45.         $this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION$batch);
  46.         return $downloadHandler->downloadObject($attachment'attachment'null$attachment->getDownloadName());
  47.     }
  48.     /**
  49.      *
  50.      * @Route("new/{id}", name="new", requirements={"id"="\d+"}):Response
  51.      */
  52.     public function new(EntityManagerInterface $emBatch $batchRequest $request): Response
  53.     {
  54.         $this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION$batch);
  55.         $attachment = new BatchCollegeAttachment();
  56.         $attachment->setBatch($batch);
  57.         $user $this->getUser();
  58.         $attachment->setCollege($user->getAdminCollege());
  59.         $form $this->createForm(AttachmentType::class, $attachment);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted() && $form->isValid()) {
  62.             $em->persist($attachment);
  63.             $em->flush();
  64.             $this->addFlash('notice''操作成功');
  65.             return $this->redirectToRoute('admin_batch_college_attachment_list', [
  66.                 'id' => $batch->getId()
  67.             ]);
  68.         }
  69.         return $this->render('admin/batch_college_attachment/edit.html.twig', [
  70.             'title' => $batch->getName() . '上传院校附件',
  71.             'batch' => $batch,
  72.             'form' => $form->createView()
  73.         ]);
  74.     }
  75.     /**
  76.      *
  77.      * @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
  78.      */
  79.     public function delete(EntityManagerInterface $emBatchCollegeAttachment $attachment): Response
  80.     {
  81.         $batch $attachment->getBatch();
  82.         $this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION$batch);
  83.         $user $this->getUser();
  84.         if ($user->getAdminCollege() == $attachment->getCollege()) {
  85.             $em->remove($attachment);
  86.             $em->flush();
  87.             $this->addFlash('notice''操作成功');
  88.         } else {
  89.             $this->addFlash('notice''没有权限');
  90.         }
  91.         return $this->redirectToRoute('admin_batch_college_attachment_list', [
  92.             'id' => $batch->getId()
  93.         ]);
  94.     }
  95. }