<?php
namespace App\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Barcodes\Listing as BarcodeListing;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\Element\Note;
class BarcodeListener {
public function onPostAdd (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if($e instanceof AssetEvent) {
// do something with the asset
$foo = $e->getAsset();
} else if ($e instanceof DocumentEvent) {
// do something with the document
$foo = $e->getDocument();
} else if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
return;
if (get_class($dataObject) == "Pimcore\Model\DataObject\Product" && $this->getLevel($dataObject) == 3) {
$dataObject->setEAN($this->getBarcode($dataObject));
}
//$this->GetParentItemCount($parentPath->getId());
}
}
}
public function onPreDelete (ElementEventInterface $e) {
if($e instanceof AssetEvent) {
// do something with the asset
$foo = $e->getAsset();
} else if ($e instanceof DocumentEvent) {
// do something with the document
$foo = $e->getDocument();
} else if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
if ($dataObject instanceof Product && $dataObject->isPublished() == false) {
$this->RemoveBarcode($dataObject);
}
}
}
private function RemoveBarcode($dataObject) {
$currentBarcode = $dataObject->getEAN();
if (!empty($currentBarcode)) {
$barcodeObject = new BarcodeListing();
$barcodes = $barcodeObject->filterByBarcode($currentBarcode);
if (!empty($barcodes)) {
$barcodes->current()->setUsed(false);
$barcodes->current()->setProduct(null);
$barcodes->current()->save();
// $noteData = [
// [
// 'name'=>'Product',
// 'type'=>'Object',
// 'value'=>$dataObject->getSKU()
// ]
// ];
// $this->AddNote($barcodes->current(), $dataObject, "product_delete", "Unpublished associated product deleted", $noteData);
}
}
}
private function AddNote($object, $dataObject, $type, $title, $data) {
$object = DataObject::getById($object->getId());
$note = new Note();
$note->setElement($object);
$note->setDate(time());
$note->setType($type);
$note->setTitle($title);
$note->setUser(0);
foreach ($data as $noteData) {
$note->addData($noteData['name'], $noteData['type'], $noteData['value']);
}
$note->save();
}
public function GetBarcode($dataObject) {
$currentBarcode = $dataObject->getEAN();
if (!empty($currentBarcode)) {
$barcodeObject = new BarcodeListing();
$barcodes = $barcodeObject->filterByBarcode($currentBarcode);
if (!empty($barcodes)) {
$barcode = $barcodes->current();
$barcode->setProduct($dataObject);
$barcode->setUsed(true);
$barcode->save();
}
}
}
private function GetBaseFolder($parentPath) {
$baseFolder = explode("/", $parentPath);
if (count($baseFolder) >= 2) {
return $baseFolder[1];
}
}
private function GetParentKey($parentPath) {
$pathParts = explode("/", $parentPath);
if (count($pathParts) >= 1) {
return $pathParts[count($pathParts)-1];
}
}
private function fieldExists($dataObject, $field) {
try {
$function = "get".$field;
$dataObject->$function();
return true;
} catch (\Exception $e) {
return false;
}
}
private function GetLevel(DataObject $dataObject) {
$levelCount = 0;
do {
$levelCount++;
$dataObject = $dataObject->getParent();
if ($levelCount > 5) {
break;
}
} while ($dataObject->getType() != "folder");
return $levelCount;
}
}