<?php
namespace AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use \Pimcore\Model\DataObject;
use \Pimcore\Model\Asset;
class DataObjectEventsListener implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
const LANGUAGES = array(
"it_IT",
"de",
"en",
"fr_FR",
"es_ES"
);
public static function getSubscribedEvents()
{
return [
\Pimcore\Event\DataObjectEvents::PRE_UPDATE => 'onPreUpdate'
];
}
// Working only backend
public function onPreUpdate(\Pimcore\Event\Model\DataObjectEvent $event) {
// https://github.com/pimcore/pimcore/blob/9271c03f39c085c9bea3e297e4b6013b71f4bc32/lib/Event/DataObjectEvents.php#L51
$element = $event->getElement();
$id = $element->getId();
// \Pimcore\Log\Simple::log("miolog", get_class($element));
if ($element instanceof DataObject\Variante)
$this->parseVarianteApprofondimenti($element);
if ($element instanceof DataObject\Design)
$this->parseDesignApprofondimenti($element);
if ($element instanceof DataObject\Prodotto)
$this->parseProdottoLaboratorioCheckList($element);
}
private function parseVarianteApprofondimenti($variante) {
$languages = self::LANGUAGES;
// Disable Fallback languages
\Pimcore\Model\DataObject\Localizedfield::setGetFallbackValues(false);
// Retrieve parent design
$_varianteParent = $variante->getParent();
while ( !($_varianteParent instanceof DataObject\Design)) {
if (is_null($_varianteParent))
return;
$_varianteParent = $_varianteParent->getParent();
}
if (is_null($_varianteParent))
return;
// Retrieve approfondimenti design
$_approfondimentiDesign = $_varianteParent->getApprofondimento_new();
// Retrieve approfondimenti variante
$_approfondimentiVariante = $variante->getApprofondimento_new();
// Merge approfondimenti
$_approfondimenti = array_merge($_approfondimentiDesign, $_approfondimentiVariante);
$_approfondimenti = array_filter($_approfondimenti, function ($_v) {
if (strtoupper($_v->getTipo()) == "NOTA" && strtoupper($_v->getArgomento()) == 5000)
return false;
return true;
});
// Unique approfondimenti
$_approfondimenti = array_unique($_approfondimenti, SORT_REGULAR);
// Sort by ordinamento
usort($_approfondimenti, function ($a, $b) {
$ordinamento_a = $a->getOrdinamento();
$ordinamento_b = $b->getOrdinamento();
if ($ordinamento_a == $ordinamento_b)
return 0;
return ($ordinamento_a < $ordinamento_b) ? -1 : 1;
});
// Map value
foreach ($languages as $language) {
$_approfondimentiMap = array_map(function ($v) use ($language) {
return $v->getRiga_approfondimento($language);
}, $_approfondimenti);
$_approfondimentiMap = array_filter($_approfondimentiMap, function ($v) {
$v = trim($v);
if (empty($v))
return false;
if ($v == "")
return false;
return true;
});
// Implode rows
$_approfondimentiImplode = "• " . implode(PHP_EOL . "• ", $_approfondimentiMap);
// Set value
$variante->setApprofondimento_preview($_approfondimentiImplode, $language);
}
}
private function parseDesignApprofondimenti($design) {
$languages = self::LANGUAGES;
// Disable Fallback languages
\Pimcore\Model\DataObject\Localizedfield::setGetFallbackValues(false);
// Merge approfondimenti
$_approfondimenti = $design->getApprofondimento_new();
// Unique approfondimenti
$_approfondimenti = array_unique($_approfondimenti, SORT_REGULAR);
// Sort by ordinamento
usort($_approfondimenti, function ($a, $b) {
$ordinamento_a = $a->getOrdinamento();
$ordinamento_b = $b->getOrdinamento();
if ($ordinamento_a == $ordinamento_b)
return 0;
return ($ordinamento_a < $ordinamento_b) ? -1 : 1;
});
// Map value
foreach ($languages as $language) {
$_approfondimentiMap = array_map(function ($v) use ($language) {
return $v->getRiga_approfondimento($language);
}, $_approfondimenti);
$_approfondimentiMap = array_filter($_approfondimentiMap, function ($v) {
$v = trim($v);
if (empty($v))
return false;
if ($v == "")
return false;
return true;
});
// Implode rows
$_approfondimentiImplode = "• " . implode(PHP_EOL . "• ", $_approfondimentiMap);
// Set value
$design->setApprofondimento_preview($_approfondimentiImplode, $language);
}
}
private function parseProdottoLaboratorioCheckList($prodottoLaboratorio) {
$_documentoChecklist = $prodottoLaboratorio->getDocumento_checklist_key();
// Get document
$_documentoLab = DataObject\Doclab::getByPath("/Lab-reference/Documenti/DocProdotti/CheckList/{$_documentoChecklist}");
if (is_null($_documentoLab)) {
$prodottoLaboratorio->setDocumento_checklist(null);
$prodottoLaboratorio->setDocumento_checklist_preview(null);
return;
}
$prodottoLaboratorio->setDocumento_checklist($_documentoLab);
// Get asset
$_assetLab = $_documentoLab->getDocumento_asset();
if (is_null($_assetLab))
$_assetLab = Asset::getById(1258);
$prodottoLaboratorio->setDocumento_checklist_preview($_assetLab);
}
}