nineskeletor/src/Controller/PageController.php

549 lines
20 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Page;
use App\Form\PageSubmitType;
use App\Form\PageUpdateEditorType;
use App\Form\PageUpdateToolType;
use App\Form\PageUpdateURLType;
use App\Form\PageUpdateWidgetType;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class PageController extends AbstractController
{
private $data = 'page';
private $entity = "App\Entity\Page";
private $twig = 'Page/';
private $route = 'app_admin_page_usage';
public function list($access, $usage)
{
return $this->render($this->twig.'list.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => true,
'access' => $access,
'usage' => $usage,
'istemplate' => false,
]);
}
public function tablelist($access, $usage, 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'];
$alluser = $query['alluser'];
// On sauvegarde en session le flag alluser
$request->getSession()->set('alluserpage', $alluser);
// Query de base
$qbase = $em->getManager()->createQueryBuilder()->from($this->entity, 'table');
$qsearch = $em->getManager()->createQueryBuilder()->from($this->entity, 'table');
if ('false' == $alluser) {
$qbase->where('table.user is null');
$qbase->andWhere('table.parentfor is null');
$qsearch->where('table.user is null');
$qsearch->andWhere('table.parentfor is null');
} else {
$qbase->from('App:User', 'user')
->where('table.user=user');
$qsearch->from('App:User', 'user')
->where('table.user=user');
}
if ('false' == $alluser) {
$qsearch->andwhere('table.id LIKE :value OR table.name LIKE :value');
} else {
$qsearch->andWhere('table.id LIKE :value OR table.name LIKE :value OR user.username 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 4:
if ('true' == $alluser) {
$qb->orderBy('user.username', $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);
$route = str_replace('_usage', '_'.$usage, $route);
$action = '';
// $action.="<a href='".$this->generateUrl($route.'_update', array('id'=>$data->getId()))."'><i class='fa fa-file fa-fw'></i></a>";
$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>";
$action .= "<a href='".$this->generateUrl($route.'_delete', ['id' => $data->getId()])."' data-method='delete'><i class='fa fa-trash fa-fw fa-2x'></i></a>";
$user = '';
if ($data->getUser()) {
$user .= "<img src='".$this->generateUrl('app_minio_image', ['file' => 'avatar/'.$data->getUser()->getAvatar()])."' class='avatar' style='margin:0px 5px 0px 0px;display:inline-block;'>";
$user .= $data->getUser()->getUsername();
}
$icon = '';
if ($data->getFonticon()) {
$icon .= "<i class='".$data->getFonticon()." fa-fw'></i>&nbsp;";
}
array_push($output['data'], [
$action,
$data->getRoworder(),
$icon.$data->getName(),
$data->getPagecategory()->getName(),
$user,
]);
}
// 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)) {
// Type Tools
if ($entity->getPagecategory()->getId() < 0) {
return $this->createForm(PageUpdateToolType::class, $entity, [
'mode' => 'update',
'access' => $access,
]);
}
// Type URL
if (1 == $entity->getPagecategory()->getId()) {
return $this->createForm(PageUpdateURLType::class, $entity, [
'mode' => 'update',
'access' => $access,
]);
}
// Type Widget
elseif (2 == $entity->getPagecategory()->getId()) {
return $this->createForm(PageUpdateWidgetType::class, $entity, [
'mode' => 'update',
'access' => $access,
]);
}
// Type Editeur
elseif (3 == $entity->getPagecategory()->getId()) {
return $this->createForm(PageUpdateEditorType::class, $entity, [
'mode' => 'update',
'access' => $access,
]);
}
} else {
return $this->createForm(PageSubmitType::class, $entity, [
'mode' => 'update',
'access' => $access,
'user' => $this->getUser(),
]);
}
}
public function submit($access, $usage, Request $request, ManagerRegistry $em): Response
{
$entity = new Page();
$entity->setMaxwidth(0);
$entity->setRoworder(0);
$form = $this->entityForm($entity, $access, $em);
$form->handleRequest($request);
// Une page d'usage groupe doit avoir au moins un group de selectionné
$datausage = $form->get('usage')->getData();
if ('group' == $datausage && $entity->getGroups()->isEmpty()) {
$form->addError(new FormError('Vous devez selectionner au minimum un groupe'));
}
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
if ('all' == $access) {
$entity->setUser($this->getUser());
}
// Si template on duplique le template
if ($entity->getPage()) {
$page = $em->getRepository("App\Entity\Page")->clonePage($this->getUser(), $entity->getPage());
$page->setName($entity->getName());
$page->setRoworder($entity->getRoworder());
$page->setMaxwidth($entity->getMaxwidth());
foreach ($entity->getGroups() as $group) {
$page->addGroup($group);
}
$em->getManager()->persist($page);
$em->getManager()->flush();
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
$route = str_replace('_usage', '_'.$usage, $route);
return $this->redirect($this->generateUrl($route.'_update', ['id' => $page->getId()]));
} else {
$em->getManager()->persist($entity);
$em->getManager()->flush();
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
$route = str_replace('_usage', '_'.$usage, $route);
return $this->redirect($this->generateUrl($route.'_update', ['id' => $entity->getId()]));
}
}
return $this->render($this->twig.'submit.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ('admin' == $access),
'maxwidth' => ('user' == $access),
$this->data => $entity,
'mode' => 'submit',
'usage' => $usage,
'access' => $access,
'form' => $form->createView(),
]);
}
public function update($id, $access, $usage, Request $request, ManagerRegistry $em): Response
{
$entity = $em->getRepository($this->entity)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find entity.');
}
// On s'assure que l'utilisateur à la permission de modifier
if ('all' == $access) {
$em->getRepository($this->entity)->getPermission($this->getUser(), $entity, $cansee, $canupdate, $canadd);
if (!$canupdate) {
throw $this->createAccessDeniedException('Permission denied');
}
}
// Création du formulaire
$form = $this->entityForm($entity, $access, $em);
$form->handleRequest($request);
if ($form->get('submit')->isClicked() && $form->isValid()) {
$em->getManager()->flush();
if ('admin' == $access) {
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
$route = str_replace('_usage', '_'.$usage, $route);
return $this->redirect($this->generateUrl($route.'_view', ['id' => $id]));
} else {
return $this->redirect($this->generateUrl('app_home', ['id' => $id]));
}
}
// Type URL
if (1 == $entity->getPagecategory()->getId()) {
return $this->render($this->twig.'updateurl.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ('admin' == $access),
'maxwidth' => ('all' == $access),
$this->data => $entity,
'access' => $access,
'usage' => $usage,
'mode' => 'update',
'form' => $form->createView(),
]);
}
// Type Widget
elseif (2 == $entity->getPagecategory()->getId()) {
return $this->render($this->twig.'updatewidget.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ('admin' == $access),
'maxwidth' => ('all' == $access),
$this->data => $entity,
'access' => $access,
'mode' => 'update',
'usage' => $usage,
'form' => $form->createView(),
]);
}
// Type Editeur
elseif (3 == $entity->getPagecategory()->getId()) {
return $this->render($this->twig.'updateeditor.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ('admin' == $access),
'maxwidth' => ('all' == $access),
$this->data => $entity,
'access' => $access,
'usage' => $usage,
'mode' => 'update',
'form' => $form->createView(),
]);
}
}
public function delete($id, $access, $usage, 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, $canadd);
if (!$canupdate) {
throw $this->createAccessDeniedException('Permission denied');
}
}
// Tentative de suppression
try {
$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
if ('admin' == $access) {
$route = str_replace('_admin_', '_'.$access.'_', $this->route);
$route = str_replace('_usage', '_'.$usage, $route);
return $this->redirect($this->generateUrl($route));
} else {
return $this->redirect($this->generateUrl('app_home'));
}
}
public function order($access, Request $request, ManagerRegistry $em): Response
{
$output = [];
$id = $request->request->get('id');
$order = $request->request->get('order');
$entity = $em->getRepository($this->entity)->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find entity.');
}
// On s'assure que l'utilisateur à la permission
if ('all' == $access) {
$em->getRepository($this->entity)->getPermission($this->getUser(), $entity, $cansee, $canupdate, $canadd);
if (!$canupdate) {
throw $this->createAccessDeniedException('Permission denied');
}
}
$entity->setRoworder($order);
$em->getManager()->flush();
return new JsonResponse($output);
}
public function view($id, $access, $usage, Request $request, ManagerRegistry $em): Response
{
$entity = $em->getRepository($this->entity)->find($id);
if (!$entity) {
return $this->redirect($this->generateUrl('app_home'));
}
// Permissions
if ('admin' == $access) {
$canupdate = true;
} else {
// On s'assure que l'utilisateur à la permission de voir
$em->getRepository($this->entity)->getPermission($this->getUser(), $entity, $cansee, $canupdate, $canadd);
if (!$cansee) {
return $this->redirect($this->generateUrl('app_home'));
}
}
// Type Calendrier
if (-100 == $entity->getPageCategory()->getId()) {
$entity->setUrl($this->generateUrl('app_user_calendar_view'));
return $this->render($this->twig.'viewurl.html.twig', [
'useheader' => ('admin' == $access),
'usemenu' => ('admin' == $access),
'usesidebar' => ('admin' == $access),
$this->data => $entity,
'access' => $access,
'canupdate' => $canupdate,
'usage' => $usage,
]);
}
// Type Blob
if (-90 == $entity->getPageCategory()->getId()) {
$entity->setUrl($this->generateUrl('app_user_blog_view'));
return $this->render($this->twig.'viewurl.html.twig', [
'useheader' => ('admin' == $access),
'usemenu' => ('admin' == $access),
'usesidebar' => ('admin' == $access),
$this->data => $entity,
'access' => $access,
'canupdate' => $canupdate,
'usage' => $usage,
]);
}
// Type URL
if (1 == $entity->getPageCategory()->getId()) {
return $this->render($this->twig.'viewurl.html.twig', [
'useheader' => ('admin' == $access),
'usemenu' => ('admin' == $access),
'usesidebar' => ('admin' == $access),
$this->data => $entity,
'access' => $access,
'canupdate' => $canupdate,
'usage' => $usage,
]);
}
// Type Widgets
if (2 == $entity->getPageCategory()->getId()) {
return $this->render($this->twig.'viewwidget.html.twig', [
'useheader' => ('admin' == $access),
'usemenu' => ('admin' == $access),
'usesidebar' => ('admin' == $access),
$this->data => $entity,
'access' => $access,
'canupdate' => $canupdate,
'widgets' => $em->getRepository("App\Entity\Widget")->getWidgetAccess($access, 'config'),
'usage' => "$usage",
]);
}
// Type Editeur
if (3 == $entity->getPageCategory()->getId()) {
return $this->render($this->twig.'vieweditor.html.twig', [
'useheader' => ('config' == $access),
'usemenu' => ('admin' == $access),
'usesidebar' => ('config' == $access),
$this->data => $entity,
'access' => $access,
'canupdate' => $canupdate,
'usage' => $usage,
]);
}
}
public function application($access, Request $request, ManagerRegistry $em): Response
{
$entity = $em->getRepository($this->entity)->findOneBy(['parentfor' => 'app']);
if (!$entity) {
return $this->redirect($this->generateUrl('app_home'));
}
$canupdate = false;
return $this->render($this->twig.'viewwidget.html.twig', [
'useheader' => ('admin' == $access),
'usemenu' => false,
'usesidebar' => ('admin' == $access),
$this->data => $entity,
'access' => $access,
'canupdate' => $canupdate,
'mode' => 'view',
'widgets' => $em->getRepository('App\Entity\Widget')->getWidgetAccess($access, 'config'),
'usage' => 'portal',
'selwidget' => null,
]);
}
protected function getPreference($user, $key, $id, $default)
{
$preference = $user->getPreference();
$return = $default;
if (is_array($preference)) {
if (array_key_exists($key, $preference)) {
if (array_key_exists($id, $preference[$key])) {
$return = $preference[$key][$id];
}
}
}
return $return;
}
protected function setPreference($user, $key, $id, $value)
{
$preference = $user->getPreference();
$toupdate = false;
if (!array_key_exists($key, $preference)) {
$toupdate = true;
$preference[$key] = [];
}
if (!array_key_exists($id, $preference[$key])) {
$toupdate = true;
$preference[$key][$id] = $value;
}
if ($value && $preference[$key][$id] != $value) {
$toupdate = true;
$preference[$key][$id] = $value;
}
// Mise à jour des préferences
if ($toupdate) {
$em = $this->getDoctrine()->getManager();
$user->setPreference($preference);
$em->persist($this->getUser());
$em->flush();
}
}
}