src/Controller/BaseController.php line 215

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Idempiere\AdWindowAccess;
  4. use App\Repository\Idempiere\AdRoleRepository;
  5. use App\Repository\Idempiere\AdWindowAccessRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Security;
  13. use Twig\Environment;
  14. use Twig\Extra\Intl\IntlExtension;
  15. use Twig\Loader\FilesystemLoader;
  16. class BaseController extends AbstractController
  17. {
  18.     /**
  19.      * Accion de Documento
  20.      * 
  21.      * @var array Estatus
  22.      */
  23.     protected $docaction = [
  24.         'CO' => [
  25.             'code'  => 'CO',
  26.             'name'  => 'ToComplete',
  27.             'flag'  => 'success'
  28.         ],
  29.         'VO' => [
  30.             'code'  => 'VO',
  31.             'name'  => 'Cancel',
  32.             'flag'  => 'danger'
  33.         ],
  34.         'CL' => [
  35.             'code'  => 'CL',
  36.             'name'  => 'Close',
  37.             'flag'  => 'warning',
  38.         ]
  39.     ];
  40.     /**
  41.      * Estatus de Documento
  42.      * 
  43.      * @var array Estatus
  44.      */
  45.     protected $docstatus = [
  46.         'DR' => [
  47.             'code'  => 'DR',
  48.             'name'  => 'Draft',
  49.             'flag'  => 'info'
  50.         ],
  51.         'IP' => [
  52.             'code'  => 'IP',
  53.             'name'  => 'In Progress',
  54.             'flag'  => 'warning'
  55.         ],
  56.         'AP' => [
  57.             'code'  => 'AP',
  58.             'name'  => 'Approved',
  59.             'flag'  => 'warning'
  60.         ],
  61.         'NA' => [
  62.             'code'  => 'NA',
  63.             'name'  => 'Not Approved',
  64.             'flag'  => 'danger'
  65.         ],
  66.         'CO' => [
  67.             'code'  => 'CO',
  68.             'name'  => 'Complete',
  69.             'flag'  => 'success'
  70.         ],
  71.         'VO' => [
  72.             'code'  => 'VO',
  73.             'name'  => 'Voided',
  74.             'flag'  => 'danger'
  75.         ],
  76.         'RE' => [
  77.             'code'  => 'RE',
  78.             'name'  => 'Reversed',
  79.             'flag'  => 'danger'
  80.         ],
  81.         'IN' => [
  82.             'code'  => 'IN',
  83.             'name'  => 'Invalid',
  84.             'flag'  => 'danger'
  85.         ],
  86.         'CL' => [
  87.             'code'  => 'CL',
  88.             'name'  => 'Closed',
  89.             'flag'  => 'dark',
  90.         ]
  91.     ];
  92.     /**
  93.      * Prioridad de Documento
  94.      * 
  95.      * @var array Niveles de Prioridad
  96.      */
  97.     protected $priorityrule = [
  98.         => 'Priority.Urgent',
  99.         => 'Priority.High',
  100.         => 'Priority.Medium',
  101.         => 'Priority.Low',
  102.         => 'Priority.Minor'
  103.     ];
  104.     /**
  105.      * Templates Handler
  106.      * @var FilesystemLoader
  107.      */
  108.     protected $loader;
  109.     protected $pdf;
  110.     /** 
  111.      * Public Directory 
  112.      * @var String
  113.      */
  114.     protected $publicDir;
  115.     /**
  116.      * Request Stack
  117.      * @var RequestStack
  118.      */
  119.     protected $requestStack;
  120.     /**
  121.      * Security Manager
  122.      * @var Security
  123.      */
  124.     protected $security;
  125.     /** 
  126.      * Session Interface 
  127.      * @var SessionInterface
  128.      */
  129.     protected $session;
  130.     protected $status = [
  131.         'VO' => [
  132.             'flag' => 'danger',
  133.             'name' => 'anulado'
  134.         ],
  135.         'IN' => [
  136.             'flag' => 'danger',
  137.             'name' => 'invalido'
  138.         ],
  139.         'CL' => [
  140.             'flag' => 'dark',
  141.             'name' => 'cerrado'
  142.         ],
  143.         'CO' => [
  144.             'flag' => 'success',
  145.             'name' => 'completo'
  146.         ],
  147.         'NA' => [
  148.             'flag' => 'danger',
  149.             'name' => 'no aprobado'
  150.         ],
  151.         'AP' => [
  152.             'flag' => 'warning',
  153.             'name' => 'aprobado'
  154.         ],
  155.         'IP' => [
  156.             'flag' => 'warning',
  157.             'name' => 'en progreso'
  158.         ],
  159.         'DR' => [
  160.             'flag' => 'info',
  161.             'name' => 'borrador'
  162.         ]
  163.     ];
  164.     /** 
  165.      * Twig Engine 
  166.      *  @var Enviroment 
  167.      */
  168.     protected $twig;
  169.     public function __construct(RequestStack $requestStackSecurity $security)
  170.     {
  171.         $this->requestStack $requestStack;
  172.         $this->security $security;
  173.         $this->session $requestStack->getSession();
  174.         $this->publicDir dirname(__FILE__) . '/../../public/';
  175.         $this->loader   = new FilesystemLoader('../templates');
  176.         $this->twig     = new Environment($this->loader, [
  177.             'cache'         => '../templates/cache',
  178.             'autoescape'    => false,
  179.             'debug'         => true
  180.         ]);
  181.         $this->twig->addExtension(new IntlExtension());
  182.         $base64     = new \Twig\TwigFilter('base64''base64_encode');
  183.         $this->twig->addFilter($base64);
  184.         
  185.         $num_format  = new \Twig\TwigFunction('number_format''number_format');
  186.         $this->twig->addFunction($num_format);
  187.     }
  188.     /**
  189.      * Ruta raiz - corroborar la sesion del usuario
  190.      * @Route("/", name="app_index")
  191.      * 
  192.      * @return RedirectResponse Vista
  193.      */
  194.     public function index(): RedirectResponse
  195.     
  196.         return is_null($this->security->getUser()) ? $this->redirectToRoute('app_login') : $this->redirectToRoute('app_dashboard'); 
  197.     }
  198.     /**
  199.      * Funcion para enrutar la salida del sistema
  200.      * @Route("/salir", name="app_logout")
  201.      * 
  202.      * @return RedirectResponse Vista
  203.      */
  204.     public function logout(): RedirectResponse { return $this->redirectToRoute('app_login'); }
  205.     /** 
  206.      * Verifica los accesos del rol a la ventana
  207.      * 
  208.      * @param ManagerRegistry $manager Administrador de conexiones a la BD
  209.      * @param int $windowID Identificador de la ventana
  210.      * @param int $roleID Identificador del rol
  211.      * 
  212.      * @return boolean Permiso de lectura
  213.      * 
  214.      */
  215.     public function VerifyWindow(ManagerRegistry $managerInt $windowID 0Int $roleID 0): bool
  216.     {
  217.         $window $this->WindowAccess($manager$windowID$roleID);
  218.         if ( !is_null($window) )
  219.             return $window->getIsactive();
  220.        
  221.         return false;
  222.     }
  223.     /** 
  224.      * Accesos del rol a la ventana
  225.      * 
  226.      * @param ManagerRegistry $manager Administrador de conexiones a la BD
  227.      * @param int $windowID Identificador de la ventana
  228.      * @param int $roleID Identificador del rol
  229.      * 
  230.      * @return null|AdWindowAccess Permiso
  231.      */
  232.     public function WindowAccess(ManagerRegistry $managerInt $windowID 0Int $roleID 0):? AdWindowAccess
  233.     {
  234.         $RWindowAccess = new AdWindowAccessRepository($manager);
  235.         $window $RWindowAccess->findOneBy(['ad_window_id'  => $windowID'ad_role_id'    => $roleID]);
  236.         if ( !is_null($window) ) 
  237.             return $window;
  238.         $RRole = new AdRoleRepository($manager);
  239.         $role $RRole->find($roleID);
  240.         $roleIncludes $role->getAdRoleIncludeds();
  241.         foreach ($roleIncludes as $role) {
  242.             $window $this->WindowAccess($manager$windowID$role->getIncludedRoleId());
  243.             if ( !is_null($window) ) 
  244.                 return $window;
  245.         }
  246.        
  247.         return null;
  248.     }
  249.     /**
  250.      * Create a directory
  251.      * 
  252.      * @param string $targetDir Ruta de la carpeta destino
  253.      * @param string $folder Nombre de carpeta a crear
  254.      * @return string Nombre de carpeta
  255.      */
  256.     public function createDir($targetDir$folder)
  257.     {
  258.         if (!is_dir($this->publicDir $targetDir $folder)) {
  259.             mkdir($this->publicDir $targetDir $folder);
  260.         }
  261.         return $targetDir $folder;
  262.     }
  263.     public function replaceUrl(String $relativePath): String
  264.     {
  265.         return str_replace($this->getParameter('app.url'), $this->publicDir$relativePath);
  266.     }
  267. }
  268. ?>