local/modules/HookFetes/Actions/AgendaFetes.php line 119

  1. <?php
  2. /*************************************************************************************/
  3. /*      This file is part of the Thelia package.                                     */
  4. /*                                                                                   */
  5. /*      Copyright (c) OpenStudio                                                     */
  6. /*      email : dev@thelia.net                                                       */
  7. /*      web : http://www.thelia.net                                                  */
  8. /*                                                                                   */
  9. /*      For the full copyright and license information, please view the LICENSE.txt  */
  10. /*      file that was distributed with this source code.                             */
  11. /*************************************************************************************/
  12. namespace HookFetes\Actions;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Propel\Runtime\ActiveQuery\ModelCriteria;
  17. use Thelia\Tools\URL;
  18. use Thelia\Core\Event\UpdatePositionEvent;
  19. use Thelia\Action\BaseAction;
  20. use HookFetes\Model\AgendaFetesQuery;
  21. use HookFetes\Model\AgendaFetes as AgendaFetesModel;
  22. use HookFetes\Events\HookFetesEvents;
  23. use HookFetes\Events\HookFetesDeleteEvent;
  24. use HookFetes\Events\HookFetesUpdateEvent;
  25. use HookFetes\Events\HookFetesCreateEvent;
  26. /**
  27.  * Class AgendaFetes
  28.  * @package HookFetes\Actions
  29.  * @author François Carfantan <f.carfantan@orange.fr>
  30.  */
  31. class AgendaFetes  implements EventSubscriberInterface
  32. {
  33.     
  34.     public function create(HookFetesCreateEvent $event$eventNameEventDispatcherInterface $dispatcher)
  35.     {
  36.         $fete = new AgendaFetesModel();
  37.         $maxpos AgendaFetesQuery::create()
  38.             ->select('max_pos')
  39.             ->addAsColumn('max_pos''MAX(position)')
  40.             ->findOne();
  41.         $fete
  42.             ->setTitle($event->getTitle())
  43.             ->setDepartement($event->getDepartement())
  44.             ->setVille($event->getVille())
  45.             ->setDebut($event->getDebut())
  46.             ->setFin($event->getFin())
  47.             ->setLien($event->getLien())
  48.             ->setPosition($maxpos 1)
  49.             ->save();
  50.         $event->setHookFetes($fete);
  51.     }
  52.     /**
  53.      * Change a product feature
  54.      *
  55.      * @param \Thelia\Core\Event\Feature\HookFetesUpdateEvent $event
  56.      * @param $eventName
  57.      * @param EventDispatcherInterface $dispatcher
  58.      */
  59.     public function update(HookFetesUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher)
  60.     {
  61.         if (null !== $fete AgendaFetesQuery::create()->findPk($event->getFeteId())) {
  62.             $fete
  63.                 ->setTitle($event->getTitle())
  64.                 ->setDepartement($event->getDepartement())
  65.                 ->setVille($event->getVille())
  66.                 ->setDebut($event->getDebut())
  67.                 ->setFin($event->getFin())
  68.                 ->setLien($event->getLien())
  69.                 ->save();
  70.             $event->setHookFetes($fete);
  71.         }
  72.     }
  73.     /**
  74.      * Delete a product feature entry
  75.      *
  76.      * @param HookFetesDeleteEvent $event
  77.      * @param $eventName
  78.      * @param EventDispatcherInterface $dispatcher
  79.      */
  80.     public function delete(HookFetesDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher)
  81.     {
  82.         if (null !== ($fete AgendaFetesQuery::create()->findPk($event->getFeteId()))) {
  83.             $fetes_suivantes AgendaFetesQuery::create()
  84.                 ->filterByPosition(array('min' => $fete->getPosition() + 1))
  85.                 ->find();
  86.             $fete
  87.                 ->delete();
  88.             foreach ($fetes_suivantes as $fete_suivante) {
  89.                 $fete_suivante->setPosition($fete_suivante->getPosition() - 1);
  90.                 $fete_suivante->save();
  91.             }
  92.             $event->setHookFetes($fete);
  93.         }
  94.         return $this->redirectToConfigurationPage();
  95.     }
  96.     /**
  97.      * Changes position, selecting absolute ou relative change.
  98.      *
  99.      * @param UpdatePositionEvent $event
  100.      * @param $eventName
  101.      * @param EventDispatcherInterface $dispatcher
  102.      */
  103.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher)
  104.     {
  105.          $this->genericUpdatePosition(AgendaFetesQuery::create(), $event$dispatcher);
  106.     }
  107.     public function genericUpdatePosition(ModelCriteria $queryUpdatePositionEvent $eventEventDispatcherInterface $dispatcher null)
  108.     {
  109.          if (null !== $object $query->findPk($event->getObjectId())) {
  110.             if (!isset(class_uses($object)['Thelia\Model\Tools\PositionManagementTrait'])) {
  111.                 throw new \InvalidArgumentException("Your model does not implement the PositionManagementTrait trait");
  112.             }
  113.             $mode $event->getMode();
  114.             if ($mode == UpdatePositionEvent::POSITION_ABSOLUTE) {
  115.                 $object->changeAbsolutePosition($event->getPosition());
  116.             } elseif ($mode == UpdatePositionEvent::POSITION_UP) {
  117.                 $object->movePositionUp();
  118.             } elseif ($mode == UpdatePositionEvent::POSITION_DOWN) {
  119.                 $object->movePositionDown();
  120.             }
  121.         }
  122.     }
  123.     /**
  124.      * {@inheritDoc}
  125.      */
  126.     public static function getSubscribedEvents()
  127.     {
  128.         return array(
  129.             HookFetesEvents::FETES_UPDATE_POSITION => array("updatePosition"128),
  130.             HookFetesEvents::FETES_CREATE => array("create"128),
  131.             HookFetesEvents::FETES_DELETE => array("delete"128),
  132.             HookFetesEvents::FETES_UPDATE => array("update"128)
  133.         );
  134.     }
  135.     protected function redirectToConfigurationPage()
  136.     {
  137.         return new RedirectResponse(URL::getInstance()->absoluteUrl('/admin/module/HookFetes'));
  138.     }
  139. }