src/PromemoriaBundle/Services/HookAssetEvent.php line 36

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: lucamontanera
  5.  * Date: 2019-03-15
  6.  * Time: 15:41
  7.  */
  8. namespace App\PromemoriaBundle\Services;
  9. use Pimcore\Event\Model\AssetEvent;
  10. use Pimcore\Event\Model\ElementEventInterface;
  11. use Pimcore\Model\Metadata\Predefined\Listing;
  12. use Pimcore\Model\Element\Tag;
  13. class HookAssetEvent {
  14.     public function onPreAdd(ElementEventInterface $e) {
  15.         if($e instanceof AssetEvent) {
  16.             $asset $e->getAsset();
  17.         }
  18.     }
  19.     public function onPostAdd(ElementEventInterface $e) {
  20.         if($e instanceof AssetEvent) {
  21.             $asset $e->getAsset();
  22.             $dir $asset->getParent();
  23.             if($dir->getType() === "folder"){
  24.                 $tags Tag::getTagsForElement("asset"$dir->getId());
  25.                 Tag::setTagsForElement("asset"$asset->getId(), $tags);
  26.             }
  27.             $this->onPostUpdate($e);
  28.         }
  29.     }
  30.     public function onPostUpdate(ElementEventInterface $e) {
  31.         if($e instanceof AssetEvent) {
  32.             $asset $e->getAsset();
  33.             $this->ocr($asset);
  34.             $this->siegfried($asset);
  35.             $this->sfogliatore($asset"putFile");
  36.             $this->tagFromColor($asset);
  37.             $this->syncAsset($asset);
  38.         }
  39.     }
  40.     public function onPostDelete(ElementEventInterface $e) {
  41.         if($e instanceof AssetEvent) {
  42.             $asset $e->getAsset();
  43.             $this->sfogliatore($asset"delFile");
  44.             $this->syncAsset($asset"delete");
  45.         }
  46.     }
  47.     private function tagFromColor($asset){
  48.         if($asset->getType()==='image'){
  49.             $colorService = new \App\PromemoriaBundle\Strumenti\ColorService($asset->getData());
  50.             $tags $colorService->getTag();
  51.             foreach($tags as $tagId){
  52.                 Tag::addTagToElement("asset"$asset->getId(), \Pimcore\Model\Element\Tag::getById($tagId));
  53.             }
  54.         }
  55.     }
  56.     private function siegfried($asset){
  57.         $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  58.         if(isset($config["siegfried"])){
  59.             $sf shell_exec('sf -json "' $asset->getFileSystemPath(). '"');
  60.             $version json_decode($sfTRUE);
  61.             if($version && isset($version["files"]) && isset($version["files"][0]) && isset($version["files"][0]["matches"])){
  62.                 $mime $version["files"][0]["matches"][0]["mime"];
  63.                 $versione $version["files"][0]["matches"][0]["version"];
  64.                 $asset->addMetadata("filesize""input"$version["files"][0]["filesize"]);
  65.                 $asset->addMetadata("modified""input"$version["files"][0]["modified"]);
  66.                 $asset->addMetadata("format""input"$version["files"][0]["matches"][0]["format"]);
  67.                 $asset->addMetadata("version""input"$versione);
  68.                 $asset->addMetadata("mime""input"$mime);
  69.                 //calcolo indice
  70.                 $digitalpreservation json_decode(file_get_contents(__DIR__ "/../digitalpreservation.json"), TRUE);
  71.                 $valori array_filter($digitalpreservation, function($value) use ($mime$versione){
  72.                     return $value["mimetype"] === $mime && (!isset($value["version"]) || $value["version"] == $versione);
  73.                 });
  74.                 if($valori){
  75.                     $asset->addMetadata("indice""input"array_values($valori)[0]["indice"]);
  76.                 }
  77.                 $asset->getDao()->update();
  78.             }
  79.         }
  80.     }
  81.     private function ocr($asset){
  82.         $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  83.         if(isset($config["ocr"]) && $asset->getMimetype() === 'application/pdf'){
  84.             $ocr shell_exec('pdftotext "/var/www/html/public/var/assets' $asset->getRealPath() . $asset->getFileName() . '" -');
  85.             $asset->addMetadata("ocr""textarea"$ocr);
  86.             $asset->getDao()->update();
  87.         }
  88.     }
  89.     private function sfogliatore($asset$action){
  90.         $config \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  91.         if(isset($config["sfogliatoreJwtToken"]) && $asset->getMimetype() === 'application/pdf' && !empty(\Pimcore\Tool::getHostUrl())){
  92.             $urlSfogliatore = isset($config["sfogliatoreUrl"]) ?: "https://sfogliatore.promemoriagroup.com";
  93.             $data = [
  94.                 "id" => strval($asset->getId()),
  95.                 "url" => \Pimcore\Tool::getHostUrl() . $asset->getFullPath()
  96.             ];
  97.             $data_string json_encode($data);
  98.             $ch curl_init($urlSfogliatore "/" $action);
  99.             curl_setopt($chCURLOPT_CUSTOMREQUEST"POST");
  100.             curl_setopt($chCURLOPT_POSTFIELDS$data_string);
  101.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  102.             curl_setopt($chCURLOPT_HTTPHEADER, [
  103.                 'Content-Type: application/json',
  104.                 'Content-Length: ' strlen($data_string),
  105.                 'Authorization: Bearer ' $config["sfogliatoreJwtToken"]
  106.             ]);
  107.             curl_exec($ch);
  108.         }
  109.     }
  110.     private function syncAsset($asset$action="sync"){
  111.         $asset_id $asset->getId();
  112.         shell_exec("php /var/www/html/slim-sync/index.php /{$action}/asset/{$asset_id} >/dev/null 2>/dev/null &");
  113.     }
  114. }