237 lines
8.4 KiB
PHP
237 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Bookmark;
|
|
use App\Form\BookmarkType;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class BookmarkController extends AbstractController
|
|
{
|
|
private $data = 'bookmark';
|
|
private $entity = "App\Entity\Bookmark";
|
|
private $twig = 'Bookmark/';
|
|
private $route = 'app_admin_bookmark';
|
|
|
|
public function submit($access, $idpage, $idwidget, $touser, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$usage = $request->query->get('usage');
|
|
|
|
// Initialisation de l'enregistrement
|
|
$data = new Bookmark();
|
|
|
|
$pagewidget = null;
|
|
if ('false' == $touser) {
|
|
// On s'assure que le widget existe
|
|
$pagewidget = $em->getRepository("App\Entity\Pagewidget")->find($idwidget);
|
|
if (!$pagewidget) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Vérifier que l'on peut générer un pagewidgetslide
|
|
if ('all' == $access) {
|
|
if (!$em->getRepository("App\Entity\Pagewidget")->getCanadd($this->getUser(), $pagewidget, $usage)) {
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(BookmarkType::class, $data, ['mode' => 'submit']);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
// Bookmark widget ou desktop
|
|
if ('true' == $touser) {
|
|
$data->setUser($this->getUser());
|
|
} else {
|
|
$data->setPagewidget($pagewidget);
|
|
}
|
|
|
|
// Icon
|
|
$idicon = $form->get('idicon')->getData();
|
|
$icon = $em->getRepository('App\Entity\Icon')->findoneby(['id' => $idicon]);
|
|
$data->setIcon($icon);
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->persist($data);
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la page
|
|
return $this->redirect($this->generateUrl('app_'.$access.'_page_'.$usage.'_view', ['id' => $idpage]));
|
|
}
|
|
|
|
$icons = $em->getRepository('App\Entity\Icon')->findBy(['user' => null]);
|
|
$iconsuser = null;
|
|
if ('all' == $access) {
|
|
$iconsuser = $em->getRepository('App\Entity\Icon')->findBy(['user' => $this->getUser()]);
|
|
}
|
|
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => ('admin' == $access),
|
|
'usemenu' => false,
|
|
'usesidebar' => ('admin' == $access),
|
|
'entity' => $data,
|
|
'icons' => $icons,
|
|
'iconsuser' => $iconsuser,
|
|
'mode' => 'submit',
|
|
'access' => $access,
|
|
'idpage' => $idpage,
|
|
'idwidget' => $idwidget,
|
|
'touser' => $touser,
|
|
'form' => $form->createView(),
|
|
'usage' => $usage,
|
|
]);
|
|
}
|
|
|
|
public function update($id, $access, $idpage, $idwidget, $touser, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$usage = $request->query->get('usage');
|
|
|
|
// Récupération de l'enregistrement courant
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
if ('false' == $touser) {
|
|
// On s'assure que le widget existe
|
|
$pagewidget = $em->getRepository("App\Entity\Pagewidget")->find($idwidget);
|
|
if (!$pagewidget) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Vérifier que l'on peut générer un pagewidgetslide
|
|
if ('all' == $access) {
|
|
if (!$em->getRepository("App\Entity\Pagewidget")->getCanadd($this->getUser(), $pagewidget, $usage)) {
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(BookmarkType::class, $data, [
|
|
'mode' => 'update',
|
|
'idicon' => ($data->getIcon() ? $data->getIcon()->getId() : null),
|
|
]);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
// Icon
|
|
$idicon = $form->get('idicon')->getData();
|
|
$icon = $em->getRepository('App\Entity\Icon')->findoneby(['id' => $idicon]);
|
|
$data->setIcon($icon);
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la page
|
|
return $this->redirect($this->generateUrl('app_'.$access.'_page_'.$usage.'_view', ['id' => $idpage]));
|
|
}
|
|
|
|
$icons = $em->getRepository('App\Entity\Icon')->findBy(['user' => null]);
|
|
$iconsuser = null;
|
|
if ('all' == $access) {
|
|
$iconsuser = $em->getRepository('App\Entity\Icon')->findBy(['user' => $this->getUser()]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => ('admin' == $access),
|
|
'usemenu' => false,
|
|
'usesidebar' => ('admin' == $access),
|
|
'entity' => $data,
|
|
'icons' => $icons,
|
|
'iconsuser' => $iconsuser,
|
|
'mode' => 'update',
|
|
'access' => $access,
|
|
'idpage' => $idpage,
|
|
'idwidget' => $idwidget,
|
|
'touser' => $touser,
|
|
'form' => $form->createView(),
|
|
'usage' => $usage,
|
|
]);
|
|
}
|
|
|
|
public function delete($id, $access, $idpage, $idwidget, $touser, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$usage = $request->query->get('usage');
|
|
|
|
// Récupération de l'enregistrement courant
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
if ('false' == $touser) {
|
|
// On s'assure que le widget existe
|
|
$pagewidget = $em->getRepository("App\Entity\Pagewidget")->find($idwidget);
|
|
if (!$pagewidget) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Vérifier que l'on peut générer un pagewidgetslide
|
|
if ('all' == $access) {
|
|
if (!$em->getRepository("App\Entity\Pagewidget")->getCanadd($this->getUser(), $pagewidget, $usage)) {
|
|
throw $this->createAccessDeniedException('Permission denied');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Supprimer la donnée
|
|
$em->getManager()->remove($data);
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la page
|
|
return $this->redirect($this->generateUrl('app_'.$access.'_page_'.$usage.'_view', ['id' => $idpage]));
|
|
}
|
|
|
|
public function heart(Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$output = [];
|
|
$iditem = $request->request->get('iditem');
|
|
|
|
// On s'assure que l'item existe
|
|
$item = $em->getRepository("App\Entity\Item")->find($iditem);
|
|
if (!$item) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// On s'assure que l'item n'existe pas déjà dans les bookmark de l'utilisateur
|
|
$user = $this->getUser();
|
|
$bookmark = $em->getRepository($this->entity)->findOneBy(['user' => $user, 'item' => $item]);
|
|
if (!$bookmark) {
|
|
$bookmark = new Bookmark();
|
|
$bookmark->setTitle($item->getTitle());
|
|
$bookmark->setSubtitle($item->getSubtitle());
|
|
$bookmark->setUrl($item->getUrl());
|
|
$bookmark->setIcon($item->getIcon());
|
|
$bookmark->setColor($item->getColor());
|
|
$bookmark->setTarget($item->getTarget());
|
|
$bookmark->setItem($item);
|
|
$bookmark->setUser($user);
|
|
|
|
$em->getManager()->persist($bookmark);
|
|
$em->getManager()->flush();
|
|
}
|
|
|
|
$output = $bookmark->getId();
|
|
|
|
return new JsonResponse($output);
|
|
}
|
|
}
|