<?php
namespace App\Controller;
use App\Entity\Idempiere\AdWindowAccess;
use App\Repository\Idempiere\AdRoleRepository;
use App\Repository\Idempiere\AdWindowAccessRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
use Twig\Extra\Intl\IntlExtension;
use Twig\Loader\FilesystemLoader;
class BaseController extends AbstractController
{
/**
* Accion de Documento
*
* @var array Estatus
*/
protected $docaction = [
'CO' => [
'code' => 'CO',
'name' => 'ToComplete',
'flag' => 'success'
],
'VO' => [
'code' => 'VO',
'name' => 'Cancel',
'flag' => 'danger'
],
'CL' => [
'code' => 'CL',
'name' => 'Close',
'flag' => 'warning',
]
];
/**
* Estatus de Documento
*
* @var array Estatus
*/
protected $docstatus = [
'DR' => [
'code' => 'DR',
'name' => 'Draft',
'flag' => 'info'
],
'IP' => [
'code' => 'IP',
'name' => 'In Progress',
'flag' => 'warning'
],
'AP' => [
'code' => 'AP',
'name' => 'Approved',
'flag' => 'warning'
],
'NA' => [
'code' => 'NA',
'name' => 'Not Approved',
'flag' => 'danger'
],
'CO' => [
'code' => 'CO',
'name' => 'Complete',
'flag' => 'success'
],
'VO' => [
'code' => 'VO',
'name' => 'Voided',
'flag' => 'danger'
],
'RE' => [
'code' => 'RE',
'name' => 'Reversed',
'flag' => 'danger'
],
'IN' => [
'code' => 'IN',
'name' => 'Invalid',
'flag' => 'danger'
],
'CL' => [
'code' => 'CL',
'name' => 'Closed',
'flag' => 'dark',
],
'LS' => [
'code' => 'LS',
'name' => 'Loss of Sales',
'flag' => 'danger',
],
'PD' => [
'code' => 'PD',
'name' => 'Pendiente por despachar',
'flag' => 'info',
],
'??' => [
'code' => '??',
'name' => '??',
'flag' => 'info',
],
'PC' => [
'code' => 'PC',
'name' => 'Pendiente por cerrar',
'flag' => 'info',
]
];
/**
* Prioridad de Documento
*
* @var array Niveles de Prioridad
*/
protected $priorityrule = [
1 => 'Priority.Urgent',
3 => 'Priority.High',
5 => 'Priority.Medium',
7 => 'Priority.Low',
9 => 'Priority.Minor'
];
/**
* Templates Handler
* @var FilesystemLoader
*/
protected $loader;
protected $pdf;
/**
* Public Directory
* @var String
*/
protected $publicDir;
/**
* Request Stack
* @var RequestStack
*/
protected $requestStack;
/**
* Security Manager
* @var Security
*/
protected $security;
/**
* Session Interface
* @var SessionInterface
*/
protected $session;
protected $status = [
'VO' => [
'flag' => 'danger',
'name' => 'anulado'
],
'IN' => [
'flag' => 'danger',
'name' => 'invalido'
],
'CL' => [
'flag' => 'dark',
'name' => 'cerrado'
],
'CO' => [
'flag' => 'success',
'name' => 'completo'
],
'NA' => [
'flag' => 'danger',
'name' => 'no aprobado'
],
'AP' => [
'flag' => 'warning',
'name' => 'aprobado'
],
'IP' => [
'flag' => 'warning',
'name' => 'en progreso'
],
'DR' => [
'flag' => 'info',
'name' => 'borrador'
]
];
/**
* Twig Engine
* @var Enviroment
*/
protected $twig;
public function __construct(RequestStack $requestStack, Security $security)
{
$this->requestStack = $requestStack;
$this->session = $requestStack->getSession();
$this->security = $security;
$this->publicDir = dirname(__FILE__) . '/../../public/';
}
/**
* Ruta raiz - corroborar la sesion del usuario
* @Route("/", name="app_index")
*
* @return RedirectResponse Vista
*/
public function index(): RedirectResponse
{
return is_null($this->security->getUser()) ? $this->redirectToRoute('app_login') : $this->redirectToRoute('app_dashboard');
}
/**
* Funcion para enrutar la salida del sistema
* @Route("/salir", name="app_logout")
*
* @return RedirectResponse Vista
*/
public function logout(): RedirectResponse { return $this->redirectToRoute('app_login'); }
/**
* Verifica los accesos del rol a la ventana
*
* @param ManagerRegistry $manager Administrador de conexiones a la BD
* @param int $windowID Identificador de la ventana
* @param int $roleID Identificador del rol
*
* @return boolean Permiso de lectura
*
*/
public function VerifyWindow(ManagerRegistry $manager, Int $windowID = 0, Int $roleID = 0): bool
{
$window = $this->WindowAccess($manager, $windowID, $roleID);
if ( !is_null($window) )
return $window->isActive();
return false;
}
/**
* Accesos del rol a la ventana
*
* @param ManagerRegistry $manager Administrador de conexiones a la BD
* @param int $windowID Identificador de la ventana
* @param int $roleID Identificador del rol
*
* @return null|AdWindowAccess Permiso
*/
public function WindowAccess(ManagerRegistry $manager, Int $windowID = 0, Int $roleID = 0):? AdWindowAccess
{
$RWindowAccess = new AdWindowAccessRepository($manager);
$window = $RWindowAccess->findOneBy(['ad_window_id' => $windowID, 'ad_role_id' => $roleID]);
if ( !is_null($window) )
return $window;
$RRole = new AdRoleRepository($manager);
$role = $RRole->find($roleID);
$roleIncludes = $role->getAdRoleIncludeds();
foreach ($roleIncludes as $role) {
$window = $this->WindowAccess($manager, $windowID, $role->getIncludedRoleId());
if ( !is_null($window) )
return $window;
}
return null;
}
/**
* Create a directory
*
* @param string $targetDir Ruta de la carpeta destino
* @param string $folder Nombre de carpeta a crear
* @return string Nombre de carpeta
*/
public function createDir($targetDir, $folder)
{
if (!is_dir($this->publicDir . $targetDir . $folder)) {
mkdir($this->publicDir . $targetDir . $folder);
}
return $targetDir . $folder;
}
public function replaceUrl(String $relativePath): String
{
return str_replace($this->getParameter('app.url'), $this->publicDir, $relativePath);
}
}
?>