core/lib/Thelia/Action/Category.php line 87

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