createOrUpdate($event, new Answer()); } public function update(AnswerEvent $event) { $model = $this->getAnswer($event); $this->createOrUpdate($event, $model); } public function delete(AnswerEvent $event) { $this->getAnswer($event)->delete(); } protected function createOrUpdate(AnswerEvent $event, Answer $model) { $con = Propel::getConnection(AnswerTableMap::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 !== $correct = $event->getCorrect()) { $model->setCorrect($correct); } if (null !== $title = $event->getTitle()) { $model->setTitle($title); } if (null !== $description = $event->getDescription()) { $model->setDescription($description); } if (null !== $questionId = $event->getQuestionId()) { $model->setQuestionId($questionId); } $model->save($con); $con->commit(); } catch (\Exception $e) { $con->rollback(); throw $e; } $event->setAnswer($model); } protected function getAnswer(AnswerEvent $event) { $model = AnswerQuery::create()->findPk($event->getId()); if (null === $model) { throw new \RuntimeException(sprintf( "The 'answer' id '%d' doesn't exist", $event->getId() )); } return $model; } public function toggleVisibility(ToggleVisibilityEvent $event) { $this->genericToggleVisibility(new AnswerQuery(), $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( AnswerEvents::CREATE => array("create", 128), AnswerEvents::UPDATE => array("update", 128), AnswerEvents::DELETE => array("delete", 128), AnswerEvents::TOGGLE_VISIBILITY => array("toggleVisibility", 128), TheliaEvents::FORM_BEFORE_BUILD . ".answer_create" => array("beforeCreateFormBuild", 128), TheliaEvents::FORM_BEFORE_BUILD . ".answer_update" => array("beforeUpdateFormBuild", 128), TheliaEvents::FORM_AFTER_BUILD . ".answer_create" => array("afterCreateFormBuild", 128), TheliaEvents::FORM_AFTER_BUILD . ".answer_update" => array("afterUpdateFormBuild", 128), ); } }