<?php
namespace App\Controller\Admin;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use Vich\UploaderBundle\Handler\DownloadHandler;
use App\Entity\BatchCollegeAttachment;
use App\Entity\Batch;
use App\Security\BatchVoter;
use App\Form\AttachmentType;
use App\Repository\BatchCollegeAttachmentRepository;
/**
*
* @Route("/admin/batch-college-attachment/", name="admin_batch_college_attachment_")
* @author "wendell.zheng <wxzheng@ustc.edu.cn>"
*/
class BatchCollegeAttachmentController extends AbstractController
{
/**
*
* @Route("list/{id}", name="list", requirements={"id"="\d+"})
*/
public function list(Batch $batch, BatchCollegeAttachmentRepository $re): Response
{
$user = $this->getUser();
$this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION, $batch);
return $this->render('admin/batch_college_attachment/list.html.twig', [
'title' => $batch->getName() . ' - 学院附件列表',
'batch' => $batch,
'entities' => $re->findBy([
'batch' => $batch,
'college' => $user->getAdminCollege()
])
]);
}
/**
*
* @Route("download/{id}", name="download", requirements={"id"="\d+"})
*/
public function download(BatchCollegeAttachment $attachment, DownloadHandler $downloadHandler): Response
{
$batch = $attachment->getBatch();
$this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION, $batch);
return $downloadHandler->downloadObject($attachment, 'attachment', null, $attachment->getDownloadName());
}
/**
*
* @Route("new/{id}", name="new", requirements={"id"="\d+"}):Response
*/
public function new(EntityManagerInterface $em, Batch $batch, Request $request): Response
{
$this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION, $batch);
$attachment = new BatchCollegeAttachment();
$attachment->setBatch($batch);
$user = $this->getUser();
$attachment->setCollege($user->getAdminCollege());
$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('admin_batch_college_attachment_list', [
'id' => $batch->getId()
]);
}
return $this->render('admin/batch_college_attachment/edit.html.twig', [
'title' => $batch->getName() . '上传院校附件',
'batch' => $batch,
'form' => $form->createView()
]);
}
/**
*
* @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
*/
public function delete(EntityManagerInterface $em, BatchCollegeAttachment $attachment): Response
{
$batch = $attachment->getBatch();
$this->denyAccessUnlessGranted(BatchVoter::COLLEGE_ACTION, $batch);
$user = $this->getUser();
if ($user->getAdminCollege() == $attachment->getCollege()) {
$em->remove($attachment);
$em->flush();
$this->addFlash('notice', '操作成功');
} else {
$this->addFlash('notice', '没有权限');
}
return $this->redirectToRoute('admin_batch_college_attachment_list', [
'id' => $batch->getId()
]);
}
}