src/Controller/BaseController.php line 221

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. 'LS' => [
  92. 'code' => 'LS',
  93. 'name' => 'Loss of Sales',
  94. 'flag' => 'danger',
  95. ],
  96. 'PD' => [
  97. 'code' => 'PD',
  98. 'name' => 'Pendiente por despachar',
  99. 'flag' => 'info',
  100. ],
  101. '??' => [
  102. 'code' => '??',
  103. 'name' => '??',
  104. 'flag' => 'info',
  105. ],
  106. 'PC' => [
  107. 'code' => 'PC',
  108. 'name' => 'Pendiente por cerrar',
  109. 'flag' => 'info',
  110. ]
  111. ];
  112. /**
  113. * Prioridad de Documento
  114. *
  115. * @var array Niveles de Prioridad
  116. */
  117. protected $priorityrule = [
  118. 1 => 'Priority.Urgent',
  119. 3 => 'Priority.High',
  120. 5 => 'Priority.Medium',
  121. 7 => 'Priority.Low',
  122. 9 => 'Priority.Minor'
  123. ];
  124. /**
  125. * Templates Handler
  126. * @var FilesystemLoader
  127. */
  128. protected $loader;
  129. protected $pdf;
  130. /**
  131. * Public Directory
  132. * @var String
  133. */
  134. protected $publicDir;
  135. /**
  136. * Request Stack
  137. * @var RequestStack
  138. */
  139. protected $requestStack;
  140. /**
  141. * Security Manager
  142. * @var Security
  143. */
  144. protected $security;
  145. /**
  146. * Session Interface
  147. * @var SessionInterface
  148. */
  149. protected $session;
  150. protected $status = [
  151. 'VO' => [
  152. 'flag' => 'danger',
  153. 'name' => 'anulado'
  154. ],
  155. 'IN' => [
  156. 'flag' => 'danger',
  157. 'name' => 'invalido'
  158. ],
  159. 'CL' => [
  160. 'flag' => 'dark',
  161. 'name' => 'cerrado'
  162. ],
  163. 'CO' => [
  164. 'flag' => 'success',
  165. 'name' => 'completo'
  166. ],
  167. 'NA' => [
  168. 'flag' => 'danger',
  169. 'name' => 'no aprobado'
  170. ],
  171. 'AP' => [
  172. 'flag' => 'warning',
  173. 'name' => 'aprobado'
  174. ],
  175. 'IP' => [
  176. 'flag' => 'warning',
  177. 'name' => 'en progreso'
  178. ],
  179. 'DR' => [
  180. 'flag' => 'info',
  181. 'name' => 'borrador'
  182. ]
  183. ];
  184. /**
  185. * Twig Engine
  186. * @var Enviroment
  187. */
  188. protected $twig;
  189. public function __construct(RequestStack $requestStack, Security $security)
  190. {
  191. $this->requestStack = $requestStack;
  192. $this->session = $requestStack->getSession();
  193. $this->security = $security;
  194. $this->publicDir = dirname(__FILE__) . '/../../public/';
  195. }
  196. /**
  197. * Ruta raiz - corroborar la sesion del usuario
  198. * @Route("/", name="app_index")
  199. *
  200. * @return RedirectResponse Vista
  201. */
  202. public function index(): RedirectResponse
  203. {
  204. return is_null($this->security->getUser()) ? $this->redirectToRoute('app_login') : $this->redirectToRoute('app_dashboard');
  205. }
  206. /**
  207. * Funcion para enrutar la salida del sistema
  208. * @Route("/salir", name="app_logout")
  209. *
  210. * @return RedirectResponse Vista
  211. */
  212. public function logout(): RedirectResponse
  213. {
  214. return $this->redirectToRoute('app_login');
  215. }
  216. /**
  217. * Verifica los accesos del rol a la ventana
  218. *
  219. * @param ManagerRegistry $manager Administrador de conexiones a la BD
  220. * @param int $windowID Identificador de la ventana
  221. * @param int $roleID Identificador del rol
  222. *
  223. * @return boolean Permiso de lectura
  224. *
  225. */
  226. public function VerifyWindow(ManagerRegistry $manager, Int $windowID = 0, Int $roleID = 0): bool
  227. {
  228. $window = $this->WindowAccess($manager, $windowID, $roleID);
  229. if (!is_null($window))
  230. return $window->isActive();
  231. return false;
  232. }
  233. /**
  234. * Accesos del rol a la ventana
  235. *
  236. * @param ManagerRegistry $manager Administrador de conexiones a la BD
  237. * @param int $windowID Identificador de la ventana
  238. * @param int $roleID Identificador del rol
  239. *
  240. * @return null|AdWindowAccess Permiso
  241. */
  242. public function WindowAccess(ManagerRegistry $manager, Int $windowID = 0, Int $roleID = 0): ?AdWindowAccess
  243. {
  244. $RWindowAccess = new AdWindowAccessRepository($manager);
  245. $window = $RWindowAccess->findOneBy(['ad_window_id' => $windowID, 'ad_role_id' => $roleID]);
  246. if (!is_null($window))
  247. return $window;
  248. $RRole = new AdRoleRepository($manager);
  249. $role = $RRole->find($roleID);
  250. $roleIncludes = $role->getAdRoleIncludeds();
  251. foreach ($roleIncludes as $role) {
  252. $window = $this->WindowAccess($manager, $windowID, $role->getIncludedRoleId());
  253. if (!is_null($window))
  254. return $window;
  255. }
  256. return null;
  257. }
  258. /**
  259. * Create a directory
  260. * @author rbellorin@sumagroups.com
  261. * @version 2026-01-21
  262. *
  263. * @param string $targetDir Ruta de la carpeta destino (relativa a /public)
  264. * @param string $folder Nombre de carpeta a crear
  265. * @return string Ruta relativa creada (ej: assets/img/product/1000001)
  266. */
  267. public function createDir(string $targetDir, string $folder): string
  268. {
  269. // Normaliza slashes
  270. $targetDir = rtrim($targetDir, '/') . '/';
  271. $folder = trim($folder, '/');
  272. $absolutePath = rtrim($this->publicDir, '/') . '/' . $targetDir . $folder;
  273. if (!is_dir($absolutePath)) {
  274. // 0775 recomendado (grupo web). Cambia a 0777 si tu servidor lo exige.
  275. $ok = @mkdir($absolutePath, 0775, true);
  276. // Si falló y sigue sin existir, explota con error claro
  277. if (!$ok && !is_dir($absolutePath)) {
  278. $err = error_get_last();
  279. throw new \RuntimeException(sprintf(
  280. 'No se pudo crear el directorio "%s". %s',
  281. $absolutePath,
  282. $err['message'] ?? 'Error desconocido'
  283. ));
  284. }
  285. // Por si umask recorta permisos
  286. @chmod($absolutePath, 0775);
  287. }
  288. return $targetDir . $folder;
  289. }
  290. public function replaceUrl(String $relativePath): String
  291. {
  292. return str_replace($this->getParameter('app.url'), $this->publicDir, $relativePath);
  293. }
  294. }