src/Controller/Applicant/ProjectController.php line 29

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\Entity\Batch;
  11. use App\Form\ProjectType;
  12. use App\Security\BatchVoter;
  13. use App\Security\ProjectVoter;
  14. use App\Repository\ProjectRepository;
  15. /**
  16.  *
  17.  * @Route("/applicant/project/", name="applicant_project_")
  18.  * @author "wendell.zheng <wxzheng@ustc.edu.cn>"
  19.  */
  20. class ProjectController extends AbstractController implements UserInfoController
  21. {
  22.     /**
  23.      *
  24.      * @Route("index/{id}", name="index", requirements={"id"="\d+"})
  25.      */
  26.     public function index(ProjectRepository $prBatch $batch): Response
  27.     {
  28.         $projects $pr->findBy([
  29.             'user' => $this->getUser(),
  30.             'batch' => $batch
  31.         ]);
  32.         return $this->render('applicant/project/index.html.twig', [
  33.             'title' => $batch->getName(),
  34.             'entities' => $projects,
  35.             'batch' => $batch
  36.         ]);
  37.     }
  38.     /**
  39.      *
  40.      * @Route("new/{id}", name="new", requirements={"id"="\d+"})
  41.      */
  42.     public function new(Request $requestEntityManagerInterface $emBatch $batch): Response
  43.     {
  44.         $this->denyAccessUnlessGranted(BatchVoter::APPLICANT_ACTION$batch);
  45.         $project = new Project();
  46.         $project->setBatch($batch);
  47.         $project->setUser($this->getUser());
  48.         return $this->doEdit($project$request$em$batch);
  49.     }
  50.     /**
  51.      *
  52.      * @Route("edit/{id}", name="edit", requirements={"id"="\d+"})
  53.      */
  54.     public function edit(Request $requestEntityManagerInterface $emProject $project): Response
  55.     {
  56.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_EDIT$project);
  57.         return $this->doEdit($project$request$em$project->getBatch());
  58.     }
  59.     /**
  60.      *
  61.      * @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
  62.      */
  63.     public function delete(EntityManagerInterface $emProject $project): Response
  64.     {
  65.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_DELETE$project);
  66.         $attachments $project->getAttachments();
  67.         if (count($attachments)) {
  68.             foreach ($attachments as $attachment) {
  69.                 $em->remove($attachment);
  70.             }
  71.         }
  72.         $em->remove($project);
  73.         $em->flush();
  74.         return $this->redirectToRoute('applicant_project_index', [
  75.             'id' => $project->getBatch()
  76.                 ->getId()
  77.         ]);
  78.     }
  79.     /**
  80.      *
  81.      * @Route("download/{id}", name="download", requirements={"id"="\d+"})
  82.      */
  83.     public function download(Project $projectDownloadHandler $downloadHandler): Response
  84.     {
  85.         $this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_VIEW$project);
  86.         return $downloadHandler->downloadObject($project'doc'null$project->getDocDownloadName());
  87.     }
  88.     /**
  89.      *
  90.      * @Route("my", name="my")
  91.      */
  92.     public function my(ProjectRepository $pr): Response
  93.     {
  94.         $projects $pr->findBy([
  95.             'user' => $this->getUser()
  96.         ]);
  97.         return $this->render('applicant/project/my.html.twig', [
  98.             'title' => '提交记录',
  99.             'entities' => $projects
  100.         ]);
  101.     }
  102.     protected function doEdit(Project $projectRequest $requestEntityManagerInterface $emBatch $batch): Response
  103.     {
  104.         $title $project->getId() ? '编辑' '提交材料';
  105.         $form $this->createForm(ProjectType::class, $project);
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             $em->persist($project);
  109.             $em->flush();
  110.             $this->addFlash('notice''操作成功');
  111.             return $this->redirectToRoute('applicant_project_index', [
  112.                 'id' => $batch->getId()
  113.             ]);
  114.         }
  115.         return $this->render('applicant/project/edit.html.twig', [
  116.             'title' => $batch->getName() . ' - ' $title,
  117.             'batch' => $batch,
  118.             'form' => $form->createView()
  119.         ]);
  120.     }
  121. }