<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace IndabaExtensionBundle\EventListener;
use Symfony\Component\EventDispatcher\GenericEvent;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject;
/**
* Description of DataObjectEventListener
*
* @author PcCom
*/
/**
* object in array $objects
* [0] => Array
* (
* [id] => 13010
* [idx] => 0
* [key] => 0021-05
* [sortBy] => key
* [sortOrder] => ASC
* [text] => 0021-05
* [type] => variant
* [path] => /genebre/products/genebre/000000-000999/0000/0021/0021-05
* [basePath] => /genebre/products/genebre/000000-000999/0000/0021/
* [elementType] => object
* [locked] =>
* [lockOwner] =>
* [isTarget] => 1
* [allowDrop] =>
* [allowChildren] => 1
* [leaf] =>
* [cls] => pimcore_class_icon
* [published] => 1
* [className] => Product
* [allowVariants] => 1
* [icon] => /bundles/app/icons/valve-variant.svg
* [iconCls] =>
* [qtipCfg] => Array
* (
* [title] => ID: 13010
* [text] => Type: Product
* )
*
* [expanded] => 1
* [permissions] => Array
* (
* [save] => 1
* [unpublish] => 1
* [lEdit] => 1
* [lView] => 1
* [layouts] => 1
* [list] => 1
* [view] => 1
* [publish] => 1
* [delete] => 1
* [rename] => 1
* [create] => 1
* [settings] => 1
* [versions] => 1
* [properties] => 1
* )
*
* [expandable] =>
* [loaded] => 1
* )
*/
class DataObjectEventListener
{
public function onResolveElementData(GenericEvent $event)
{
$objects = $event->getArgument('objects');
foreach ($objects as $key => $object) {
$objects[$key] = $this->setNodeConfig($object);
}
$event->setArgument('objects', $objects);
}
public function onPostAdd(DataObjectEvent $event)
{
$obj = $event->getObject();
$objectbrickConfig = \Pimcore::getContainer()->getParameter("indabaExtension.dataObject.objectbricks");
if (\is_array($objectbrickConfig) && \method_exists($obj, "getClassname") && \array_key_exists($obj->getClassname(), $objectbrickConfig)) {
foreach($objectbrickConfig[$obj->getClassname()] as $obField) {
if ($obField["addIfCreate"]) {
$field = $obField["field"];
$this->addObjectbricks($obj->getId(), $field );
}
}
}
}
protected function addObjectbricks(int $objId, string $field)
{
$obj = DataObject::getById($objId);
if ($obj) {
$allowedString = $obj->getProperty("allowedObjectbricks");
$allowedList = array();
if (!empty($allowedString)) {
$allowedList = explode(",", str_replace(" ", "", $allowedString));
}
$getter = "get" . ucfirst($field);
if (method_exists($obj, $getter)) {
foreach ($allowedList as $class) {
$classname = "\\Pimcore\\Model\\DataObject\\Objectbrick\\Data\\" . $class;
if (class_exists($classname)) {
$objectbricks = new $classname($obj);
$objectbricks->setFieldname($field);
$objectbricks->save($obj);
$setter = "set" . $class;
$obj->$getter()->$setter($objectbricks);
}
}
$obj->setOmitMandatoryCheck(true);
$obj->save();
}
}
}
/**
* Arguments:
* - objectData | array | contains the export data of the object
* - context | array | context information - default ['source' => 'pimcore-export']
* - requestedLanguage | string | requested language
* - helperDefinitions | array | containing the column definition from the grid view
* - localeService | \Pimcore\Localization\LocaleService
* - returnMappedFieldNames | bool | if "true" the objectData is an associative array, otherwise it is an indexed array
*
* @Event("Pimcore\Event\Model\DataObjectEvent")
*/
public function onPostCsvItemExport(DataObjectEvent $event)
{
$object = $event->getObject();
if ($event->getArgument('returnMappedFieldNames')) {
$objectData = $this->correctCsvItem($object, $event->getArgument('objectData'));
$event->setArgument('objectData', $objectData);
}
// \Pimcore\Log\Simple::log("DataObjectEventListener", "ObjectId: " . $object->getId());
// \Pimcore\Log\Simple::log("DataObjectEventListener", "ClassName: " . $object->getClassName());
// \Pimcore\Log\Simple::log("DataObjectEventListener", "**************** objectData ******************");
// \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('objectData'), true));
// \Pimcore\Log\Simple::log("DataObjectEventListener", "**************** context ******************");
// \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('context'), true));
// \Pimcore\Log\Simple::log("DataObjectEventListener", "RequestedLanguage: " . $event->getArgument('requestedLanguage'));
// \Pimcore\Log\Simple::log("DataObjectEventListener", "**************** helperDefinitions ******************");
// \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('helperDefinitions'), true));
// \Pimcore\Log\Simple::log("DataObjectEventListener", "****************returnMappedFieldNames ******************");
// \Pimcore\Log\Simple::log("DataObjectEventListener", print_r($event->getArgument('returnMappedFieldNames'), true));
}
protected function setNodeConfig($object)
{
$child = \Pimcore\Model\DataObject\AbstractObject::getById($object['id']) ;
// add from indabasolutions allowed classes inside this element
if (\Pimcore::getContainer()->getParameter("indabaExtension.dataObject.onlyAllowedClass")) {
$object['allowedClassIDS'] = $child->getProperty("allowedClass");
}
if ($child->getType() != 'folder') {
$object['published'] = $child->isPublished();
$object['className'] = $child->getClass()->getName();
if (!$child->isPublished()) {
$object['cls'] .= 'pimcore_unpublished ';
}
$object['allowVariants'] = $child->getClass()->getAllowVariants();
}
return $object;
}
protected function correctCsvItem($object, $objectData)
{
foreach ($objectData as $key => $value) {
$fieldDefinition = $object->getClass()->getFieldDefinition($key);
if ($fieldDefinition) {
switch ($fieldDefinition->fieldtype) {
case "inputQuantityValue":
case "quantityValue":
$fieldValue = $fieldDefinition->getForCsvExport($object);
$values = explode("_", $fieldValue);
$unitAbbreviation = "";
if (count($values) == 2) {
$unit = DataObject\QuantityValue\Unit::getById($values[1]);
if ($unit) {
$unitAbbreviation = $unit->getAbbreviation();
}
}
$value = trim($values[0] . ' ' . $unitAbbreviation);
break;
case "select":
$optionValue = $fieldDefinition->getForCsvExport($object);
foreach ($fieldDefinition->options as $option) {
if ($option['value'] == $optionValue) {
$value = $option['key'];
}
}
}
$objectData[$key] = $value ;
}
}
return $objectData;
}
}