/** * @param ConferenceInputModel $model * @Method('GET', 'POST') * @Validatetoken('token') * @return View */ public function add(ConferenceInputModel $model) { if (!$model->isValid()) { return new View('conferences', 'add', $model); } $service = new ConferencesService($this->dbContext); if (HttpContext::getInstance()->isPost()) { $result = $service->addConference($model); if (!$result->hasError()) { $this->addInfoMessage($result->getMessage()); $this->redirect('conferences', 'own'); } else { $this->addErrorMessage($result->getMessage()); $this->redirect('conferences', 'own'); } } else { return new View('conferences', 'add', new ConferenceInputModel()); } }
/** * @param ConferenceInputModel $model * @return ServiceResponse * @throws \Exception */ public function addConference(ConferenceInputModel $model) : ServiceResponse { if (strtotime($model->getEndDate()) <= strtotime($model->getStartDate())) { return new ServiceResponse(1, 'End date must be after Start date.'); } $conference = new Conference($model->getTitle(), $model->getStartDate(), $model->getEndDate(), HttpContext::getInstance()->getIdentity()->getUserId(), null); $this->dbContext->getConferencesRepository()->add($conference); $this->dbContext->saveChanges(); $title = $model->getTitle(); $organiser = HttpContext::getInstance()->getIdentity()->getUsername(); $message = "New conference titled '{$title}' was added by user {$organiser}."; $notyService = new NotificationsService($this->dbContext); $notyService->postToAll($message); return new ServiceResponse(null, 'Conference added successfully.'); }