ninenote/src/ninenote-1.0/src/Controller/AssignmentteacherController...

197 lines
6.5 KiB
PHP
Executable File

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\FormError;
use App\Entity\Assignmentteacher as Entity;
use App\Entity\Assignment as Assignment;
use App\Form\AssignmentteacherType as Form;
class AssignmentteacherController extends AbstractController
{
private $data = "assignmentteacher";
private $route = "app_assignmentteacher";
private $render = "Assignmentteacher/";
private $entity = "App:Assignmentteacher";
public function list($id,Request $request)
{
$em = $this->getDoctrine()->getManager();
$classroom=$em->getRepository("App:Classroom")->find($id);
if(!$classroom) return $this->redirectToRoute("app_classroom");
$datas = $em->getRepository($this->entity)->findBy(["classroom"=>$classroom]);
return $this->render($this->render.'list.html.twig',[
"classroom" => $classroom,
$this->data."s" => $datas,
"useheader" => true,
"usesidebar" => true,
]);
}
public function submit($id,Request $request) {
$em = $this->getDoctrine()->getManager();
$classroom=$em->getRepository("App:Classroom")->find($id);
if(!$classroom) return $this->redirectToRoute("app_classroom");
$datas = $em->getRepository($this->entity)->findBy(["classroom"=>$classroom]);
$data = new Entity();
$data->setClassroom($classroom);
$data->setIsprimary(false);
// Création du formulaire
$form = $this->createForm(Form::class,$data,array("mode"=>"submit","idclassroom"=>$id));
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur erreur
$this->getErrorForm(null,$form,$request,$data,"submit");
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
$data = $form->getData();
// On gére les matières du professeur
$this->addMatter($data);
$em->persist($data);
$em->flush();
// Retour à la liste
return $this->redirectToRoute($this->route,["id"=>$id]);
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
'useheader' => true,
'usesidebar' => true,
'classroom' => $classroom,
$this->data => $data,
'mode' => 'submit',
'form' => $form->createView()
]);
}
public function update($id,Request $request)
{
// Initialisation de l'enregistrement
$em = $this->getDoctrine()->getManager();
$data=$em->getRepository($this->entity)->find($id);
// Création du formulaire
$form = $this->createForm(Form::class,$data,array("mode"=>"update","idclassroom"=>$data->getClassroom()->getId()));
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur erreur
$this->getErrorForm(null,$form,$request,$data,"update");
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
$data = $form->getData();
// On gére les matières du professeur
$this->addMatter($data);
// On enregistre
$em->persist($data);
$em->flush();
// Retour à la liste
return $this->redirectToRoute($this->route,["id"=>$data->getClassroom()->getId()]);
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
'useheader' => true,
'usesidebar' => true,
'classroom' => $data->getClassroom(),
$this->data => $data,
'mode' => 'update',
'form' => $form->createView()
]);
}
public function delete($id,Request $request)
{
// Initialisation de l'enregistrement
$em = $this->getDoctrine()->getManager();
$data=$em->getRepository($this->entity)->find($id);
// Controle avant suppression
$error=false;
if($id<0) $error=true;
if($error) {
return $this->redirectToRoute($this->route."_update",["id"=>$id]);
}
else {
$classroomid=$data->getClassroom()->getId();
$em->remove($data);
$em->flush();
// Retour à la liste
return $this->redirectToRoute($this->route,["id"=>$classroomid]);
}
}
protected function addMatter($data) {
$em = $this->getDoctrine()->getManager();
// On génère les teachers
$tbassignments=[];
foreach($data->getMatters() as $matter) {
$assignmentmatter=$em->getRepository("App:Assignmentmatter")->findOneBy(["matter"=>$matter,"classroom"=>$data->getClassroom()]);
$assignment=$em->getRepository("App:Assignment")->findOneBy(["assignmentmatter"=>$assignmentmatter,"assignmentteacher"=>$data]);
if(!$assignment) {
$assignment=new Assignment();
$assignment->setAssignmentmatter($assignmentmatter);
$assignment->setAssignmentteacher($data);
$assignment->setClassroom($data->getClassroom());
$em->persist($assignment);
}
array_push($tbassignments,$assignment->getId());
}
// On purge les teachers
foreach($data->getAssignments() as $assignment) {
if(!in_array($assignment->getId(),$tbassignments)) {
$em->remove($assignment);
$em->flush();
}
}
}
protected function getErrorForm($id,$form,$request,$data,$mode) {
$em = $this->getDoctrine()->getManager();
if ($form->get('submit')->isClicked() && $mode=="submit") {
}
if ($form->get('submit')->isClicked() && ($mode=="submit" || $mode=="update")) {
}
if ($form->get('submit')->isClicked() && !$form->isValid()) {
$this->get('session')->getFlashBag()->clear();
$errors = $form->getErrors();
foreach( $errors as $error ) {
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
}
}
}
}