src/EventListener/ObjectAddListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Model\Attribute\IdSequenceGenerator;
  4. use App\Model\Product\AutoFields;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Pimcore\Event\Model\AssetEvent;
  8. use Pimcore\Event\Model\DocumentEvent;
  9. use Pimcore\Model\DataObject;
  10. use Pimcore\Model\DataObject\Barcodes\Listing as BarcodeListing;
  11. use Pimcore\Model\DataObject\Colour;
  12. use Pimcore\Model\DataObject\Product;
  13. use Pimcore\Model\DataObject\Size;
  14. use Pimcore\Model\DataObject\SizeGroup;
  15. use Pimcore\Model\DataObject\Supplier;
  16. class ObjectAddListener {
  17.     public function onPreAdd (ElementEventInterface $e) {
  18.         if($e instanceof DataObjectEvent) {
  19.             $dataObject $e->getObject();
  20.             if ($dataObject->getType() != "folder") {
  21.                 $parentPath $dataObject->getParent();
  22.                 if ($dataObject instanceof Product) {
  23.                     $autoFields = new AutoFields();
  24.                     $dataObject $autoFields->ProductLevel($dataObject);
  25.                     if ($dataObject->getType() == "variant") {
  26.                         $dataObject $autoFields->LaunchMonth($dataObject);
  27.                         //$dataObject = $this->setSkuAndKey($dataObject);
  28.                     } else if ($dataObject->getType() == "object") {
  29.                         $dataObject->setSKU($dataObject->__toString());
  30.                     }
  31.                 //} else if (($dataObject->getParent()->getType() == "Folder" && $dataObject->getParent()->getKey() == "Attributes") || $dataObject instanceof Supplier) {
  32.                 } else if (method_exists($dataObject"setName") || $dataObject instanceof Supplier) {
  33.                     $dataObject->setName($dataObject->__toString());
  34.                     if ($dataObject instanceof Colour || $dataObject instanceof Size || $dataObject instanceof Supplier) {
  35.                         $min $dataObject instanceof Supplier 9000000 100;
  36.                         $max $dataObject instanceof Supplier 10000000 999;
  37.                         $idGenerator = new IdSequenceGenerator($min$max$dataObject->getClassName());
  38.                         $nextId $idGenerator->GetNextNumber();
  39.                         if (is_int($nextId)) {
  40.                             $function "set".$dataObject->getClassName()."ID";
  41.                             $dataObject->$function($nextId);
  42.                         }
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     /**
  49.      * Adds SKU and Key information if required
  50.      * 
  51.      * This should only run on products that have been added using the UI rather than anything having been entered using the Buyplans.
  52.      * 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
  53.      * that exist on the object you're creating. e.g 123456.001.001, 123456.001.002
  54.      *
  55.      * @param Product $dataObject
  56.      * @return $dataObject
  57.      */
  58.     private function setSkuAndKey(Product $dataObject) {
  59.         if (empty($dataObject->getSKU())) {
  60.             if ($dataObject->getType() == "variant") {
  61.                 if (empty($dataObject->getSize()) && empty($dataObject->getColour())) {
  62.                     $sequence sprintf("%03d", ($dataObject->getParent->getChildAmount(null)+1));
  63.                     $dataObject->setSKU($dataObject->getParent()->getKey() . "." $sequence);
  64.                     $dataObject->setKey($dataObject->getParent()->getKey() . "." $sequence);
  65.                     
  66.                 }
  67.             }
  68.         }
  69.         if ($this->getLevel($dataObject) == && empty($dataObject->getEAN())) {
  70.             $dataObject->setEAN($this->getBarcode());
  71.         }
  72.         return $dataObject;
  73.     }
  74.     /**
  75.      * Returns the class name without the namespace
  76.      *
  77.      * @param string $classname
  78.      * @return string
  79.      */
  80.     private function get_class_name($classname): string
  81.     {
  82.         if ($pos strrpos($classname'\\')) return substr($classname$pos 1);
  83.         return $pos;
  84.     }
  85.     private function GetClassificationStore(DataObject $dataObject) {
  86.         $dataObject->getTestStore()->setGroupCollectionMappings([2]);
  87.         return $dataObject;
  88.     }
  89.     /**
  90.      * Returns an unused barcode
  91.      *
  92.      * @return string
  93.      */
  94.     public function GetBarcode() {
  95.         $barcodeObject = new BarcodeListing();
  96.         $barcodes $barcodeObject->filterByUsed(NULL'IS');
  97.         if (count($barcodes) > 0) {
  98.             return $barcodes->current()->getBarcode();
  99.         }
  100.     }
  101.     public function onPostAdd(ElementEventInterface $e) {
  102.         if($e instanceof AssetEvent) {
  103.             // do something with the asset
  104.             $foo $e->getAsset();
  105.         } else if ($e instanceof DocumentEvent) {
  106.             // do something with the document
  107.             $foo $e->getDocument();
  108.         } else if ($e instanceof DataObjectEvent) {
  109.             $dataObject $e->getObject();
  110.             $autoFields = new AutoFields();
  111.             $dataObject $autoFields->ProductLevel($dataObject);
  112.             if ($dataObject instanceof SizeGroup) {
  113.                 if (empty($dataObject->getSizeGroupID())) {
  114.                     $dataObject->setSizeGroupID($dataObject->getId());
  115.                     $dataObject->save();
  116.                 }
  117.             }
  118.         }
  119.     }
  120.     /**
  121.      * Returns the base DIR that the object exists in e.g Products or Attributes
  122.      *
  123.      * @param string $parentPath
  124.      * @return string
  125.      */
  126.     private function GetBaseFolder($parentPath) {
  127.         $baseFolder explode("/"$parentPath);
  128.         if (count($baseFolder) >= 2) {
  129.             return $baseFolder[1];
  130.         }
  131.     }
  132.     private function GetParentKey($parentPath) {
  133.         $pathParts explode("/"$parentPath);
  134.         if (count($pathParts) >= 1) {
  135.             return $pathParts[count($pathParts)-1];
  136.         }
  137.     }
  138.     /**
  139.      * Checks to see if a get request exists on the object by the given field
  140.      *
  141.      * @param DataObject $dataObject
  142.      * @param string $field
  143.      * @return bool
  144.      */
  145.     private function fieldExists($dataObject$field) {
  146.         $function "get".$field;
  147.         return method_exists($dataObject$function);
  148.     }
  149.     /**
  150.      * Returns the level of the product so we know if it is a colour or size variant
  151.      *
  152.      * @param DataObject $dataObject
  153.      * @return integer
  154.      */
  155.     private function GetLevel(DataObject $dataObject) {
  156.         $levelCount 0;
  157.         do {
  158.             $levelCount++;
  159.             $dataObject $dataObject->getParent();
  160.             if ($levelCount 5) {
  161.                 break;
  162.             }
  163.         } while ($dataObject->getType() != "folder");
  164.         return $levelCount;
  165.     }
  166.     // public function onPreAdd (ElementEventInterface $e) {
  167.     //     if($e instanceof AssetEvent) {
  168.     //         // do something with the asset
  169.     //         $foo = $e->getAsset();
  170.     //     } else if ($e instanceof DocumentEvent) {
  171.     //         // do something with the document
  172.     //         $foo = $e->getDocument();
  173.     //     } else if ($e instanceof DataObjectEvent) {
  174.     //         // do something with the object
  175.     //         $dataObject = $e->getObject();
  176.     //         $className = $this->get_class_name(get_class($dataObject));
  177.     //         if ($dataObject->getType() != "folder") {
  178.     //             $parentPath = $dataObject->getParent();
  179.     //             $baseFolder = $this->GetBaseFolder($parentPath);
  180.     //             if ($baseFolder == "Products") {
  181.     //                 $autoFields = new AutoFields();
  182.     //                 $dataObject = $autoFields->ProductLevel($dataObject);
  183.     //                 if ($dataObject->getType() == "variant") {
  184.     //                     $dataObject = $autoFields->LaunchMonth($dataObject);
  185.     //                     //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
  186.     //                     $parentKey = $this->GetParentKey($parentPath);
  187.     //                     if (empty($dataObject->getSize()) && empty($dataObject->getColour())) {
  188.     //                         $sequence = sprintf("%03d", ($parentPath->getChildAmount(null)+1));
  189.     //                         $dataObject->setSKU($parentKey . "." . $sequence);
  190.     //                         $dataObject->setKey($parentKey . "." . $sequence);
  191.     //                         if ($this->getLevel($dataObject) == 3) {
  192.     //                             if (empty($dataObject->getEAN())) {
  193.     //                                 $dataObject->setEAN($this->getBarcode());
  194.     //                             }
  195.     //                         }
  196.     //                     }
  197.     //                 } else if ($dataObject->getType() != "folder") {
  198.     //                     if ($this->fieldExists($dataObject, "SKU")) {
  199.     //                         $dataObject->setSKU($dataObject->__toString());
  200.     //                         //$dataObject = $this->GetClassificationStore($dataObject);
  201.     //                     }
  202.     //                 } else {
  203.     //                     $dataObject->setProperty("ARCHIVED", "bool", false);
  204.     //                 }
  205.     //             } else if ($baseFolder == "Attributes" || $className == "Suppliers") {
  206.     //                 $dataObject->setName($dataObject->__toString());
  207.     //                 if ($className == "Colour" || $className == "Size") {
  208.     //                     $idGenerator = new IdSequenceGenerator(100,999, $className);
  209.     //                     $nextId = $idGenerator->GetNextNumber();
  210.     //                     if (is_int($nextId)) {
  211.     //                         $function = "set".$className."ID";
  212.     //                         $dataObject->$function($nextId);
  213.     //                     }
  214.     //                 }
  215.     //             }
  216.     //         }
  217.     //         //$this->GetParentItemCount($parentPath->getId());
  218.     //     }
  219.     // }
  220. }