src/PromemoriaBundle/Services/HookObjectEvent.php line 44

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