src/AssetBundle/EventListener/AssetListener.php line 78

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 Enterprise License (PEL)
  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 PEL
  13.  */
  14. namespace App\AssetBundle\EventListener;
  15. use Pimcore\Event\Model\AssetEvent;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. use Pimcore\Log\ApplicationLogger;
  18. use Pimcore\Model\DataObject\ClassDefinition;
  19. use Pimcore\Model\DataObject;
  20. class AssetListener {
  21.     /**
  22.      * @var Pimcore\Log\ApplicationLogger
  23.      */
  24.     protected $logger NULL;
  25.     /**
  26.      * @var ContainerInterface 
  27.      */
  28.     protected $container NULL;
  29.     /**
  30.      *
  31.      * @var string 
  32.      */
  33.     protected $className NULL;
  34.     /**
  35.      *
  36.      * @var string 
  37.      */
  38.     protected $imageFieldName NULL;
  39.     /**
  40.      *
  41.      * @var string 
  42.      */
  43.     protected $namingRule NULL;
  44.     /**
  45.      * Constructor
  46.      * @param ContainerInterface $container
  47.      * @param Pimcore\Log\ApplicationLogger $logger
  48.      */
  49.     public function __construct(ContainerInterface $containerApplicationLogger $logger) {
  50.         $this->container $container;
  51.         $this->logger $logger;
  52.         $this->className $container->getParameter("productClass");
  53.         $this->imageFieldName $container->getParameter("imageFieldName");
  54.         $this->seperator $container->getParameter("seperator");
  55.         $websiteSettingName $container->getParameter("websiteSettingName");
  56.         $namingRuleData \Pimcore\Model\WebsiteSetting::getByName($websiteSettingName);
  57. //        $this->namingRule = $namingRuleData->getData();
  58. //        $websiteConfig = \Pimcore\Tool\Frontend::getWebsiteConfig();
  59. //        $this->namingRule = $websiteConfig->get($websiteSettingName);
  60.     }
  61.     /**
  62.      * 
  63.      * @param AssetEvent $event
  64.      */
  65.     public function onPostAddAsset(AssetEvent $event) {
  66.         try {
  67.             $asset $event->getAsset();
  68.             $asset->setProperty('active''select''InActive');
  69. //            $websiteConfig = \Pimcore\Tool\Frontend::getWebsiteConfig();
  70. //            $assetSandBoxFolder = $websiteConfig->get("assetSandboxFolder");
  71. //            if ($assetSandBoxFolder instanceof Asset\Folder) {
  72. //                if (strpos($asset->getFullPath(), $assetSandBoxFolder->getFullPath()) !== false) {
  73. //                    $asset->setProperty('active', 'select', 'InActive');
  74. //                }
  75. //            }
  76.             $asset->save();
  77.             if ($asset instanceof \Pimcore\Model\Asset\Image) {
  78.                 if ($this->namingRule && $this->seperator && $this->checkClassExists() && $this->getFieldDefinition($this->imageFieldName)) {
  79.                     $listingClass "\\Pimcore\\Model\\DataObject\\" $this->className "\\Listing";
  80.                     $conditionArray = array();
  81.                     $namingConventionArray explode($this->seperator$this->namingRule);
  82.                     $filename pathinfo($asset->getFullPath(), PATHINFO_FILENAME);
  83.                     $fileNameArray explode($this->seperator$filename);
  84.                     if (count($namingConventionArray) == count($fileNameArray)) {
  85.                         $listing = new $listingClass();
  86.                         $listing->setObjectTypes([DataObject\AbstractObject::OBJECT_TYPE_VARIANTDataObject\AbstractObject::OBJECT_TYPE_OBJECT]);
  87.                         foreach ($fileNameArray as $key => $data) {
  88.                             if ($namingConventionArray[$key] && $namingConventionArray[$key] != "") {
  89.                                 $fielDefinition $this->getFieldDefinition($namingConventionArray[$key]);
  90.                                 if ($fielDefinition instanceof ClassDefinition\Data\ObjectsMetadata || $fielDefinition instanceof ClassDefinition\Data\Objects) {
  91.                                     $conditionArray [] = $namingConventionArray[$key] . " like " $listing->quote("%," $data ",%");
  92.                                 } else {
  93.                                     $conditionArray [] = $namingConventionArray[$key] . " like " $listing->quote($data);
  94.                                 }
  95.                             }
  96.                         }
  97.                         if (count($conditionArray) > 0) {
  98.                             $condition implode(" AND "$conditionArray);
  99.                             $listing->setCondition($condition);
  100.                             $listing->setLimit(1);
  101.                             $objects $listing->load();
  102.                             if (is_array($objects) && count($objects) > 0) {
  103.                                 $object $objects[0];
  104.                                 $setter "set" ucfirst($this->imageFieldName);
  105.                                 $object->$setter($asset);
  106.                                 $object->save();
  107.                             }
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.         } catch (Exception $ex) {
  113.             
  114.         }
  115.     }
  116.     public function getFieldDefinition($fieldName) {
  117.         $classDefinition $this->getClassDefinition();
  118.         if ($classDefinition) {
  119.             $fieldDefinition $classDefinition->getFieldDefinition($fieldName);
  120.             if ($fieldDefinition instanceof ClassDefinition\Data) {
  121.                 return $fieldDefinition;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     /**
  127.      * 
  128.      * @return boolean
  129.      */
  130.     public function checkClassExists() {
  131.         $classDefinition $this->getClassDefinition();
  132.         if ($classDefinition) {
  133.             return true;
  134.         }
  135.         return false;
  136.     }
  137.     /**
  138.      * 
  139.      * @param string $className
  140.      * @return ClassDefinition | boolean
  141.      */
  142.     public function getClassDefinition() {
  143.         $classDefinition ClassDefinition::getByName($this->className);
  144.         if ($classDefinition instanceof ClassDefinition) {
  145.             return $classDefinition;
  146.         }
  147.         return false;
  148.     }
  149. }