<?php
/**
* Created by PhpStorm.
* User: lucamontanera
* Date: 2019-03-15
* Time: 17:32
*/
namespace App\PromemoriaBundle\Services;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\DataObjectEvents;
class HookObjectEvent {
public function onPreAdd(ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj->getType() !== 'folder'){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(isset($config["autoNumDef"])){
$this->addNumDef($obj);
}
$this->addDcTitle($obj);
$this->addAutoMeta($obj);
$this->addInventory($obj);
$this->onPreUpdate($e);
}
}
}
public function onPreUpdate(ElementEventInterface $e){
if($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj->getType() !== 'folder'){
$this->updateCodiceGerarchico($obj);
$this->updateDate($obj);
$this->updateKey($obj);
}
}
}
public function onPostAdd(ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj->getType() !== 'folder'){
$this->addAutoTag($obj);
$this->syncObject($obj);
\App\PromemoriaBundle\PromemoriaService::calculateAccuracy($obj);
}
}
}
public function onPostUpdate(ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj->getType() !== 'folder'){
$this->syncObject($obj);
}
}
}
public function onPostDelete(ElementEventInterface $e) {
if($e instanceof DataObjectEvent) {
$obj = $e->getObject();
if($obj->getType() !== 'folder'){
$this->syncObject($obj, "delete");
}
}
}
private function addDcTitle($obj){
$title = $this->getDcTitle($obj);
if(empty($title) && method_exists($obj, "setDcTitle")){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
method_exists($obj, 'getLocalizedfields') ? $obj->setDcTitle($obj->getKey(), isset($config["defaultLang"]) ? $config["defaultLang"] : 'it') : $obj->setDcTitle($obj->getKey());
}
}
private function addAutoMeta($obj){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(!empty($config["autometa"])){
$method = "set" . ucfirst($config["autometa"]["field"]);
if(method_exists($obj, $method)){
$parent = $obj->getParent();
while($parent->getId() > 1){
if($parent->getId() == $config["autometa"]["parent"]){
$obj->$method($config["autometa"]["value"]);
break;
}
$parent = $parent->getParent();
}
}
}
}
private function addAutoTag($obj){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(!empty($config["autotag"])){
$parent = $obj->getParent();
while($parent->getId() > 1){
if($parent->getId() == $config["autotag"]["parent"]){
$tag = \Pimcore\Model\Element\Tag::getById((int)$config["autotag"]["value"]);
\Pimcore\Model\Element\Tag::addTagToElement('object', $obj->getId(), $tag);
break;
}
$parent = $parent->getParent();
}
}
}
private function addInventory($obj){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(!empty($config["inventory"]) && method_exists($obj, "setInventory")){
$className = $obj->getClassName();
$classListing = "\Pimcore\Model\DataObject\\". $className . "\Listing";
$list = new $classListing();
$numero = $list->count() + 1;
$obj->setInventory($config["inventory"] . "_" . strtoupper(substr($className, 0, 3) . "_" . str_pad($numero, 4, "0", STR_PAD_LEFT)));
}
}
private function addNumDef($obj){
$numDef = $this->getNumDef($obj);
if(empty($numDef) && method_exists($obj, 'setDef_num')){
$value = 1;
$siblings = $obj->getSiblings(["object"], true);
if(!empty($siblings)){
$last = end($siblings);
if(method_exists($last, "getDef_num")) {
$value = !empty($last->getDef_num()) ? (intval($last->getDef_num()) + 1) : 1;
}
}
$obj->setDef_num($value);
}
}
private function updateKey($obj){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
$key = [];
if(!empty($config["prefixKey"]) && method_exists($obj, "get" . ucfirst($config["prefixKey"]))){
$key[] = $obj->{"get" . ucfirst($config["prefixKey"])}();
} else if(method_exists($obj, "getCodice_gerarchico")){
$key[] = $obj->getCodice_gerarchico();
} else {
$key[] = $this->getNumDef($obj) ?: $this->getNumProv($obj);
}
$key[] = $this->getDcTitle($obj);
if(method_exists($obj, "getCodice")){
$key[] = $obj->getCodice();
}
$key[] = "(" . $obj->getId() . ")";
$key = implode(" ", array_values(array_filter($key)));
if(!empty($key)){
$obj->setKey(\Pimcore\Model\Element\Service::getValidKey($key, 'object'));
}
}
private function updateCodiceGerarchico($obj){
if(method_exists($obj, 'setCodice_gerarchico')){
$codiceGerarchico = [$this->getNumDef($obj)];
$parent = $obj;
while($parent = $parent->getParent()){
$codiceGerarchico[] = $this->getNumDef($parent);
}
$obj->setCodice_gerarchico(implode(".", array_reverse(array_values(array_filter($codiceGerarchico)))));
}
}
private function updateDate($obj){
if(method_exists($obj, "getDatetext") && empty($obj->getDatetext())){
$date = [];
$from = method_exists($obj, "getFrom") ? $obj->getFrom() : null;
$to = method_exists($obj, "getTo") ? $obj->getTo() : null;
if(!empty($from)){
$date[] = $from->format("d-m-Y");
if(!empty($to)){
$date[] = $to->format("d-m-Y");
}
$date = implode(" - ", array_values(array_filter($date)));
$obj->setDatetext($date);
}
}
}
private function getNumDef($obj){
$key = [];
$key[] = method_exists($obj, "getDef_prefix") ? $obj->getDef_prefix() : "";
$key[] = method_exists($obj, "getDef_num") ? $obj->getDef_num() : "";
$key[] = method_exists($obj, "getDef_suffix") ? $obj->getDef_suffix() : "";
return implode(" ", array_values(array_filter($key)));
}
private function getNumProv($obj){
$key = [];
$key[] = method_exists($obj, "getProv_prefix") ? $obj->getProv_prefix() : "";
$key[] = method_exists($obj, "getProv_num") ? $obj->getProv_num() : "";
$key[] = method_exists($obj, "getProv_suffix") ? $obj->getProv_suffix() : "";
return implode(" ", array_values(array_filter($key)));
}
private function getDcTitle($obj){
if(method_exists($obj, 'getLocalizedfields')){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
return method_exists($obj, "getDcTitle") ? $obj->getDcTitle(isset($config["defaultLang"]) ? $config["defaultLang"] : 'it') : "";
}else{
return method_exists($obj, "getDcTitle") ? $obj->getDcTitle() : "";
}
}
private function syncObject($object, $action="sync"){
$object_id = $object->getId();
shell_exec("php /var/www/html/slim-sync/index.php /{$action}/object/{$object_id} >/dev/null 2>/dev/null &");
}
}