<?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 NRFListener
{
public function __construct()
{
}
public function onPostUpdate(ElementEventInterface $e)
{
if ($e instanceof DataObjectEvent) {
/* @var Product $data */
$data = $e->getObject();
if ($data instanceof \Pimcore\Model\DataObject\NRFConfig) {
if ($data->getCreatenow()) {
$translator = \Pimcore::getContainer()->get("translator");
$translator->setLocale($data->getSprache());
$pimcoreLocale = \Pimcore::getContainer()->get("pimcore.locale");
$pimcoreLocale->setLocale($data->getSprache());
$output = new \OutputBundle\OutputFormat\AsCSV();
$output->setLanguage($data->getSprache());
if (method_exists($data, "getDelimiter")) {
$delimiter = $data->getDelimiter();
$output->setDelimiter($delimiter);
}
if (method_exists($data, "getQuotes")) {
$quotes = $data->getQuotes();
$output->setQuotes($quotes);
}
$channel = \OutputBundle\Service::getOutputDataConfig($data, "csv-nrf");
foreach ($channel as $property) {
$output->createNode($property, $data, null);
}
$dataExport = $output->get();
$exportFolder = Asset::getByPath("/exports/nrf");
if ($exportFolder && $exportFolder->getId() > 0) {
$parentId = $exportFolder->getId();
$asset = Asset::getByPath("/exports/nrf/{$data->getKey()}.csv");
if ($asset) {
$asset->delete();
}
Asset::create($parentId, ["filename" => $data->getKey() . ".csv",
"type" => "plain/text",
"data" => $dataExport]);
}
}
}
}
}
}