PortableInfobox/includes/services/PortableInfoboxRenderService.php

289 lines
7.2 KiB
PHP
Raw Normal View History

<?php
use PortableInfobox\Helpers\PortableInfoboxImagesHelper;
use PortableInfobox\Helpers\PortableInfoboxTemplateEngine;
class PortableInfoboxRenderService {
// keep synced with css variables (--pi-width)
const DEFAULT_DESKTOP_INFOBOX_WIDTH = 270;
2017-01-10 12:20:13 +00:00
const DEFAULT_DESKTOP_THUMBNAIL_WIDTH = 350;
2016-12-29 10:28:26 +00:00
protected $templateEngine;
2016-12-29 10:28:26 +00:00
protected $imagesWidth = self::DEFAULT_DESKTOP_THUMBNAIL_WIDTH;
protected $infoboxWidth = self::DEFAULT_DESKTOP_INFOBOX_WIDTH;
2016-12-28 12:10:57 +00:00
protected $inlineStyles;
2016-12-29 10:28:26 +00:00
private $helper;
public function __construct() {
2018-08-08 09:31:33 +00:00
$this->templateEngine = new PortableInfoboxTemplateEngine();
}
/**
* renders infobox
*
* @param array $infoboxdata
2015-07-02 11:55:39 +00:00
*
2018-08-16 09:25:53 +00:00
* @param string $theme
* @param string $layout
* @param string $accentColor
* @param string $accentColorText
* @return string - infobox HTML
*/
2016-12-28 12:10:57 +00:00
public function renderInfobox( array $infoboxdata, $theme, $layout, $accentColor, $accentColorText ) {
$this->inlineStyles = $this->getInlineStyles( $accentColor, $accentColorText );
2016-12-29 10:28:26 +00:00
$infoboxHtmlContent = $this->renderChildren( $infoboxdata );
2015-07-02 11:55:39 +00:00
if ( !empty( $infoboxHtmlContent ) ) {
$output = $this->renderItem( 'wrapper', [
'content' => $infoboxHtmlContent,
'theme' => $theme,
2018-07-25 15:01:45 +00:00
'layout' => $layout
] );
} else {
$output = '';
}
return $output;
}
2016-12-29 13:17:48 +00:00
protected function getImageHelper() {
2016-12-29 10:28:26 +00:00
if ( !isset( $this->helper ) ) {
2016-12-29 10:58:27 +00:00
$this->helper = new PortableInfoboxImagesHelper();
2016-12-29 10:28:26 +00:00
}
return $this->helper;
}
/**
* Produces HTML output for item type and data
*
2018-08-16 09:25:53 +00:00
* @param string $type
* @param array $data
* @return string
*/
protected function render( $type, array $data ) {
return $this->templateEngine->render( $type, $data );
}
/**
* renders part of infobox
*
* @param string $type
* @param array $data
*
* @return string - HTML
*/
protected function renderItem( $type, array $data ) {
2016-12-29 10:28:26 +00:00
switch ( $type ) {
case 'group':
$result = $this->renderGroup( $data );
break;
case 'header':
$result = $this->renderHeader( $data );
break;
2018-08-12 09:45:29 +00:00
case 'media':
2018-08-12 10:00:05 +00:00
case 'audio':
case 'image':
case 'video':
2018-08-12 09:45:29 +00:00
$result = $this->renderMedia( $data );
2016-12-29 10:28:26 +00:00
break;
case 'title':
$result = $this->renderTitle( $data );
break;
default:
$result = $this->render( $type, $data );
break;
}
2016-12-29 10:28:26 +00:00
return $result;
}
/**
* renders group infobox component
*
* @param array $groupData
2015-07-02 11:55:39 +00:00
*
* @return string - group HTML markup
*/
protected function renderGroup( $groupData ) {
2018-08-12 09:45:29 +00:00
$cssClasses = [];
$groupHTMLContent = '';
2017-01-11 10:27:23 +00:00
$children = $groupData['value'];
2016-12-29 10:28:26 +00:00
$layout = $groupData['layout'];
$collapse = $groupData['collapse'];
2017-01-10 14:24:39 +00:00
$rowItems = $groupData['row-items'];
2017-01-10 14:24:39 +00:00
if ( $rowItems > 0 ) {
2017-01-11 10:27:23 +00:00
$items = $this->createSmartGroups( $children, $rowItems );
2017-01-10 14:24:39 +00:00
$groupHTMLContent .= $this->renderChildren( $items );
} elseif ( $layout === 'horizontal' ) {
$groupHTMLContent .= $this->renderItem(
'horizontal-group-content',
2017-01-11 10:27:23 +00:00
$this->createHorizontalGroupData( $children )
);
} else {
2017-01-11 10:27:23 +00:00
$groupHTMLContent .= $this->renderChildren( $children );
}
2017-01-11 10:27:23 +00:00
if ( $collapse !== null && count( $children ) > 0 && $children[0]['type'] === 'header' ) {
$cssClasses[] = 'pi-collapse';
$cssClasses[] = 'pi-collapse-' . $collapse;
}
return $this->render( 'group', [
'content' => $groupHTMLContent,
'cssClasses' => implode( ' ', $cssClasses )
] );
}
/**
* If image element has invalid thumbnail, doesn't render this element at all.
2015-07-02 11:55:39 +00:00
*
2018-08-16 09:25:53 +00:00
* @param array $data
* @return string
*/
2018-08-12 09:45:29 +00:00
protected function renderMedia( $data ) {
2016-12-29 13:17:48 +00:00
$helper = $this->getImageHelper();
2018-08-12 09:45:29 +00:00
$images = [];
foreach ( $data as $dataItem ) {
$extendedItem = $dataItem;
$extendedItem = $helper->extendImageData( $extendedItem, $this->imagesWidth, $this->infoboxWidth );
if ( !!$extendedItem ) {
$images[] = $extendedItem;
}
}
2015-07-02 11:55:39 +00:00
if ( count( $images ) === 0 ) {
return '';
}
if ( count( $images ) === 1 ) {
2016-12-29 10:28:26 +00:00
$data = $images[0];
2018-08-12 09:45:29 +00:00
$templateName = 'media';
} else {
// More than one image means image collection
$data = $helper->extendImageCollectionData( $images );
2018-08-12 09:45:29 +00:00
$templateName = 'media-collection';
}
return $this->render( $templateName, $data );
}
2016-12-28 12:10:57 +00:00
2016-12-29 10:28:26 +00:00
protected function renderTitle( $data ) {
$data['inlineStyles'] = $this->inlineStyles;
return $this->render( 'title', $data );
}
protected function renderHeader( $data ) {
$data['inlineStyles'] = $this->inlineStyles;
return $this->render( 'header', $data );
}
protected function renderChildren( $children ) {
$result = '';
foreach ( $children as $child ) {
$type = $child['type'];
if ( $this->templateEngine->isSupportedType( $type ) ) {
$result .= $this->renderItem( $type, $child['data'] );
}
}
return $result;
}
2016-12-28 12:10:57 +00:00
private function getInlineStyles( $accentColor, $accentColorText ) {
$backgroundColor = empty( $accentColor ) ? '' : "background-color:{$accentColor};";
$color = empty( $accentColorText ) ? '' : "color:{$accentColorText};";
2016-12-28 12:36:57 +00:00
return "{$backgroundColor}{$color}";
2016-12-28 12:10:57 +00:00
}
2016-12-29 10:28:26 +00:00
private function createHorizontalGroupData( $groupData ) {
$horizontalGroupData = [
2018-08-12 09:45:29 +00:00
'labels' => [],
'values' => [],
2016-12-29 10:28:26 +00:00
'renderLabels' => false
];
foreach ( $groupData as $item ) {
$data = $item['data'];
if ( $item['type'] === 'data' ) {
array_push( $horizontalGroupData['labels'], $data['label'] );
array_push( $horizontalGroupData['values'], $data['value'] );
if ( !empty( $data['label'] ) ) {
$horizontalGroupData['renderLabels'] = true;
}
} elseif ( $item['type'] === 'header' ) {
$horizontalGroupData['header'] = $data['value'];
$horizontalGroupData['inlineStyles'] = $this->inlineStyles;
2016-12-29 10:28:26 +00:00
}
}
return $horizontalGroupData;
}
2016-12-29 10:58:27 +00:00
2017-01-10 14:24:39 +00:00
private function createSmartGroups( $groupData, $rowCapacity ) {
2018-08-12 09:45:29 +00:00
$result = [];
2017-01-11 10:27:23 +00:00
$rowSpan = 0;
2018-08-12 09:45:29 +00:00
$rowItems = [];
2017-01-10 14:24:39 +00:00
foreach ( $groupData as $item ) {
$data = $item['data'];
if ( $item['type'] === 'data' && ( !isset( $data['layout'] ) || $data['layout'] !== 'default' ) ) {
2017-01-12 13:11:46 +00:00
if ( !empty( $rowItems ) && $rowSpan + $data['span'] > $rowCapacity ) {
2017-01-12 11:40:56 +00:00
$result[] = $this->createSmartGroupItem( $rowItems, $rowSpan );
2017-01-11 10:27:23 +00:00
$rowSpan = 0;
2018-08-12 09:45:29 +00:00
$rowItems = [];
2017-01-10 14:24:39 +00:00
}
2017-01-11 10:27:23 +00:00
$rowSpan += $data['span'];
$rowItems[] = $item;
2017-01-10 14:24:39 +00:00
} else {
// smart wrapping works only for data tags
2017-01-11 10:27:23 +00:00
if ( !empty( $rowItems ) ) {
2017-01-12 11:40:56 +00:00
$result[] = $this->createSmartGroupItem( $rowItems, $rowSpan );
2017-01-11 10:27:23 +00:00
$rowSpan = 0;
2018-08-16 09:25:53 +00:00
$rowItems = [];
2017-01-10 14:24:39 +00:00
}
$result[] = $item;
}
}
2017-01-12 11:40:56 +00:00
if ( !empty( $rowItems ) ) {
$result[] = $this->createSmartGroupItem( $rowItems, $rowSpan );
}
2017-01-10 14:24:39 +00:00
2017-01-12 11:40:56 +00:00
return $result;
2017-01-10 14:24:39 +00:00
}
2017-01-12 11:40:56 +00:00
private function createSmartGroupItem( $rowItems, $rowSpan ) {
return [
'type' => 'smart-group',
2017-01-12 13:11:46 +00:00
'data' => $this->createSmartGroupSections( $rowItems, $rowSpan )
2017-01-12 11:40:56 +00:00
];
}
2017-01-12 13:11:46 +00:00
private function createSmartGroupSections( $rowItems, $capacity ) {
return array_reduce( $rowItems, function ( $result, $item ) use ( $capacity ) {
$styles = "width: calc({$item['data']['span']} / $capacity * 100%);";
$label = $item['data']['label'] ?? "";
if ( !empty( $label ) ) {
$result['renderLabels'] = true;
2017-01-11 14:04:09 +00:00
}
2017-01-12 13:11:46 +00:00
$result['labels'][] = [ 'value' => $label, 'inlineStyles' => $styles ];
$result['values'][] = [ 'value' => $item['data']['value'], 'inlineStyles' => $styles ];
2017-01-12 13:11:46 +00:00
return $result;
2018-08-12 09:45:29 +00:00
}, [ 'labels' => [], 'values' => [], 'renderLabels' => false ] );
2017-01-10 14:24:39 +00:00
}
}