src/Utils/FilterSession.php line 144

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Session\Session;
  6. class FilterSession
  7. {
  8.     private $requestStack;
  9.     private $session;
  10.     private $nombre_variable;
  11.     private $nombre_variable_get;
  12.     private $data;
  13.     private $data_get;
  14.     private $page;
  15.     private $limit;
  16.     private $sort;
  17.     private $order;
  18.     private $params;
  19.     public function __construct(Request $request null$filters = [], $form_name '', ?ParameterBagInterface $params null)
  20.     {
  21.         //$this->session = new Session();
  22.         $this->session $request->getSession();
  23.         $this->nombre_variable 'filter_'.('' == $form_name $request->get('_route') : $form_name);
  24.         $this->nombre_variable_get 'filter_'.('' == $form_name $request->get('_route') : $form_name).'_get';
  25.         $this->nombre_variable_form 'filter_'.('' == $form_name $request->get('_route') : $form_name).'_form';
  26.         $this->data = [];
  27.         $this->data_get = [];
  28.         $this->params $params;
  29.         if (null !== $request && ('1' == $request->query->get('clear') || '1' == $request->get('clear'))) {
  30.             // Eliminar filtro actual de la sesión
  31.             $this->eliminarFiltro();
  32.         }
  33.         if ('' != $form_name && ($request->query->all()[$form_name] ?? null)) {
  34.             // Para recibir el formulario enviado por get para aplicar el filtro al formulario...
  35.             $this->añadirFiltrosForm($request->query->all()[$form_name]);
  36.         }
  37.         // Finalmente aplicar las variables de ordenación y paginación.
  38.         if ($filters && is_countable($filters) && count($filters) > 0) {
  39.             $this->añadirFiltrosOrdenar($request->query->get('sort'), $request->query->get('order'), $filters['sort'], $filters['order']);
  40.             $this->añadirFiltrosPaginacion($request->query->getInt('page'), $request->query->getInt('limit'), $filters['page'], $filters['limit']);
  41.         }
  42.     }
  43.     public function getPage()
  44.     {
  45.         return $this->page;
  46.     }
  47.     public function getLimit()
  48.     {
  49.         return $this->limit $this->limit $this->params->get('PaginationLimit');
  50.     }
  51.     public function getOrder()
  52.     {
  53.         return $this->order;
  54.     }
  55.     public function getSort()
  56.     {
  57.         return $this->sort;
  58.     }
  59.     public function getData()
  60.     {
  61.         return array_merge($this->data$this->data_get);
  62.     }
  63.     public function getSortKnp()
  64.     {
  65.         //defaultSortFieldName
  66.         //defaultSortDirection
  67.         return [
  68.             //$this->params->get('DEFAULT_SORT_FIELD_NAME') => $this->order,
  69.             //$this->params->get('DEFAULT_SORT_DIRECTION') => $this->sort
  70.             'defaultSortFieldName' => $this->sort,
  71.             'defaultSortDirection' => $this->order,
  72.         ];
  73.     }
  74.     public function añadirFiltrosForm($data)
  75.     {
  76.         $this->añadirFiltrosForm2($data);
  77.         foreach ($data as $key => $dat) {
  78.             if (is_object($dat)) {
  79.                 if ($dat instanceof \ArrayAccess) {
  80.                     $data[$key] = [];
  81.                     foreach ($dat as $key2 => $d) {
  82.                         if (method_exists($d'getid')) {
  83.                             $data[$key][$key2] = $d->getId();
  84.                         } elseif ($d instanceof \DateTime) {
  85.                             $data[$key][$key2] = $d->format('Y-m-d H:i');
  86.                         }
  87.                     }
  88.                 } else {
  89.                     if (method_exists($dat'getid')) {
  90.                         $data[$key] = $dat->getId();
  91.                     } elseif ($dat instanceof \DateTime) {
  92.                         $data[$key] = $dat->format('Y-m-d H:i');
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         $this->data $data;
  98.         $this->session->set($this->nombre_variable$data);
  99.     }
  100.     public function añadirFiltrosForm2($data)
  101.     {
  102.         $this->session->set($this->nombre_variable_form$data);
  103.     }
  104.     public function añadirFiltrosOrdenar($request_sort$request_order$default_sort$default_order 'DESC')
  105.     {
  106.         $this->sort = ($request_sort $request_sort $default_sort);
  107.         $this->order = ($request_order $request_order $default_order);
  108.         if (null == $request_sort && null == $request_order) {
  109.             if (isset($this->session->get($this->nombre_variable_get)['sort'], $this->session->get($this->nombre_variable_get)['order'])) {
  110.                 $this->sort $this->session->get($this->nombre_variable_get)['sort'];
  111.                 $this->order $this->session->get($this->nombre_variable_get)['order'];
  112.                 $this->data_get['sort'] = $this->sort;
  113.                 $this->data_get['order'] = $this->order;
  114.             }
  115.         } else {
  116.             $this->data_get['sort'] = $this->sort;
  117.             $this->data_get['order'] = $this->order;
  118.             $this->session->set($this->nombre_variable_get$this->data_get);
  119.         }
  120.     }
  121.     public function añadirFiltrosPaginacion($page$limit$defaul_page 1$default_limit 12)
  122.     {
  123.         $this->page = ($page $page $defaul_page);
  124.         $this->limit = ($limit $limit $default_limit);
  125.         if (== $page && == $limit) {
  126.             if (isset($this->session->get($this->nombre_variable_get)['page']) || isset($this->session->get($this->nombre_variable_get)['limite'])) {
  127.                 $this->page $this->session->get($this->nombre_variable_get)['page'];
  128.                 $this->limit $this->session->get($this->nombre_variable_get)['limit'];
  129.             } else {
  130.                 $this->page $defaul_page;
  131.                 $this->limit $default_limit;
  132.             }
  133.         } else {
  134.             $this->data_get['page'] = $page;
  135.             $this->data_get['limit'] = $limit;
  136.             $this->session->set($this->nombre_variable_get$this->data_get);
  137.         }
  138.     }
  139.     public function eliminarFiltro()
  140.     {
  141.         $this->session->remove($this->nombre_variable);
  142.         $this->session->remove($this->nombre_variable_form);
  143.         $this->session->remove($this->nombre_variable_get);
  144.     }
  145.     public function checkFiltroForm($form false)
  146.     {
  147.         if ($form) {
  148.             return $this->session->get($this->nombre_variable_form);
  149.         }
  150.         if (null != $this->session->get($this->nombre_variable)) {  //Si existen filtros que van por formulario almacenados en la sessión guardar para enviar...
  151.             $this->data $this->session->get($this->nombre_variable);
  152.         }
  153.         return $this->data;
  154.     }
  155. }