src/PromemoriaBundle/Services/HookObjectEvent.php line 54

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