<?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\Entity\Batch;
use App\Form\ProjectType;
use App\Security\BatchVoter;
use App\Security\ProjectVoter;
use App\Repository\ProjectRepository;
/**
*
* @Route("/applicant/project/", name="applicant_project_")
* @author "wendell.zheng <wxzheng@ustc.edu.cn>"
*/
class ProjectController extends AbstractController implements UserInfoController
{
/**
*
* @Route("index/{id}", name="index", requirements={"id"="\d+"})
*/
public function index(ProjectRepository $pr, Batch $batch): Response
{
$projects = $pr->findBy([
'user' => $this->getUser(),
'batch' => $batch
]);
return $this->render('applicant/project/index.html.twig', [
'title' => $batch->getName(),
'entities' => $projects,
'batch' => $batch
]);
}
/**
*
* @Route("new/{id}", name="new", requirements={"id"="\d+"})
*/
public function new(Request $request, EntityManagerInterface $em, Batch $batch): Response
{
$this->denyAccessUnlessGranted(BatchVoter::APPLICANT_ACTION, $batch);
$project = new Project();
$project->setBatch($batch);
$project->setUser($this->getUser());
return $this->doEdit($project, $request, $em, $batch);
}
/**
*
* @Route("edit/{id}", name="edit", requirements={"id"="\d+"})
*/
public function edit(Request $request, EntityManagerInterface $em, Project $project): Response
{
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_EDIT, $project);
return $this->doEdit($project, $request, $em, $project->getBatch());
}
/**
*
* @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
*/
public function delete(EntityManagerInterface $em, Project $project): Response
{
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_DELETE, $project);
$attachments = $project->getAttachments();
if (count($attachments)) {
foreach ($attachments as $attachment) {
$em->remove($attachment);
}
}
$em->remove($project);
$em->flush();
return $this->redirectToRoute('applicant_project_index', [
'id' => $project->getBatch()
->getId()
]);
}
/**
*
* @Route("download/{id}", name="download", requirements={"id"="\d+"})
*/
public function download(Project $project, DownloadHandler $downloadHandler): Response
{
$this->denyAccessUnlessGranted(ProjectVoter::APPLICANT_VIEW, $project);
return $downloadHandler->downloadObject($project, 'doc', null, $project->getDocDownloadName());
}
/**
*
* @Route("my", name="my")
*/
public function my(ProjectRepository $pr): Response
{
$projects = $pr->findBy([
'user' => $this->getUser()
]);
return $this->render('applicant/project/my.html.twig', [
'title' => '提交记录',
'entities' => $projects
]);
}
protected function doEdit(Project $project, Request $request, EntityManagerInterface $em, Batch $batch): Response
{
$title = $project->getId() ? '编辑' : '提交材料';
$form = $this->createForm(ProjectType::class, $project);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($project);
$em->flush();
$this->addFlash('notice', '操作成功');
return $this->redirectToRoute('applicant_project_index', [
'id' => $batch->getId()
]);
}
return $this->render('applicant/project/edit.html.twig', [
'title' => $batch->getName() . ' - ' . $title,
'batch' => $batch,
'form' => $form->createView()
]);
}
}