createOrUpdate($event, new Question()); } public function update(QuestionEvent $event) { $model = $this->getQuestion($event); $this->createOrUpdate($event, $model); } public function delete(QuestionEvent $event) { $this->getQuestion($event)->delete(); } protected function createOrUpdate(QuestionEvent $event, Question $model) { $con = Propel::getConnection(QuestionTableMap::DATABASE_NAME); $con->beginTransaction(); try { $model->setLocale($event->getLocale()); if (null !== $id = $event->getId()) { $model->setId($id); } if (null !== $visible = $event->getVisible()) { $model->setVisible($visible); } if (null !== $title = $event->getTitle()) { $model->setTitle($title); } if (null !== $description = $event->getDescription()) { $model->setDescription($description); } if (null !== $gameId = $event->getGameId()) { $model->setGameId($gameId); } $model->save($con); $con->commit(); } catch (\Exception $e) { $con->rollback(); throw $e; } $event->setQuestion($model); } protected function getQuestion(QuestionEvent $event) { $model = QuestionQuery::create()->findPk($event->getId()); if (null === $model) { throw new \RuntimeException(sprintf( "The 'question' id '%d' doesn't exist", $event->getId() )); } return $model; } public function toggleVisibility(ToggleVisibilityEvent $event) { $this->genericToggleVisibility(new QuestionQuery(), $event); } public function beforeCreateFormBuild(TheliaFormEvent $event) { } public function beforeUpdateFormBuild(TheliaFormEvent $event) { } public function afterCreateFormBuild(TheliaFormEvent $event) { } public function afterUpdateFormBuild(TheliaFormEvent $event) { } /** * Returns an array of event names this subscriber wants to listen to. * * The array keys are event names and the value can be: * * * The method name to call (priority defaults to 0) * * An array composed of the method name to call and the priority * * An array of arrays composed of the method names to call and respective * priorities, or 0 if unset * * For instance: * * * array('eventName' => 'methodName') * * array('eventName' => array('methodName', $priority)) * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) * * @return array The event names to listen to * * @api */ public static function getSubscribedEvents() { return array( QuestionEvents::CREATE => array("create", 128), QuestionEvents::UPDATE => array("update", 128), QuestionEvents::DELETE => array("delete", 128), QuestionEvents::TOGGLE_VISIBILITY => array("toggleVisibility", 128), TheliaEvents::FORM_BEFORE_BUILD . ".question_create" => array("beforeCreateFormBuild", 128), TheliaEvents::FORM_BEFORE_BUILD . ".question_update" => array("beforeUpdateFormBuild", 128), TheliaEvents::FORM_AFTER_BUILD . ".question_create" => array("afterCreateFormBuild", 128), TheliaEvents::FORM_AFTER_BUILD . ".question_update" => array("afterUpdateFormBuild", 128), ); } }