<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\ConfRoles;
use App\Entity\ConfMenus;
use App\Entity\ConfMenuRol;
use App\Entity\User;
class AdminController extends EasyAdminController
{
/**
* @Route("/", name="easyadmin")
* @Route("/", name="admin")
*
* The 'admin' route is deprecated since version 1.8.0 and it will be removed in 2.0.
*
* @param Request $request
*
* @return RedirectResponse|Response
*/
public function indexAction(Request $request)
{
$user = $this->getUser();
if($user){
$query = $request->query;
if (!$query->get('entity')) {
// $menus = $em->getRepository(ConfMenuRol::class)->createQueryBuilder("cmr")
// ->select("m.id,m.orden,m.nombre,m.ruta,m.icono,mp.id as padre_id")
// ->join("cmr.confMenu","m")
// ->join("cmr.confRol","r")
// ->join("r.users", "u")
// ->leftJoin("m.confMenuPadre","mp")
// ->where("cmr.activo = 1 and m.activo = 1 AND u.id={$user->getId()}")
// ->orderBy("m.orden")
// ->distinct()
// ->getQuery()->getResult();
// $menu2 = array_filter($menus, function($m) {
// if(is_null($m['padre_id'])) return true;
// });
// if(count($menu2) > 0) {
// return $this->render(reset($menu2)['ruta']);
// }
//return $this->render('dashboard/index.html.twig', []);
return $this->redirectToRoute("reportes_produccion_render");
}
return parent::indexAction($request);
}
return $this->render('app.html.twig', []);
}
public function recordsAction()
{
// get all the criteria for the search
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$search = $this->request->get('search', null);
$order = $this->request->get('order', null);
$offset = $this->request->get('offset', null);
$sort = $this->request->get('sort', 'id');
$limit = $this->request->get('limit', null);
// get the searchable fields
$user = $this->getUser();
$class = $this->entity['class'];
$em = $this->getDoctrine()->getManagerForClass($class);
$searchableFields = array_keys($this->entity['search']['fields']);
//dd($this->entity);
//die;
$criteria = new Criteria();
if ($search != "") {
// FIXME:
foreach ($searchableFields as $field) {
$criteria->orWhere($criteria->expr()->contains($field, $search));
}
}
if ($order) {
$criteria->orderBy([$sort => $order]); // FIXME:
}
if (!is_null($offset)) {
$criteria->setFirstResult($offset);
}
if ($limit) {
$criteria->setMaxResults($limit);
}
if ($user->getEmpresa() && method_exists($class, 'getEmpresa')) {
$empresa = $user->getEmpresa();
$criteria->andWhere($criteria->expr()->eq('empresa', $empresa));
}
//dump($class);
if ($user->getEmpresa() && $class == 'App\Entity\Empresa' && method_exists($class, 'getId')) {
$empresa = $user->getEmpresa();
$criteria->andWhere($criteria->expr()->eq('id', $empresa));
}
//dd($this->entity);
$records = $em->getRepository($class)->matching($criteria);
$fields = $this->entity['list']['fields'];
$fields[] = 'id';
$fields = array_map(function ($field) {
return $field;
}, array_keys($fields));
$attr = [];
foreach ($fields as $k => $f){
if (strpos($f, '.') !== false) {
$a=explode('.', $f);
if(!array_key_exists($a[0],$attr)) { $attr[$a[0]]=[]; }
$attr[$a[0]][]=$a[1];
}else{
$attr[]=$f;
}
}
$attr = sort($attr);
//dump($fields);
// Serialize the data
return $this->json([
'rows' => $records,
'total' => count($records),
'totalNotFiltered' => count($records) // FIXME: get the proper total
], 200, [], [
AbstractNormalizer::ATTRIBUTES => $attr,
'groups'=>'default',
'circular_reference_handler' => function ($object) {
return $object->getId();
}
]);
}
protected function persistEntity($entity)
{
$this->em->persist($entity);
$this->em->flush();
}
protected function updateEntity($entity)
{
$this->em->flush();
}
protected function removeEntity($entity)
{
$this->em->remove($entity);
$this->em->flush();
}
}