Some checks failed
Cadoles/nineskeletor/pipeline/head There was a failure building this commit
323 lines
11 KiB
PHP
323 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Page;
|
|
use App\Form\PagetemplateSubmitType;
|
|
use App\Form\PageUpdateWidgetType;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\FormError;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class PagetemplateController extends AbstractController
|
|
{
|
|
private $data = 'page';
|
|
private $entity = "App\Entity\Page";
|
|
private $twig = 'Pagetemplate/';
|
|
private $route = 'app_admin_pagetemplate';
|
|
|
|
public function list($access): Response
|
|
{
|
|
return $this->render($this->twig.'list.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
]);
|
|
}
|
|
|
|
public function tablelist($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$query = $request->query->all();
|
|
$start = $query['start'];
|
|
$length = $query['length'];
|
|
$search = $query['search'];
|
|
$draw = $query['draw'];
|
|
$ordercolumn = $query['order'][0]['column'];
|
|
$orderdir = $query['order'][0]['dir'];
|
|
|
|
// Query de base
|
|
$qbase = $em->getManager()->createQueryBuilder()->from($this->entity, 'table');
|
|
$qsearch = $em->getManager()->createQueryBuilder()->from($this->entity, 'table');
|
|
|
|
$qbase->where('table.user is null');
|
|
$qbase->andWhere('table.parentfor is not null');
|
|
$qsearch->where('table.user is null');
|
|
$qsearch->andWhere('table.parentfor is not null');
|
|
$qsearch->andwhere('table.id LIKE :value OR table.name LIKE :value OR table.parentfor LIKE :value');
|
|
$qsearch->setParameter('value', '%'.$search['value'].'%');
|
|
|
|
// Nombre total d'enregistrement
|
|
$total = $qbase->select('COUNT(table)')->getQuery()->getSingleScalarResult();
|
|
|
|
// Nombre d'enregistrement filtré
|
|
if ('' == $search['value']) {
|
|
$totalf = $total;
|
|
} else {
|
|
$totalf = $qsearch->select('COUNT(table)')->getQuery()->getSingleScalarResult();
|
|
}
|
|
|
|
// Parcours des Enregistrement
|
|
if ('' == $search['value']) {
|
|
$qb = $qbase->select('table');
|
|
} else {
|
|
$qb = $qsearch->select('table');
|
|
}
|
|
|
|
// Order
|
|
if ($ordercolumn) {
|
|
switch ($ordercolumn) {
|
|
case 1:
|
|
$qb->orderBy('table.roworder', $orderdir);
|
|
break;
|
|
case 2:
|
|
$qb->orderBy('table.name', $orderdir);
|
|
break;
|
|
case 3:
|
|
$qb->orderBy('table.parentfor', $orderdir);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Execution de la requete d'affichage
|
|
$datas = $qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult();
|
|
|
|
// Construction du tableau de retour
|
|
$output = [
|
|
'draw' => $draw,
|
|
'recordsFiltered' => $totalf,
|
|
'recordsTotal' => $total,
|
|
'data' => [],
|
|
];
|
|
foreach ($datas as $data) {
|
|
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
|
|
$action = '';
|
|
$action .= "<a href='".$this->generateUrl($route.'_update', ['id' => $data->getId()])."'><i class='fa fa-cog fa-fw fa-2x'></i></a>";
|
|
$action .= "<a href='".$this->generateUrl($route.'_view', ['id' => $data->getId()])."'><i class='fa fa-eye fa-fw fa-2x'></i></a>";
|
|
|
|
array_push($output['data'], [
|
|
$action,
|
|
$data->getRoworder(),
|
|
$data->getName(),
|
|
$data->getParentfor(),
|
|
]);
|
|
}
|
|
|
|
// Retour
|
|
return new Response(json_encode($output), 200);
|
|
}
|
|
|
|
private function entityForm(Page $entity, $access, $em)
|
|
{
|
|
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
|
|
|
|
if ($em->getManager()->contains($entity)) {
|
|
return $this->createForm(PageUpdateWidgetType::class, $entity, [
|
|
'mode' => 'update',
|
|
'access' => $access,
|
|
'for' => $entity->getParentfor(),
|
|
]);
|
|
} else {
|
|
return $this->createForm(PagetemplateSubmitType::class, $entity, [
|
|
'mode' => 'update',
|
|
'access' => $access,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function submit($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$pagecategory = $em->getRepository("App\Entity\Pagecategory")->find(2);
|
|
$data = new Page();
|
|
$data->setMaxwidth(0);
|
|
$data->setRoworder(0);
|
|
$data->setParentfor('user');
|
|
$data->setPagecategory($pagecategory);
|
|
|
|
$form = $this->entityForm($data, $access, $em);
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
if ('app' == $data->getParentfor()) {
|
|
$tmp = $em->getRepository($this->entity)->findOneBy(['parentfor' => 'app']);
|
|
if ($tmp) {
|
|
$form->addError(new FormError("Il ne peut avoir qu'un seul template de type Application"));
|
|
}
|
|
}
|
|
|
|
$data = $form->getData();
|
|
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
// Sauvegarde
|
|
$em->getManager()->persist($data);
|
|
$em->getManager()->flush();
|
|
|
|
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
|
|
|
|
return $this->redirect($this->generateUrl($route.'_update', ['id' => $data->getId()]));
|
|
}
|
|
|
|
return $this->render('Pagetemplate\submit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'mode' => 'submit',
|
|
'access' => $access,
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function update($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Création du formulaire
|
|
$form = $this->entityForm($data, $access, $em);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$em->getManager()->flush();
|
|
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
|
|
|
|
return $this->redirect($this->generateUrl($route.'_view', ['id' => $id]));
|
|
}
|
|
|
|
return $this->render('Page\updatewidget.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'access' => $access,
|
|
'mode' => 'updatetemplate',
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function delete($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// On s'assure que l'utilisateur à la permission de supprimer
|
|
if ('all' == $access) {
|
|
$em->getRepository($this->entity)->getPermission($this->getUser(), $data, $cansee, $canupdate);
|
|
if (!$canupdate) {
|
|
throw $this->createNotFoundException('Permission denied');
|
|
}
|
|
}
|
|
|
|
// Tentative de suppression
|
|
try {
|
|
$group = $em->getRepository("App\Entity\Group")->findOneBy(['pagetemplate' => $data]);
|
|
if ($group) {
|
|
throw new \Exception('Impossible de supprimer ce modèle, il est utilisé par au moins un groupe');
|
|
}
|
|
|
|
if ('app' == $data->getParentfor()) {
|
|
throw new \Exception('Vous ne pouvez pas supprimer un template de type Application');
|
|
}
|
|
|
|
$em->getManager()->remove($data);
|
|
$em->getManager()->flush();
|
|
} catch (\Exception $e) {
|
|
$request->getSession()->getFlashBag()->add('error', $e->getMessage());
|
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route).'_update', ['id' => $id]);
|
|
}
|
|
|
|
// Retour
|
|
return $this->redirect($this->generateUrl($this->route));
|
|
}
|
|
|
|
public function seleclist(Request $request)
|
|
{
|
|
// S'assurer que c'est un appel ajax
|
|
if (!$request->isXmlHttpRequest()) {
|
|
return new JsonResponse(['message' => 'Interdit'], 400);
|
|
}
|
|
|
|
$output = [];
|
|
$em = $this->getDoctrine()->getManager();
|
|
$page_limit = $request->query->get('page_limit');
|
|
$q = $request->query->get('q');
|
|
$usage = $request->query->get('usage');
|
|
|
|
$qb = $em->createQueryBuilder();
|
|
$qb->select('table')->from('App:Page', 'table')
|
|
->where('table.name LIKE :value')
|
|
->andWhere('table.parentfor=:usage')
|
|
->setParameter('value', '%'.$q.'%')
|
|
->setParameter('usage', $usage)
|
|
->orderBy('table.name');
|
|
|
|
$datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach ($datas as $data) {
|
|
array_push($output, ['id' => $data->getId(), 'text' => $data->getName()]);
|
|
}
|
|
|
|
$response = new Response(json_encode($output));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
|
|
return $response;
|
|
}
|
|
|
|
public function view($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Permissions
|
|
$canupdate = true;
|
|
|
|
dump($em->getRepository("App\Entity\Widget")->getWidgetAccess($access));
|
|
|
|
return $this->render('Page\viewwidget.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => true,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'access' => $access,
|
|
'canupdate' => $canupdate,
|
|
'mode' => 'viewtemplate',
|
|
'widgets' => $em->getRepository("App\Entity\Widget")->getWidgetAccess($access),
|
|
'usage' => 'config',
|
|
'group' => '',
|
|
'look' => 'view',
|
|
'selwidget' => null,
|
|
]);
|
|
}
|
|
|
|
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();
|
|
$validator = $this->get('validator');
|
|
$errors = $validator->validate($data);
|
|
foreach ($errors as $error) {
|
|
$request->getSession()->getFlashBag()->add('error', $error->getMessage());
|
|
}
|
|
|
|
$errors = $form->getErrors();
|
|
foreach ($errors as $error) {
|
|
$request->getSession()->getFlashBag()->add('error', $error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|