155 lines
4.4 KiB
PHP
155 lines
4.4 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\QuestionTableMap;
|
|
use Contest\Event\QuestionEvent;
|
|
use Contest\Event\QuestionEvents;
|
|
use Contest\Model\QuestionQuery;
|
|
use Contest\Model\Question;
|
|
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 QuestionAction
|
|
* @package Contest\Action
|
|
* @author TheliaStudio
|
|
*/
|
|
class QuestionAction extends BaseAction implements EventSubscriberInterface
|
|
{
|
|
public function create(QuestionEvent $event)
|
|
{
|
|
$this->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),
|
|
);
|
|
}
|
|
}
|