. */ namespace SP\Controller; use SP\Core\Init; use SP\Core\SessionUtil; use SP\Http\JsonResponse; use SP\Http\Request; use SP\Util\Json; /** * Class RequestControllerTrait * * @package SP\Controller */ trait RequestControllerTrait { /** * @var int */ protected $actionId; /** * @var int|array */ protected $itemId; /** * @var int */ protected $activeTab; /** * @var JsonResponse */ protected $jsonResponse; /** * @var string */ protected $sk; /** * inicializar las propiedades */ protected function init() { $this->jsonResponse = new JsonResponse(); $this->checkSession(); $this->analyzeRequest(); $this->preActionChecks(); } /** * Analizar la petición HTTP y establecer las propiedades del elemento */ protected function analyzeRequest() { $this->sk = Request::analyze('sk'); $this->actionId = Request::analyze('actionId', 0); $this->itemId = Request::analyze('itemId', 0); $this->activeTab = Request::analyze('activeTab', 0); } /** * Comprobaciones antes de realizar una acción */ protected function preActionChecks() { if (!$this->sk || !$this->actionId || !SessionUtil::checkSessionKey($this->sk)) { $this->invalidAction(); } } protected function invalidAction() { $this->jsonResponse->setDescription(_('Acción Inválida')); Json::returnJson($this->jsonResponse); } /** * Comprobar si la sesión está activa * * @throws \SP\Core\Exceptions\SPException */ protected function checkSession() { if (!Init::isLoggedIn()) { $this->jsonResponse->setDescription(_('La sesión no se ha iniciado o ha caducado')); $this->jsonResponse->setStatus(10); Json::returnJson($this->jsonResponse); } } }