vendor/thelia/core/lib/Thelia/Action/Category.php line 203

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 Propel\Runtime\Propel;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Thelia\Core\Event\Category\CategoryAddContentEvent;
  17. use Thelia\Core\Event\Category\CategoryCreateEvent;
  18. use Thelia\Core\Event\Category\CategoryDeleteContentEvent;
  19. use Thelia\Core\Event\Category\CategoryDeleteEvent;
  20. use Thelia\Core\Event\Category\CategoryToggleVisibilityEvent;
  21. use Thelia\Core\Event\Category\CategoryUpdateEvent;
  22. use Thelia\Core\Event\File\FileDeleteEvent;
  23. use Thelia\Core\Event\TheliaEvents;
  24. use Thelia\Core\Event\UpdatePositionEvent;
  25. use Thelia\Core\Event\UpdateSeoEvent;
  26. use Thelia\Core\Event\ViewCheckEvent;
  27. use Thelia\Model\Category as CategoryModel;
  28. use Thelia\Model\CategoryAssociatedContent;
  29. use Thelia\Model\CategoryAssociatedContentQuery;
  30. use Thelia\Model\CategoryDocumentQuery;
  31. use Thelia\Model\CategoryImageQuery;
  32. use Thelia\Model\CategoryQuery;
  33. use Thelia\Model\Map\CategoryTableMap;
  34. class Category extends BaseAction implements EventSubscriberInterface
  35. {
  36.     /**
  37.      * Create a new category entry.
  38.      *
  39.      * @param $eventName
  40.      */
  41.     public function create(CategoryCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  42.     {
  43.         $category = new CategoryModel();
  44.         $category
  45.             ->setLocale($event->getLocale())
  46.             ->setParent($event->getParent())
  47.             ->setVisible($event->getVisible())
  48.             ->setTitle($event->getTitle())
  49.             ->save()
  50.         ;
  51.         $event->setCategory($category);
  52.     }
  53.     /**
  54.      * Change a category.
  55.      *
  56.      * @param $eventName
  57.      */
  58.     public function update(CategoryUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  59.     {
  60.         if (null !== $category CategoryQuery::create()->findPk($event->getCategoryId())) {
  61.             $category
  62.                 ->setDefaultTemplateId($event->getDefaultTemplateId() == null $event->getDefaultTemplateId())
  63.                 ->setLocale($event->getLocale())
  64.                 ->setTitle($event->getTitle())
  65.                 ->setDescription($event->getDescription())
  66.                 ->setChapo($event->getChapo())
  67.                 ->setPostscriptum($event->getPostscriptum())
  68.                 ->setParent($event->getParent())
  69.                 ->setVisible($event->getVisible())
  70.                 ->save();
  71.             $event->setCategory($category);
  72.         }
  73.     }
  74.     /**
  75.      * Change a Category SEO.
  76.      *
  77.      * @param $eventName
  78.      *
  79.      * @return object
  80.      */
  81.     public function updateSeo(UpdateSeoEvent $event$eventNameEventDispatcherInterface $dispatcher)
  82.     {
  83.         return $this->genericUpdateSeo(CategoryQuery::create(), $event$dispatcher);
  84.     }
  85.     /**
  86.      * Delete a category entry.
  87.      *
  88.      * @param $eventName
  89.      *
  90.      * @throws \Exception
  91.      */
  92.     public function delete(CategoryDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  93.     {
  94.         if (null !== $category CategoryQuery::create()->findPk($event->getCategoryId())) {
  95.             $con Propel::getWriteConnection(CategoryTableMap::DATABASE_NAME);
  96.             $con->beginTransaction();
  97.             try {
  98.                 $fileList = ['images' => [], 'documentList' => []];
  99.                 // Get category's files to delete after category deletion
  100.                 $fileList['images']['list'] = CategoryImageQuery::create()
  101.                     ->findByCategoryId($event->getCategoryId());
  102.                 $fileList['images']['type'] = TheliaEvents::IMAGE_DELETE;
  103.                 $fileList['documentList']['list'] = CategoryDocumentQuery::create()
  104.                     ->findByCategoryId($event->getCategoryId());
  105.                 $fileList['documentList']['type'] = TheliaEvents::DOCUMENT_DELETE;
  106.                 // Delete category
  107.                 $category
  108.                     ->delete($con);
  109.                 $event->setCategory($category);
  110.                 // Dispatch delete category's files event
  111.                 foreach ($fileList as $fileTypeList) {
  112.                     foreach ($fileTypeList['list'] as $fileToDelete) {
  113.                         $fileDeleteEvent = new FileDeleteEvent($fileToDelete);
  114.                         $dispatcher->dispatch($fileDeleteEvent$fileTypeList['type']);
  115.                     }
  116.                 }
  117.                 $con->commit();
  118.             } catch (\Exception $e) {
  119.                 $con->rollback();
  120.                 throw $e;
  121.             }
  122.         }
  123.     }
  124.     /**
  125.      * Toggle category visibility. No form used here.
  126.      *
  127.      * @param $eventName
  128.      */
  129.     public function toggleVisibility(CategoryToggleVisibilityEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  130.     {
  131.         $category $event->getCategory();
  132.         $category
  133.             ->setVisible($category->getVisible() ? false true)
  134.             ->save()
  135.             ;
  136.         $event->setCategory($category);
  137.     }
  138.     /**
  139.      * Changes position, selecting absolute ou relative change.
  140.      *
  141.      * @param $eventName
  142.      */
  143.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  144.     {
  145.         $this->genericUpdatePosition(CategoryQuery::create(), $event$dispatcher);
  146.     }
  147.     public function addContent(CategoryAddContentEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  148.     {
  149.         if (CategoryAssociatedContentQuery::create()
  150.             ->filterByContentId($event->getContentId())
  151.              ->filterByCategory($event->getCategory())->count() <= 0) {
  152.             $content = new CategoryAssociatedContent();
  153.             $content
  154.                 ->setCategory($event->getCategory())
  155.                 ->setContentId($event->getContentId())
  156.                 ->save()
  157.             ;
  158.         }
  159.     }
  160.     public function removeContent(CategoryDeleteContentEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  161.     {
  162.         $content CategoryAssociatedContentQuery::create()
  163.             ->filterByContentId($event->getContentId())
  164.             ->filterByCategory($event->getCategory())->findOne()
  165.         ;
  166.         if ($content !== null) {
  167.             $content
  168.                 ->delete();
  169.         }
  170.     }
  171.     /**
  172.      * Check if is a category view and if category_id is visible.
  173.      */
  174.     public function viewCheck(ViewCheckEvent $eventstring $eventNameEventDispatcherInterface $dispatcher): void
  175.     {
  176.         if ($event->getView() == 'category') {
  177.             $category CategoryQuery::create()
  178.                 ->filterById($event->getViewId())
  179.                 ->filterByVisible(1)
  180.                 ->count();
  181.             if ($category == 0) {
  182.                 $dispatcher->dispatch($eventTheliaEvents::VIEW_CATEGORY_ID_NOT_VISIBLE);
  183.             }
  184.         }
  185.     }
  186.     /**
  187.      * @throws NotFoundHttpException
  188.      */
  189.     public function viewcategoryIdNotVisible(ViewCheckEvent $event): void
  190.     {
  191.         throw new NotFoundHttpException();
  192.     }
  193.     /**
  194.      * {@inheritDoc}
  195.      */
  196.     public static function getSubscribedEvents()
  197.     {
  198.         return [
  199.             TheliaEvents::CATEGORY_CREATE => ['create'128],
  200.             TheliaEvents::CATEGORY_UPDATE => ['update'128],
  201.             TheliaEvents::CATEGORY_DELETE => ['delete'128],
  202.             TheliaEvents::CATEGORY_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  203.             TheliaEvents::CATEGORY_UPDATE_POSITION => ['updatePosition'128],
  204.             TheliaEvents::CATEGORY_UPDATE_SEO => ['updateSeo'128],
  205.             TheliaEvents::CATEGORY_ADD_CONTENT => ['addContent'128],
  206.             TheliaEvents::CATEGORY_REMOVE_CONTENT => ['removeContent'128],
  207.             TheliaEvents::VIEW_CHECK => ['viewCheck'128],
  208.             TheliaEvents::VIEW_CATEGORY_ID_NOT_VISIBLE => ['viewcategoryIdNotVisible'128],
  209.         ];
  210.     }
  211. }