src/Controller/Applicant/ProjectAttachmentController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Applicant;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Vich\UploaderBundle\Handler\DownloadHandler;
  9. use App\Entity\Project;
  10. use App\Security\ProjectVoter;
  11. use App\Entity\ProjectAttachment;
  12. use App\Form\AttachmentType;
  13. /**
  14.  *
  15.  * @Route("/applicant/project/attachment/", name="applicant_project_attachment_")
  16.  * @author "wendell.zheng <wxzheng@ustc.edu.cn>"
  17.  */
  18. class ProjectAttachmentController extends AbstractController implements UserInfoController
  19. {
  20.     /**
  21.      *
  22.      * @Route("index/{id}", name="index", requirements={"id"="\d+"})
  23.      */
  24.     public function index(Project $project): Response
  25.     {
  26.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_VIEW$project);
  27.         return $this->render('applicant/project_attachment/index.html.twig', [
  28.             'title' => $project->getName(),
  29.             'entities' => $project->getAttachments(),
  30.             'project' => $project
  31.         ]);
  32.     }
  33.     /**
  34.      *
  35.      * @Route("new/{id}", name="new", requirements={"id"="\d+"}):Response
  36.      */
  37.     public function new(EntityManagerInterface $emProject $projectRequest $request): Response
  38.     {
  39.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_EDIT$project);
  40.         $attachment = new ProjectAttachment();
  41.         $attachment->setProject($project);
  42.         $form $this->createForm(AttachmentType::class, $attachment);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             $em->persist($attachment);
  46.             $em->flush();
  47.             $this->addFlash('notice''操作成功');
  48.             return $this->redirectToRoute('applicant_project_attachment_index', [
  49.                 'id' => $project->getId()
  50.             ]);
  51.         }
  52.         return $this->render('applicant/project_attachment/edit.html.twig', [
  53.             'title' => $project->getName() . '上传附件',
  54.             'project' => $project,
  55.             'form' => $form->createView()
  56.         ]);
  57.     }
  58.     /**
  59.      *
  60.      * @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
  61.      */
  62.     public function delete(EntityManagerInterface $emProjectAttachment $attachment): Response
  63.     {
  64.         $project $attachment->getProject();
  65.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_EDIT$project);
  66.         $em->remove($attachment);
  67.         $em->flush();
  68.         $this->addFlash('notice''操作成功');
  69.         return $this->redirectToRoute('applicant_project_attachment_index', [
  70.             'id' => $project->getId()
  71.         ]);
  72.     }
  73.     /**
  74.      *
  75.      * @Route("download/{id}", name="download", requirements={"id"="\d+"})
  76.      */
  77.     public function download(ProjectAttachment $attachmentDownloadHandler $downloadHandler): Response
  78.     {
  79.         $project $attachment->getProject();
  80.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_VIEW$project);
  81.         return $downloadHandler->downloadObject($attachment'attachment'null$attachment->getDownloadName());
  82.     }
  83. }