vendor/thelia/core/lib/Thelia/Action/Brand.php line 119

Open in your IDE?
  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.      * @param $eventName
  47.      */
  48.     public function update(BrandUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  49.     {
  50.         if (null !== $brand BrandQuery::create()->findPk($event->getBrandId())) {
  51.             $brand
  52.                 ->setVisible($event->getVisible())
  53.                 ->setLogoImageId((int) ($event->getLogoImageId()) == null $event->getLogoImageId())
  54.                 ->setLocale($event->getLocale())
  55.                 ->setTitle($event->getTitle())
  56.                 ->setDescription($event->getDescription())
  57.                 ->setChapo($event->getChapo())
  58.                 ->setPostscriptum($event->getPostscriptum())
  59.                 ->save()
  60.             ;
  61.             $event->setBrand($brand);
  62.         }
  63.     }
  64.     /**
  65.      * Toggle Brand visibility.
  66.      *
  67.      * @throws \Exception
  68.      * @throws \Propel\Runtime\Exception\PropelException
  69.      */
  70.     public function toggleVisibility(BrandToggleVisibilityEvent $event): void
  71.     {
  72.         $brand $event->getBrand();
  73.         $brand
  74.             ->setVisible(!$brand->getVisible())
  75.             ->save();
  76.         $event->setBrand($brand);
  77.     }
  78.     /**
  79.      * Change Brand SEO.
  80.      *
  81.      * @param $eventName
  82.      *
  83.      * @return object
  84.      */
  85.     public function updateSeo(UpdateSeoEvent $event$eventNameEventDispatcherInterface $dispatcher)
  86.     {
  87.         return $this->genericUpdateSeo(BrandQuery::create(), $event$dispatcher);
  88.     }
  89.     public function delete(BrandDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  90.     {
  91.         if (null !== $brand BrandQuery::create()->findPk($event->getBrandId())) {
  92.             $brand->delete();
  93.             $event->setBrand($brand);
  94.         }
  95.     }
  96.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  97.     {
  98.         $this->genericUpdatePosition(BrandQuery::create(), $event$dispatcher);
  99.     }
  100.     /**
  101.      * Check if is a brand view and if brand_id is visible.
  102.      */
  103.     public function viewCheck(ViewCheckEvent $eventstring $eventNameEventDispatcherInterface $dispatcher): void
  104.     {
  105.         if ($event->getView() == 'brand') {
  106.             $brand BrandQuery::create()
  107.                 ->filterById($event->getViewId())
  108.                 ->filterByVisible(1)
  109.                 ->count();
  110.             if ($brand == 0) {
  111.                 $dispatcher->dispatch($eventTheliaEvents::VIEW_BRAND_ID_NOT_VISIBLE);
  112.             }
  113.         }
  114.     }
  115.     /**
  116.      * @throws NotFoundHttpException
  117.      */
  118.     public function viewBrandIdNotVisible(ViewCheckEvent $event): void
  119.     {
  120.         throw new NotFoundHttpException();
  121.     }
  122.     /**
  123.      * {@inheritdoc}
  124.      */
  125.     public static function getSubscribedEvents()
  126.     {
  127.         return [
  128.             TheliaEvents::BRAND_CREATE => ['create'128],
  129.             TheliaEvents::BRAND_UPDATE => ['update'128],
  130.             TheliaEvents::BRAND_DELETE => ['delete'128],
  131.             TheliaEvents::BRAND_UPDATE_SEO => ['updateSeo'128],
  132.             TheliaEvents::BRAND_UPDATE_POSITION => ['updatePosition'128],
  133.             TheliaEvents::BRAND_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  134.             TheliaEvents::VIEW_CHECK => ['viewCheck'128],
  135.             TheliaEvents::VIEW_BRAND_ID_NOT_VISIBLE => ['viewBrandIdNotVisible'128],
  136.         ];
  137.     }
  138. }