Some checks failed
Cadoles/nineskeletor/pipeline/head There was a failure building this commit
180 lines
6.2 KiB
PHP
180 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Item;
|
|
use App\Form\ItemType;
|
|
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 ItemController extends AbstractController
|
|
{
|
|
private $data = 'item';
|
|
private $entity = "App\Entity\Item";
|
|
private $twig = 'Item/';
|
|
private $route = 'app_admin_item';
|
|
|
|
public function list($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$itemcategorys = $em->getRepository('App\Entity\Itemcategory')->findBy([], ['rowOrder' => 'asc']);
|
|
$groups = $em->getRepository('App\Entity\Group')->findBy([], ['label' => 'asc']);
|
|
|
|
return $this->render($this->twig.'list.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
'itemcategorys' => $itemcategorys,
|
|
'groups' => $groups,
|
|
]);
|
|
}
|
|
|
|
public function selectlist($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$output = [];
|
|
$page_limit = $request->query->get('page_limit');
|
|
$q = $request->query->get('q');
|
|
|
|
$qb = $em->getManager()->createQueryBuilder();
|
|
$qb->select('table')->from($this->entity, 'table')
|
|
->where('table.title LIKE :value')
|
|
->setParameter('value', '%'.$q.'%')
|
|
->orderBy('table.title');
|
|
|
|
$datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach ($datas as $data) {
|
|
array_push($output, ['id' => $data->getId(), 'text' => $data->getLabel()]);
|
|
}
|
|
$ret_string['results'] = $output;
|
|
|
|
return new JsonResponse($ret_string);
|
|
}
|
|
|
|
public function submit($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = new Item();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(ItemType::class, $data, ['mode' => 'submit','access'=>$access]);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
$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 liste
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
$this->data => $data,
|
|
'mode' => 'submit',
|
|
'icons' => $em->getRepository('App\Entity\Icon')->findAll(),
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function update($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(ItemType::class, $data, ['mode' => 'update', 'access' => $access,'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();
|
|
|
|
$idicon = $form->get('idicon')->getData();
|
|
$icon = $em->getRepository('App\Entity\Icon')->findoneby(['id' => $idicon]);
|
|
$data->setIcon($icon);
|
|
|
|
// Sauvegarde
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->twig.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'access' => $access,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'icons' => $em->getRepository('App\Entity\Icon')->findAll(),
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
public function delete($id, $access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
// 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]);
|
|
}
|
|
|
|
return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route));
|
|
}
|
|
|
|
public function order($access, Request $request, ManagerRegistry $em): Response
|
|
{
|
|
$output = [];
|
|
$id = $request->request->get('id');
|
|
$categoryid = $request->request->get('categoryid');
|
|
$order = $request->request->get('order');
|
|
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
$data->setRoworder($order);
|
|
$itemcategory=$em->getRepository("App\Entity\Itemcategory")->find($categoryid);
|
|
if($itemcategory) $data->setItemcategory($itemcategory);
|
|
|
|
$em->getManager()->flush();
|
|
|
|
return new JsonResponse($output);
|
|
}
|
|
}
|