<?php
namespace App\Controller\Super;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Batch;
use App\Entity\Notice;
use App\Form\NoticeType;
/**
*
* @Route("/super/notice/", name="super_notice_")
* @author "wendell.zheng <wxzheng@ustc.edu.cn>"
*/
class NoticeController extends BaseController
{
/**
*
* @Route("list/{id}", name="list", requirements={"id"="\d+"})
*/
public function list(Batch $batch): Response
{
return $this->render('super/notice/list.html.twig', [
'name' => $this->getLowerName(),
'title' => $batch->getName() . ' - 通知列表',
'batch' => $batch,
'entities' => $batch->getNotices()
]);
}
/**
*
* @Route("new/{id}", name="new", requirements={"id"="\d+"})
*/
public function new(Batch $batch, Request $request): Response
{
$notice = new Notice();
$notice->setBatch($batch);
return $this->noticeEdit($notice, $batch, $request);
}
/**
*
* @Route("edit/{id}", name="edit", requirements={"id"="\d+"})
*/
public function edit(Notice $notice, Request $request)
{
$batch = $notice->getBatch();
return $this->noticeEdit($notice, $batch, $request);
}
/**
*
* @Route("delete/{id}", name="delete", requirements={"id"="\d+"})
*/
public function delete(Notice $notice): Response
{
$attachments = $notice->getAttachments();
if (count($attachments)) {
foreach ($attachments as $attachment) {
$this->em->remove($attachment);
}
}
return $this->doDelete($notice, 'super_notice_list', [
'id' => $notice->getBatch()
->getId()
]);
}
protected function noticeEdit(Notice $notice, Batch $batch, Request $request): Response
{
$title = $notice->getId() ? '编辑' : '新建';
$form = $this->createForm(NoticeType::class, $notice);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($notice);
$this->em->flush();
$this->addFlash('notice', '操作成功');
return $this->redirectToRoute('super_notice_list', [
'id' => $batch->getId()
]);
}
return $this->render('super/notice/edit.html.twig', [
'title' => $batch->getName() . ' - ' . $title . '通知',
'batch' => $batch,
'form' => $form->createView()
]);
}
}