src/Helper/DocumentHelper.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Helper;
  3. use Pimcore\Model\Document;
  4. use Pimcore\Model\Document\Editable;
  5. use App\Helper\ObjectJson;
  6. use App\Helper\Text\PrettyText;
  7. class DocumentHelper
  8. {
  9.     public static function getPageUrl(Document $page)
  10.     {
  11.         try {
  12.             return $page->getPrettyUrl() ?? '/'strtolower(PrettyText::getPretty($page->getTitle() ?? 'page')) .'~'$page->getId();
  13.         } catch (\Throwable $e) {
  14.         }
  15.         return null;
  16.     }
  17.     public static function getDataByDocument($document)
  18.     {
  19.         $data = [];
  20.         $blockEditables = [];
  21.         try {
  22.             if ($document instanceof Document\Page || $document instanceof Document\Snippet) {
  23.                 $editables $document->getEditables();
  24.                 foreach ($editables as $field => $editable) {
  25.                     if ($editable instanceof Editable\Block) {
  26.                         $blockEditables[] = $field;
  27.                     }
  28.                     $data[$field] = self::getValueByType($editable,$field);
  29.                 }
  30.                 if (!empty($blockEditables)) {
  31.                     foreach ($blockEditables as $field) {
  32.                         $totalLoop $data[$field];
  33.                         unset($data[$field]); // xóa dữ liệu block ban đầu
  34.                         foreach ($totalLoop as $loop) {
  35.                             $loopData = []; // dữ liệu mới cho block
  36.                             foreach ($data as $name => $value) {
  37.                                 $find $field .":"$loop .".";
  38.                                 if (strpos($name$find) !== false) {
  39.                                     $elmentName substr($namestrlen($find));
  40.                                     $loopData[$elmentName] = $value;
  41.                                     unset($data[$name]);
  42.                                 }
  43.                             }
  44.                             $data[$field][] = $loopData;
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         } catch (\Throwable $e) {
  50.         }
  51.         return $data;
  52.     }
  53.     public static function getDataByPath($path)
  54.     {
  55.         $data = [];
  56.         try {
  57.             $document Document::getByPath($path);
  58.             if ($document instanceof Document\Link) {
  59.                 $document Document::getByPath($document->getHref());
  60.             }
  61.             return self::getDataByDocument($document);
  62.         } catch (\Throwable $e) {
  63.             
  64.         }
  65.         return $data;
  66.     }
  67.     public static function getValueByType($editable,$field)
  68.     {
  69.         if ($editable instanceof Editable\Input
  70.             || $editable instanceof Editable\Textarea
  71.             || $editable instanceof Editable\Checkbox
  72.             || $editable instanceof Editable\Select
  73.             || $editable instanceof Editable\Multiselect
  74.             || $editable instanceof Editable\Block
  75.         ) {
  76.             return $editable->getData();
  77.         }
  78.         if ($editable instanceof Editable\Wysiwyg) {
  79.             return PrettyText::formatWysiwyg($editable->getData());
  80.         }
  81.         if ($editable instanceof Editable\Date) {
  82.             return $editable->getData() ? $editable->getData()->format('d-m-Y') : null;
  83.         }
  84.         if ($editable instanceof Editable\Image) {
  85.             if($field=='logo'||$field=='logoMobile'){
  86.                 return $editable->isEmpty() ?  null AssetHelper::getThumbnailLink($editable->getImage(),'logo');
  87.             }
  88.             if($field=='logoFooter'){
  89.                 return $editable->isEmpty() ?  null AssetHelper::getThumbnailLink($editable->getImage(),'logoFooter');
  90.             }
  91.             return $editable->isEmpty() ?  null AssetHelper::getLink($editable->getImage());
  92.         }
  93.         $hiden=['provider'];
  94.         if ($editable instanceof Editable\Relation){
  95.             return $editable->isEmpty() ? null ObjectJson::getJson($editable->getElement(),$hiden);
  96.         }
  97.         if ($editable instanceof Editable\Relations){
  98.             $elements = [];
  99.             
  100.             if (!$editable->isEmpty()) {
  101.                
  102.                 foreach ($editable->getElements() as $element) {
  103.                     if (method_exists($element"getJson")) {
  104.                         $elements[] = $element->getJson();
  105.                     }else{
  106.                         $elements[] = ObjectJson::getJson($element,$hiden);
  107.                     }
  108.                 }
  109.             }
  110.             
  111.             return $elements;
  112.         }
  113.         if ($editable instanceof Editable\Link) {
  114.             $link = [
  115.                 'isPage' => false,
  116.                 'urlOrSlug' => '',
  117.                 'nameOrTitle' => ''
  118.             ];
  119.             if (!$editable->isEmpty()) {
  120.                 $internal $editable->getData()['internal'];
  121.                 $link['urlOrSlug'] = $editable->getHref();
  122.                 $link['nameOrTitle'] = $editable->getText();
  123.                 if ($internal) {
  124.                     $internalType $editable->getData()['internalType'];
  125.                     if ($internalType == 'document') {
  126.                         $internalId $editable->getData()['internalId'];
  127.                         $page Document::getById($internalId);
  128.                         if ($page) {
  129.                             $link['isPage'] = true;
  130.                             $link['urlOrSlug'] = self::getPageUrl($page);
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.             return $link;
  136.         }
  137.         if ($editable instanceof Editable\Renderlet) {
  138.             if ($editable->getSubType() == "folder") {
  139.                 $folder $editable->getO();
  140.                 if ($folder instanceof \Pimcore\Model\Asset\Folder) {
  141.                     $imageFolders = [];
  142.                     $images $folder->getChildren();
  143.                     foreach ($images as $image) {
  144.                         if ($image instanceof \Pimcore\Model\Asset\Image) {
  145.                             
  146.                             $imageLink AssetHelper::getLink($image);
  147.                             if ($imageLink) {
  148.                                 $imageFolders[] = $imageLink;
  149.                             }
  150.                         }
  151.                     }
  152.                     return $imageFolders;
  153.                 }
  154.             }
  155.         }
  156.         if ($editable instanceof Editable\Video) {
  157.             $editableData $editable->getData();
  158.             $type $editableData['type'];
  159.             $id $editableData['id'];
  160.             $data = [
  161.                 'type' => $type,
  162.                 'data' => '',
  163.                 'link' => $id,
  164.                 'image' => ''
  165.             ];
  166.             if ($type == 'asset') {
  167.                 $link AssetHelper::getLink(\Pimcore\Model\Asset::getById($id));
  168.                 
  169.                 $data['data'] = $link;
  170.             }
  171.             if ($type == 'youtube') {
  172.                 $videoID explode("?v="$id);
  173.                 if (count($videoID) == 1) {
  174.                     $videoID $videoID[0];
  175.                 } else {
  176.                     $videoID $videoID[1];
  177.                     $videoID explode("&"$videoID)[0];
  178.                 }
  179.                 $data['data'] = $videoID;
  180.                 $data['image'] = "https://img.youtube.com/vi/"$videoID ."/0.jpg";
  181.             }
  182.             return $data;
  183.         }
  184.         return $editable->getData();
  185.     }
  186. }