<?php
namespace App\EventListener;
//use Pimcore\Cache;
//use Pimcore\Db;
use Pimcore\Logger;
use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
use Pimcore\Model\DataObject\Prodotti;
use Pimcore\Model\DataObject\Service;
use Symfony\Component\EventDispatcher\GenericEvent;
use \Pimcore\Model\DataObject;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
// use Pimcore\Event\Model\ElementEventInterface;
// use Pimcore\Event\Model\DataObjectEvent;
class MyEventListener
{
public function updateProdottiGiacenze(ElementEventInterface $e)
{
$object = $e->getObject();
if ($e instanceof DataObjectEvent) {
// Logger::debug("DataObjectEvent................." . print_r($e->getElement(),true));
//Logger::debug("DataObjectEvent................." . print_r($e->getArguments(),true));
// do something with the object
if ($object instanceof Prodotti) {
//\Pimcore\Log\Simple::log('stefanos', "update prodotti giacenze");
//Logger::debug("prodottoooo onupdate" . $object->getCOD());
$COD = $object->getCOD();
//\Pimcore\Log\Simple::log('stefanos', "COD ".$object->getCOD());
// \Pimcore\Log\Simple::log('stefanos', "COD ".$object->getId());
// $entries = new DataObject\Prodotti\Listing();
// //$entries->setCondition("name LIKE :name AND date > :date", ["name" => "%bernie%", "date" => time()]);
// $entries->filterByCOD($COD);
// $entries->load();
$results = DataObject\Prodotti::getByCOD($COD, ['limit' => 1000, 'offset' => 0,'unpublished' => true]);
foreach ($results as $entry) {
\Pimcore\Log\Simple::log('stefanos', "mag ciclo ".$entry->getMagazzino()." != mag obj= ".$object->getMagazzino(). " stock ciclo ".$entry->getIn_stock()." != object stock". $object->getIn_stock());
if(($entry->getMagazzino() != $object->getMagazzino()) || ($entry->getIn_stock() != $object->getIn_stock())){
//Logger::debug("prodottoooo onupdate ".$entry->getCOD()." ". $entry->getNome());
//\Pimcore\Log\Simple::log('stefanos', "prodottoooo onupdate ".$entry->getCOD()." ". $entry->getNome() );
\Pimcore\Log\Simple::log('stefanos', "mag ciclo ".$entry->getMagazzino()." != mag obj= ".$object->getMagazzino(). " stock ciclo ".$entry->getIn_stock()." != object stock". $object->getIn_stock());
$entry->setMagazzino($object->getMagazzino());
$entry->setIn_stock($object->getIn_stock());
$entry->save();
}
}
// $object->setMagazzino(15);
// $object->setIn_stock(false);
//$object->save();
}
//Logger::debug("fired onupdate");
// we don't have to call save here as we are in the pre-update event anyway ;-)
}
}
public function test(ElementEventInterface $e) {
\Pimcore\Log\Simple::log('stefanos', "ElementEventInterface event fired ");
$object = $e->getObject();
if ($e instanceof DataObjectEvent) {
if ($object instanceof Prodotti) {
//\Pimcore\Log\Simple::log('stefanos', "COD ".print_r($object,true));
\Pimcore\Log\Simple::log('stefanos', "COD ".$object->getCOD());
//$lastModifiedUser = $object->getModificationUser();
//$lastModifiedUser = User::getById($object->getModificationUser());
\Pimcore\Log\Simple::log('stefanos', "COD ".date('H:i'));
//$object->setIn_stock(1);
//$object->save();
}
}
}
public function OnPreUpdateSetCODchiaveForManualInput(DataObjectEvent $e) {
$object = $e->getObject();
if ($e instanceof DataObjectEvent) {
if ($object instanceof Prodotti) {
$cod_chiave=$object->getCODchiave();
$org=$object->getOrganizzazione();
$cod=$object->getCOD();
if($cod && $org){
if($cod_chiave==""){
\Pimcore\Log\Simple::log('stefanos', "cod chiave null");
//\Pimcore\Log\Simple::log('stefanos', $cod." ".$cod_chiave." ".$org);
if($org=="/Organizzazioni/BeCulture"){
$orgPrefix="beculture";
}else if($org=="/Organizzazioni/Biennale di Venezia"){
$orgPrefix="biennaledivenezia";
}else if($org=="/Organizzazioni/Galleria degli Uffizi"){
$orgPrefix="uffizi";
}else if($org=="/Organizzazioni/Firenze Musei Store"){
$orgPrefix="firenzemusei";
}
$object->setCODchiave($orgPrefix.$cod);
}else{
\Pimcore\Log\Simple::log('stefanos', "cod chiave presente");
\Pimcore\Log\Simple::log('stefanos', $cod." ".$cod_chiave." ".$org);
}
}
}
}
}
//\Pimcore\Log\Simple::log('stefanos', "on pre add");
//\Pimcore\Log\Simple::log('stefanos', $object->getCODchiave);
//\Pimcore\Log\Simple::log('stefanos', "COD ".print_r($object,true));
//\Pimcore\Log\Simple::log('stefanos', "COD ".$object->getCOD());
//$lastModifiedUser = $object->getModificationUser();
//$lastModifiedUser = User::getById($object->getModificationUser());
//\Pimcore\Log\Simple::log('stefanos', "COD ".date('H:i'));
//$object->setIn_stock(1);
//$object->save();
// public function onUpdate (ElementEventInterface $e) {
// if ($e instanceof DataObjectEvent) {
// // do something with the object
// $foo = $e->getObject();
// Logger::debug("onupdate");
// // we don't have to call save here as we are in the pre-update event anyway ;-)
// }
// }
public function selectCustomLayout(GenericEvent $event)
{
$object = $event->getArgument("object");
//Logger::debug($object->getId());
if ($object instanceof Prodotti) {
//get product type
$tipo_prodotto = $object->getTipo_prodotto();
Logger::debug($tipo_prodotto);
if ($tipo_prodotto!="") {
//data element that is send to Pimcore backend UI
$dataEl = $event->getArgument("data");
$data = $this->doModifyCustomLayouts($dataEl, $object, (int)$tipo_prodotto, [0, -1]);
$event->setArgument("data", $data);
}
// switch ($tipo_prodotto) {
// case "Abbigliamento":
// $data = $this->doModifyCustomLayouts($data, $object, 2, [0,-1]);
// $event->setArgument("data", $data);
// break;
// case "Libro":
// $data = $this->doModifyCustomLayouts($data, $object, 1, [0,-1]);
// $event->setArgument("data", $data);
// break;
// case "Oggettistica":
// $data = $this->doModifyCustomLayouts($data, $object, 3, [0,-1]);
// $event->setArgument("data", $data);
// break;
// default:
// break;
// }
//Logger::debug("layout id ".$data['currentLayoutId']);
//set tipo_prodotto with current layout
//$customLayout = CustomLayout::getById($data['currentLayoutId']);
// if (!is_null($customLayout)) {
// Logger::debug(print_r($customLayout->getName(), true));
// $object->setTipo_prodotto($customLayout->getName());
// //$object->save();
// //object.reload($object);
// //
// $objId = $object->getId();
// $db = Db::get();
// $sql = "UPDATE object_1 SET tipo_prodotto = '" . $customLayout->getName() . "' WHERE oo_id = '" . $object->getId() . "' AND oo_classId = '1' AND oo_className = 'Prodotti' ";
// Logger::debug($sql);
// $db->query($sql);
// Cache::clearAll();
// //$object->reload();
// }
}
}
/**
*
*/
private function doModifyCustomLayouts($data, $object, $customLayoutToSelect = null, $layoutsToRemove = [])
{
if ($customLayoutToSelect != null) {
//set current layout to subcategory layout
$data['currentLayoutId'] = $customLayoutToSelect;
$customLayout = CustomLayout::getById($customLayoutToSelect);
$data['layout'] = $customLayout->getLayoutDefinitions();
Service::enrichLayoutDefinition($data["layout"], $object);
}
if (!empty($layoutsToRemove)) {
//remove master layout from valid layouts
$validLayouts = $data["validLayouts"];
foreach ($validLayouts as $key => $validLayout) {
if (in_array($validLayout['id'], $layoutsToRemove)) {
unset($validLayouts[$key]);
}
}
$data["validLayouts"] = array_values($validLayouts);
}
return $data;
}
}