<?php
namespace Jabber\GenericBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Product;
class SpellCheck {
public function onPreUpdate (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
$dataObject->setGetInheritedValues(false);
$changes = [];
$fields = $this->GetFieldArray();
$dictionary = $this->GetDictionary();
$keys = $this->GetSearchArray($dictionary);
foreach ($fields as $fieldname) {
$value = $this->GetValue($fieldname, $dataObject);
if ($value != false && !empty($value)) {
$newValue = $this->ReplaceSpellings($value, $keys, $dictionary);
$dataObject = $this->SetValue($fieldname, $dataObject, $newValue);
if ($value != $newValue) {
$changes[] = "<b>$fieldname</b><br><b>Original value:</b> $value<br><b>New Value:</b> $newValue<br>";
}
}
}
//if ($this->NotificationRequired("last", $dataObject->getSKU())) {
//$this->NotifyUsers($changes, "PIMCORE Spelling Corrections: " . $dataObject->getSKU());
//}
//$this->NotificationRequired("add", $dataObject->getSKU());
}
}
}
}
private function NotifyUsers($changes, $subject) {
if (count($changes) > 0) {
$htmlString = implode("<br><hr>", $changes);
$mail = new \Pimcore\Mail();
$addresses = ['james.broady@pourmoi.co.uk', 'annabelle.roberts@pourmoi.co.uk'];
foreach ($addresses as $address) {
$mail->addTo($address);
}
$mail->setSubject($subject);
$mail->html($htmlString);
$mail->send();
}
}
/**
* Gets the fields that require checks
*
* @return array
*/
public function GetFieldArray() {
return [
'Name',
'BulletPoints'
];
}
public function NotificationRequired($type, $sku) {
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$explodedString = explode(".", $sku);
$skuParts = [];
foreach ($explodedString as $key=>$value) {
if ($key < 2) {
$skuParts[] = $value;
}
}
$sku = implode(".", $skuParts);
if ($type == "add") {
$redis->set('pimcore_dictionary_notification', $sku);
} else if ($type == "last") {
$lastSku = $redis->get('pimcore_dictionary_notification');
if ($lastSku == $sku) {
return false;
}
}
return true;
}
/**
* Gets the dictionary from the Redis store
*
* @return void
*/
public function GetDictionary() {
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$setMembers = $redis->sMembers("pimcore_dictionary");
$memberArray = [];
foreach ($setMembers as $key=>$member) {
$memberData = json_decode($member, true);
$memberArray[array_key_first($memberData)] = $memberData[array_key_first($memberData)];
}
return $memberArray;
}
/**
* Gets the search keys for a dictionary
*
* Keys will be wrapped in regex
*
* @param array $dictionary
* @return array
*/
public function GetSearchArray($dictionary) {
$keys = array_keys($dictionary);
foreach ($keys as $key=>$value) {
unset($keys[$key]);
$replacementString = $dictionary[$value];
$keys['regular_case']['pattern'][] = "/\b$value\b/";
$keys['regular_case']['replacement'][] = $replacementString;
$keys['proper_case']['pattern'][] = "/\b" . ucwords($value) . "\b/";
$keys['proper_case']['replacement'][] = ucwords($replacementString);
//$keys['regular_case'][$value] = "/\b$replacementString\b/";
//$keys['proper_case'][ucwords($value)] = "/\b" . ucwords($replacementString) . "\b/";
}
return $keys;
}
/**
* Returns the cleaned string
*
* @param string $string
* @param array $keys
* @param array $replacements
* @return string
*/
public function ReplaceSpellings($string, $keys, $replacements) {
foreach ($keys as $key) {
foreach ($key['replacement'] as $replacementKey=>$replacement) {
if (strpos($replacement, "™") !== false) {
if (strpos(strtolower($string), strtolower($replacement)) !== false) {
unset($key['replacement'][$replacementKey]);
unset($key['pattern'][$replacementKey]);
}
}
}
$string = preg_replace($key['pattern'], $key['replacement'], $string);
}
return $string;
}
/**
* Gets the value from the object returns bool if the field doesn't exist
*
* @param string $field
* @param Product $object
* @return mixed
*/
public function GetValue($field, $object) {
$function = "get" . $field;
if (method_exists($object, $function)) {
return $object->$function();
}
return false;
}
/**
* Sets the value from the object returns bool if the field doesn't exist
*
* @param string $field
* @param Product $object
* @return mixed
*/
public function SetValue($field, $object, $value) {
$function = "set" . $field;
if (method_exists($object, $function)) {
$object->$function($value);
return $object;
}
return false;
}
}