2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
use PortableInfobox\Helpers\PortableInfoboxTemplateEngine;
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2017-07-07 13:12:32 +00:00
|
|
|
class PortableInfoboxRenderService {
|
2018-08-09 09:49:10 +00:00
|
|
|
// keep synced with css variables (--pi-width)
|
2017-01-13 12:57:17 +00:00
|
|
|
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
|
|
|
|
2016-12-27 14:34:21 +00:00
|
|
|
protected $templateEngine;
|
2016-12-28 12:10:57 +00:00
|
|
|
protected $inlineStyles;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2017-07-07 13:12:32 +00:00
|
|
|
public function __construct() {
|
2018-08-08 09:31:33 +00:00
|
|
|
$this->templateEngine = new PortableInfoboxTemplateEngine();
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-12-23 00:04:00 +00:00
|
|
|
* @param string $type
|
2019-02-02 22:34:48 +00:00
|
|
|
* @param string $itemName
|
2015-04-27 14:05:31 +00:00
|
|
|
* @return string - infobox HTML
|
|
|
|
*/
|
2018-10-02 07:41:19 +00:00
|
|
|
public function renderInfobox(
|
2019-02-02 22:34:48 +00:00
|
|
|
array $infoboxdata, $theme, $layout, $accentColor, $accentColorText, $type, $itemName
|
2018-10-02 07:41:19 +00:00
|
|
|
) {
|
2016-12-28 12:10:57 +00:00
|
|
|
$this->inlineStyles = $this->getInlineStyles( $accentColor, $accentColorText );
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2016-12-29 10:28:26 +00:00
|
|
|
$infoboxHtmlContent = $this->renderChildren( $infoboxdata );
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2015-07-02 11:55:39 +00:00
|
|
|
if ( !empty( $infoboxHtmlContent ) ) {
|
2016-03-29 10:31:03 +00:00
|
|
|
$output = $this->renderItem( 'wrapper', [
|
2016-03-29 10:12:11 +00:00
|
|
|
'content' => $infoboxHtmlContent,
|
|
|
|
'theme' => $theme,
|
2018-12-23 00:04:00 +00:00
|
|
|
'layout' => $layout,
|
2019-02-02 22:34:48 +00:00
|
|
|
'type' => $type,
|
|
|
|
'item-name' => $itemName
|
2016-03-29 10:31:03 +00:00
|
|
|
] );
|
2015-04-27 14:05:31 +00:00
|
|
|
} else {
|
|
|
|
$output = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2016-12-27 14:09:55 +00:00
|
|
|
/**
|
|
|
|
* Produces HTML output for item type and data
|
|
|
|
*
|
2018-08-16 09:25:53 +00:00
|
|
|
* @param string $type
|
2016-12-27 14:09:55 +00:00
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-27 14:34:21 +00:00
|
|
|
protected function render( $type, array $data ) {
|
2016-12-27 14:58:01 +00:00
|
|
|
return $this->templateEngine->render( $type, $data );
|
2016-12-27 14:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* renders part of infobox
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param array $data
|
|
|
|
*
|
2016-12-27 14:34:21 +00:00
|
|
|
* @return string - HTML
|
2016-12-27 14:09:55 +00:00
|
|
|
*/
|
2016-12-27 14:34:21 +00:00
|
|
|
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':
|
|
|
|
$result = $this->renderMedia( $data );
|
2016-12-29 10:28:26 +00:00
|
|
|
break;
|
|
|
|
case 'title':
|
|
|
|
$result = $this->renderTitle( $data );
|
|
|
|
break;
|
2019-02-24 22:05:18 +00:00
|
|
|
case 'panel':
|
|
|
|
$result = $this->renderPanel( $data );
|
|
|
|
break;
|
|
|
|
case 'section':
|
|
|
|
$result = '';
|
|
|
|
break;
|
2016-12-29 10:28:26 +00:00
|
|
|
default:
|
|
|
|
$result = $this->render( $type, $data );
|
|
|
|
break;
|
2016-12-27 14:09:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 10:28:26 +00:00
|
|
|
return $result;
|
2016-12-27 14:09:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
/**
|
|
|
|
* renders group infobox component
|
|
|
|
*
|
|
|
|
* @param array $groupData
|
2015-07-02 11:55:39 +00:00
|
|
|
*
|
2015-04-27 14:05:31 +00:00
|
|
|
* @return string - group HTML markup
|
|
|
|
*/
|
2018-10-02 07:41:19 +00:00
|
|
|
protected function renderGroup( array $groupData ) {
|
2018-08-12 09:45:29 +00:00
|
|
|
$cssClasses = [];
|
2015-04-27 14:05:31 +00:00
|
|
|
$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'];
|
2015-04-27 14:05:31 +00:00
|
|
|
|
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' ) {
|
2015-07-28 09:46:41 +00:00
|
|
|
$groupHTMLContent .= $this->renderItem(
|
|
|
|
'horizontal-group-content',
|
2017-01-11 10:27:23 +00:00
|
|
|
$this->createHorizontalGroupData( $children )
|
2015-07-28 09:46:41 +00:00
|
|
|
);
|
|
|
|
} else {
|
2017-01-11 10:27:23 +00:00
|
|
|
$groupHTMLContent .= $this->renderChildren( $children );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 10:27:23 +00:00
|
|
|
if ( $collapse !== null && count( $children ) > 0 && $children[0]['type'] === 'header' ) {
|
2015-10-26 21:59:34 +00:00
|
|
|
$cssClasses[] = 'pi-collapse';
|
|
|
|
$cssClasses[] = 'pi-collapse-' . $collapse;
|
|
|
|
}
|
|
|
|
|
2016-12-27 14:09:55 +00:00
|
|
|
return $this->render( 'group', [
|
2015-10-26 21:59:34 +00:00
|
|
|
'content' => $groupHTMLContent,
|
2019-02-02 22:34:48 +00:00
|
|
|
'cssClasses' => implode( ' ', $cssClasses ),
|
|
|
|
'item-name' => $groupData['item-name']
|
2015-10-26 21:59:34 +00:00
|
|
|
] );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 13:18:20 +00:00
|
|
|
/**
|
2016-12-27 14:09:55 +00:00
|
|
|
* 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
|
2015-07-01 13:18:20 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-10-02 07:41:19 +00:00
|
|
|
protected function renderMedia( array $data ) {
|
2018-08-22 14:20:49 +00:00
|
|
|
if ( count( $data ) === 0 || !$data[0] ) {
|
2016-12-27 14:09:55 +00:00
|
|
|
return '';
|
2015-07-21 23:13:28 +00:00
|
|
|
}
|
2015-07-20 13:00:35 +00:00
|
|
|
|
2018-08-22 14:20:49 +00:00
|
|
|
if ( count( $data ) === 1 ) {
|
|
|
|
$data = $data[0];
|
2018-08-12 09:45:29 +00:00
|
|
|
$templateName = 'media';
|
2016-12-27 14:09:55 +00:00
|
|
|
} else {
|
|
|
|
// More than one image means image collection
|
2019-02-02 22:56:16 +00:00
|
|
|
$data = [
|
|
|
|
'images' => $data,
|
2019-02-03 16:49:06 +00:00
|
|
|
'source' => $data[0]['source'],
|
|
|
|
'item-name' => $data[0]['item-name']
|
2019-02-02 22:56:16 +00:00
|
|
|
];
|
2018-08-12 09:45:29 +00:00
|
|
|
$templateName = 'media-collection';
|
2015-07-21 23:13:28 +00:00
|
|
|
}
|
2015-07-20 13:00:35 +00:00
|
|
|
|
2016-12-27 14:09:55 +00:00
|
|
|
return $this->render( $templateName, $data );
|
2015-07-20 13:00:35 +00:00
|
|
|
}
|
2016-12-28 12:10:57 +00:00
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
protected function renderTitle( array $data ) {
|
2016-12-29 10:28:26 +00:00
|
|
|
$data['inlineStyles'] = $this->inlineStyles;
|
|
|
|
|
|
|
|
return $this->render( 'title', $data );
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
protected function renderHeader( array $data ) {
|
2016-12-29 10:28:26 +00:00
|
|
|
$data['inlineStyles'] = $this->inlineStyles;
|
|
|
|
|
|
|
|
return $this->render( 'header', $data );
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
protected function renderChildren( array $children ) {
|
2016-12-29 10:28:26 +00:00
|
|
|
$result = '';
|
|
|
|
foreach ( $children as $child ) {
|
|
|
|
$type = $child['type'];
|
|
|
|
if ( $this->templateEngine->isSupportedType( $type ) ) {
|
|
|
|
$result .= $this->renderItem( $type, $child['data'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:05:18 +00:00
|
|
|
protected function renderPanel( $data, $type = 'panel' ) {
|
|
|
|
$cssClasses = [];
|
|
|
|
$sections = [];
|
|
|
|
$collapse = $data['collapse'];
|
|
|
|
$header = '';
|
|
|
|
$shouldShowToggles = false;
|
|
|
|
|
|
|
|
foreach ( $data['value'] as $index => $child ) {
|
|
|
|
switch ( $child['type'] ) {
|
|
|
|
case 'header':
|
|
|
|
if ( empty( $header ) ) {
|
|
|
|
$header = $this->renderHeader( $child['data'] );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'section':
|
|
|
|
$sectionData = $this->getSectionData( $child, $index );
|
|
|
|
// section needs to have content in order to render it
|
|
|
|
if ( !empty( $sectionData['content'] ) ) {
|
|
|
|
$sections[] = $sectionData;
|
|
|
|
if ( !empty( $sectionData['label'] ) ) {
|
|
|
|
$shouldShowToggles = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// we do not support any other tags than section and header inside panel
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $collapse !== null && count( $tabContents ) > 0 && !empty( $header ) ) {
|
|
|
|
$cssClasses[] = 'pi-collapse';
|
|
|
|
$cssClasses[] = 'pi-collapse-' . $collapse;
|
|
|
|
}
|
|
|
|
if ( count( $sections ) > 0 ) {
|
|
|
|
$sections[0]['active'] = true;
|
|
|
|
} else {
|
|
|
|
// do not render empty panel
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if ( !$shouldShowToggles ) {
|
|
|
|
$sections = array_map( function ( $content ) {
|
|
|
|
$content['active'] = true;
|
|
|
|
return $content;
|
|
|
|
}, $sections );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render( $type, [
|
|
|
|
'item-name' => $data['item-name'],
|
|
|
|
'cssClasses' => implode( ' ', $cssClasses ),
|
|
|
|
'header' => $header,
|
|
|
|
'sections' => $sections,
|
|
|
|
'shouldShowToggles' => $shouldShowToggles,
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSectionData( $section, $index ) {
|
|
|
|
$content = $this->renderChildren( $section['data']['value'] );
|
|
|
|
return [
|
|
|
|
'index' => $index,
|
|
|
|
'item-name' => $section['data']['item-name'],
|
|
|
|
'label' => $section['data']['label'],
|
|
|
|
'content' => !empty( $content ) ? $content : null
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
private function createHorizontalGroupData( array $groupData ) {
|
2016-12-29 10:28:26 +00:00
|
|
|
$horizontalGroupData = [
|
2018-12-27 01:01:19 +00:00
|
|
|
'data' => [],
|
2016-12-29 10:28:26 +00:00
|
|
|
'renderLabels' => false
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ( $groupData as $item ) {
|
|
|
|
$data = $item['data'];
|
|
|
|
|
|
|
|
if ( $item['type'] === 'data' ) {
|
2018-12-27 01:01:19 +00:00
|
|
|
$horizontalGroupData['data'][] = [
|
|
|
|
'label' => $data['label'],
|
|
|
|
'value' => $data['value'],
|
2019-02-02 22:34:48 +00:00
|
|
|
'source' => $item['data']['source'] ?? "",
|
|
|
|
'item-name' => $item['data']['item-name']
|
2018-12-27 01:01:19 +00:00
|
|
|
];
|
2016-12-29 10:28:26 +00:00
|
|
|
|
|
|
|
if ( !empty( $data['label'] ) ) {
|
|
|
|
$horizontalGroupData['renderLabels'] = true;
|
|
|
|
}
|
|
|
|
} elseif ( $item['type'] === 'header' ) {
|
|
|
|
$horizontalGroupData['header'] = $data['value'];
|
2018-08-09 09:41:31 +00:00
|
|
|
$horizontalGroupData['inlineStyles'] = $this->inlineStyles;
|
2016-12-29 10:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $horizontalGroupData;
|
|
|
|
}
|
2016-12-29 10:58:27 +00:00
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
private function createSmartGroups( array $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'];
|
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
if ( $item['type'] === 'data' && $data['layout'] !== 'default' ) {
|
2017-01-10 14:24:39 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
private function createSmartGroupItem( array $rowItems, $rowSpan ) {
|
2017-01-12 11:40:56 +00:00
|
|
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:41:19 +00:00
|
|
|
private function createSmartGroupSections( array $rowItems, $capacity ) {
|
2017-01-12 13:11:46 +00:00
|
|
|
return array_reduce( $rowItems, function ( $result, $item ) use ( $capacity ) {
|
2018-08-20 10:50:36 +00:00
|
|
|
$width = $item['data']['span'] / $capacity * 100;
|
|
|
|
$styles = "width: {$width}%";
|
2017-01-12 13:11:46 +00:00
|
|
|
|
|
|
|
$label = $item['data']['label'] ?? "";
|
|
|
|
if ( !empty( $label ) ) {
|
|
|
|
$result['renderLabels'] = true;
|
2017-01-11 14:04:09 +00:00
|
|
|
}
|
2018-12-27 01:01:19 +00:00
|
|
|
$result['data'][] = [
|
|
|
|
'label' => $label,
|
|
|
|
'value' => $item['data']['value'],
|
|
|
|
'inlineStyles' => $styles,
|
2019-02-02 22:34:48 +00:00
|
|
|
'source' => $item['data']['source'] ?? "",
|
|
|
|
'item-name' => $item['data']['item-name']
|
2018-12-27 01:01:19 +00:00
|
|
|
];
|
2017-01-11 11:48:26 +00:00
|
|
|
|
2017-01-12 13:11:46 +00:00
|
|
|
return $result;
|
2018-12-27 01:01:19 +00:00
|
|
|
}, [ 'data' => [], 'renderLabels' => false ] );
|
2017-01-10 14:24:39 +00:00
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|