bundles/SalesAppBundle/Model/Document/Page.php line 21

Open in your IDE?
  1. <?php
  2. namespace SalesAppBundle\Model\Document;
  3. use Pimcore\Log\Simple;
  4. use Pimcore\Model\Asset\Image;
  5. use SalesAppBundle\Model\DataObject\Category;
  6. use Pimcore\Model\DataObject;
  7. use Pimcore\Model\DataObject\ShareItem;
  8. use Pimcore\Model\Document\Page as BasePage;
  9. use Pimcore\Model\Document\Editable\Area;
  10. use Pimcore\Model\Document\Editable\Areablock;
  11. use Pimcore\Model\Document;
  12. use Pimcore\Model\Element\Tag;
  13. use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Image\SalesAppHotspotImageDataFormatter;
  14. use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Image\SalesAppImageDataFormatter;
  15. use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Image\SalesAppImageGalleryDataFormatter;
  16. use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Relation\SalesAppDocumentDataFormatter;
  17. use WebAppBundle\Service\DataObject\DataObjectApiManager;
  18. class Page extends BasePage
  19. {
  20.     public function serializeData($language null)
  21.     {
  22.         $service = new Document\Service();
  23.         $translations $service->getTranslations($this);
  24.         $document $this;
  25.         $localizedDocument = (array_key_exists($language$translations)) ? Document::getById($translations[$language]) : null;
  26.         if($localizedDocument) {
  27.             $document $localizedDocument;
  28.         }
  29.         $backgroundImage $document->hasEditable('show-background-image') ? $document->getEditable('show-background-image')?->frontend() : 0;
  30.         $page = array(
  31.             'type' => 'page',
  32.             'id' => $document->getId(),
  33.             'previewData' => array(
  34.                 'title' =>
  35.                     ($document->getEditable('preview-title'))
  36.                         ? $document->getEditable('preview-title')->getValue()
  37.                         : "",
  38.             ),
  39.             'new' => ($document->getProperty('new')) ? $document->getProperty('new') : false,
  40.             'hidden' => ($document->getProperty('hidden')) ? $document->getProperty('hidden') : false,
  41.             'metadata' => array(
  42.                 'createdAt' => $document->getCreationDate(),
  43.                 'modifiedAt' => $document->getModificationDate(),
  44.                 'tags' => array()
  45.             ),
  46.             'openAs' => ($document->getEditable('open-as'))
  47.                 ? $document->getEditable('open-as')->getData()
  48.                 : "default",
  49.             'columns' => 1,
  50.             'contentElements' => array(),
  51.             'showBackgroundImage' => $backgroundImage,
  52.         );
  53.         $tags Tag::getTagsForElement('document'$document->getId());
  54.         foreach ($tags as $tag) {
  55.             $page['metadata']['tags'][] = $tag->getName();
  56.         }
  57.         /** @var Image $imageData */
  58.         $imageData = ($document->getEditable('preview-image')?->getImage()) ?: null;
  59.         $page['previewData']['image'] = array();
  60.         if($imageData) {
  61.             $page['previewData']['image'] = $imageData->serializeData($languagetrue);
  62.         }
  63.         // page media item
  64.         $pageMediaItem $document->getEditable('pageMediaItem');
  65.         if($pageMediaItem && $pageMediaItem->getCount() > 0) {
  66.             $page['columns'] = 2;
  67.             foreach ($pageMediaItem->getData() as $mediaItem) {
  68.                 $page['contentElements'][] =
  69.                     $this->serializeBrickData(
  70.                         $document,
  71.                         'pageMediaItem',
  72.                         $mediaItem['type'],
  73.                         $mediaItem['key'],
  74.                         0,
  75.                         $language
  76.                     );
  77.             }
  78.         }
  79.         // Content
  80.         $contentElements $document->getEditable('contentElements');
  81.         if($contentElements instanceof Areablock) {
  82.             foreach ($contentElements->getData() as $contentElement) {
  83.                 $page['contentElements'][] =
  84.                     $this->serializeBrickData(
  85.                         $document,
  86.                         'contentElements',
  87.                         $contentElement['type'],
  88.                         $contentElement['key'],
  89.                         ($page['columns'] == 1),
  90.                         $language
  91.                     );
  92.             }
  93.         } elseif($contentElements instanceof Area) {
  94.             if($contentElements->getElement('url')) {
  95.                 $page['contentElements'][] = $this->serializeBrickData(
  96.                     $document,
  97.                     'contentElements',
  98.                     'externalSite',
  99.                     1,
  100.                     0,
  101.                     $language
  102.                 );
  103.             } elseif (($contentElements->getElement('document'))) {
  104.                 $page['contentElements'][] = $this->serializeBrickData(
  105.                     $document,
  106.                     'contentElements',
  107.                     'document',
  108.                     1,
  109.                     0,
  110.                     $language
  111.                 );
  112.             } elseif (($contentElements->getElement('integratedApp'))) {
  113.                 $page['contentElements'][] = $this->serializeBrickData(
  114.                     $document,
  115.                     'contentElements',
  116.                     'integratedApp',
  117.                     1,
  118.                     0,
  119.                     $language
  120.                 );
  121.             } elseif (($contentElements->getElement('productTile'))) {
  122.                 $page['contentElements'][] = $this->serializeBrickData(
  123.                     $document,
  124.                     'contentElements',
  125.                     'productTile',
  126.                     1,
  127.                     0,
  128.                     $language
  129.                 );
  130.             }
  131.         }
  132.         return $page;
  133.     }
  134.     public function serializeBrickData($document$element$type$key$col 0$language null)
  135.     {
  136.         $serializedData = array();
  137.         switch ($type) {
  138.             case 'headline':
  139.                 $serializedData = array(
  140.                     'type' => 'headline',
  141.                     'mimeType' => "text/plain",
  142.                     'value' => $document->getEditable($element ':' $key "." $type)->getValue()
  143.                 );
  144.                 break;
  145.             case 'subHeadline':
  146.                 $serializedData = array(
  147.                     'type' => 'subHeadline',
  148.                     'mimeType' => "text/plain",
  149.                     'value' => $document->getEditable($element ':' $key "." $type)->getValue()
  150.                 );
  151.                 break;
  152.             case 'subline':
  153.                 $serializedData = array(
  154.                     'type' => 'subline',
  155.                     'mimeType' => "text/plain",
  156.                     'value' => $document->getEditable($element ':' $key "." $type)->getValue()
  157.                 );
  158.                 break;
  159.             case 'text':
  160.                 $serializedData = array(
  161.                     'type' => 'text',
  162.                     'mimeType' => "text/html",
  163.                     'value' => $document->getEditable($element ':' $key "." $type)->getValue()
  164.                 );
  165.                 break;
  166.             case 'image':
  167.                 $serializedData = array(
  168.                     'type' => 'image',
  169.                     'mimeType' => "image",
  170.                     'layout' => ($document->getEditable($element ':' $key ".layout"))
  171.                         ? $document->getEditable($element ':' $key ".layout")->getValue()
  172.                         : "default",
  173.                     'title' => ($document->getEditable($element ':' $key ".title"))
  174.                         ? $document->getEditable($element ':' $key ".title")->getValue()
  175.                         : ""
  176.                 );
  177.                 $imageElement $document->getEditable($element ':' $key "." $type);
  178.                 $serializedData['value'] = $imageElement->getImage()->serializeData($language);
  179.                 break;
  180.             case 'video':
  181.                 $serializedData = array(
  182.                     'type' => 'video',
  183.                     'mimeType' => "video",
  184.                     'layout' => ($document->getEditable($element ':' $key ".layout"))
  185.                         ? $document->getEditable($element ':' $key ".layout")->getValue()
  186.                         : "default",
  187.                     'controls' => ($document->getEditable($element ':' $key ".controls"))
  188.                         ? $document->getEditable($element ':' $key ".controls")->isChecked()
  189.                         : false,
  190.                     'autoplay' => ($document->getEditable($element ':' $key ".autoplay"))
  191.                         ? $document->getEditable($element ':' $key ".autoplay")->isChecked()
  192.                         : false,
  193.                     'loop' => ($document->getEditable($element ':' $key ".loop"))
  194.                         ? $document->getEditable($element ':' $key ".loop")->isChecked()
  195.                         : false,
  196.                     'muted' => ($document->getEditable($element ':' $key ".muted"))
  197.                         ? $document->getEditable($element ':' $key ".muted")->isChecked()
  198.                         : false,
  199.                     'value' => $document->getEditable($element ':' $key "." $type)->getVideoAsset()->serializeData($language),
  200.                     'poster' => ($document->getEditable($element ':' $key "." $type)->getPosterAsset())
  201.                         ? $document->getEditable($element ':' $key "." $type)->getPosterAsset()->serializeData($language)
  202.                         : null
  203.                 );
  204.                 break;
  205.             case 'textImage':
  206.                 $serializedData = array(
  207.                     'type' => 'textImage',
  208.                     'mimeType' => "multipart/mixed",
  209.                     'imagePosition' => ($document->getEditable($element ':' $key ".image-position"))
  210.                         ? $document->getEditable($element ':' $key ".image-position")->getValue()
  211.                         : "left",
  212.                     'text' => $document->serializeBrickData($document$element"text"$key),
  213.                     'image' => $document->serializeBrickData($document$element"image"$key)
  214.                 );
  215.                 break;
  216.             case 'externalSite':
  217.                 $serializedData = array(
  218.                     'type' => 'externalSite',
  219.                     'mimeType' => "text/plain",
  220.                     'value' => $document->getEditable($element ':' $key ".url")->getValue()
  221.                 );
  222.                 break;
  223.             case 'document':
  224.                 $docElement $document->getEditable($element ':' $key "." $type)->getElement();
  225.                 $serializedData = array(
  226.                     'type' => 'document',
  227.                     'mimeType' => "document",
  228.                     'value' => $docElement $docElement->serializeData($language) : null
  229.                 );
  230.                 break;
  231.             case 'hotspot':
  232.                 $serializedData = array(
  233.                     'type' => 'hotspot',
  234.                     'mimeType' => "hotspot",
  235.                     'layout' => ($document->getEditable($element ':' $key ".layout"))
  236.                         ? $document->getEditable($element ':' $key ".layout")->getValue()
  237.                         : "default",
  238.                     'video' => null,
  239.                     'hotspots' => array()
  240.                 );
  241.                 $imageElement $document->getEditable($element ':' $key ".image");
  242.                 $serializedData['image'] = $imageElement->getImage()->serializeData($language);
  243.                 if($imageElement->getHotspots() && count($imageElement->getHotspots()) > 0) {
  244.                     $serializedData['hotspots'] = $imageElement->getHotspots();
  245.                 }
  246.                 $videoElement $this->getEditable($element ':' $key ".video");
  247.                 if($videoElement->getVideoAsset()) {
  248.                     $serializedData['video'] = $videoElement->getVideoAsset()->serializeData($language);
  249.                 }
  250.                 break;
  251.             case 'facts':
  252.                 $serializedData = array(
  253.                     'type' => 'facts',
  254.                     'mimeType' => "facts",
  255.                     'values' => array()
  256.                 );
  257.                 $factsElement $document->getEditable($element ':' $key "." $type);
  258.                 foreach ($factsElement->getElements() as $factElement) {
  259.                     $serializedData['values'][] =
  260.                         array(
  261.                             "label" => $factElement->getInput('label')->getValue(),
  262.                             "value" => (float) $factElement->getNumeric('value')->getValue(),
  263.                             "prefix" => $factElement->getInput('prefix')->getValue(),
  264.                             "suffix" => $factElement->getInput('suffix')->getValue(),
  265.                             "decimalPlaces" => (int) $factElement->getNumeric('decimalPlaces')->getValue(),
  266.                             "startValue" => (float) $factElement->getNumeric('startValue')->getValue(),
  267.                         );
  268.                 }
  269.                 break;
  270.             case 'teaserBox':
  271.                 $serializedData = array(
  272.                     'type' => 'teaserBox',
  273.                     'mimeType' => "teaserBox",
  274.             'layout' => ($document->getEditable($element ':' $key ".layout"))
  275.                         ? $document->getEditable($element ':' $key ".layout")->getValue()
  276.                         : "grid",
  277.                     'teasers' => array()
  278.                 );
  279.                 $teasersElement $document->getEditable($element ':' $key "." $type);
  280.                 foreach ($teasersElement->getElements() as $teaserElement) {
  281.                     if(is_object($teaserElement->getImage('image')->getImage())) {
  282.                         $serializedData['teasers'][] =
  283.                             array(
  284.                                 "image" => $teaserElement->getImage('image')->getImage()->serializeData($language),
  285.                                 "title" => $teaserElement->getInput('title')->getValue(),
  286.                                 "link" => ($teaserElement->getRelation('link')->getElement())
  287.                                     ? $teaserElement->getRelation('link')->getElement()->getId()
  288.                                     : null
  289.                             );
  290.                     }
  291.                 }
  292.                 break;
  293.             case 'button':
  294.                 $serializedData = array(
  295.                     'type' => 'button',
  296.                     'mimeType' => "text/plain",
  297.                     'link' => ($document->getEditable($element ':' $key ".link"))
  298.                         ? $document->getEditable($element ':' $key ".link")->getElement()->getId()
  299.                         : null,
  300.                     'title' => ($document->getEditable($element ':' $key ".title"))
  301.                         ? $document->getEditable($element ':' $key ".title")->getValue()
  302.                         : "",
  303.                 );
  304.                 break;
  305.             case 'youtube':
  306.                 $serializedData = array(
  307.                     'type' => 'youtube',
  308.                     'mimeType' => "text/plain",
  309.                     'value' => $document->getEditable($element ':' $key "." $type)->getValue()
  310.                 );
  311.                 break;
  312.             case 'slider':
  313.                 $serializedData = array(
  314.                     'type' => 'slider',
  315.                     'mimeType' => "multipart/mixed",
  316.                     'values' => array(),
  317.                     'layout' => ($document->getEditable($element ':' $key ".layout"))
  318.                         ? $document->getEditable($element ':' $key ".layout")->getValue()
  319.                         : "default",
  320.                     'title' => ($document->getEditable($element ':' $key ".title"))
  321.                         ? $document->getEditable($element ':' $key ".title")->getValue()
  322.                         : ""
  323.                 );
  324.                 $sliderElement $document->getEditable($element ':' $key ".values");
  325.                 foreach ($sliderElement->getElements() as $sliderElement) {
  326.                     if(is_object($sliderElement->getImage('image')->getImage())) {
  327.                         $serializedData['values'][] =
  328.                             $sliderElement->getImage('image')->getImage()->serializeData($language);
  329.                     }
  330.                 }
  331.                 break;
  332.             case 'integratedApp':
  333.                 $serializedData = array(
  334.                     'type' => 'integratedApp',
  335.                     'mimeType' => "application/zip",
  336.                     'value' => $document->getEditable($element ':' $key "." $type)->getElement()->serializeData($language)
  337.                 );
  338.                 break;
  339.             case 'productTile':
  340.                 $apiManager = new DataObjectApiManager();
  341.                 $contentElements $document->getEditable('contentElements');
  342.                 /** @var Document\Editable\Relation $productTileEditable */
  343.                 $productTileEditable $contentElements->getElement('productTile');
  344.                 /** @var DataObject\ProductTile $productTile */
  345.                 $productTile $productTileEditable->getElement();
  346.                 $params = [
  347.                     'formatters' => [
  348.                         'mainImage' => new SalesAppHotspotImageDataFormatter(),
  349.                         'teaserImage' => new SalesAppHotspotImageDataFormatter(),
  350.                         'ogImage' => new SalesAppHotspotImageDataFormatter(),
  351.                         'twitterImage' => new SalesAppHotspotImageDataFormatter(),
  352.                         'sliderImages' => new SalesAppImageGalleryDataFormatter(),
  353.                         'detailImages' => new SalesAppImageGalleryDataFormatter(),
  354.                         'image' => new SalesAppImageDataFormatter(),
  355.                         'brochures' => new SalesAppDocumentDataFormatter(),
  356.                         'caseStudies' => new SalesAppDocumentDataFormatter(),
  357.                     ]
  358.                 ];
  359.                 $serializedData = [
  360.                     'type' => 'productTile',
  361.                     'mimeType' => "application/json"
  362.                 ];
  363.                 if ($productTile) {
  364.                     $serializedData['productTile'] = $apiManager->getDataObjectData($productTile$language, [], [], $params);
  365.                 } else {
  366.                     $serializedData['productTile'] = [];
  367.                 }
  368.                 break;
  369.         }
  370.         $serializedData['col'] = $col;
  371.         return $serializedData;
  372.     }
  373.     public function getShareableData($bookmarkListId$language)
  374.     {
  375.         $shareableData $this->getEditable('preview-title');
  376.         if($this->getEditable('web-reference-url')) {
  377.             if($this->getEditable('web-reference-url') != "") {
  378.                 $shareableData '<a href="' $this->getEditable('web-reference-url') . '">' $shareableData '</a>';
  379.             } else {
  380.                 $shareableData null;
  381.             }
  382.         } elseif($this->getEditable('contentElements:1.url')) {
  383.             if($this->getEditable('contentElements:1.url') != "") {
  384.                 $shareableData '<a href="' $this->getEditable('contentElements:1.url') . '">' $shareableData '</a>';
  385.             } else {
  386.                 $shareableData null;
  387.             }
  388.         } elseif($this->getEditable('contentElements:1.document')) {
  389.             $newShareItem = new ShareItem();
  390.             $newShareItem->setUid(bin2hexrandom_bytes(24) ));
  391.             $expiryDate = new \DateTime();
  392.             $expiryDate->modify('+14 days');
  393.             $newShareItem->setExpiryDate($expiryDate);
  394.             $newShareItem->setPage($this);
  395.             $newShareItem->setParentId($bookmarkListId);
  396.             $newShareItem->setKey(\Pimcore\File::getValidFilename(
  397.                 "shareItem_" $newShareItem->getUid())
  398.             );
  399.             $newShareItem->setPublished(true);
  400.             $newShareItem->save();
  401.             $shareableData '<a href="' \Pimcore\Tool::getHostUrl('https') . '/share/' $newShareItem->getUid() . '">' $shareableData '</a>';
  402.         } else {
  403.             $shareableData null;
  404.         }
  405.         return $shareableData;
  406.     }
  407.     public function getPlainShareableData($bookmarkListId$language): ?string
  408.     {
  409.         $shareableData $this->getEditable('preview-title');
  410.         if($this->getEditable('web-reference-url')) {
  411.             if($this->getEditable('web-reference-url') != "") {
  412.                 // $shareableData = '<a href="' . $this->getElement('web-reference-url') . '">' . $shareableData . '</a>';
  413.                 $shareableData $shareableData.': '.$this->getEditable('web-reference-url').' %0D%0A';
  414.             } else {
  415.                 $shareableData null;
  416.             }
  417.         } elseif($this->getEditable('contentElements:1.url')) {
  418.             if($this->getEditable('contentElements:1.url') != "") {
  419.                 // $shareableData = '<a href="' . $this->getElement('contentElements:1.url') . '">' . $shareableData . '</a>';
  420.                 $shareableData $shareableData.': '.$this->getEditable('contentElements:1.url').' %0D%0A';
  421.             } else {
  422.                 $shareableData null;
  423.             }
  424.         } elseif($this->getEditable('contentElements:1.document')) {
  425.             $newShareItem = new ShareItem();
  426.             $newShareItem->setUid(bin2hexrandom_bytes(24) ));
  427.             $expiryDate = new \DateTime();
  428.             $expiryDate->modify('+14 days');
  429.             $newShareItem->setExpiryDate($expiryDate);
  430.             $newShareItem->setPage($this);
  431.             $newShareItem->setParentId($bookmarkListId);
  432.             $newShareItem->setKey(\Pimcore\File::getValidFilename(
  433.                 "shareItem_" $newShareItem->getUid())
  434.             );
  435.             $newShareItem->setPublished(true);
  436.             $newShareItem->save();
  437.             //$shareableData = '<a href="' . \Pimcore\Tool::getHostUrl('https') . '/share/' . $newShareItem->getUid() . '">' . $shareableData . '</a>';
  438.             $shareableData $shareableData.': '.\Pimcore\Tool::getHostUrl('https') . '/share/' $newShareItem->getUid().' %0D%0A';
  439.         } else {
  440.             $shareableData null;
  441.         }
  442.         return $shareableData;
  443.     }
  444.     public function getContextList() {
  445.         $contexts = array();
  446.         $dependencies $this->getDependencies();
  447.         foreach ($dependencies->getRequiredBy() as $requiredBy) {
  448.             if($requiredBy['type'] == 'object') {
  449.                 $object DataObject::getById($requiredBy['id']);
  450.                 if($object instanceof Category) {
  451.                     $def $object->getClass()->getFieldDefinition("contexts");
  452.                     $refKey $def->getOwnerFieldName();
  453.                     $refId $def->getOwnerClassId();
  454.                     $nonOwnerRelations $object->getRelationData($refKeyfalse$refId);
  455.                     foreach($nonOwnerRelations as $nonOwnerRelation) {
  456.                         if($nonOwnerRelation['subtype'] == "Context") {
  457.                             $contexts[$nonOwnerRelation['id']] = $nonOwnerRelation['id'];
  458.                         }
  459.                     }
  460.                 }
  461.             }
  462.         }
  463.         return $contexts;
  464.     }
  465. }