<?php
namespace App\Crud\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Crud\ParseConfig\ParseConfig;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Psr\Log\LoggerInterface;
/**
* crudController controller.
*/
class crudController extends AbstractController
{
protected $controlador;
protected $rol;
protected $routeClassName;
protected $params;
protected $classMap;
protected $request;
protected $filterData;
protected $session;
protected $translator;
protected $logger;
public function __construct(RequestStack $requestStack, TranslatorInterface $translator, LoggerInterface $logger)
{
$this->session = $requestStack->getSession();
$this->request = $requestStack->getCurrentRequest();
$this->translator = $translator;
$this->logger = $logger;
}
protected function setClassMap($routeClassName)
{
$parseConfig = new ParseConfig();
$config = $parseConfig->parse($this->container);
$config = $config['crudMapping'];
$inline = strrpos($routeClassName, '_inline');
if ($inline !== false)
{
$routeClassName = substr($routeClassName, 0, $inline);
}
// $config = $this->getParameter('crudmapping');
if (isset($config[$routeClassName]))
{
$this->classMap = $config[$routeClassName];
}
else
throw new \RuntimeException($this->translator->trans(
'error.class_not_defined',
array('%className%' => $routeClassName),
'crud'));
return $config;
}
protected function setParamsRequest()
{
$this->params = array();
$keys = $this->request->query->keys();
foreach ($keys as $index=>$key)
{
if (strpos($key,'amp;')===0)
{
$newKey = str_replace('amp;','',$key);
if (isset($this->params[$newKey]))
$this->params[$newKey][]=$this->request->get($key);
else
$this->params[$newKey]=$this->request->get($key);
}
else
$this->params[$key]=$this->request->get($key);
}
$keys = $this->request->attributes->keys();
foreach ($keys as $index=>$key)
if (strpos($key,'_')!==0)
$this->params[$key]=$this->request->get($key);
return $this->params;
}
protected function setControlador()
{
if (isset($this->classMap['showInTree']) && $this->classMap['showInTree'])
if (isset($this->classMap['treeByLevels']) && $this->classMap['treeByLevels'])
$this->controlador = 'App\Crud\Controller\proxyTreeLevelController';
else
$this->controlador = 'App\Crud\Controller\proxyTreeController';
else
$this->controlador = 'App\Crud\Controller\proxyPlainController';
}
protected function setInitialValues($rol, $routeClassName)
{
$this->rol = $rol;
$this->routeClassName = $routeClassName;
$this->setClassMap($routeClassName);
$this->setControlador();
$this->params = $this->setParamsRequest();
}
public function dispatchAction($rol, $routeClassName)
{
$this->setInitialValues($rol, $routeClassName);
$this->params['routeClassName']=$routeClassName;
$this->params['rol'] = $rol;
return $this->forward($this->controlador.'::indexAction', $this->params);
}
public function excelAction($rol, $routeClassName)
{
$this->setInitialValues($rol, $routeClassName);
$this->params['routeClassName']=$routeClassName;
$this->params['rol']=$rol;
return $this->forward($this->controlador.'::excelAction', $this->params);
}
public function indexAction($rol, $routeClassName)
{
$this->setInitialValues($rol, $routeClassName);
$params=array('rol'=> $rol, 'routeClassName'=>$routeClassName);
$filterData = $this->request->get('filterData');
if ($filterData)
$params['filterData'] = $filterData;
$page = $this->request->get('page');
if ($page)
$params['page']=$page;
$params= array_merge($this->params, $params);
return $this->forward($this->controlador.'::indexAction', $params);
}
public function newAction($rol, $routeClassName)
{
$this->setInitialValues($rol, $routeClassName);
$params=array('rol'=> $rol, 'routeClassName'=>$routeClassName);
$params= array_merge($this->params, $params);
return $this->forward($this->controlador.'::newAction', $params);
}
public function editAction($rol, $id, $routeClassName)
{
$this->setInitialValues($rol, $routeClassName);
$params=array('rol'=> $rol, 'routeClassName'=>$routeClassName, 'id'=>$id);
$params= array_merge($this->params, $params);
return $this->forward($this->controlador.'::editAction', $params);
}
public function showAction($rol, $id, $routeClassName, $tab=-1)
{
$this->setInitialValues($rol, $routeClassName);
$params=array('rol'=> $rol, 'routeClassName'=>$routeClassName, 'id'=>$id, 'tab'=>$tab);
$page = $this->request->get('page');
if ($page)
$params['page']=$page;
$params= array_merge($this->params, $params);
return $this->forward($this->controlador.'::showAction', $params);
}
public function deleteAction($rol, $id, $routeClassName)
{
$this->setInitialValues($rol, $routeClassName);
$params=array('rol'=> $rol, 'routeClassName'=>$routeClassName, 'id'=>$id);
$params= array_merge($this->params, $params);
return $this->forward($this->controlador.'::deleteAction', $params);
}
public function paginatorAction($rol, $routeClassName, $page)
{
$this->setInitialValues($rol, $routeClassName);
$params=array('rol'=> $rol, 'page'=> $page, 'routeClassName'=>$routeClassName);
$params= array_merge($this->params, $params);
return $this->forward($this->controlador.'::paginatorAction', $params);
}
public function cleanFilterAction($rol, $routeClassName)
{
$this->session->remove($routeClassName);
return $this->redirect($this->generateUrl('crud_index', array('rol'=>$rol, 'routeClassName'=>$routeClassName)));
}
public function cleanFilterInlineAction($routeClassName)
{
$this->session->remove($routeClassName);
$filterData = $this->request->get('filterData');
if ($filterData)
{
$this->session->set($routeClassName,$filterData);
}
$data= array('ok'=>true);
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
public function filterInlineAction($routeClassName)
{
$this->session->remove($routeClassName);
$this->filterData = $this->request->get('form');
$this->indexCleanFilterData();
$this->session->set($routeClassName,$this->filterData);
$data= array('ok'=>true);
$data = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
$data->headers->set('Content-Type', 'application/json');
return $data;
}
protected function indexCleanFilterData()
{
foreach ($this->filterData as $key=>$valor)
{
if ((is_array($valor) && empty($valor)) ||
(($valor instanceof \Doctrine\Common\Collections\ArrayCollection) && $valor->count()==0) ||
$valor=='')
{
unset($this->filterData[$key]);
}
}
}
}