vendor/thelia/core/lib/Thelia/Action/Content.php line 129

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\Exception\PropelException;
  13. use Propel\Runtime\Propel;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Thelia\Core\Event\Content\ContentAddFolderEvent;
  18. use Thelia\Core\Event\Content\ContentCreateEvent;
  19. use Thelia\Core\Event\Content\ContentDeleteEvent;
  20. use Thelia\Core\Event\Content\ContentRemoveFolderEvent;
  21. use Thelia\Core\Event\Content\ContentToggleVisibilityEvent;
  22. use Thelia\Core\Event\Content\ContentUpdateEvent;
  23. use Thelia\Core\Event\File\FileDeleteEvent;
  24. use Thelia\Core\Event\TheliaEvents;
  25. use Thelia\Core\Event\UpdatePositionEvent;
  26. use Thelia\Core\Event\UpdateSeoEvent;
  27. use Thelia\Core\Event\ViewCheckEvent;
  28. use Thelia\Model\Content as ContentModel;
  29. use Thelia\Model\ContentDocumentQuery;
  30. use Thelia\Model\ContentFolder;
  31. use Thelia\Model\ContentFolderQuery;
  32. use Thelia\Model\ContentImageQuery;
  33. use Thelia\Model\ContentQuery;
  34. use Thelia\Model\Map\ContentTableMap;
  35. /**
  36.  * Class Content.
  37.  *
  38.  * @author manuel raynaud <manu@raynaud.io>
  39.  */
  40. class Content extends BaseAction implements EventSubscriberInterface
  41. {
  42.     public function create(ContentCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  43.     {
  44.         $content = (new ContentModel())
  45.             ->setVisible($event->getVisible())
  46.             ->setLocale($event->getLocale())
  47.             ->setTitle($event->getTitle())
  48.             ->create($event->getDefaultFolder())
  49.         ;
  50.         $event->setContent($content);
  51.     }
  52.     /**
  53.      * process update content.
  54.      *
  55.      * @param $eventName
  56.      *
  57.      * @throws PropelException
  58.      * @throws \Exception
  59.      */
  60.     public function update(ContentUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  61.     {
  62.         if (null !== $content ContentQuery::create()->findPk($event->getContentId())) {
  63.             $con Propel::getWriteConnection(ContentTableMap::DATABASE_NAME);
  64.             $con->beginTransaction();
  65.             try {
  66.                 $content
  67.                     ->setVisible($event->getVisible())
  68.                     ->setLocale($event->getLocale())
  69.                     ->setTitle($event->getTitle())
  70.                     ->setDescription($event->getDescription())
  71.                     ->setChapo($event->getChapo())
  72.                     ->setPostscriptum($event->getPostscriptum())
  73.                     ->save($con)
  74.                 ;
  75.                 $content->setDefaultFolder($event->getDefaultFolder());
  76.                 $event->setContent($content);
  77.                 $con->commit();
  78.             } catch (PropelException $e) {
  79.                 $con->rollBack();
  80.                 throw $e;
  81.             }
  82.         }
  83.     }
  84.     /**
  85.      * Change Content SEO.
  86.      *
  87.      * @param $eventName
  88.      *
  89.      * @return object
  90.      */
  91.     public function updateSeo(UpdateSeoEvent $event$eventNameEventDispatcherInterface $dispatcher)
  92.     {
  93.         return $this->genericUpdateSeo(ContentQuery::create(), $event$dispatcher);
  94.     }
  95.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  96.     {
  97.         $this->genericUpdateDelegatePosition(
  98.             ContentFolderQuery::create()
  99.                 ->filterByContentId($event->getObjectId())
  100.                 ->filterByFolderId($event->getReferrerId()),
  101.             $event,
  102.             $dispatcher
  103.         );
  104.     }
  105.     public function toggleVisibility(ContentToggleVisibilityEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  106.     {
  107.         $content $event->getContent();
  108.         $content
  109.             ->setVisible(!$content->getVisible())
  110.             ->save();
  111.         $event->setContent($content);
  112.     }
  113.     public function delete(ContentDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  114.     {
  115.         if (null !== $content ContentQuery::create()->findPk($event->getContentId())) {
  116.             $con Propel::getWriteConnection(ContentTableMap::DATABASE_NAME);
  117.             $con->beginTransaction();
  118.             try {
  119.                 $fileList = ['images' => [], 'documentList' => []];
  120.                 $defaultFolderId $content->getDefaultFolderId();
  121.                 // Get content's files to delete after content deletion
  122.                 $fileList['images']['list'] = ContentImageQuery::create()
  123.                     ->findByContentId($event->getContentId());
  124.                 $fileList['images']['type'] = TheliaEvents::IMAGE_DELETE;
  125.                 $fileList['documentList']['list'] = ContentDocumentQuery::create()
  126.                     ->findByContentId($event->getContentId());
  127.                 $fileList['documentList']['type'] = TheliaEvents::DOCUMENT_DELETE;
  128.                 // Delete content
  129.                 $content
  130.                     ->delete($con);
  131.                 $event->setDefaultFolderId($defaultFolderId);
  132.                 $event->setContent($content);
  133.                 // Dispatch delete content's files event
  134.                 foreach ($fileList as $fileTypeList) {
  135.                     foreach ($fileTypeList['list'] as $fileToDelete) {
  136.                         $fileDeleteEvent = new FileDeleteEvent($fileToDelete);
  137.                         $dispatcher->dispatch($fileDeleteEvent$fileTypeList['type']);
  138.                     }
  139.                 }
  140.                 $con->commit();
  141.             } catch (\Exception $e) {
  142.                 $con->rollback();
  143.                 throw $e;
  144.             }
  145.         }
  146.     }
  147.     /**
  148.      * associate a folder to a content if the association already does not exists.
  149.      */
  150.     public function addFolder(ContentAddFolderEvent $event): void
  151.     {
  152.         if (ContentFolderQuery::create()
  153.             ->filterByContent($event->getContent())
  154.             ->filterByFolderId($event->getFolderId())
  155.             ->count() <= 0
  156.         ) {
  157.             $contentFolder = (new ContentFolder())
  158.                 ->setFolderId($event->getFolderId())
  159.                 ->setContent($event->getContent())
  160.                 ->setDefaultFolder(false);
  161.             $contentFolder
  162.                 ->setPosition($contentFolder->getNextPosition())
  163.                 ->save();
  164.         }
  165.     }
  166.     public function removeFolder(ContentRemoveFolderEvent $event): void
  167.     {
  168.         $contentFolder ContentFolderQuery::create()
  169.             ->filterByContent($event->getContent())
  170.             ->filterByFolderId($event->getFolderId())
  171.             ->findOne();
  172.         if (null !== $contentFolder) {
  173.             $contentFolder->delete();
  174.         }
  175.     }
  176.     /**
  177.      * Check if is a content view and if content_id is visible.
  178.      *
  179.      * @param string $eventName
  180.      */
  181.     public function viewCheck(ViewCheckEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  182.     {
  183.         if ($event->getView() == 'content') {
  184.             $content ContentQuery::create()
  185.                 ->filterById($event->getViewId())
  186.                 ->filterByVisible(1)
  187.                 ->count();
  188.             if ($content == 0) {
  189.                 $dispatcher->dispatch($eventTheliaEvents::VIEW_CONTENT_ID_NOT_VISIBLE);
  190.             }
  191.         }
  192.     }
  193.     /**
  194.      * @throws NotFoundHttpException
  195.      */
  196.     public function viewContentIdNotVisible(ViewCheckEvent $event): void
  197.     {
  198.         throw new NotFoundHttpException();
  199.     }
  200.     /**
  201.      * {@inheritdoc}
  202.      */
  203.     public static function getSubscribedEvents()
  204.     {
  205.         return [
  206.             TheliaEvents::CONTENT_CREATE => ['create'128],
  207.             TheliaEvents::CONTENT_UPDATE => ['update'128],
  208.             TheliaEvents::CONTENT_DELETE => ['delete'128],
  209.             TheliaEvents::CONTENT_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  210.             TheliaEvents::CONTENT_UPDATE_POSITION => ['updatePosition'128],
  211.             TheliaEvents::CONTENT_UPDATE_SEO => ['updateSeo'128],
  212.             TheliaEvents::CONTENT_ADD_FOLDER => ['addFolder'128],
  213.             TheliaEvents::CONTENT_REMOVE_FOLDER => ['removeFolder'128],
  214.             TheliaEvents::VIEW_CHECK => ['viewCheck'128],
  215.             TheliaEvents::VIEW_CONTENT_ID_NOT_VISIBLE => ['viewContentIdNotVisible'128],
  216.         ];
  217.     }
  218. }