vendor/blackbit/data-director/EventListener/ClassChangedListener.php line 94

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright Blackbit digital Commerce GmbH <info@blackbit.de>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8.  *
  9.  * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  10.  */
  11. namespace Blackbit\DataDirectorBundle\EventListener;
  12. use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder;
  13. use Blackbit\DataDirectorBundle\model\Dataport;
  14. use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
  15. use Blackbit\DataDirectorBundle\Tools\Installer;
  16. use Pimcore\Event\Model\DataObject\ClassDefinitionEvent;
  17. use Pimcore\Model\DataObject\ClassDefinition\Data\Localizedfields;
  18. use Pimcore\Model\DataObject\ClassDefinition\PathFormatterAwareInterface;
  19. use Pimcore\Tool;
  20. class ClassChangedListener
  21. {
  22.     /** @var ItemMoldBuilder */
  23.     private $itemMoldBuilder;
  24.     public function __construct(ItemMoldBuilder $itemMoldBuilder)
  25.     {
  26.         $this->itemMoldBuilder $itemMoldBuilder;
  27.     }
  28.     public function removeCompiledDataQuerySelectors(ClassDefinitionEvent $e) {
  29.         try {
  30.             $dummyObject $this->itemMoldBuilder->getItemMoldByClassname($e->getClassDefinition()->getName());
  31.             $classFqn \get_class($dummyObject);
  32.         } catch (\Exception $classNotFoundException) {
  33.             $classFqn 'Pimcore\\Model\\DataObject\\'.$e->getClassDefinition()->getName();
  34.         }
  35.         $directoryIterator = new \DirectoryIterator(Installer::getCachePath());
  36.         $fileNamePrefix preg_replace('/\W+/''_'$classFqn.' ');
  37.         $filterIterator = new \CallbackFilterIterator($directoryIterator, static function (\SplFileInfo $fileInfo) use ($fileNamePrefix) {
  38.             return strpos($fileInfo->getFilename(), $fileNamePrefix) === || strpos($fileInfo->getFilename(), 'Dao_'.$fileNamePrefix) === || strpos($fileInfo->getFilename(), 'Listing_'.$fileNamePrefix) === 0;
  39.         });
  40.         /** @var \SplFileInfo $compiledFileInfo */
  41.         foreach ($filterIterator as $compiledFile) {
  42.             unlink($compiledFile->getPathname());
  43.         }
  44.     }
  45.     public function removeAllCompiledDataQuerySelectors() {
  46.         /** @var \SplFileInfo $compiledFileInfo */
  47.         foreach(new \DirectoryIterator(Installer::getCachePath()) as $compiledFileInfo) {
  48.             if (!$compiledFileInfo->isDot()) {
  49.                 @unlink($compiledFileInfo->getPathname());
  50.             }
  51.         }
  52.     }
  53.     public function createStoreView(ClassDefinitionEvent $e)
  54.     {
  55.         if ($e->getClassDefinition()->getAllowInherit()) {
  56.             foreach (Tool::getValidLanguages() as $language) {
  57.                 $hasLocalizedFields false;
  58.                 foreach($e->getClassDefinition()->getFieldDefinitions() as $fieldDefinition) {
  59.                     if($fieldDefinition instanceof Localizedfields) {
  60.                         $hasLocalizedFields true;
  61.                         break;
  62.                     }
  63.                 }
  64.                 $query 'CREATE OR REPLACE VIEW `object_localized_store_'.$e->getClassDefinition()->getId().'_'.$language.'` AS SELECT * FROM `object_store_'.$e->getClassDefinition()->getId().'` JOIN `objects` ON `objects`.`o_id` = `object_store_'.$e->getClassDefinition()->getId().'`.`oo_id`';
  65.                 $parameters = [];
  66.                 if($hasLocalizedFields) {
  67.                     $query .= ' JOIN `object_localized_data_'.$e->getClassDefinition()->getId().'` ON `object_store_'.$e->getClassDefinition()->getId().'`.oo_id=`object_localized_data_'.$e->getClassDefinition()->getId().'`.ooo_id AND language=?';
  68.                     $parameters[] = $language;
  69.                 }
  70.                 PimcoreDbRepository::getInstance()->execute($query$parameters);
  71.             }
  72.         } else {
  73.             $this->removeStoreView($e);
  74.         }
  75.     }
  76.     public function removeStoreView(ClassDefinitionEvent $e)
  77.     {
  78.         foreach (Tool::getValidLanguages() as $language) {
  79.             PimcoreDbRepository::getInstance()->execute('DROP VIEW IF EXISTS `object_localized_store_'.$e->getClassDefinition()->getId().'_'.$language.'`');
  80.         }
  81.     }
  82.     public function addPreviewService(ClassDefinitionEvent $e)
  83.     {
  84.         $classDefinition $e->getClassDefinition();
  85.         if (method_exists($classDefinition'setPreviewGeneratorReference')) {
  86.             if (!$classDefinition->getPreviewGeneratorReference()) {
  87.                 $classDefinition->setPreviewGeneratorReference('@DataDirectorPreview');
  88.             }
  89.         } elseif (method_exists($classDefinition'setPreviewUrl')) {
  90.             if (!$classDefinition->getPreviewUrl()) {
  91.                 $classDefinition->setPreviewUrl('/admin/BlackbitDataDirector/import/object-preview?id=%o_id');
  92.             }
  93.         }
  94.     }
  95.     public function addPathFormatterService(ClassDefinitionEvent $e)
  96.     {
  97.         $classDefinition $e->getClassDefinition();
  98.         foreach($classDefinition->getFieldDefinitions() as $fieldDefinition) {
  99.             if($fieldDefinition instanceof PathFormatterAwareInterface && !$fieldDefinition->getPathFormatterClass() && method_exists($fieldDefinition'setPathFormatterClass')) {
  100.                 $fieldDefinition->setPathFormatterClass('@DataDirectorSearchViewPathFormatter');
  101.             } elseif($fieldDefinition instanceof Localizedfields) {
  102.                 foreach ($fieldDefinition->getFieldDefinitions() as $localizedFieldDefinition) {
  103.                     if ($localizedFieldDefinition instanceof PathFormatterAwareInterface && !$localizedFieldDefinition->getPathFormatterClass() && method_exists($localizedFieldDefinition'setPathFormatterClass')) {
  104.                         $localizedFieldDefinition->setPathFormatterClass('@DataDirectorSearchViewPathFormatter');
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }