<?php
/**
* project: Pimcore - Schutzverband Nuernberg Rostbratwuerste
* User: erikb
* Year: 2022
*/
namespace App\Document\Areabrick;
use App\Website\Tool\Image;
use Devkitchen\DocumentIndexingBundle\IndexingService\IndexingService;
use Pimcore\Http\Request\Resolver\EditmodeResolver;
use Pimcore\Model\Asset;
use Pimcore\Model\Document\Editable\Area\Info;
use Pimcore\Model\Document\Page;
use Pimcore\Translation\Translator;
class Documenslider extends AbstractAreabrick {
private $translator;
private $editmode;
private $indexingService;
public function __construct( Translator $translator, EditmodeResolver $editmodeResolver, IndexingService $indexingService) {
$this->translator = $translator;
$this->editmode = $editmodeResolver->isEditmode();
$this->indexingService = $indexingService;
}
public function getName() {
return 'Document slider';
}
public function getDescription() {
return 'Generates a teaser slidershow';
}
public function action(Info $info) {
$cssClasses = $info->getParam( 'cssClasses') ?? [];
$items = null;
$listing = null;
$columns = $info->getParam( 'columns');
$brickMode = $info->getParam( 'brickMode');
$documentTypes = $this->indexingService->getDocumentTypes();
$documentTypeStore = [];
foreach ($documentTypes as $type) {
if ($type->key != 'default' && $type->doc_count > 0) {
$documentTypeStore[] = [$type->key, $this->translator->trans( 'document-type.'.$type->key, [], 'admin', 'en')];
}
}
$documentSubtypeStore = [];
$selectedDocumentType = false;
if (!$this->getDocumentEditable( $info->getDocument(), 'select', 'document-type')->isEmpty()) {
$selectedDocumentType = $this->getDocumentEditable( $info->getDocument(), 'select', 'document-type')->getData();
$documentSubtypes = $this->indexingService->getFieldAggregated( $selectedDocumentType );
foreach($documentSubtypes as $subtype) {
$documentSubtypeStore[] = [$subtype->key, $this->translator->trans( 'document-subtype.'.$subtype->key, [], 'admin', 'en')];
}
$selectedDocumentSubtypes = [];
if (!$this->getDocumentEditable( $info->getDocument(), 'multiselect', 'document-subtype')->isEmpty()) {
$selectedDocumentSubtypes = $this->getDocumentEditable( $info->getDocument(), 'multiselect', 'document-subtype' )->getData();
}
$selectedSort = $this->getDocumentEditable( $info->getDocument(), 'select', 'sort')->getData() ?: 'documentIndex';
$selectedSortDirection = $this->getDocumentEditable( $info->getDocument(), 'select', 'sort-direction')->getData() ?: 'asc';
$resultSize = 1000;
$documentCountEditable = $this->getDocumentEditable( $info->getDocument(), 'numeric', 'document-count');
if (!$documentCountEditable->isEmpty() && $documentCountEditable->getData() > 0) {
$resultSize = $documentCountEditable->getData();
}
//$listing = $this->indexingService->listing( $selectedDocumentType, $selectedDocumentSubtypes, 'default', 'de', $selectedSort, $selectedSortDirection, $resultSize);
$listing = $this->indexingService->setOrderBy( $selectedSort )
->setOrderDirection( $selectedSortDirection )
->setResultSize( $resultSize )
->listing( $selectedDocumentType, $selectedDocumentSubtypes , 'default');
if ($listing) {
$listingCount = count( $listing );
if ($listingCount > 0) {
$items = [];
foreach($listing as $item) {
$document = Page::getById( $item->documentId );
$teaserThumbnail = null;
$teaserImageId = $brickMode == 'structure' ? $item->teaserAssetId : $item->teaserAssetBigId;
if ($teaserImageId) {
$teaserImage = Asset::getById( $teaserImageId );
}
else {
$teaserImage = $document->getProperty( 'document-teaser-image-fallback' );
}
if ($teaserImage) {
$thumbnailColumns = $brickMode == 'structure' ? 4 : $columns;
$thumbnailOverride = $brickMode == 'structure' ? 'teaser-default' : 'teaser-intro';
$gridImageData = Image::getGridThumbnailMetaData( $teaserImage, $thumbnailColumns , $this->editmode, $thumbnailOverride );
$teaserThumbnail = $teaserImage->getThumbnail( $gridImageData->thumbnail );
$loading = 'lazy';
$decoding = $this->editmode ? 'sync' : 'async';
if (!$this->editmode && ($info->getParam( 'sectionIndex') == 0 && $info->getParam( 'gridIndex') <= 1)) {
$loading = 'eager';
}
$teaserThumbnail = $teaserThumbnail->getHtml([
'imgAttributes' => [
'loading' => $loading,
'decoding' => $decoding
]
]);
}
$teaserSubtitle = null;
if ($item->documentSubtype) {
foreach($item->documentSubtype as $key) {
$teaserSubtitle[] = $this->translator->trans('filter.value.'.$key);
}
$teaserSubtitle = implode(', ', $teaserSubtitle);
}
$items[] = [
'url' => $document,
'title' => $item->teaserTitle,
'subtitle' => $teaserSubtitle,
'image' => $teaserThumbnail,
'text' => $item->teaserContent
];
}
}
}
}
$sortTypeStore[] = ['documentIndex', $this->translator->trans( 'sort-type.document-index', [], 'admin', 'en')];
$sortTypeStore[] = ['documentDate', $this->translator->trans( 'sort-type.document-date', [], 'admin', 'en')];
$sortTypeStore[] = ['teaserTitle.raw', $this->translator->trans( 'sort-type.teaser-title', [], 'admin', 'en')];
$sortTypeStore[] = ['documentTitle.raw', $this->translator->trans( 'sort-type.document-title', [], 'admin', 'en')];
$sortDirectionTypeStore[] = ['asc', $this->translator->trans( 'sort-direction.asc', [], 'admin', 'en')];
$sortDirectionTypeStore[] = ['desc', $this->translator->trans( 'sort-direction.desc', [], 'admin', 'en')];
$displayAsTypeStore[] = ['list', $this->translator->trans( 'display-as.list', [], 'admin', 'en')];
$displayAsTypeStore[] = ['grid', $this->translator->trans( 'display-as.grid', [], 'admin', 'en')];
$selectedDisplayAsType = $this->getDocumentEditable( $info->getDocument(), 'select', 'display-as')->getData() ?: 'list';
$info->setParam( 'documentTypeStore', $documentTypeStore);
$info->setParam( 'documentSubtypeStore', $documentSubtypeStore);
$info->setParam( 'selectedDocumentType', $selectedDocumentType);
$info->setParam( 'sortTypeStore', $sortTypeStore);
$info->setParam( 'sortDirectionTypeStore', $sortDirectionTypeStore);
$info->setParam( 'displayAsTypeStore', $displayAsTypeStore);
$info->setParam( 'selectedDisplayAsType', $selectedDisplayAsType);
$info->setParam( 'items', $items);
$info->setParam( 'cssClasses', $cssClasses);
}
public function needsReload(): bool {
return false;
}
// returns a custom html wrapper element (return an empty string if you don't want a wrapper element)
public function getHtmlTagOpen(Info $info) {
return '';
}
public function getHtmlTagClose(Info $info) {
return '';
}
}