Files
le-matelot/local/modules/Contest/Action/Base/AnswerAction.php
2020-01-27 08:56:08 +01:00

159 lines
4.5 KiB
PHP

<?php
/**
* This class has been generated by TheliaStudio
* For more information, see https://github.com/thelia-modules/TheliaStudio
*/
namespace Contest\Action\Base;
use Contest\Model\Map\AnswerTableMap;
use Contest\Event\AnswerEvent;
use Contest\Event\AnswerEvents;
use Contest\Model\AnswerQuery;
use Contest\Model\Answer;
use Thelia\Action\BaseAction;
use Thelia\Core\Event\ToggleVisibilityEvent;
use Thelia\Core\Event\UpdatePositionEvent;
use Propel\Runtime\Propel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\TheliaEvents;
use \Thelia\Core\Event\TheliaFormEvent;
/**
* Class AnswerAction
* @package Contest\Action
* @author TheliaStudio
*/
class AnswerAction extends BaseAction implements EventSubscriberInterface
{
public function create(AnswerEvent $event)
{
$this->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),
);
}
}