local/modules/HookFetes/Actions/AgendaFetes.php line 93
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace HookFetes\Actions;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Thelia\Tools\URL;
use Thelia\Core\Event\UpdatePositionEvent;
use Thelia\Action\BaseAction;
use HookFetes\Model\AgendaFetesQuery;
use HookFetes\Model\AgendaFetes as AgendaFetesModel;
use HookFetes\Events\HookFetesEvents;
use HookFetes\Events\HookFetesDeleteEvent;
use HookFetes\Events\HookFetesUpdateEvent;
use HookFetes\Events\HookFetesCreateEvent;
/**
* Class AgendaFetes
* @package HookFetes\Actions
* @author François Carfantan <f.carfantan@orange.fr>
*/
class AgendaFetes implements EventSubscriberInterface
{
public function create(HookFetesCreateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$fete = new AgendaFetesModel();
$maxpos = AgendaFetesQuery::create()
->select('max_pos')
->addAsColumn('max_pos', 'MAX(position)')
->findOne();
$fete
->setTitle($event->getTitle())
->setDepartement($event->getDepartement())
->setVille($event->getVille())
->setDebut($event->getDebut())
->setFin($event->getFin())
->setLien($event->getLien())
->setPosition($maxpos + 1)
->save();
$event->setHookFetes($fete);
}
/**
* Change a product feature
*
* @param \Thelia\Core\Event\Feature\HookFetesUpdateEvent $event
* @param $eventName
* @param EventDispatcherInterface $dispatcher
*/
public function update(HookFetesUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
if (null !== $fete = AgendaFetesQuery::create()->findPk($event->getFeteId())) {
$fete
->setTitle($event->getTitle())
->setDepartement($event->getDepartement())
->setVille($event->getVille())
->setDebut($event->getDebut())
->setFin($event->getFin())
->setLien($event->getLien())
->save();
$event->setHookFetes($fete);
}
}
/**
* Delete a product feature entry
*
* @param HookFetesDeleteEvent $event
* @param $eventName
* @param EventDispatcherInterface $dispatcher
*/
public function delete(HookFetesDeleteEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
if (null !== ($fete = AgendaFetesQuery::create()->findPk($event->getFeteId()))) {
$fetes_suivantes = AgendaFetesQuery::create()
->filterByPosition(array('min' => $fete->getPosition() + 1))
->find();
$fete
->delete();
foreach ($fetes_suivantes as $fete_suivante) {
$fete_suivante->setPosition($fete_suivante->getPosition() - 1);
$fete_suivante->save();
}
$event->setHookFetes($fete);
}
return $this->redirectToConfigurationPage();
}
/**
* Changes position, selecting absolute ou relative change.
*
* @param UpdatePositionEvent $event
* @param $eventName
* @param EventDispatcherInterface $dispatcher
*/
public function updatePosition(UpdatePositionEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$this->genericUpdatePosition(AgendaFetesQuery::create(), $event, $dispatcher);
}
public function genericUpdatePosition(ModelCriteria $query, UpdatePositionEvent $event, EventDispatcherInterface $dispatcher = null)
{
if (null !== $object = $query->findPk($event->getObjectId())) {
if (!isset(class_uses($object)['Thelia\Model\Tools\PositionManagementTrait'])) {
throw new \InvalidArgumentException("Your model does not implement the PositionManagementTrait trait");
}
$mode = $event->getMode();
if ($mode == UpdatePositionEvent::POSITION_ABSOLUTE) {
$object->changeAbsolutePosition($event->getPosition());
} elseif ($mode == UpdatePositionEvent::POSITION_UP) {
$object->movePositionUp();
} elseif ($mode == UpdatePositionEvent::POSITION_DOWN) {
$object->movePositionDown();
}
}
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
HookFetesEvents::FETES_UPDATE_POSITION => array("updatePosition", 128),
HookFetesEvents::FETES_CREATE => array("create", 128),
HookFetesEvents::FETES_DELETE => array("delete", 128),
HookFetesEvents::FETES_UPDATE => array("update", 128)
);
}
protected function redirectToConfigurationPage()
{
return new RedirectResponse(URL::getInstance()->absoluteUrl('/admin/module/HookFetes'));
}
}