core/lib/Thelia/Action/Folder.php line 63

  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\File\FileDeleteEvent;
  17. use Thelia\Core\Event\Folder\FolderCreateEvent;
  18. use Thelia\Core\Event\Folder\FolderDeleteEvent;
  19. use Thelia\Core\Event\Folder\FolderToggleVisibilityEvent;
  20. use Thelia\Core\Event\Folder\FolderUpdateEvent;
  21. use Thelia\Core\Event\TheliaEvents;
  22. use Thelia\Core\Event\UpdatePositionEvent;
  23. use Thelia\Core\Event\UpdateSeoEvent;
  24. use Thelia\Core\Event\ViewCheckEvent;
  25. use Thelia\Model\Folder as FolderModel;
  26. use Thelia\Model\FolderDocumentQuery;
  27. use Thelia\Model\FolderImageQuery;
  28. use Thelia\Model\FolderQuery;
  29. use Thelia\Model\Map\FolderTableMap;
  30. /**
  31.  * Class Folder.
  32.  *
  33.  * @author Manuel Raynaud <manu@raynaud.io>
  34.  */
  35. class Folder extends BaseAction implements EventSubscriberInterface
  36. {
  37.     public function update(FolderUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  38.     {
  39.         if (null !== $folder FolderQuery::create()->findPk($event->getFolderId())) {
  40.             $folder
  41.                 ->setParent($event->getParent())
  42.                 ->setVisible($event->getVisible())
  43.                 ->setLocale($event->getLocale())
  44.                 ->setTitle($event->getTitle())
  45.                 ->setDescription($event->getDescription())
  46.                 ->setChapo($event->getChapo())
  47.                 ->setPostscriptum($event->getPostscriptum())
  48.                 ->save();
  49.             $event->setFolder($folder);
  50.         }
  51.     }
  52.     /**
  53.      * Change Folder SEO.
  54.      *
  55.      * @return object
  56.      */
  57.     public function updateSeo(UpdateSeoEvent $event$eventNameEventDispatcherInterface $dispatcher)
  58.     {
  59.         return $this->genericUpdateSeo(FolderQuery::create(), $event$dispatcher);
  60.     }
  61.     public function delete(FolderDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  62.     {
  63.         if (null !== $folder FolderQuery::create()->findPk($event->getFolderId())) {
  64.             $con Propel::getWriteConnection(FolderTableMap::DATABASE_NAME);
  65.             $con->beginTransaction();
  66.             try {
  67.                 $fileList = ['images' => [], 'documentList' => []];
  68.                 // Get folder's files to delete after folder deletion
  69.                 $fileList['images']['list'] = FolderImageQuery::create()
  70.                     ->findByFolderId($event->getFolderId());
  71.                 $fileList['images']['type'] = TheliaEvents::IMAGE_DELETE;
  72.                 $fileList['documentList']['list'] = FolderDocumentQuery::create()
  73.                     ->findByFolderId($event->getFolderId());
  74.                 $fileList['documentList']['type'] = TheliaEvents::DOCUMENT_DELETE;
  75.                 // Delete folder
  76.                 $folder
  77.                     ->delete($con);
  78.                 $event->setFolder($folder);
  79.                 // Dispatch delete folder's files event
  80.                 foreach ($fileList as $fileTypeList) {
  81.                     foreach ($fileTypeList['list'] as $fileToDelete) {
  82.                         $fileDeleteEvent = new FileDeleteEvent($fileToDelete);
  83.                         $dispatcher->dispatch($fileDeleteEvent$fileTypeList['type']);
  84.                     }
  85.                 }
  86.                 $con->commit();
  87.             } catch (\Exception $e) {
  88.                 $con->rollback();
  89.                 throw $e;
  90.             }
  91.         }
  92.     }
  93.     public function create(FolderCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  94.     {
  95.         $folder = new FolderModel();
  96.         $folder
  97.             ->setParent($event->getParent())
  98.             ->setVisible($event->getVisible())
  99.             ->setLocale($event->getLocale())
  100.             ->setTitle($event->getTitle())
  101.             ->save();
  102.         $event->setFolder($folder);
  103.     }
  104.     public function toggleVisibility(FolderToggleVisibilityEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  105.     {
  106.         $folder $event->getFolder();
  107.         $folder
  108.             ->setVisible(!$folder->getVisible())
  109.             ->save();
  110.         $event->setFolder($folder);
  111.     }
  112.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  113.     {
  114.         if (null !== $folder FolderQuery::create()->findPk($event->getObjectId())) {
  115.             switch ($event->getMode()) {
  116.                 case UpdatePositionEvent::POSITION_ABSOLUTE:
  117.                     $folder->changeAbsolutePosition($event->getPosition());
  118.                     break;
  119.                 case UpdatePositionEvent::POSITION_DOWN:
  120.                     $folder->movePositionDown();
  121.                     break;
  122.                 case UpdatePositionEvent::POSITION_UP:
  123.                     $folder->movePositionUp();
  124.                     break;
  125.             }
  126.         }
  127.     }
  128.     /**
  129.      * Check if is a folder view and if folder_id is visible.
  130.      *
  131.      * @param string $eventName
  132.      */
  133.     public function viewCheck(ViewCheckEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  134.     {
  135.         if ($event->getView() == 'folder') {
  136.             $folder FolderQuery::create()
  137.                 ->filterById($event->getViewId())
  138.                 ->filterByVisible(1)
  139.                 ->count();
  140.             if ($folder == 0) {
  141.                 $dispatcher->dispatch($eventTheliaEvents::VIEW_FOLDER_ID_NOT_VISIBLE);
  142.             }
  143.         }
  144.     }
  145.     /**
  146.      * @throws NotFoundHttpException
  147.      */
  148.     public function viewFolderIdNotVisible(ViewCheckEvent $event): void
  149.     {
  150.         throw new NotFoundHttpException();
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public static function getSubscribedEvents()
  156.     {
  157.         return [
  158.             TheliaEvents::FOLDER_CREATE => ['create'128],
  159.             TheliaEvents::FOLDER_UPDATE => ['update'128],
  160.             TheliaEvents::FOLDER_DELETE => ['delete'128],
  161.             TheliaEvents::FOLDER_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  162.             TheliaEvents::FOLDER_UPDATE_POSITION => ['updatePosition'128],
  163.             TheliaEvents::FOLDER_UPDATE_SEO => ['updateSeo'128],
  164.             TheliaEvents::VIEW_CHECK => ['viewCheck'128],
  165.             TheliaEvents::VIEW_FOLDER_ID_NOT_VISIBLE => ['viewFolderIdNotVisible'128],
  166.         ];
  167.     }
  168. }