src/Controller/BaseController.php line 222

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Access\Infrastructure\Config\WindowAccessEnvGate;
  4. use App\Entity\Idempiere\AdWindowAccess;
  5. use App\Repository\Idempiere\AdRoleRepository;
  6. use App\Repository\Idempiere\AdWindowAccessRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Security;
  14. use Twig\Environment;
  15. use Twig\Extra\Intl\IntlExtension;
  16. use Twig\Loader\FilesystemLoader;
  17. class BaseController extends AbstractController
  18. {
  19. /**
  20. * Accion de Documento
  21. *
  22. * @var array Estatus
  23. */
  24. protected $docaction = [
  25. 'CO' => [
  26. 'code' => 'CO',
  27. 'name' => 'ToComplete',
  28. 'flag' => 'success'
  29. ],
  30. 'VO' => [
  31. 'code' => 'VO',
  32. 'name' => 'Cancel',
  33. 'flag' => 'danger'
  34. ],
  35. 'CL' => [
  36. 'code' => 'CL',
  37. 'name' => 'Close',
  38. 'flag' => 'warning',
  39. ]
  40. ];
  41. /**
  42. * Estatus de Documento
  43. *
  44. * @var array Estatus
  45. */
  46. protected $docstatus = [
  47. 'DR' => [
  48. 'code' => 'DR',
  49. 'name' => 'Draft',
  50. 'flag' => 'info'
  51. ],
  52. 'IP' => [
  53. 'code' => 'IP',
  54. 'name' => 'In Progress',
  55. 'flag' => 'warning'
  56. ],
  57. 'AP' => [
  58. 'code' => 'AP',
  59. 'name' => 'Approved',
  60. 'flag' => 'warning'
  61. ],
  62. 'NA' => [
  63. 'code' => 'NA',
  64. 'name' => 'Not Approved',
  65. 'flag' => 'danger'
  66. ],
  67. 'CO' => [
  68. 'code' => 'CO',
  69. 'name' => 'Complete',
  70. 'flag' => 'success'
  71. ],
  72. 'VO' => [
  73. 'code' => 'VO',
  74. 'name' => 'Voided',
  75. 'flag' => 'danger'
  76. ],
  77. 'RE' => [
  78. 'code' => 'RE',
  79. 'name' => 'Reversed',
  80. 'flag' => 'danger'
  81. ],
  82. 'IN' => [
  83. 'code' => 'IN',
  84. 'name' => 'Invalid',
  85. 'flag' => 'danger'
  86. ],
  87. 'CL' => [
  88. 'code' => 'CL',
  89. 'name' => 'Closed',
  90. 'flag' => 'dark',
  91. ],
  92. 'LS' => [
  93. 'code' => 'LS',
  94. 'name' => 'Loss of Sales',
  95. 'flag' => 'danger',
  96. ],
  97. 'PD' => [
  98. 'code' => 'PD',
  99. 'name' => 'Pendiente por despachar',
  100. 'flag' => 'info',
  101. ],
  102. '??' => [
  103. 'code' => '??',
  104. 'name' => '??',
  105. 'flag' => 'info',
  106. ],
  107. 'PC' => [
  108. 'code' => 'PC',
  109. 'name' => 'Pendiente por cerrar',
  110. 'flag' => 'info',
  111. ]
  112. ];
  113. /**
  114. * Prioridad de Documento
  115. *
  116. * @var array Niveles de Prioridad
  117. */
  118. protected $priorityrule = [
  119. 1 => 'Priority.Urgent',
  120. 3 => 'Priority.High',
  121. 5 => 'Priority.Medium',
  122. 7 => 'Priority.Low',
  123. 9 => 'Priority.Minor'
  124. ];
  125. /**
  126. * Templates Handler
  127. * @var FilesystemLoader
  128. */
  129. protected $loader;
  130. protected $pdf;
  131. /**
  132. * Public Directory
  133. * @var String
  134. */
  135. protected $publicDir;
  136. /**
  137. * Request Stack
  138. * @var RequestStack
  139. */
  140. protected $requestStack;
  141. /**
  142. * Security Manager
  143. * @var Security
  144. */
  145. protected $security;
  146. /**
  147. * Session Interface
  148. * @var SessionInterface
  149. */
  150. protected $session;
  151. protected $status = [
  152. 'VO' => [
  153. 'flag' => 'danger',
  154. 'name' => 'anulado'
  155. ],
  156. 'IN' => [
  157. 'flag' => 'danger',
  158. 'name' => 'invalido'
  159. ],
  160. 'CL' => [
  161. 'flag' => 'dark',
  162. 'name' => 'cerrado'
  163. ],
  164. 'CO' => [
  165. 'flag' => 'success',
  166. 'name' => 'completo'
  167. ],
  168. 'NA' => [
  169. 'flag' => 'danger',
  170. 'name' => 'no aprobado'
  171. ],
  172. 'AP' => [
  173. 'flag' => 'warning',
  174. 'name' => 'aprobado'
  175. ],
  176. 'IP' => [
  177. 'flag' => 'warning',
  178. 'name' => 'en progreso'
  179. ],
  180. 'DR' => [
  181. 'flag' => 'info',
  182. 'name' => 'borrador'
  183. ]
  184. ];
  185. /**
  186. * Twig Engine
  187. * @var Enviroment
  188. */
  189. protected $twig;
  190. public function __construct(RequestStack $requestStack, Security $security)
  191. {
  192. $this->requestStack = $requestStack;
  193. $this->session = $requestStack->getSession();
  194. $this->security = $security;
  195. $this->publicDir = dirname(__FILE__) . '/../../public/';
  196. }
  197. /**
  198. * Ruta raiz - corroborar la sesion del usuario
  199. * @Route("/", name="app_index")
  200. *
  201. * @return RedirectResponse Vista
  202. */
  203. public function index(): RedirectResponse
  204. {
  205. return is_null($this->security->getUser()) ? $this->redirectToRoute('app_login') : $this->redirectToRoute('app_dashboard');
  206. }
  207. /**
  208. * Funcion para enrutar la salida del sistema
  209. * @Route("/salir", name="app_logout")
  210. *
  211. * @return RedirectResponse Vista
  212. */
  213. public function logout(): RedirectResponse
  214. {
  215. return $this->redirectToRoute('app_login');
  216. }
  217. /**
  218. * Verifica los accesos del rol a la ventana
  219. *
  220. * @param ManagerRegistry $manager Administrador de conexiones a la BD
  221. * @param int $windowID Identificador de la ventana
  222. * @param int $roleID Identificador del rol
  223. *
  224. * @return boolean Permiso de lectura
  225. *
  226. */
  227. public function VerifyWindow(ManagerRegistry $manager, Int $windowID = 0, Int $roleID = 0): bool
  228. {
  229. $window = $this->WindowAccess($manager, $windowID, $roleID);
  230. if (!is_null($window))
  231. return $window->isActive();
  232. return false;
  233. }
  234. /**
  235. * Accesos del rol a la ventana
  236. *
  237. * @param ManagerRegistry $manager Administrador de conexiones a la BD
  238. * @param int $windowID Identificador de la ventana
  239. * @param int $roleID Identificador del rol
  240. *
  241. * @return null|AdWindowAccess Permiso
  242. */
  243. public function WindowAccess(ManagerRegistry $manager, Int $windowID = 0, Int $roleID = 0): ?AdWindowAccess
  244. {
  245. if (!WindowAccessEnvGate::isAllowed($windowID, $roleID)) {
  246. return null;
  247. }
  248. return $this->resolveWindowAccess($manager, $windowID, $roleID);
  249. }
  250. /**
  251. * Resuelve el acceso a la ventana recorriendo la jerarquia de roles incluidos.
  252. * Separado de WindowAccess() para que el gate por variable de entorno solo
  253. * se evalue una vez, contra el rol original solicitante.
  254. *
  255. * @return null|AdWindowAccess Permiso
  256. */
  257. private function resolveWindowAccess(ManagerRegistry $manager, int $windowID, int $roleID): ?AdWindowAccess
  258. {
  259. $RWindowAccess = new AdWindowAccessRepository($manager);
  260. $window = $RWindowAccess->findOneBy(['ad_window_id' => $windowID, 'ad_role_id' => $roleID]);
  261. if (!is_null($window))
  262. return $window;
  263. $RRole = new AdRoleRepository($manager);
  264. $role = $RRole->find($roleID);
  265. $roleIncludes = $role->getAdRoleIncludeds();
  266. foreach ($roleIncludes as $role) {
  267. $window = $this->resolveWindowAccess($manager, $windowID, $role->getIncludedRoleId());
  268. if (!is_null($window))
  269. return $window;
  270. }
  271. return null;
  272. }
  273. /**
  274. * Create a directory
  275. * @author rbellorin@sumagroups.com
  276. * @version 2026-01-21
  277. *
  278. * @param string $targetDir Ruta de la carpeta destino (relativa a /public)
  279. * @param string $folder Nombre de carpeta a crear
  280. * @return string Ruta relativa creada (ej: assets/img/product/1000001)
  281. */
  282. public function createDir(string $targetDir, string $folder): string
  283. {
  284. // Normaliza slashes
  285. $targetDir = rtrim($targetDir, '/') . '/';
  286. $folder = trim($folder, '/');
  287. $absolutePath = rtrim($this->publicDir, '/') . '/' . $targetDir . $folder;
  288. if (!is_dir($absolutePath)) {
  289. // 0775 recomendado (grupo web). Cambia a 0777 si tu servidor lo exige.
  290. $ok = @mkdir($absolutePath, 0775, true);
  291. // Si falló y sigue sin existir, explota con error claro
  292. if (!$ok && !is_dir($absolutePath)) {
  293. $err = error_get_last();
  294. throw new \RuntimeException(sprintf(
  295. 'No se pudo crear el directorio "%s". %s',
  296. $absolutePath,
  297. $err['message'] ?? 'Error desconocido'
  298. ));
  299. }
  300. // Por si umask recorta permisos
  301. @chmod($absolutePath, 0775);
  302. }
  303. return $targetDir . $folder;
  304. }
  305. public function replaceUrl(String $relativePath): String
  306. {
  307. return str_replace($this->getParameter('app.url'), $this->publicDir, $relativePath);
  308. }
  309. }