. */ namespace SP\Controller; use SP\Core\Init; use SP\Core\Messages\LogMessage; 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; /** * @var LogMessage */ protected $LogMessage; /** * inicializar las propiedades * * @internal param array $checKItems Lista de elementos a analizar */ 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 * * @throws \SP\Core\Exceptions\SPException */ protected function preActionChecks() { if (!$this->sk || !$this->actionId || !SessionUtil::checkSessionKey($this->sk)) { $this->invalidAction(); } } /** * Acción no disponible * * @throws \SP\Core\Exceptions\SPException */ protected function invalidAction() { $this->JsonResponse->setDescription(__('Acción Inválida', false)); 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', false)); $this->JsonResponse->setStatus(10); Json::returnJson($this->JsonResponse); } } }