src/Utils/CustomFilter.php line 35

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