162 lines
5.4 KiB
PHP
162 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Form\ChildheaderType as Form;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
class ChildheaderController extends AbstractController
|
|
{
|
|
private $data = 'childheader';
|
|
private $route = 'app_childheader';
|
|
private $render = 'Childheader/';
|
|
private $entity = "App\Entity\Childheader";
|
|
|
|
public function submit($idchild, Request $request, ManagerRegistry $em)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$child = $em->getRepository("App\Entity\Child")->find($idchild);
|
|
if (!$child) {
|
|
throw $this->createNotFoundException('Not exist');
|
|
}
|
|
|
|
$modal = $request->get('modal');
|
|
|
|
return $this->render($this->render.'upload.html.twig', [
|
|
'useheader' => false,
|
|
'usesidebar' => false,
|
|
'idchild' => $idchild,
|
|
'modal' => $modal,
|
|
]);
|
|
}
|
|
|
|
public function update($catparent, $idparent, $id, Request $request, ManagerRegistry $em)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
$idchild = $data->getChild()->getId();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class, $data, ['mode' => 'update']);
|
|
|
|
// 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();
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute('app_child_update', ['catparent' => $catparent, 'idparent' => $idparent, 'idchild' => $idchild]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
'catparent' => $catparent,
|
|
'idparent' => $idparent,
|
|
'idchild' => $idchild,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function delete($catparent, $idparent, $id, Request $request, ManagerRegistry $em)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
$idchild = $data->getChild()->getId();
|
|
|
|
try {
|
|
$em->getManager()->remove($data);
|
|
$em->getManager()->flush();
|
|
} catch (\Exception $e) {
|
|
$request->getSession()->getFlashBag()->add('error', $e->getMessage());
|
|
}
|
|
|
|
return $this->redirectToRoute('app_child_update', ['catparent' => $catparent, 'idparent' => $idparent, 'idchild' => $idchild]);
|
|
}
|
|
|
|
public function view($id, Request $request, ManagerRegistry $em)
|
|
{
|
|
// Récupérer le childheader
|
|
$data = $em->getRepository("App\Entity\Childheader")->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Not exist');
|
|
}
|
|
|
|
$imageprev = $em->getRepository($this->entity)->findPrev($data->getChild()->getId(), $data->getRoworder());
|
|
$imagenext = $em->getRepository($this->entity)->findNext($data->getChild()->getId(), $data->getRoworder());
|
|
|
|
return $this->render($this->render.'image.html.twig', [
|
|
$this->data => $data,
|
|
'useheader' => false,
|
|
'usesidebar' => false,
|
|
'maxwidth' => true,
|
|
'imageprev' => $imageprev,
|
|
'imagenext' => $imagenext,
|
|
]);
|
|
}
|
|
|
|
public function select($idchild, Request $request, ManagerRegistry $em)
|
|
{
|
|
// S'assurer que c'est un appel ajax
|
|
if (!$request->isXmlHttpRequest()) {
|
|
return new JsonResponse(['message' => 'Interdit'], 400);
|
|
}
|
|
|
|
$child = $em->getRepository("App\Entity\Child")->find($idchild);
|
|
|
|
$childheaders = $child->getChildheaders();
|
|
$output = [];
|
|
foreach ($childheaders as $childheader) {
|
|
array_push($output, ['id' => $childheader->getId(), 'filename' => $childheader->getFilename(), 'credit' => $childheader->getCredit()]);
|
|
}
|
|
|
|
return new JsonResponse($output);
|
|
}
|
|
|
|
public function order($idchild, Request $request, ManagerRegistry $em)
|
|
{
|
|
$childheaderids = explode(',', $request->get('lstordered'));
|
|
$i = 1;
|
|
foreach ($childheaderids as $id) {
|
|
$childheader = $em->getRepository($this->entity)->find($id);
|
|
if ($childheader) {
|
|
$childheader->setRoworder($i);
|
|
$em->getManager()->flush();
|
|
}
|
|
++$i;
|
|
}
|
|
|
|
return new JsonResponse([]);
|
|
}
|
|
|
|
protected function getErrorForm($id, $form, $request, $data, $mode)
|
|
{
|
|
if ($form->get('submit')->isClicked() && 'delete' == $mode) {
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && 'submit' == $mode) {
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
}
|