<?php
namespace OutputBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\DataObject\Product;
class AlgoliaListener
{
private $clientId = ''; // Comes from WebsiteSetting: algoliaClientId
private $apiKey = ''; // Comes from WebsiteSetting: algoliaApiKeyAdmin
private $client = false;
public function __construct()
{
$this->clientId = "";
try {
$clientIdSetting = \Pimcore\Model\WebsiteSetting::getByName('algoliaClientId');
if ($clientIdSetting) {
$this->clientId = $clientIdSetting->getData();
}
} catch (\Exception $e) {
return;
}
$this->apiKey = "";
try {
$apiKeySetting = \Pimcore\Model\WebsiteSetting::getByName('algoliaApiKeyAdmin');
if ($apiKeySetting) {
$this->apiKey = $apiKeySetting->getData();
}
} catch (\Exception $e) {
return;
}
if ($this->apiKey && $this->clientId) {
$this->client = new \AlgoliaSearch\Client($this->clientId, $this->apiKey);
}
}
public function onPostUpdate(ElementEventInterface $e)
{
if (!$this->client) {
return;
}
if ($e instanceof DataObjectEvent) {
$data = $e->getObject();
if ($data instanceof Product) {
/* Ist bei Mefa entstanden */
if (method_exists($data, "getAritkelnummer")) {
if ($data->getAritkelnummer()) {
$success = true;
}
}
/* nach Möglichkeit immer nur über diese Methode gehen */
if (method_exists($data, "getOSDoIndexProduct")) {
if ($data->getOSDoIndexProduct()) {
$success = true;
}
}
}
if($success){
$bulkProcess = new \OutputBundle\Model\Algolia\BulkProcess($this->client);
$bulkProcess->update([$data]);
}
}
return;
}
public function onPostDelete(ElementEventInterface $e)
{
if (!$this->client) {
return;
}
if ($e instanceof DataObjectEvent) {
$algoliaSync = false;
try {
$algolyaSyncSetting = \Pimcore\Model\WebsiteSetting::getByName('algoliaSync');
if ($algolyaSyncSetting) {
$algoliaSync = $algolyaSyncSetting->getData();
}
} catch (\Exception $e) {
return;
}
try {
$algoliaSkipVariantsSetting = \Pimcore\Model\WebsiteSetting::getByName('algoliaSkipVariants');
if ($algoliaSkipVariantsSetting) {
$algoliaSkipVariants = $algoliaSkipVariantsSetting->getData();
}
} catch (\Exception $e) {
//
$algoliaSkipVariants = false;
}
$data = $e->getObject();
$data->setGetInheritedValues(1);
$type = $data->getType();
if ($type === "variant" && $algoliaSkipVariants) {
return;
}
$indexName = $data->getProperty('algoliaIndexName');
if ($indexName) {
$index = $this->client->initIndex($indexName);
}
if ($algoliaSync && $indexName && !$data instanceof Folder) {
// $index->deleteObject($data->getId());
}
}
}
}