<?php
namespace App\Controller\Applicant;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use Vich\UploaderBundle\Handler\DownloadHandler;
use App\Entity\Project;
use App\Security\ProjectVoter;
use App\Entity\ProjectAttachment;
use App\Form\AttachmentType;
/**
*
* @Route("/applicant/project/attachment/", name="applicant_project_attachment_")
* @author "wendell.zheng <wxzheng@ustc.edu.cn>"
*/
class ProjectAttachmentController extends AbstractController implements UserInfoController
{
/**
*
* @Route("index/{id}", name="index", requirements={"id"="\d+"})
*/
public function index(Project $project): Response
{
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_VIEW, $project);
return $this->render('applicant/project_attachment/index.html.twig', [
'title' => $project->getName(),
'entities' => $project->getAttachments(),
'project' => $project
]);
}
/**
*
* @Route("new/{id}", name="new", requirements={"id"="\d+"}):Response
*/
public function new(EntityManagerInterface $em, Project $project, Request $request): Response
{
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_EDIT, $project);
$attachment = new ProjectAttachment();
$attachment->setProject($project);
$form = $this->createForm(AttachmentType::class, $attachment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($attachment);
$em->flush();
$this->addFlash('notice', '操作成功');
return $this->redirectToRoute('applicant_project_attachment_index', [
'id' => $project->getId()
]);
}
return $this->render('applicant/project_attachment/edit.html.twig', [
'title' => $project->getName() . '上传附件',
'project' => $project,
'form' => $form->createView()
]);
}
/**
*
* @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
*/
public function delete(EntityManagerInterface $em, ProjectAttachment $attachment): Response
{
$project = $attachment->getProject();
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_EDIT, $project);
$em->remove($attachment);
$em->flush();
$this->addFlash('notice', '操作成功');
return $this->redirectToRoute('applicant_project_attachment_index', [
'id' => $project->getId()
]);
}
/**
*
* @Route("download/{id}", name="download", requirements={"id"="\d+"})
*/
public function download(ProjectAttachment $attachment, DownloadHandler $downloadHandler): Response
{
$project = $attachment->getProject();
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_VIEW, $project);
return $downloadHandler->downloadObject($attachment, 'attachment', null, $attachment->getDownloadName());
}
}