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)) { $attr[$a[0]]=[]; }
  112.                 $attr[$a[0]][]=$a[1];
  113.             }else{
  114.                 $attr[]=$f;
  115.             }
  116.         }
  117.         $attr sort($attr);
  118.         //dump($fields);
  119.         // Serialize the data
  120.         return $this->json([
  121.             'rows' => $records,
  122.             'total' => count($records),
  123.             'totalNotFiltered' => count($records// FIXME: get the proper total
  124.         ], 200, [], [
  125.             AbstractNormalizer::ATTRIBUTES => $attr,
  126.             'groups'=>'default',
  127.             'circular_reference_handler' => function ($object) {
  128.                 return $object->getId();
  129.             }
  130.         ]);
  131.     }
  132.     protected function persistEntity($entity)
  133.     {
  134.         $this->em->persist($entity);
  135.         $this->em->flush();
  136.     }
  137.     protected function updateEntity($entity)
  138.     {
  139.         $this->em->flush();
  140.     }
  141.     protected function removeEntity($entity)
  142.     {
  143.         $this->em->remove($entity);
  144.         $this->em->flush();
  145.     }
  146. }