libraries/OutputBundle/EventListener/AlgoliaListener.php line 45

Open in your IDE?
  1. <?php
  2. namespace OutputBundle\EventListener;
  3. use Pimcore\Event\Model\ElementEventInterface;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject\Folder;
  6. use Pimcore\Model\DataObject\Product;
  7. class AlgoliaListener
  8. {
  9.     private $clientId ''// Comes from WebsiteSetting: algoliaClientId
  10.     private $apiKey ''// Comes from WebsiteSetting: algoliaApiKeyAdmin
  11.     private $client false;
  12.     public function __construct()
  13.     {
  14.         $this->clientId "";
  15.         try {
  16.             $clientIdSetting \Pimcore\Model\WebsiteSetting::getByName('algoliaClientId');
  17.             if ($clientIdSetting) {
  18.                 $this->clientId $clientIdSetting->getData();
  19.             }
  20.         } catch (\Exception $e) {
  21.             return;
  22.         }
  23.         $this->apiKey "";
  24.         try {
  25.             $apiKeySetting \Pimcore\Model\WebsiteSetting::getByName('algoliaApiKeyAdmin');
  26.             if ($apiKeySetting) {
  27.                 $this->apiKey $apiKeySetting->getData();
  28.             }
  29.         } catch (\Exception $e) {
  30.             return;
  31.         }
  32.         if ($this->apiKey && $this->clientId) {
  33.             $this->client = new \AlgoliaSearch\Client($this->clientId$this->apiKey);
  34.         }
  35.     }
  36.     public function onPostUpdate(ElementEventInterface $e)
  37.     {
  38.         if (!$this->client) {
  39.             return;
  40.         }
  41.         if ($e instanceof DataObjectEvent) {
  42.             $data $e->getObject();
  43.             if ($data instanceof Product) {
  44.                 /* Ist bei Mefa entstanden */
  45.                 if (method_exists($data"getAritkelnummer")) {
  46.                     if ($data->getAritkelnummer()) {
  47.                         $success true;
  48.                     }
  49.                 }
  50.                 /* nach Möglichkeit immer nur über diese Methode gehen */
  51.                 if (method_exists($data"getOSDoIndexProduct")) {
  52.                     if ($data->getOSDoIndexProduct()) {
  53.                         $success true;
  54.                     }
  55.                 }
  56.             }
  57.             if($success){
  58.                 $bulkProcess = new \OutputBundle\Model\Algolia\BulkProcess($this->client);
  59.                 $bulkProcess->update([$data]);
  60.             }
  61.         }
  62.         return;
  63.     }
  64.     public function onPostDelete(ElementEventInterface $e)
  65.     {
  66.         if (!$this->client) {
  67.             return;
  68.         }
  69.         if ($e instanceof DataObjectEvent) {
  70.             $algoliaSync false;
  71.             try {
  72.                 $algolyaSyncSetting \Pimcore\Model\WebsiteSetting::getByName('algoliaSync');
  73.                 if ($algolyaSyncSetting) {
  74.                     $algoliaSync $algolyaSyncSetting->getData();
  75.                 }
  76.             } catch (\Exception $e) {
  77.                 return;
  78.             }
  79.             try {
  80.                 $algoliaSkipVariantsSetting \Pimcore\Model\WebsiteSetting::getByName('algoliaSkipVariants');
  81.                 if ($algoliaSkipVariantsSetting) {
  82.                     $algoliaSkipVariants $algoliaSkipVariantsSetting->getData();
  83.                 }
  84.             } catch (\Exception $e) {
  85.                 //
  86.                 $algoliaSkipVariants false;
  87.             }
  88.             $data $e->getObject();
  89.             $data->setGetInheritedValues(1);
  90.             $type $data->getType();
  91.             if ($type === "variant" && $algoliaSkipVariants) {
  92.                 return;
  93.             }
  94.             $indexName $data->getProperty('algoliaIndexName');
  95.             if ($indexName) {
  96.                 $index $this->client->initIndex($indexName);
  97.             }
  98.             if ($algoliaSync && $indexName && !$data instanceof Folder) {
  99.                // $index->deleteObject($data->getId());
  100.             }
  101.         }
  102.     }
  103. }