src/Controller/AdminController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
  9. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  10. use Symfony\Component\Serializer\Serializer;
  11. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  12. use Doctrine\ORM\EntityManager;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use App\Entity\ConfRoles;
  15. use App\Entity\ConfMenus;
  16. use App\Entity\ConfMenuRol;
  17. use App\Entity\User;
  18. class AdminController extends EasyAdminController
  19. {
  20.     /**
  21.      * @Route("/", name="easyadmin")
  22.      * @Route("/", name="admin")
  23.      *
  24.      * The 'admin' route is deprecated since version 1.8.0 and it will be removed in 2.0.
  25.      *
  26.      * @param Request $request
  27.      *
  28.      * @return RedirectResponse|Response
  29.      */
  30.     public function indexAction(Request $request)
  31.     {
  32.         $user $this->getUser();
  33.         if ($user) {
  34.             $query $request->query;
  35.             if (!$query->get('entity')) {
  36.                 // $menus = $em->getRepository(ConfMenuRol::class)->createQueryBuilder("cmr")
  37.                 //     ->select("m.id,m.orden,m.nombre,m.ruta,m.icono,mp.id as padre_id")
  38.                 //     ->join("cmr.confMenu","m")
  39.                 //     ->join("cmr.confRol","r")
  40.                 //     ->join("r.users", "u")
  41.                 //     ->leftJoin("m.confMenuPadre","mp")
  42.                 //     ->where("cmr.activo = 1 and m.activo = 1 AND u.id={$user->getId()}")
  43.                 //     ->orderBy("m.orden")
  44.                 //     ->distinct()
  45.                 //     ->getQuery()->getResult();
  46.                 // $menu2 = array_filter($menus, function($m) {
  47.                 //     if(is_null($m['padre_id'])) return true;
  48.                 // });
  49.                 // if(count($menu2) > 0) {
  50.                 //     return $this->render(reset($menu2)['ruta']);
  51.                 // }
  52.                 //return $this->render('dashboard/index.html.twig', []);
  53.                 return $this->redirectToRoute("reportes_produccion_render");
  54.             }
  55.             return parent::indexAction($request);
  56.         }
  57.         return $this->render('app.html.twig', []);
  58.     }
  59.     public function recordsAction()
  60.     {
  61.         // get all the criteria for the search
  62.         error_reporting(E_ERROR E_WARNING E_PARSE);
  63.         $search $this->request->get('search'null);
  64.         $order $this->request->get('order'null);
  65.         $offset $this->request->get('offset'null);
  66.         $sort $this->request->get('sort''id');
  67.         $limit $this->request->get('limit'null);
  68.         // get the searchable fields
  69.         $user $this->getUser();
  70.         $class $this->entity['class'];
  71.         $em $this->getDoctrine()->getManagerForClass($class);
  72.         $searchableFields array_keys($this->entity['search']['fields']);
  73.         //dd($this->entity);
  74.         //die;
  75.         $criteria = new Criteria();
  76.         if ($search != "") {
  77.             // FIXME:
  78.             foreach ($searchableFields as $field) {
  79.                 $criteria->orWhere($criteria->expr()->contains($field$search));
  80.             }
  81.         }
  82.         if ($order) {
  83.             $criteria->orderBy([$sort => $order]); // FIXME:
  84.         }
  85.         if (!is_null($offset)) {
  86.             $criteria->setFirstResult($offset);
  87.         }
  88.         if ($limit) {
  89.             $criteria->setMaxResults($limit);
  90.         }
  91.         if ($user->getEmpresa() && method_exists($class'getEmpresa')) {
  92.             $empresa $user->getEmpresa();
  93.             $criteria->andWhere($criteria->expr()->eq('empresa'$empresa));
  94.         }
  95.         //dump($class);
  96.         if ($user->getEmpresa() && $class == 'App\Entity\Empresa' && method_exists($class'getId')) {
  97.             $empresa $user->getEmpresa();
  98.             $criteria->andWhere($criteria->expr()->eq('id'$empresa));
  99.         }
  100.         //dd($this->entity);
  101.         $records $em->getRepository($class)->matching($criteria);
  102.         $fields $this->entity['list']['fields'];
  103.         $fields[] = 'id';
  104.         $fields array_map(function ($field) {
  105.             return $field;
  106.         }, array_keys($fields));
  107.         $attr = [];
  108.         foreach ($fields as $k => $f) {
  109.             if (strpos($f'.') !== false) {
  110.                 $a explode('.'$f);
  111.                 if (!array_key_exists($a[0], $attr)) {
  112.                     $attr[$a[0]] = [];
  113.                 }
  114.                 $attr[$a[0]][] = $a[1];
  115.             } else {
  116.                 $attr[] = $f;
  117.             }
  118.         }
  119.         $attr sort($attr);
  120.         //dump($fields);
  121.         // Serialize the data
  122.         return $this->json([
  123.             'rows' => $records,
  124.             'total' => count($records),
  125.             'totalNotFiltered' => count($records// FIXME: get the proper total
  126.         ], 200, [], [
  127.             AbstractNormalizer::ATTRIBUTES => $attr,
  128.             'groups' => 'default',
  129.             'circular_reference_handler' => function ($object) {
  130.                 return $object->getId();
  131.             }
  132.         ]);
  133.     }
  134.     protected function persistEntity($entity)
  135.     {
  136.         $this->em->persist($entity);
  137.         $this->em->flush();
  138.     }
  139.     protected function updateEntity($entity)
  140.     {
  141.         $this->em->flush();
  142.     }
  143.     protected function removeEntity($entity)
  144.     {
  145.         $this->em->remove($entity);
  146.         $this->em->flush();
  147.     }
  148.     /**
  149.      * @Route("/obtenerMenuReporte", name="obtener_menu_reporte")
  150.      */
  151.     public function obtenerMenuReporte(EntityManagerInterface $em)
  152.     {
  153.         $qb $em->createQueryBuilder();
  154.         $qb->select(
  155.             ["m.id""m.nombre""m.codigo""m.fechaExpiracion"]
  156.         )
  157.             ->from('App\Entity\MenuReporte''m')
  158.             ->where("m.visible = true");
  159.         return $this->json($qb->getQuery()->getResult());
  160.     }
  161. }