src/Crud/Controller/crudController.php line 111

Open in your IDE?
  1. <?php
  2. namespace App\Crud\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use App\Crud\ParseConfig\ParseConfig;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Psr\Log\LoggerInterface;
  8. /**
  9.  * crudController controller.
  10.  */
  11. class crudController extends AbstractController
  12. {
  13.     protected $controlador;
  14.     protected $rol;
  15.     protected $routeClassName;
  16.     protected $params;
  17.     protected $classMap;
  18.     protected $request;
  19.     protected $filterData;
  20.     protected $session;
  21.     protected $translator;
  22.     protected $logger;
  23.     
  24.     public function __construct(RequestStack $requestStackTranslatorInterface $translatorLoggerInterface $logger)
  25.     {
  26.         $this->session $requestStack->getSession();
  27.         $this->request $requestStack->getCurrentRequest();
  28.         $this->translator $translator;
  29.         $this->logger $logger;
  30.     }
  31.     
  32.     protected function setClassMap($routeClassName)
  33.     {
  34.         $parseConfig = new ParseConfig();
  35.         $config $parseConfig->parse($this->container);
  36.         $config $config['crudMapping'];
  37.         $inline strrpos($routeClassName'_inline');
  38.         if ($inline !== false)
  39.         {
  40.             $routeClassName substr($routeClassName0$inline);
  41.         }
  42. //        $config = $this->getParameter('crudmapping');
  43.         if (isset($config[$routeClassName]))
  44.         {
  45.             $this->classMap $config[$routeClassName];
  46.         }
  47.         else
  48.            throw new \RuntimeException($this->translator->trans(
  49.                                                      'error.class_not_defined',
  50.                                                      array('%className%' => $routeClassName),
  51.                                                      'crud'));
  52.         return $config;
  53.     }   
  54.     
  55.     protected function setParamsRequest()
  56.     {
  57.         $this->params = array();
  58.         $keys $this->request->query->keys();
  59.         foreach ($keys as $index=>$key)
  60.         {
  61.             if (strpos($key,'amp;')===0)
  62.             {
  63.                 $newKey str_replace('amp;','',$key);
  64.                 if (isset($this->params[$newKey]))
  65.                     $this->params[$newKey][]=$this->request->get($key); 
  66.                 else
  67.                     $this->params[$newKey]=$this->request->get($key); 
  68.             }
  69.             else
  70.                $this->params[$key]=$this->request->get($key); 
  71.         }
  72.         $keys $this->request->attributes->keys();
  73.         foreach ($keys as $index=>$key)
  74.            if (strpos($key,'_')!==0)
  75.              $this->params[$key]=$this->request->get($key); 
  76.         return $this->params;
  77.     }
  78.     
  79.     protected function setControlador()
  80.     {
  81.        if (isset($this->classMap['showInTree']) && $this->classMap['showInTree'])
  82.          if (isset($this->classMap['treeByLevels']) && $this->classMap['treeByLevels'])
  83.            $this->controlador 'App\Crud\Controller\proxyTreeLevelController';
  84.          else
  85.            $this->controlador 'App\Crud\Controller\proxyTreeController';
  86.        else    
  87.          $this->controlador 'App\Crud\Controller\proxyPlainController';
  88.     }        
  89.     
  90.     protected function setInitialValues($rol$routeClassName)
  91.     {
  92.         $this->rol $rol;
  93.         $this->routeClassName $routeClassName;
  94.         $this->setClassMap($routeClassName);
  95.         $this->setControlador();
  96.         $this->params $this->setParamsRequest();
  97.     }
  98.     
  99.     public function dispatchAction($rol$routeClassName)
  100.     {
  101.         $this->setInitialValues($rol$routeClassName);
  102.         $this->params['routeClassName']=$routeClassName;
  103.         $this->params['rol'] = $rol;
  104.         return $this->forward($this->controlador.'::indexAction'$this->params);
  105.     }
  106.     
  107.     public function excelAction($rol$routeClassName)
  108.     {
  109.         $this->setInitialValues($rol$routeClassName);
  110.         $this->params['routeClassName']=$routeClassName;
  111.         $this->params['rol']=$rol;
  112.         return $this->forward($this->controlador.'::excelAction'$this->params);
  113.     }
  114.     
  115.     public function indexAction($rol$routeClassName)
  116.     {
  117.         $this->setInitialValues($rol$routeClassName);
  118.         $params=array('rol'=> $rol'routeClassName'=>$routeClassName);
  119.         $filterData $this->request->get('filterData');
  120.         if ($filterData
  121.           $params['filterData'] = $filterData
  122.         $page $this->request->get('page');
  123.         if ($page)
  124.           $params['page']=$page;  
  125.         $params=  array_merge($this->params$params);
  126.         return $this->forward($this->controlador.'::indexAction'$params);
  127.     }
  128.     
  129.     public function newAction($rol$routeClassName)
  130.     {
  131.         $this->setInitialValues($rol$routeClassName);
  132.         $params=array('rol'=> $rol'routeClassName'=>$routeClassName);
  133.         $params=  array_merge($this->params$params);
  134.         return $this->forward($this->controlador.'::newAction'$params);
  135.     }
  136.     
  137.     public function editAction($rol$id$routeClassName)
  138.     {
  139.         $this->setInitialValues($rol$routeClassName);
  140.         $params=array('rol'=> $rol'routeClassName'=>$routeClassName'id'=>$id);
  141.         $params=  array_merge($this->params$params);
  142.         return $this->forward($this->controlador.'::editAction'$params);
  143.     }
  144.     
  145.     public function showAction($rol$id$routeClassName$tab=-1)
  146.     {
  147.         $this->setInitialValues($rol$routeClassName);
  148.         $params=array('rol'=> $rol'routeClassName'=>$routeClassName'id'=>$id'tab'=>$tab);
  149.         $page $this->request->get('page');
  150.         if ($page)
  151.           $params['page']=$page;  
  152.         $params=  array_merge($this->params$params);
  153.         return $this->forward($this->controlador.'::showAction'$params);
  154.     }
  155.     
  156.     public function deleteAction($rol$id$routeClassName)
  157.     {
  158.         $this->setInitialValues($rol$routeClassName);
  159.         $params=array('rol'=> $rol'routeClassName'=>$routeClassName'id'=>$id);
  160.         $params=  array_merge($this->params$params);
  161.         return $this->forward($this->controlador.'::deleteAction'$params);
  162.     }
  163.     
  164.     public function paginatorAction($rol$routeClassName$page)
  165.     {
  166.         $this->setInitialValues($rol$routeClassName);
  167.         $params=array('rol'=> $rol'page'=> $page'routeClassName'=>$routeClassName);
  168.         $params=  array_merge($this->params$params);
  169.         return $this->forward($this->controlador.'::paginatorAction'$params);
  170.     }
  171.     
  172.     public function cleanFilterAction($rol$routeClassName)
  173.     {
  174.        $this->session->remove($routeClassName);
  175.        return $this->redirect($this->generateUrl('crud_index', array('rol'=>$rol'routeClassName'=>$routeClassName)));
  176.     }
  177.     public function cleanFilterInlineAction($routeClassName)
  178.     {
  179.         $this->session->remove($routeClassName);
  180.         $filterData $this->request->get('filterData');
  181.         if ($filterData)
  182.         {
  183.             $this->session->set($routeClassName,$filterData);
  184.         }
  185.         $data= array('ok'=>true);
  186.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  187.         $data->headers->set('Content-Type''application/json');
  188.         return $data;
  189.     }
  190.     public function filterInlineAction($routeClassName)
  191.     {
  192.         $this->session->remove($routeClassName);
  193.         $this->filterData $this->request->get('form');
  194.         $this->indexCleanFilterData();
  195.         $this->session->set($routeClassName,$this->filterData);
  196.         $data= array('ok'=>true);
  197.         $data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
  198.         $data->headers->set('Content-Type''application/json');
  199.         return $data;
  200.     }
  201.     
  202.     protected function indexCleanFilterData()
  203.     {
  204.         foreach ($this->filterData as $key=>$valor)
  205.         {
  206.             if ((is_array($valor) && empty($valor)) ||
  207.                 (($valor instanceof \Doctrine\Common\Collections\ArrayCollection) && $valor->count()==0) ||
  208.                 $valor=='')
  209.             {
  210.                 unset($this->filterData[$key]);
  211.             }
  212.         }
  213.     }
  214. }