vendor/pimcore/pimcore/bundles/AdminBundle/EventListener/GridConfigListener.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\EventListener;
  15. use Pimcore\Db;
  16. use Pimcore\Event\DataObjectClassDefinitionEvents;
  17. use Pimcore\Event\DataObjectEvents;
  18. use Pimcore\Event\Model\DataObject\ClassDefinitionEvent;
  19. use Pimcore\Event\Model\DataObjectEvent;
  20. use Pimcore\Event\Model\UserRoleEvent;
  21. use Pimcore\Event\UserRoleEvents;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. /**
  24.  * @internal
  25.  */
  26. class GridConfigListener implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             DataObjectClassDefinitionEvents::POST_DELETE => 'onClassDelete',
  35.             UserRoleEvents::POST_DELETE => 'onUserDelete',
  36.             DataObjectEvents::POST_DELETE => 'onObjectDelete',
  37.         ];
  38.     }
  39.     /**
  40.      * @param DataObjectEvent $event
  41.      */
  42.     public function onObjectDelete($event)
  43.     {
  44.         $object $event->getObject();
  45.         $objectId $object->getId();
  46.         $this->cleanupGridConfigFavourites('objectId = ' $objectId);
  47.     }
  48.     /**
  49.      * @param ClassDefinitionEvent $event
  50.      */
  51.     public function onClassDelete($event)
  52.     {
  53.         $class $event->getClassDefinition();
  54.         $classId $class->getId();
  55.         // collect gridConfigs for that class id
  56.         $db Db::get();
  57.         $gridConfigIds $db->fetchCol('select id from gridconfigs where classId = ?'$classId);
  58.         if ($gridConfigIds) {
  59.             $db->query('delete from gridconfig_shares where gridConfigId in (' implode($gridConfigIds) . ')');
  60.         }
  61.         $this->cleanupGridConfigs('classId = ' $db->quote($classId));
  62.         $this->cleanupGridConfigFavourites('classId = ' $db->quote($classId));
  63.     }
  64.     /**
  65.      * @param UserRoleEvent $event
  66.      */
  67.     public function onUserDelete($event)
  68.     {
  69.         $user $event->getUserRole();
  70.         $userId $user->getId();
  71.         $db Db::get();
  72.         $gridConfigIds $db->fetchCol('select id from gridconfigs where ownerId = ' $userId);
  73.         if ($gridConfigIds) {
  74.             $db->query('delete from gridconfig_shares where gridConfigId in (' implode($gridConfigIds) . ')');
  75.         }
  76.         $this->cleanupGridConfigs('ownerId = ' $userId);
  77.         $this->cleanupGridConfigFavourites('ownerId = ' $userId);
  78.     }
  79.     protected function cleanupGridConfigs($condition)
  80.     {
  81.         $db Db::get();
  82.         $db->query('DELETE FROM gridconfigs where ' $condition);
  83.     }
  84.     protected function cleanupGridConfigFavourites($condition)
  85.     {
  86.         $db Db::get();
  87.         $db->query('DELETE FROM gridconfig_favourites where ' $condition);
  88.     }
  89. }