<?php
namespace OutputBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\DataObject\Product;
class BMECatListener
{
public function __construct()
{
}
public function onPostUpdate(ElementEventInterface $e)
{
if ($e instanceof DataObjectEvent) {
/* @var Product $data */
$data = $e->getObject();
if ($data instanceof \Pimcore\Model\DataObject\BMECat) {
// Do not autocreate any more! Use Command!
return;
if ($data->getCreatenow()) {
// Set Customer (for Customer Price)
$oldCustomItem = false;
if ($data->getKunde()) {
$customer = $data->getKunde();
/* @var \Pimcore\Model\DataObject\Customer $customer */
if (method_exists($customer, "getCustomernumber")) {
$kn = $customer->getCustomernumber();
if ($kn) {
$environment = \Pimcore::getKernel()->getContainer()->get("pimcore_ecommerce.environment");
$oldCustomItem = $environment->getCustomItem("ordercustomernumber");
$environment->setCustomItem("ordercustomernumber", $kn);
}
}
}
$translator = \Pimcore::getKernel()->getContainer()->get("translator");
$translator->setLocale($data->getSprache());
$pimcoreLocale = \Pimcore::getKernel()->getContainer()->get("pimcore.locale");
$pimcoreLocale->setLocale($data->getSprache());
$output = new \OutputBundle\OutputFormat\Xml();
$output->setLanguage($data->getSprache());
// remember option ETIM
$output->customVariables["ETIM"] = $data->getETIM();
$channel = \OutputBundle\Service::getOutputDataConfig($data, "bmecat");
foreach ($channel as $property) {
$output->createNode($property, $data, null);
}
$dataExport = $output->getXml();
$exportFolder = Asset::getByPath("/exports/bmecat");
if ($exportFolder && $exportFolder->getId() > 0) {
$parentId = $exportFolder->getId();
$asset = Asset::getByPath("/exports/bmecat/{$data->getKey()}.xml");
if ($asset) {
$asset->delete();
}
Asset::create($parentId, ["filename" => $data->getKey() . ".xml",
"type" => "application/xml",
"data" => $dataExport]);
}
if ($oldCustomItem) {
$environment->setCustomItem("ordercustomernumber", $oldCustomItem);
}
}
}
}
}
}