<?php
namespace SalesAppBundle\Model\Document;
use Pimcore\Log\Simple;
use Pimcore\Model\Asset\Image;
use SalesAppBundle\Model\DataObject\Category;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ShareItem;
use Pimcore\Model\Document\Page as BasePage;
use Pimcore\Model\Document\Editable\Area;
use Pimcore\Model\Document\Editable\Areablock;
use Pimcore\Model\Document;
use Pimcore\Model\Element\Tag;
use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Image\SalesAppHotspotImageDataFormatter;
use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Image\SalesAppImageDataFormatter;
use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Image\SalesAppImageGalleryDataFormatter;
use SalesAppBundle\Service\DataObject\DataComponents\Formatter\Relation\SalesAppDocumentDataFormatter;
use WebAppBundle\Service\DataObject\DataObjectApiManager;
class Page extends BasePage
{
public function serializeData($language = null)
{
$service = new Document\Service();
$translations = $service->getTranslations($this);
$document = $this;
$localizedDocument = (array_key_exists($language, $translations)) ? Document::getById($translations[$language]) : null;
if($localizedDocument) {
$document = $localizedDocument;
}
$backgroundImage = $document->hasEditable('show-background-image') ? $document->getEditable('show-background-image')?->frontend() : 0;
$page = array(
'type' => 'page',
'id' => $document->getId(),
'previewData' => array(
'title' =>
($document->getEditable('preview-title'))
? $document->getEditable('preview-title')->getValue()
: "",
),
'new' => ($document->getProperty('new')) ? $document->getProperty('new') : false,
'hidden' => ($document->getProperty('hidden')) ? $document->getProperty('hidden') : false,
'metadata' => array(
'createdAt' => $document->getCreationDate(),
'modifiedAt' => $document->getModificationDate(),
'tags' => array()
),
'openAs' => ($document->getEditable('open-as'))
? $document->getEditable('open-as')->getData()
: "default",
'columns' => 1,
'contentElements' => array(),
'showBackgroundImage' => $backgroundImage,
);
$tags = Tag::getTagsForElement('document', $document->getId());
foreach ($tags as $tag) {
$page['metadata']['tags'][] = $tag->getName();
}
/** @var Image $imageData */
$imageData = ($document->getEditable('preview-image')?->getImage()) ?: null;
$page['previewData']['image'] = array();
if($imageData) {
$page['previewData']['image'] = $imageData->serializeData($language, true);
}
// page media item
$pageMediaItem = $document->getEditable('pageMediaItem');
if($pageMediaItem && $pageMediaItem->getCount() > 0) {
$page['columns'] = 2;
foreach ($pageMediaItem->getData() as $mediaItem) {
$page['contentElements'][] =
$this->serializeBrickData(
$document,
'pageMediaItem',
$mediaItem['type'],
$mediaItem['key'],
0,
$language
);
}
}
// Content
$contentElements = $document->getEditable('contentElements');
if($contentElements instanceof Areablock) {
foreach ($contentElements->getData() as $contentElement) {
$page['contentElements'][] =
$this->serializeBrickData(
$document,
'contentElements',
$contentElement['type'],
$contentElement['key'],
($page['columns'] == 1 ? 0 : 1),
$language
);
}
} elseif($contentElements instanceof Area) {
if($contentElements->getElement('url')) {
$page['contentElements'][] = $this->serializeBrickData(
$document,
'contentElements',
'externalSite',
1,
0,
$language
);
} elseif (($contentElements->getElement('document'))) {
$page['contentElements'][] = $this->serializeBrickData(
$document,
'contentElements',
'document',
1,
0,
$language
);
} elseif (($contentElements->getElement('integratedApp'))) {
$page['contentElements'][] = $this->serializeBrickData(
$document,
'contentElements',
'integratedApp',
1,
0,
$language
);
} elseif (($contentElements->getElement('productTile'))) {
$page['contentElements'][] = $this->serializeBrickData(
$document,
'contentElements',
'productTile',
1,
0,
$language
);
}
}
return $page;
}
public function serializeBrickData($document, $element, $type, $key, $col = 0, $language = null)
{
$serializedData = array();
switch ($type) {
case 'headline':
$serializedData = array(
'type' => 'headline',
'mimeType' => "text/plain",
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getValue()
);
break;
case 'subHeadline':
$serializedData = array(
'type' => 'subHeadline',
'mimeType' => "text/plain",
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getValue()
);
break;
case 'subline':
$serializedData = array(
'type' => 'subline',
'mimeType' => "text/plain",
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getValue()
);
break;
case 'text':
$serializedData = array(
'type' => 'text',
'mimeType' => "text/html",
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getValue()
);
break;
case 'image':
$serializedData = array(
'type' => 'image',
'mimeType' => "image",
'layout' => ($document->getEditable($element . ':' . $key . ".layout"))
? $document->getEditable($element . ':' . $key . ".layout")->getValue()
: "default",
'title' => ($document->getEditable($element . ':' . $key . ".title"))
? $document->getEditable($element . ':' . $key . ".title")->getValue()
: ""
);
$imageElement = $document->getEditable($element . ':' . $key . "." . $type);
$serializedData['value'] = $imageElement->getImage()->serializeData($language);
break;
case 'video':
$serializedData = array(
'type' => 'video',
'mimeType' => "video",
'layout' => ($document->getEditable($element . ':' . $key . ".layout"))
? $document->getEditable($element . ':' . $key . ".layout")->getValue()
: "default",
'controls' => ($document->getEditable($element . ':' . $key . ".controls"))
? $document->getEditable($element . ':' . $key . ".controls")->isChecked()
: false,
'autoplay' => ($document->getEditable($element . ':' . $key . ".autoplay"))
? $document->getEditable($element . ':' . $key . ".autoplay")->isChecked()
: false,
'loop' => ($document->getEditable($element . ':' . $key . ".loop"))
? $document->getEditable($element . ':' . $key . ".loop")->isChecked()
: false,
'muted' => ($document->getEditable($element . ':' . $key . ".muted"))
? $document->getEditable($element . ':' . $key . ".muted")->isChecked()
: false,
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getVideoAsset()->serializeData($language),
'poster' => ($document->getEditable($element . ':' . $key . "." . $type)->getPosterAsset())
? $document->getEditable($element . ':' . $key . "." . $type)->getPosterAsset()->serializeData($language)
: null
);
break;
case 'textImage':
$serializedData = array(
'type' => 'textImage',
'mimeType' => "multipart/mixed",
'imagePosition' => ($document->getEditable($element . ':' . $key . ".image-position"))
? $document->getEditable($element . ':' . $key . ".image-position")->getValue()
: "left",
'text' => $document->serializeBrickData($document, $element, "text", $key),
'image' => $document->serializeBrickData($document, $element, "image", $key)
);
break;
case 'externalSite':
$serializedData = array(
'type' => 'externalSite',
'mimeType' => "text/plain",
'value' => $document->getEditable($element . ':' . $key . ".url")->getValue()
);
break;
case 'document':
$docElement = $document->getEditable($element . ':' . $key . "." . $type)->getElement();
$serializedData = array(
'type' => 'document',
'mimeType' => "document",
'value' => $docElement ? $docElement->serializeData($language) : null
);
break;
case 'hotspot':
$serializedData = array(
'type' => 'hotspot',
'mimeType' => "hotspot",
'layout' => ($document->getEditable($element . ':' . $key . ".layout"))
? $document->getEditable($element . ':' . $key . ".layout")->getValue()
: "default",
'video' => null,
'hotspots' => array()
);
$imageElement = $document->getEditable($element . ':' . $key . ".image");
$serializedData['image'] = $imageElement->getImage()->serializeData($language);
if($imageElement->getHotspots() && count($imageElement->getHotspots()) > 0) {
$serializedData['hotspots'] = $imageElement->getHotspots();
}
$videoElement = $this->getEditable($element . ':' . $key . ".video");
if($videoElement->getVideoAsset()) {
$serializedData['video'] = $videoElement->getVideoAsset()->serializeData($language);
}
break;
case 'facts':
$serializedData = array(
'type' => 'facts',
'mimeType' => "facts",
'values' => array()
);
$factsElement = $document->getEditable($element . ':' . $key . "." . $type);
foreach ($factsElement->getElements() as $factElement) {
$serializedData['values'][] =
array(
"label" => $factElement->getInput('label')->getValue(),
"value" => (float) $factElement->getNumeric('value')->getValue(),
"prefix" => $factElement->getInput('prefix')->getValue(),
"suffix" => $factElement->getInput('suffix')->getValue(),
"decimalPlaces" => (int) $factElement->getNumeric('decimalPlaces')->getValue(),
"startValue" => (float) $factElement->getNumeric('startValue')->getValue(),
);
}
break;
case 'teaserBox':
$serializedData = array(
'type' => 'teaserBox',
'mimeType' => "teaserBox",
'layout' => ($document->getEditable($element . ':' . $key . ".layout"))
? $document->getEditable($element . ':' . $key . ".layout")->getValue()
: "grid",
'teasers' => array()
);
$teasersElement = $document->getEditable($element . ':' . $key . "." . $type);
foreach ($teasersElement->getElements() as $teaserElement) {
if(is_object($teaserElement->getImage('image')->getImage())) {
$serializedData['teasers'][] =
array(
"image" => $teaserElement->getImage('image')->getImage()->serializeData($language),
"title" => $teaserElement->getInput('title')->getValue(),
"link" => ($teaserElement->getRelation('link')->getElement())
? $teaserElement->getRelation('link')->getElement()->getId()
: null
);
}
}
break;
case 'button':
$serializedData = array(
'type' => 'button',
'mimeType' => "text/plain",
'link' => ($document->getEditable($element . ':' . $key . ".link"))
? $document->getEditable($element . ':' . $key . ".link")->getElement()->getId()
: null,
'title' => ($document->getEditable($element . ':' . $key . ".title"))
? $document->getEditable($element . ':' . $key . ".title")->getValue()
: "",
);
break;
case 'youtube':
$serializedData = array(
'type' => 'youtube',
'mimeType' => "text/plain",
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getValue()
);
break;
case 'slider':
$serializedData = array(
'type' => 'slider',
'mimeType' => "multipart/mixed",
'values' => array(),
'layout' => ($document->getEditable($element . ':' . $key . ".layout"))
? $document->getEditable($element . ':' . $key . ".layout")->getValue()
: "default",
'title' => ($document->getEditable($element . ':' . $key . ".title"))
? $document->getEditable($element . ':' . $key . ".title")->getValue()
: ""
);
$sliderElement = $document->getEditable($element . ':' . $key . ".values");
foreach ($sliderElement->getElements() as $sliderElement) {
if(is_object($sliderElement->getImage('image')->getImage())) {
$serializedData['values'][] =
$sliderElement->getImage('image')->getImage()->serializeData($language);
}
}
break;
case 'integratedApp':
$serializedData = array(
'type' => 'integratedApp',
'mimeType' => "application/zip",
'value' => $document->getEditable($element . ':' . $key . "." . $type)->getElement()->serializeData($language)
);
break;
case 'productTile':
$apiManager = new DataObjectApiManager();
$contentElements = $document->getEditable('contentElements');
/** @var Document\Editable\Relation $productTileEditable */
$productTileEditable = $contentElements->getElement('productTile');
/** @var DataObject\ProductTile $productTile */
$productTile = $productTileEditable->getElement();
$params = [
'formatters' => [
'mainImage' => new SalesAppHotspotImageDataFormatter(),
'teaserImage' => new SalesAppHotspotImageDataFormatter(),
'ogImage' => new SalesAppHotspotImageDataFormatter(),
'twitterImage' => new SalesAppHotspotImageDataFormatter(),
'sliderImages' => new SalesAppImageGalleryDataFormatter(),
'detailImages' => new SalesAppImageGalleryDataFormatter(),
'image' => new SalesAppImageDataFormatter(),
'brochures' => new SalesAppDocumentDataFormatter(),
'caseStudies' => new SalesAppDocumentDataFormatter(),
]
];
$serializedData = [
'type' => 'productTile',
'mimeType' => "application/json"
];
if ($productTile) {
$serializedData['productTile'] = $apiManager->getDataObjectData($productTile, $language, [], [], $params);
} else {
$serializedData['productTile'] = [];
}
break;
}
$serializedData['col'] = $col;
return $serializedData;
}
public function getShareableData($bookmarkListId, $language)
{
$shareableData = $this->getEditable('preview-title');
if($this->getEditable('web-reference-url')) {
if($this->getEditable('web-reference-url') != "") {
$shareableData = '<a href="' . $this->getEditable('web-reference-url') . '">' . $shareableData . '</a>';
} else {
$shareableData = null;
}
} elseif($this->getEditable('contentElements:1.url')) {
if($this->getEditable('contentElements:1.url') != "") {
$shareableData = '<a href="' . $this->getEditable('contentElements:1.url') . '">' . $shareableData . '</a>';
} else {
$shareableData = null;
}
} elseif($this->getEditable('contentElements:1.document')) {
$newShareItem = new ShareItem();
$newShareItem->setUid(bin2hex( random_bytes(24) ));
$expiryDate = new \DateTime();
$expiryDate->modify('+14 days');
$newShareItem->setExpiryDate($expiryDate);
$newShareItem->setPage($this);
$newShareItem->setParentId($bookmarkListId);
$newShareItem->setKey(\Pimcore\File::getValidFilename(
"shareItem_" . $newShareItem->getUid())
);
$newShareItem->setPublished(true);
$newShareItem->save();
$shareableData = '<a href="' . \Pimcore\Tool::getHostUrl('https') . '/share/' . $newShareItem->getUid() . '">' . $shareableData . '</a>';
} else {
$shareableData = null;
}
return $shareableData;
}
public function getPlainShareableData($bookmarkListId, $language): ?string
{
$shareableData = $this->getEditable('preview-title');
if($this->getEditable('web-reference-url')) {
if($this->getEditable('web-reference-url') != "") {
// $shareableData = '<a href="' . $this->getElement('web-reference-url') . '">' . $shareableData . '</a>';
$shareableData = $shareableData.': '.$this->getEditable('web-reference-url').' %0D%0A';
} else {
$shareableData = null;
}
} elseif($this->getEditable('contentElements:1.url')) {
if($this->getEditable('contentElements:1.url') != "") {
// $shareableData = '<a href="' . $this->getElement('contentElements:1.url') . '">' . $shareableData . '</a>';
$shareableData = $shareableData.': '.$this->getEditable('contentElements:1.url').' %0D%0A';
} else {
$shareableData = null;
}
} elseif($this->getEditable('contentElements:1.document')) {
$newShareItem = new ShareItem();
$newShareItem->setUid(bin2hex( random_bytes(24) ));
$expiryDate = new \DateTime();
$expiryDate->modify('+14 days');
$newShareItem->setExpiryDate($expiryDate);
$newShareItem->setPage($this);
$newShareItem->setParentId($bookmarkListId);
$newShareItem->setKey(\Pimcore\File::getValidFilename(
"shareItem_" . $newShareItem->getUid())
);
$newShareItem->setPublished(true);
$newShareItem->save();
//$shareableData = '<a href="' . \Pimcore\Tool::getHostUrl('https') . '/share/' . $newShareItem->getUid() . '">' . $shareableData . '</a>';
$shareableData = $shareableData.': '.\Pimcore\Tool::getHostUrl('https') . '/share/' . $newShareItem->getUid().' %0D%0A';
} else {
$shareableData = null;
}
return $shareableData;
}
public function getContextList() {
$contexts = array();
$dependencies = $this->getDependencies();
foreach ($dependencies->getRequiredBy() as $requiredBy) {
if($requiredBy['type'] == 'object') {
$object = DataObject::getById($requiredBy['id']);
if($object instanceof Category) {
$def = $object->getClass()->getFieldDefinition("contexts");
$refKey = $def->getOwnerFieldName();
$refId = $def->getOwnerClassId();
$nonOwnerRelations = $object->getRelationData($refKey, false, $refId);
foreach($nonOwnerRelations as $nonOwnerRelation) {
if($nonOwnerRelation['subtype'] == "Context") {
$contexts[$nonOwnerRelation['id']] = $nonOwnerRelation['id'];
}
}
}
}
}
return $contexts;
}
}