. */ namespace SP\Forms; use SP\Core\Acl\ActionsInterface; use SP\Core\Exceptions\ValidationException; use SP\Core\Messages\NoticeMessage; use SP\DataModel\NoticeData; use SP\Http\Request; /** * Class NoticeForm * * @package SP\Forms */ class NoticeForm extends FormBase implements FormInterface { /** * @var NoticeData */ protected $noticeData; /** * Validar el formulario * * @param $action * @return bool * @throws \SP\Core\Exceptions\ValidationException */ public function validate($action) { switch ($action) { case ActionsInterface::NOTICE_USER_CREATE: case ActionsInterface::NOTICE_USER_EDIT: $this->analyzeRequestData(); $this->checkCommon(); break; } return true; } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData() { $Description = new NoticeMessage(); $Description->addDescription(Request::analyze('notice_description')); $this->noticeData = new NoticeData(); $this->noticeData->setId($this->itemId); $this->noticeData->setType(Request::analyze('notice_type')); $this->noticeData->setComponent(Request::analyze('notice_component')); $this->noticeData->setDescription($Description); $this->noticeData->setUserId(Request::analyze('notice_user', 0)); if ($this->noticeData->getUserId() === 0) { $this->noticeData->setOnlyAdmin(Request::analyze('notice_onlyadmin', 0, false, 1)); $this->noticeData->setSticky(Request::analyze('notice_sticky', 0, false, 1)); } } /** * @throws ValidationException */ private function checkCommon() { if (!$this->noticeData->getComponent()) { throw new ValidationException(__u('Es necesario un componente')); } if (!$this->noticeData->getType()) { throw new ValidationException(__u('Es necesario un tipo')); } if (!$this->noticeData->getDescription()) { throw new ValidationException(__u('Es necesaria una descripción')); } if (!$this->noticeData->getUserId() && !$this->noticeData->isOnlyAdmin() && !$this->noticeData->isSticky()) { throw new ValidationException(__u('Es necesario un destinatario')); } } /** * @return NoticeData */ public function getItemData() { return $this->noticeData; } }