<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\AssetBundle\EventListener;
use Pimcore\Event\Model\AssetEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject;
class AssetListener {
/**
* @var Pimcore\Log\ApplicationLogger
*/
protected $logger = NULL;
/**
* @var ContainerInterface
*/
protected $container = NULL;
/**
*
* @var string
*/
protected $className = NULL;
/**
*
* @var string
*/
protected $imageFieldName = NULL;
/**
*
* @var string
*/
protected $namingRule = NULL;
/**
* Constructor
* @param ContainerInterface $container
* @param Pimcore\Log\ApplicationLogger $logger
*/
public function __construct(ContainerInterface $container, ApplicationLogger $logger) {
$this->container = $container;
$this->logger = $logger;
$this->className = $container->getParameter("productClass");
$this->imageFieldName = $container->getParameter("imageFieldName");
$this->seperator = $container->getParameter("seperator");
$websiteSettingName = $container->getParameter("websiteSettingName");
$namingRuleData = \Pimcore\Model\WebsiteSetting::getByName($websiteSettingName);
// $this->namingRule = $namingRuleData->getData();
// $websiteConfig = \Pimcore\Tool\Frontend::getWebsiteConfig();
// $this->namingRule = $websiteConfig->get($websiteSettingName);
}
/**
*
* @param AssetEvent $event
*/
public function onPostAddAsset(AssetEvent $event) {
try {
$asset = $event->getAsset();
$asset->setProperty('active', 'select', 'InActive');
// $websiteConfig = \Pimcore\Tool\Frontend::getWebsiteConfig();
// $assetSandBoxFolder = $websiteConfig->get("assetSandboxFolder");
// if ($assetSandBoxFolder instanceof Asset\Folder) {
// if (strpos($asset->getFullPath(), $assetSandBoxFolder->getFullPath()) !== false) {
// $asset->setProperty('active', 'select', 'InActive');
// }
// }
$asset->save();
if ($asset instanceof \Pimcore\Model\Asset\Image) {
if ($this->namingRule && $this->seperator && $this->checkClassExists() && $this->getFieldDefinition($this->imageFieldName)) {
$listingClass = "\\Pimcore\\Model\\DataObject\\" . $this->className . "\\Listing";
$conditionArray = array();
$namingConventionArray = explode($this->seperator, $this->namingRule);
$filename = pathinfo($asset->getFullPath(), PATHINFO_FILENAME);
$fileNameArray = explode($this->seperator, $filename);
if (count($namingConventionArray) == count($fileNameArray)) {
$listing = new $listingClass();
$listing->setObjectTypes([DataObject\AbstractObject::OBJECT_TYPE_VARIANT, DataObject\AbstractObject::OBJECT_TYPE_OBJECT]);
foreach ($fileNameArray as $key => $data) {
if ($namingConventionArray[$key] && $namingConventionArray[$key] != "") {
$fielDefinition = $this->getFieldDefinition($namingConventionArray[$key]);
if ($fielDefinition instanceof ClassDefinition\Data\ObjectsMetadata || $fielDefinition instanceof ClassDefinition\Data\Objects) {
$conditionArray [] = $namingConventionArray[$key] . " like " . $listing->quote("%," . $data . ",%");
} else {
$conditionArray [] = $namingConventionArray[$key] . " like " . $listing->quote($data);
}
}
}
if (count($conditionArray) > 0) {
$condition = implode(" AND ", $conditionArray);
$listing->setCondition($condition);
$listing->setLimit(1);
$objects = $listing->load();
if (is_array($objects) && count($objects) > 0) {
$object = $objects[0];
$setter = "set" . ucfirst($this->imageFieldName);
$object->$setter($asset);
$object->save();
}
}
}
}
}
} catch (Exception $ex) {
}
}
public function getFieldDefinition($fieldName) {
$classDefinition = $this->getClassDefinition();
if ($classDefinition) {
$fieldDefinition = $classDefinition->getFieldDefinition($fieldName);
if ($fieldDefinition instanceof ClassDefinition\Data) {
return $fieldDefinition;
}
}
return false;
}
/**
*
* @return boolean
*/
public function checkClassExists() {
$classDefinition = $this->getClassDefinition();
if ($classDefinition) {
return true;
}
return false;
}
/**
*
* @param string $className
* @return ClassDefinition | boolean
*/
public function getClassDefinition() {
$classDefinition = ClassDefinition::getByName($this->className);
if ($classDefinition instanceof ClassDefinition) {
return $classDefinition;
}
return false;
}
}