. * */ namespace SP\Controller; defined('APP_ROOT') || die(_('No es posible acceder directamente a este archivo')); use SP\Core\Acl; use SP\Core\Exceptions\FileNotFoundException; use SP\Core\Init; use SP\Core\Session; use SP\Core\Exceptions\SPException; use SP\Core\DiFactory; use SP\Core\Template; use SP\Core\UI\ThemeIconsBase; use SP\Http\JsonResponse; /** * Clase base para los controladores */ abstract class ControllerBase { /** * Constantes de errores */ const ERR_UNAVAILABLE = 0; const ERR_ACCOUNT_NO_PERMISSION = 1; const ERR_PAGE_NO_PERMISSION = 2; const ERR_UPDATE_MPASS = 3; const ERR_OPERATION_NO_PERMISSION = 4; /** * Instancia del motor de plantillas a utilizar * * @var Template */ public $view; /** * Módulo a usar * * @var int */ protected $action; /** * Instancia de los iconos del tema visual * * @var ThemeIconsBase */ protected $icons; /** * Nombre del controlador * * @var string */ protected $controllerName; /** * @var JsonResponse */ protected $Json; /** * Constructor * * @param $template Template con instancia de plantilla */ public function __construct(Template $template = null) { $class = get_called_class(); $this->controllerName = substr($class, strrpos($class, '\\') + 1, -strlen('Controller')); $this->view = null === $template ? $this->getTemplate() : $template; $this->view->setBase(strtolower($this->controllerName)); $this->icons = DiFactory::getTheme()->getIcons(); $this->setViewVars(); } /** * Obtener una nueva instancia del motor de plantillas. * * @param null $template string con el nombre de la plantilla * @return Template */ protected function getTemplate($template = null) { return new Template($template); } /** * @return int El id de la acción */ public function getAction() { return $this->action; } /** * Establecer el módulo a presentar. * * @param int $action El id de la acción */ public function setAction($action) { $this->action = (int)$action; } /** * Renderizar los datos de la plantilla y mostrarlos * * @throws FileNotFoundException */ public function view() { echo $this->view->render(); } /** * Renderizar los datos de la plantilla y devolverlos * * @throws FileNotFoundException */ public function render() { return $this->view->render(); } /** * Obtener los datos para la vista de depuración */ public function getDebug() { global $memInit; $this->view->addTemplate('debug', 'common'); $this->view->assign('time', Init::microtime_float() - $this->view->timeStart); $this->view->assign('memInit', $memInit / 1000); $this->view->assign('memEnd', memory_get_usage() / 1000); } /** * @return JsonResponse */ public function getJson() { return $this->Json; } /** * @param JsonResponse $Json */ public function setJson(JsonResponse $Json) { $this->Json = $Json; } /** * Establecer la instancia del motor de plantillas a utilizar. * * @param Template $template */ protected function setTemplate(Template $template) { $this->view = $template; } /** * Comprobar si está permitido el acceso al módulo/página. * * @param null $action La acción a comprobar * @return bool */ protected function checkAccess($action = null) { $checkAction = $this->action; if (null !== $action) { $checkAction = $action; } return Session::getUserData()->isUserIsAdminApp() || Acl::checkUserAccess($checkAction); } /** * Establecer la plantilla de error con el código indicado. * * @param int $type int con el tipo de error * @param bool $reset * @param bool $fancy */ protected function showError($type, $reset = true, $fancy = false) { $errorsTypes = [ self::ERR_UNAVAILABLE => ['txt' => _('Opción no disponible'), 'hint' => _('Consulte con el administrador')], self::ERR_ACCOUNT_NO_PERMISSION => ['txt' => _('No tiene permisos para acceder a esta cuenta'), 'hint' => _('Consulte con el administrador')], self::ERR_PAGE_NO_PERMISSION => ['txt' => _('No tiene permisos para acceder a esta página'), 'hint' => _('Consulte con el administrador')], self::ERR_OPERATION_NO_PERMISSION => ['txt' => _('No tiene permisos para realizar esta operación'), 'hint' => _('Consulte con el administrador')], self::ERR_UPDATE_MPASS => ['txt' => _('Clave maestra actualizada'), 'hint' => _('Reinicie la sesión para cambiarla')] ]; if ($reset) { $this->view->resetTemplates(); } if ($fancy) { $this->view->addTemplate('errorfancy'); } else { $this->view->addTemplate('error'); } $this->view->append('errors', [ 'type' => SPException::SP_WARNING, 'description' => $errorsTypes[$type]['txt'], 'hint' => $errorsTypes[$type]['hint']] ); } private function setViewVars() { global $timeStart; $this->view->assign('timeStart', $timeStart); $this->view->assign('icons', $this->icons); $this->view->assign('SessionUserData', Session::getUserData()); } }