src/PromemoriaBundle/Services/HookObjectEvent.php line 32

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: lucamontanera
  5.  * Date: 2019-03-15
  6.  * Time: 17:32
  7.  */
  8. namespace App\PromemoriaBundle\Services;
  9. use Pimcore\Event\Model\ElementEventInterface;
  10. use Pimcore\Event\Model\DataObjectEvent;
  11. use Pimcore\Event\DataObjectEvents;
  12. class HookObjectEvent {
  13.     public function onPreAdd(ElementEventInterface $e) {
  14.         if($e instanceof DataObjectEvent) {
  15.             $obj $e->getObject();
  16.             if($obj->getType() !== 'folder'){
  17.                 $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  18.                 if(isset($config["autoNumDef"])){
  19.                     $this->addNumDef($obj);
  20.                 }
  21.                 $this->addDcTitle($obj);
  22.                 $this->addInventory($obj);
  23.                 $this->onPreUpdate($e);
  24.             }
  25.         }
  26.     }
  27.     public function onPreUpdate(ElementEventInterface $e){
  28.         if($e instanceof DataObjectEvent) {
  29.             $obj $e->getObject();
  30.             if($obj->getType() !== 'folder'){
  31.                 $this->updateCodiceGerarchico($obj);
  32.                 $this->updateDate($obj);
  33.                 $this->updateKey($obj);
  34.             }
  35.         }
  36.     }
  37.     public function onPostAdd(ElementEventInterface $e) {
  38.         if($e instanceof DataObjectEvent) {
  39.             $obj $e->getObject();
  40.             if($obj->getType() !== 'folder'){
  41.                 $this->syncObject($obj);
  42.                 \App\PromemoriaBundle\PromemoriaService::calculateAccuracy($obj);
  43.             }
  44.         }
  45.     }
  46.     public function onPostUpdate(ElementEventInterface $e) {
  47.         if($e instanceof DataObjectEvent) {
  48.             $obj $e->getObject();
  49.             if($obj->getType() !== 'folder'){
  50.                 $this->syncObject($obj);
  51.             }
  52.         }
  53.     }
  54.     public function onPostDelete(ElementEventInterface $e) {
  55.         if($e instanceof DataObjectEvent) {
  56.             $obj $e->getObject();
  57.             if($obj->getType() !== 'folder'){
  58.                 $this->syncObject($obj"delete");
  59.             }
  60.         }
  61.     }
  62.     private function addDcTitle($obj){
  63.         $title $this->getDcTitle($obj);
  64.         if(empty($title) && method_exists($obj"setDcTitle")){
  65.             $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  66.             method_exists($obj'getLocalizedfields') ? $obj->setDcTitle($obj->getKey(), isset($config["defaultLang"]) ? $config["defaultLang"] : 'it') : $obj->setDcTitle($obj->getKey());
  67.         }
  68.     }
  69.     private function addInventory($obj){
  70.         $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  71.         if(!empty($config["inventory"]) && method_exists($obj"setInventory")){
  72.             $className $obj->getClassName();
  73.             $classListing "\Pimcore\Model\DataObject\\"$className "\Listing";
  74.             $list = new $classListing();
  75.             $numero $list->count() + 1;
  76.             $obj->setInventory($config["inventory"] . "_" strtoupper(substr($className03) . "_" str_pad($numero4"0"STR_PAD_LEFT)));
  77.         }
  78.     }
  79.     private function addNumDef($obj){
  80.         $numDef $this->getNumDef($obj);
  81.         if(empty($numDef) && method_exists($obj'setDef_num')){
  82.             $value 1;
  83.             $siblings $obj->getSiblings(["object"], true);
  84.             if(!empty($siblings)){
  85.                 $last end($siblings);
  86.                 if(method_exists($last"getDef_num")) {
  87.                     $value = !empty($last->getDef_num()) ? (intval($last->getDef_num()) + 1) : 1;
  88.                 }
  89.             }
  90.             $obj->setDef_num($value);
  91.         }
  92.     }
  93.     private function updateKey($obj){
  94.         $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  95.         $key = [];
  96.         if(!empty($config["prefixKey"]) && method_exists($obj"get" ucfirst($config["prefixKey"]))){
  97.             $key[] = $obj->{"get" ucfirst($config["prefixKey"])}();
  98.         } else if(method_exists($obj"getCodice_gerarchico")){
  99.             $key[] = $obj->getCodice_gerarchico();
  100.         } else {
  101.             $key[] = $this->getNumDef($obj);
  102.         }
  103.         $key[] = $this->getDcTitle($obj);
  104.         if(method_exists($obj"getCodice")){
  105.             $key[] = $obj->getCodice();
  106.         }
  107.         $key[] = "(" $obj->getId() . ")";
  108.         $key implode(" "array_values(array_filter($key)));
  109.         if(!empty($key)){
  110.             $obj->setKey(\Pimcore\Model\Element\Service::getValidKey($key'object'));
  111.         }
  112.     }
  113.     private function updateCodiceGerarchico($obj){
  114.         if(method_exists($obj'setCodice_gerarchico')){
  115.             $codiceGerarchico = [$this->getNumDef($obj)];
  116.             $parent $obj;
  117.             while($parent $parent->getParent()){
  118.                 $codiceGerarchico[] = $this->getNumDef($parent);
  119.             }
  120.             $obj->setCodice_gerarchico(implode("."array_reverse(array_values(array_filter($codiceGerarchico)))));
  121.         }
  122.     }
  123.     private function updateDate($obj){
  124.         if(method_exists($obj"getDatetext") && empty($obj->getDatetext())){
  125.             $date = [];
  126.             $from method_exists($obj"getFrom") ? $obj->getFrom() : null;
  127.             $to method_exists($obj"getTo") ? $obj->getTo() : null;
  128.             if(!empty($from)){
  129.                 $date[] = $from->format("d-m-Y");
  130.                 if(!empty($to)){
  131.                     $date[] = $to->format("d-m-Y");
  132.                 }
  133.                 $date implode(" - "array_values(array_filter($date)));
  134.                 $obj->setDatetext($date);
  135.             }
  136.         }
  137.     }
  138.     private function getNumDef($obj){
  139.         $key = [];
  140.         $key[] = method_exists($obj"getDef_prefix") ? $obj->getDef_prefix() : "";
  141.         $key[] = method_exists($obj"getDef_num") ? $obj->getDef_num() : "";
  142.         $key[] = method_exists($obj"getDef_suffix") ? $obj->getDef_suffix() : "";
  143.         return implode(" "array_values(array_filter($key)));
  144.     }
  145.     private function getDcTitle($obj){
  146.         if(method_exists($obj'getLocalizedfields')){
  147.             $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  148.             return method_exists($obj"getDcTitle") ? $obj->getDcTitle(isset($config["defaultLang"]) ? $config["defaultLang"] : 'it') : "";
  149.         }else{
  150.             return method_exists($obj"getDcTitle") ? $obj->getDcTitle() : "";
  151.         }
  152.     }
  153.     private function syncObject($object$action="sync"){
  154.         $object_id $object->getId();
  155.         shell_exec("php /var/www/html/slim-sync/index.php /{$action}/object/{$object_id} >/dev/null 2>/dev/null &");
  156.     }
  157. }