<?php
namespace App\EventListener;
use App\Model\Attribute\IdSequenceGenerator;
use App\Model\Product\AutoFields;
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\Colour;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\Size;
use Pimcore\Model\DataObject\SizeGroup;
use Pimcore\Model\DataObject\Supplier;
class ObjectAddListener {
public function onPreAdd (ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject->getType() != "folder") {
$parentPath = $dataObject->getParent();
if ($dataObject instanceof Product) {
$autoFields = new AutoFields();
$dataObject = $autoFields->ProductLevel($dataObject);
if ($dataObject->getType() == "variant") {
$dataObject = $autoFields->LaunchMonth($dataObject);
//$dataObject = $this->setSkuAndKey($dataObject);
} else if ($dataObject->getType() == "object") {
$dataObject->setSKU($dataObject->__toString());
}
//} else if (($dataObject->getParent()->getType() == "Folder" && $dataObject->getParent()->getKey() == "Attributes") || $dataObject instanceof Supplier) {
} else if (method_exists($dataObject, "setName") || $dataObject instanceof Supplier) {
$dataObject->setName($dataObject->__toString());
if ($dataObject instanceof Colour || $dataObject instanceof Size || $dataObject instanceof Supplier) {
$min = $dataObject instanceof Supplier ? 9000000 : 100;
$max = $dataObject instanceof Supplier ? 10000000 : 999;
$idGenerator = new IdSequenceGenerator($min, $max, $dataObject->getClassName());
$nextId = $idGenerator->GetNextNumber();
if (is_int($nextId)) {
$function = "set".$dataObject->getClassName()."ID";
$dataObject->$function($nextId);
}
}
}
}
}
}
/**
* Adds SKU and Key information if required
*
* This should only run on products that have been added using the UI rather than anything having been entered using the Buyplans.
* The reason for this is because we're establishing the SKU within our import script whereas here we're creating a SKU based on the number of products
* that exist on the object you're creating. e.g 123456.001.001, 123456.001.002
*
* @param Product $dataObject
* @return $dataObject
*/
private function setSkuAndKey(Product $dataObject) {
if (empty($dataObject->getSKU())) {
if ($dataObject->getType() == "variant") {
if (empty($dataObject->getSize()) && empty($dataObject->getColour())) {
$sequence = sprintf("%03d", ($dataObject->getParent->getChildAmount(null)+1));
$dataObject->setSKU($dataObject->getParent()->getKey() . "." . $sequence);
$dataObject->setKey($dataObject->getParent()->getKey() . "." . $sequence);
}
}
}
if ($this->getLevel($dataObject) == 3 && empty($dataObject->getEAN())) {
$dataObject->setEAN($this->getBarcode());
}
return $dataObject;
}
/**
* Returns the class name without the namespace
*
* @param string $classname
* @return string
*/
private function get_class_name($classname): string
{
if ($pos = strrpos($classname, '\\')) return substr($classname, $pos + 1);
return $pos;
}
private function GetClassificationStore(DataObject $dataObject) {
$dataObject->getTestStore()->setGroupCollectionMappings([2]);
return $dataObject;
}
/**
* Returns an unused barcode
*
* @return string
*/
public function GetBarcode() {
$barcodeObject = new BarcodeListing();
$barcodes = $barcodeObject->filterByUsed(NULL, 'IS');
if (count($barcodes) > 0) {
return $barcodes->current()->getBarcode();
}
}
public function onPostAdd(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) {
$dataObject = $e->getObject();
$autoFields = new AutoFields();
$dataObject = $autoFields->ProductLevel($dataObject);
if ($dataObject instanceof SizeGroup) {
if (empty($dataObject->getSizeGroupID())) {
$dataObject->setSizeGroupID($dataObject->getId());
$dataObject->save();
}
}
}
}
/**
* Returns the base DIR that the object exists in e.g Products or Attributes
*
* @param string $parentPath
* @return string
*/
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];
}
}
/**
* Checks to see if a get request exists on the object by the given field
*
* @param DataObject $dataObject
* @param string $field
* @return bool
*/
private function fieldExists($dataObject, $field) {
$function = "get".$field;
return method_exists($dataObject, $function);
}
/**
* Returns the level of the product so we know if it is a colour or size variant
*
* @param DataObject $dataObject
* @return integer
*/
private function GetLevel(DataObject $dataObject) {
$levelCount = 0;
do {
$levelCount++;
$dataObject = $dataObject->getParent();
if ($levelCount > 5) {
break;
}
} while ($dataObject->getType() != "folder");
return $levelCount;
}
// public function onPreAdd (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();
// $className = $this->get_class_name(get_class($dataObject));
// if ($dataObject->getType() != "folder") {
// $parentPath = $dataObject->getParent();
// $baseFolder = $this->GetBaseFolder($parentPath);
// if ($baseFolder == "Products") {
// $autoFields = new AutoFields();
// $dataObject = $autoFields->ProductLevel($dataObject);
// if ($dataObject->getType() == "variant") {
// $dataObject = $autoFields->LaunchMonth($dataObject);
// //For Variants the input is ignored and instead the SKU and key is created appendid with a suffix based on the count of variations existing
// $parentKey = $this->GetParentKey($parentPath);
// if (empty($dataObject->getSize()) && empty($dataObject->getColour())) {
// $sequence = sprintf("%03d", ($parentPath->getChildAmount(null)+1));
// $dataObject->setSKU($parentKey . "." . $sequence);
// $dataObject->setKey($parentKey . "." . $sequence);
// if ($this->getLevel($dataObject) == 3) {
// if (empty($dataObject->getEAN())) {
// $dataObject->setEAN($this->getBarcode());
// }
// }
// }
// } else if ($dataObject->getType() != "folder") {
// if ($this->fieldExists($dataObject, "SKU")) {
// $dataObject->setSKU($dataObject->__toString());
// //$dataObject = $this->GetClassificationStore($dataObject);
// }
// } else {
// $dataObject->setProperty("ARCHIVED", "bool", false);
// }
// } else if ($baseFolder == "Attributes" || $className == "Suppliers") {
// $dataObject->setName($dataObject->__toString());
// if ($className == "Colour" || $className == "Size") {
// $idGenerator = new IdSequenceGenerator(100,999, $className);
// $nextId = $idGenerator->GetNextNumber();
// if (is_int($nextId)) {
// $function = "set".$className."ID";
// $dataObject->$function($nextId);
// }
// }
// }
// }
// //$this->GetParentItemCount($parentPath->getId());
// }
// }
}