core/lib/Thelia/Action/Content.php line 206

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