core/lib/Thelia/Action/Brand.php line 93

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Thelia\Core\Event\Brand\BrandCreateEvent;
  16. use Thelia\Core\Event\Brand\BrandDeleteEvent;
  17. use Thelia\Core\Event\Brand\BrandToggleVisibilityEvent;
  18. use Thelia\Core\Event\Brand\BrandUpdateEvent;
  19. use Thelia\Core\Event\TheliaEvents;
  20. use Thelia\Core\Event\UpdatePositionEvent;
  21. use Thelia\Core\Event\UpdateSeoEvent;
  22. use Thelia\Core\Event\ViewCheckEvent;
  23. use Thelia\Model\Brand as BrandModel;
  24. use Thelia\Model\BrandQuery;
  25. /**
  26.  * Class Brand.
  27.  *
  28.  * @author  Franck Allimant <franck@cqfdev.fr>
  29.  */
  30. class Brand extends BaseAction implements EventSubscriberInterface
  31. {
  32.     public function create(BrandCreateEvent $event): void
  33.     {
  34.         $brand = new BrandModel();
  35.         $brand
  36.             ->setVisible($event->getVisible())
  37.             ->setLocale($event->getLocale())
  38.             ->setTitle($event->getTitle())
  39.             ->save()
  40.         ;
  41.         $event->setBrand($brand);
  42.     }
  43.     /**
  44.      * process update brand.
  45.      */
  46.     public function update(BrandUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  47.     {
  48.         if (null !== $brand BrandQuery::create()->findPk($event->getBrandId())) {
  49.             $brand
  50.                 ->setVisible($event->getVisible())
  51.                 ->setLogoImageId((int) $event->getLogoImageId() == null $event->getLogoImageId())
  52.                 ->setLocale($event->getLocale())
  53.                 ->setTitle($event->getTitle())
  54.                 ->setDescription($event->getDescription())
  55.                 ->setChapo($event->getChapo())
  56.                 ->setPostscriptum($event->getPostscriptum())
  57.                 ->save()
  58.             ;
  59.             $event->setBrand($brand);
  60.         }
  61.     }
  62.     /**
  63.      * Toggle Brand visibility.
  64.      *
  65.      * @throws \Exception
  66.      * @throws \Propel\Runtime\Exception\PropelException
  67.      */
  68.     public function toggleVisibility(BrandToggleVisibilityEvent $event): void
  69.     {
  70.         $brand $event->getBrand();
  71.         $brand
  72.             ->setVisible(!$brand->getVisible())
  73.             ->save();
  74.         $event->setBrand($brand);
  75.     }
  76.     /**
  77.      * Change Brand SEO.
  78.      *
  79.      * @return object
  80.      */
  81.     public function updateSeo(UpdateSeoEvent $event$eventNameEventDispatcherInterface $dispatcher)
  82.     {
  83.         return $this->genericUpdateSeo(BrandQuery::create(), $event$dispatcher);
  84.     }
  85.     public function delete(BrandDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  86.     {
  87.         if (null !== $brand BrandQuery::create()->findPk($event->getBrandId())) {
  88.             $brand->delete();
  89.             $event->setBrand($brand);
  90.         }
  91.     }
  92.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  93.     {
  94.         $this->genericUpdatePosition(BrandQuery::create(), $event$dispatcher);
  95.     }
  96.     /**
  97.      * Check if is a brand view and if brand_id is visible.
  98.      */
  99.     public function viewCheck(ViewCheckEvent $eventstring $eventNameEventDispatcherInterface $dispatcher): void
  100.     {
  101.         if ($event->getView() == 'brand') {
  102.             $brand BrandQuery::create()
  103.                 ->filterById($event->getViewId())
  104.                 ->filterByVisible(1)
  105.                 ->count();
  106.             if ($brand == 0) {
  107.                 $dispatcher->dispatch($eventTheliaEvents::VIEW_BRAND_ID_NOT_VISIBLE);
  108.             }
  109.         }
  110.     }
  111.     /**
  112.      * @throws NotFoundHttpException
  113.      */
  114.     public function viewBrandIdNotVisible(ViewCheckEvent $event): void
  115.     {
  116.         throw new NotFoundHttpException();
  117.     }
  118.     /**
  119.      * {@inheritdoc}
  120.      */
  121.     public static function getSubscribedEvents()
  122.     {
  123.         return [
  124.             TheliaEvents::BRAND_CREATE => ['create'128],
  125.             TheliaEvents::BRAND_UPDATE => ['update'128],
  126.             TheliaEvents::BRAND_DELETE => ['delete'128],
  127.             TheliaEvents::BRAND_UPDATE_SEO => ['updateSeo'128],
  128.             TheliaEvents::BRAND_UPDATE_POSITION => ['updatePosition'128],
  129.             TheliaEvents::BRAND_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  130.             TheliaEvents::VIEW_CHECK => ['viewCheck'128],
  131.             TheliaEvents::VIEW_BRAND_ID_NOT_VISIBLE => ['viewBrandIdNotVisible'128],
  132.         ];
  133.     }
  134. }