src/EventListener/SyncDataObjectsWithAlgoliaListener.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Exception;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Model\Asset;
  7. use Pimcore\Model\Asset\Image;
  8. use Pimcore\Model\DataObject\Folder;
  9. use Pimcore\Model\DataObject\Product;
  10. use Pimcore\Model\WebsiteSetting;
  11. class SyncDataObjectsWithAlgoliaListener extends BaseAlgoliaListener
  12. {
  13.     /**
  14.      * @throws Exception
  15.      */
  16.     public function onPostUpdate(ElementEventInterface $e)
  17.     {
  18.         // only if algolia is enabled
  19.         if ($this->isAlgoliaEnabled()) {
  20.             if ($e instanceof DataObjectEvent) {
  21.                 $obj $e->getObject();
  22.                 if ($obj instanceof Product) {
  23.                     // don't allow products starting with specific existing path
  24.                     if ($excludedWs WebsiteSetting::getByName("excludePathFromAlgoliaIndexing")) {
  25.                         $excludedPath $excludedWs->getData();
  26.                         if (str_starts_with($obj->getPath(), $excludedPath)) {
  27.                             $folder Folder::getByPath($excludedPath);
  28.                             if ($folder instanceof Folder) {
  29.                                 // remove from index because it should be excluded
  30.                                 $this->getIndex()->deleteObject($obj->getId());
  31.                                 return;
  32.                             }
  33.                         }
  34.                     }
  35.                     // don't allow product parents
  36.                     if ($obj->hasChildren()) {
  37.                         return;
  38.                     }
  39.                     $images = [];
  40.                     $documents = [];
  41.                     // get product images
  42.                     foreach ($obj->getImages()->getItems() as $item) {
  43.                         if ($image $item->getImage()) {
  44.                             if ($image instanceof Image) {
  45.                                 // push only active images for algolia indexing
  46.                                 if ($image->getHasMetaData() && $image->getMetadata("indexAlgolia")) {
  47.                                     $images[] = [
  48.                                         "thumbnail" => !is_string($item->getThumbnail()) ? $item->getThumbnail("printdefault")->getPath() : "",
  49.                                         "originalImage" => $image->getFullPath() ?? "",
  50.                                         "fileSize" => $image->getFileSize() ?? ""
  51.                                     ];
  52.                                 }
  53.                             }
  54.                         }
  55.                     }
  56.                     // get product documents
  57.                     foreach ($obj->getDocuments() as $document) {
  58.                         // allow all assets
  59.                         if ($document instanceof Asset) {
  60.                             // push only active images for algolia indexing
  61.                             if ($document->getHasMetaData() && $document->getMetadata("indexAlgolia")) {
  62.                                 $documents[] = [
  63.                                     "document" => $document->getFullPath() ?? "",
  64.                                     "filename" => $document->getFilename() ?? "",
  65.                                     "fileSize" => $document->getFileSize() ?? ""
  66.                                 ];
  67.                             }
  68.                         }
  69.                     }
  70.                     // if no images and documents found; or product is unpublished, remove product from index and cancel further processing
  71.                     if ((sizeof($images) <= && sizeof($documents) <= 0) || !$obj->getPublished()) {
  72.                         $this->getIndex()->deleteObject($obj->getId());
  73.                         return;
  74.                     }
  75.                     $indexableData = [
  76.                         "description" => $obj->getDesignations('de'), // TODO-dkfn20 allgemeine translation statt immer 'de'?
  77.                         "images" => $images,
  78.                         "documents" => $documents,
  79.                         "orderno" => $obj->getOrdernumber(),
  80.                         "longnumber" => $obj->getLongnumber()
  81.                     ];
  82.                     // Has to be assigned separately before saveObject() call to prevent unique objectID error!
  83.                     $indexableData['objectID'] = $obj->getId();
  84.                     $this->getIndex()->saveObject($indexableData);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     /**
  90.      * @throws Exception
  91.      */
  92.     public function onPostDelete(ElementEventInterface $e)
  93.     {
  94.         if ($e instanceof DataObjectEvent) {
  95.             $obj $e->getObject();
  96.             if ($obj instanceof Product) {
  97.                 if ($this->getIndex()) {
  98.                     $this->getIndex()->deleteObject($obj->getId());
  99.                 }
  100.             }
  101.         }
  102.     }
  103. }