bundles/Jabber/GenericBundle/EventListener/SpellCheck.php line 12

Open in your IDE?
  1. <?php 
  2. namespace Jabber\GenericBundle\EventListener;
  3. use Pimcore\Event\Model\ElementEventInterface;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject\Product;
  6. class SpellCheck {
  7.     public function onPreUpdate (ElementEventInterface $e) {
  8.         if (!$e->hasArgument('isAutoSave')) {
  9.             if ($e instanceof DataObjectEvent) {
  10.                 $dataObject $e->getObject();
  11.                 if ($dataObject instanceof Product) {
  12.                     $dataObject->setGetInheritedValues(false);
  13.                     $changes = [];
  14.                     $fields $this->GetFieldArray();
  15.                     $dictionary $this->GetDictionary();
  16.                     $keys $this->GetSearchArray($dictionary);
  17.                     foreach ($fields as $fieldname) {
  18.                         $value $this->GetValue($fieldname$dataObject);
  19.                         if ($value != false && !empty($value)) {
  20.                             $newValue $this->ReplaceSpellings($value$keys$dictionary);
  21.                             $dataObject $this->SetValue($fieldname$dataObject$newValue);
  22.                             if ($value != $newValue) {
  23.                                 $changes[] = "<b>$fieldname</b><br><b>Original value:</b> $value<br><b>New Value:</b> $newValue<br>";
  24.                             }
  25.                         }
  26.                     }
  27.                     //if ($this->NotificationRequired("last", $dataObject->getSKU())) {
  28.                         //$this->NotifyUsers($changes, "PIMCORE Spelling Corrections: " . $dataObject->getSKU());
  29.                     //}
  30.                     //$this->NotificationRequired("add", $dataObject->getSKU());
  31.                 }
  32.             }
  33.         }
  34.     }
  35.     private function NotifyUsers($changes$subject) {
  36.         if (count($changes) > 0) {
  37.             $htmlString implode("<br><hr>"$changes);
  38.             $mail = new \Pimcore\Mail();
  39.             $addresses = ['james.broady@pourmoi.co.uk''annabelle.roberts@pourmoi.co.uk'];
  40.             foreach ($addresses as $address) {
  41.                 $mail->addTo($address);
  42.             }
  43.             $mail->setSubject($subject);
  44.             $mail->html($htmlString);
  45.             $mail->send();
  46.         }
  47.     }
  48.     /**
  49.      * Gets the fields that require checks
  50.      *
  51.      * @return array
  52.      */
  53.     public function GetFieldArray() {
  54.         return [
  55.             'Name',
  56.             'BulletPoints'
  57.         ];
  58.     }
  59.     public function NotificationRequired($type$sku) {
  60.         $redis = new \Redis();
  61.         $redis->connect('127.0.0.1'6379);
  62.         $explodedString explode("."$sku);
  63.         $skuParts = [];
  64.         foreach ($explodedString as $key=>$value) {
  65.             if ($key 2) {
  66.                 $skuParts[] = $value;
  67.             }
  68.         }
  69.         $sku implode("."$skuParts);
  70.         if ($type == "add") {
  71.             $redis->set('pimcore_dictionary_notification'$sku);
  72.         } else if ($type == "last") {
  73.             $lastSku $redis->get('pimcore_dictionary_notification');
  74.             if ($lastSku == $sku) {
  75.                 return false;
  76.             }
  77.         }
  78.         return true;
  79.     }
  80.     
  81.     /**
  82.      * Gets the dictionary from the Redis store
  83.      *
  84.      * @return void
  85.      */
  86.     public function GetDictionary() {
  87.         $redis = new \Redis();
  88.         $redis->connect('127.0.0.1'6379);
  89.         $setMembers $redis->sMembers("pimcore_dictionary");
  90.         $memberArray = [];
  91.         foreach ($setMembers as $key=>$member) {
  92.             $memberData json_decode($membertrue);
  93.             $memberArray[array_key_first($memberData)] = $memberData[array_key_first($memberData)];
  94.         }
  95.         return $memberArray;
  96.     }
  97.     /**
  98.      * Gets the search keys for a dictionary
  99.      * 
  100.      * Keys will be wrapped in regex
  101.      *
  102.      * @param array $dictionary
  103.      * @return array
  104.      */
  105.     public function GetSearchArray($dictionary) {
  106.         $keys array_keys($dictionary);
  107.         foreach ($keys as $key=>$value) {
  108.             unset($keys[$key]);
  109.             $replacementString $dictionary[$value];
  110.             $keys['regular_case']['pattern'][] = "/\b$value\b/";
  111.             $keys['regular_case']['replacement'][] = $replacementString;
  112.             $keys['proper_case']['pattern'][] = "/\b" ucwords($value) . "\b/";
  113.             $keys['proper_case']['replacement'][] = ucwords($replacementString);
  114.             //$keys['regular_case'][$value] = "/\b$replacementString\b/";
  115.             //$keys['proper_case'][ucwords($value)] = "/\b" . ucwords($replacementString) . "\b/";
  116.         }
  117.         return $keys;
  118.     }
  119.     /**
  120.      * Returns the cleaned string
  121.      *
  122.      * @param string $string
  123.      * @param array $keys
  124.      * @param array $replacements
  125.      * @return string
  126.      */
  127.     public function ReplaceSpellings($string$keys$replacements) {
  128.         foreach ($keys as $key) {
  129.             foreach ($key['replacement'] as $replacementKey=>$replacement) {
  130.                 if (strpos($replacement"™") !== false) {
  131.                     if (strpos(strtolower($string), strtolower($replacement)) !== false) {
  132.                         unset($key['replacement'][$replacementKey]);
  133.                         unset($key['pattern'][$replacementKey]);
  134.                     }
  135.                 }
  136.             }
  137.             $string preg_replace($key['pattern'], $key['replacement'], $string);
  138.         }
  139.         return $string;
  140.     }
  141.     /**
  142.      * Gets the value from the object returns bool if the field doesn't exist
  143.      *
  144.      * @param string $field
  145.      * @param Product $object
  146.      * @return mixed
  147.      */
  148.     public function GetValue($field$object) {
  149.         $function "get" $field;
  150.         if (method_exists($object$function)) {
  151.             return $object->$function();
  152.         }
  153.         return false;
  154.     }
  155.     /**
  156.      * Sets the value from the object returns bool if the field doesn't exist
  157.      *
  158.      * @param string $field
  159.      * @param Product $object
  160.      * @return mixed
  161.      */
  162.     public function SetValue($field$object$value) {
  163.         $function "set" $field;
  164.         if (method_exists($object$function)) {
  165.             $object->$function($value);
  166.             return $object;
  167.         }
  168.         return false;
  169.     }
  170. }