<?php 
/** 
 * Copyright Blackbit digital Commerce GmbH <info@blackbit.de> 
 * 
 * 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. 
 * 
 * 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. 
 * 
 * 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 
 */ 
 
namespace Blackbit\DataDirectorBundle\EventListener; 
 
use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder; 
use Blackbit\DataDirectorBundle\model\Dataport; 
use Blackbit\DataDirectorBundle\model\PimcoreDbRepository; 
use Blackbit\DataDirectorBundle\Tools\Installer; 
use Pimcore\Event\Model\DataObject\ClassDefinitionEvent; 
use Pimcore\Model\DataObject\ClassDefinition\Data\Localizedfields; 
use Pimcore\Tool; 
 
class ClassChangedListener 
{ 
    /** @var ItemMoldBuilder */ 
    private $itemMoldBuilder; 
 
    public function __construct(ItemMoldBuilder $itemMoldBuilder) 
    { 
        $this->itemMoldBuilder = $itemMoldBuilder; 
    } 
 
    public function removeCompiledDataQuerySelectors(ClassDefinitionEvent $e) { 
        try { 
            $dummyObject = $this->itemMoldBuilder->getItemMoldByClassname($e->getClassDefinition()->getName()); 
            $classFqn = \get_class($dummyObject); 
        } catch (\Exception $classNotFoundException) { 
            $classFqn = 'Pimcore\\Model\\DataObject\\'.$e->getClassDefinition()->getName(); 
        } 
 
        $directoryIterator = new \DirectoryIterator(Installer::getCachePath()); 
        $fileNamePrefix = preg_replace('/\W+/', '_', $classFqn.' '); 
        $filterIterator = new \CallbackFilterIterator($directoryIterator, static function (\SplFileInfo $fileInfo) use ($fileNamePrefix) { 
            return strpos($fileInfo->getFilename(), $fileNamePrefix) === 0 || strpos($fileInfo->getFilename(), 'Dao_'.$fileNamePrefix) === 0 || strpos($fileInfo->getFilename(), 'Listing_'.$fileNamePrefix) === 0; 
        }); 
        /** @var \SplFileInfo $compiledFileInfo */ 
        foreach ($filterIterator as $compiledFile) { 
            unlink($compiledFile->getPathname()); 
        } 
    } 
 
    public function removeAllCompiledDataQuerySelectors() { 
        /** @var \SplFileInfo $compiledFileInfo */ 
        foreach(new \DirectoryIterator(Installer::getCachePath()) as $compiledFileInfo) { 
            if (!$compiledFileInfo->isDot()) { 
                @unlink($compiledFileInfo->getPathname()); 
            } 
        } 
    } 
 
    public function createStoreView(ClassDefinitionEvent $e) 
    { 
        if ($e->getClassDefinition()->getAllowInherit()) { 
            foreach (Tool::getValidLanguages() as $language) { 
                $hasLocalizedFields = false; 
                foreach($e->getClassDefinition()->getFieldDefinitions() as $fieldDefinition) { 
                    if($fieldDefinition instanceof Localizedfields) { 
                        $hasLocalizedFields = true; 
                        break; 
                    } 
                } 
 
                $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`'; 
                $parameters = []; 
                if($hasLocalizedFields) { 
                    $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=?'; 
                    $parameters[] = $language; 
                } 
 
                PimcoreDbRepository::getInstance()->execute($query, $parameters); 
            } 
        } else { 
            $this->removeStoreView($e); 
        } 
    } 
 
    public function removeStoreView(ClassDefinitionEvent $e) 
    { 
        foreach (Tool::getValidLanguages() as $language) { 
            PimcoreDbRepository::getInstance()->execute('DROP VIEW IF EXISTS `object_localized_store_'.$e->getClassDefinition()->getId().'_'.$language.'`'); 
        } 
    } 
}