Files
2020-01-27 08:56:08 +01:00

151 lines
4.2 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\GameTableMap;
use Contest\Event\GameEvent;
use Contest\Event\GameEvents;
use Contest\Model\GameQuery;
use Contest\Model\Game;
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 GameAction
* @package Contest\Action
* @author TheliaStudio
*/
class GameAction extends BaseAction implements EventSubscriberInterface
{
public function create(GameEvent $event)
{
$this->createOrUpdate($event, new Game());
}
public function update(GameEvent $event)
{
$model = $this->getGame($event);
$this->createOrUpdate($event, $model);
}
public function delete(GameEvent $event)
{
$this->getGame($event)->delete();
}
protected function createOrUpdate(GameEvent $event, Game $model)
{
$con = Propel::getConnection(GameTableMap::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);
}
$model->save($con);
$con->commit();
} catch (\Exception $e) {
$con->rollback();
throw $e;
}
$event->setGame($model);
}
protected function getGame(GameEvent $event)
{
$model = GameQuery::create()->findPk($event->getId());
if (null === $model) {
throw new \RuntimeException(sprintf(
"The 'game' id '%d' doesn't exist",
$event->getId()
));
}
return $model;
}
public function toggleVisibility(ToggleVisibilityEvent $event)
{
$this->genericToggleVisibility(new GameQuery(), $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(
GameEvents::CREATE => array("create", 128),
GameEvents::UPDATE => array("update", 128),
GameEvents::DELETE => array("delete", 128),
GameEvents::TOGGLE_VISIBILITY => array("toggleVisibility", 128),
TheliaEvents::FORM_BEFORE_BUILD . ".game_create" => array("beforeCreateFormBuild", 128),
TheliaEvents::FORM_BEFORE_BUILD . ".game_update" => array("beforeUpdateFormBuild", 128),
TheliaEvents::FORM_AFTER_BUILD . ".game_create" => array("afterCreateFormBuild", 128),
TheliaEvents::FORM_AFTER_BUILD . ".game_update" => array("afterUpdateFormBuild", 128),
);
}
}