mirror of
https://github.com/Universal-Omega/PortableInfobox.git
synced 2024-11-15 20:09:44 +00:00
a692e008b3
Added support for <tabber> Hero image still works correct
192 lines
5.2 KiB
PHP
192 lines
5.2 KiB
PHP
<?php
|
|
|
|
use Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper;
|
|
|
|
class PortableInfoboxRenderService extends WikiaService {
|
|
const MOBILE_TEMPLATE_POSTFIX = '-mobile';
|
|
const MEDIA_CONTEXT_INFOBOX_HERO_IMAGE = 'infobox-hero-image';
|
|
const MEDIA_CONTEXT_INFOBOX = 'infobox';
|
|
|
|
private static $templates = [
|
|
'wrapper' => 'PortableInfoboxWrapper.mustache',
|
|
'title' => 'PortableInfoboxItemTitle.mustache',
|
|
'header' => 'PortableInfoboxItemHeader.mustache',
|
|
'image' => 'PortableInfoboxItemImage.mustache',
|
|
'image-mobile' => 'PortableInfoboxItemImageMobile.mustache',
|
|
'data' => 'PortableInfoboxItemData.mustache',
|
|
'group' => 'PortableInfoboxItemGroup.mustache',
|
|
'horizontal-group-content' => 'PortableInfoboxHorizontalGroupContent.mustache',
|
|
'navigation' => 'PortableInfoboxItemNavigation.mustache',
|
|
'hero-mobile' => 'PortableInfoboxItemHeroMobile.mustache',
|
|
'image-collection' => 'PortableInfoboxItemImageCollection.mustache',
|
|
'image-collection-mobile' => 'PortableInfoboxItemImageCollection.mustache' // TODO: Create separated template for mobile
|
|
];
|
|
private $templateEngine;
|
|
|
|
function __construct() {
|
|
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
|
|
->setPrefix( self::getTemplatesDir() );
|
|
}
|
|
|
|
public static function getTemplatesDir() {
|
|
return dirname( __FILE__ ) . '/../templates';
|
|
}
|
|
|
|
public static function getTemplates() {
|
|
return self::$templates;
|
|
}
|
|
|
|
/**
|
|
* renders infobox
|
|
*
|
|
* @param array $infoboxdata
|
|
*
|
|
* @return string - infobox HTML
|
|
*/
|
|
public function renderInfobox( array $infoboxdata, $theme, $layout ) {
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
$helper = new PortableInfoboxRenderServiceHelper();
|
|
$infoboxHtmlContent = '';
|
|
$heroData = [ ];
|
|
|
|
foreach ( $infoboxdata as $item ) {
|
|
$data = $item[ 'data' ];
|
|
$type = $item[ 'type' ];
|
|
|
|
switch ( $type ) {
|
|
case 'group':
|
|
$infoboxHtmlContent .= $this->renderGroup( $data );
|
|
break;
|
|
case 'navigation':
|
|
$infoboxHtmlContent .= $this->renderItem( 'navigation', $data );
|
|
break;
|
|
default:
|
|
if ( $helper->isWikiaMobile() && $helper->isValidHeroDataItem( $item, $heroData ) ) {
|
|
$heroData[ $type ] = $data;
|
|
continue;
|
|
}
|
|
|
|
if ( $helper->isTypeSupportedInTemplates( $type, self::getTemplates() ) ) {
|
|
$infoboxHtmlContent .= $this->renderItem( $type, $data );
|
|
};
|
|
}
|
|
}
|
|
|
|
if ( !empty( $heroData ) ) {
|
|
$infoboxHtmlContent = $this->renderInfoboxHero( $heroData ) . $infoboxHtmlContent;
|
|
}
|
|
|
|
if ( !empty( $infoboxHtmlContent ) ) {
|
|
$output = $this->renderItem( 'wrapper',
|
|
[ 'content' => $infoboxHtmlContent, 'theme' => $theme, 'layout' => $layout ] );
|
|
} else {
|
|
$output = '';
|
|
}
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* renders group infobox component
|
|
*
|
|
* @param array $groupData
|
|
*
|
|
* @return string - group HTML markup
|
|
*/
|
|
private function renderGroup( $groupData ) {
|
|
$helper = new PortableInfoboxRenderServiceHelper();;
|
|
$groupHTMLContent = '';
|
|
$dataItems = $groupData[ 'value' ];
|
|
$layout = $groupData[ 'layout' ];
|
|
|
|
if ( $layout === 'horizontal' ) {
|
|
$groupHTMLContent .= $this->renderItem(
|
|
'horizontal-group-content',
|
|
$helper->createHorizontalGroupData( $dataItems )
|
|
);
|
|
} else {
|
|
foreach ( $dataItems as $item ) {
|
|
$type = $item[ 'type' ];
|
|
|
|
if ( $helper->isTypeSupportedInTemplates( $type, self::getTemplates() ) ) {
|
|
$groupHTMLContent .= $this->renderItem( $type, $item[ 'data' ] );
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->renderItem( 'group', [ 'content' => $groupHTMLContent ] );
|
|
}
|
|
|
|
/**
|
|
* renders infobox hero component
|
|
*
|
|
* @param array $data - infobox hero component data
|
|
*
|
|
* @return string
|
|
*/
|
|
private function renderInfoboxHero( $data ) {
|
|
$helper = new PortableInfoboxRenderServiceHelper();
|
|
|
|
if ( array_key_exists( 'image', $data ) ) {
|
|
$image = $data[ 'image' ][ 0 ];
|
|
$image[ 'context' ] = self::MEDIA_CONTEXT_INFOBOX_HERO_IMAGE;
|
|
$image = $helper->extendImageData( $image );
|
|
$data['image'] = $image;
|
|
$markup = $this->renderItem( 'hero-mobile', $data );
|
|
} else {
|
|
$markup = $this->renderItem( 'title', $data[ 'title' ] );
|
|
}
|
|
|
|
return $markup;
|
|
}
|
|
|
|
/**
|
|
* renders part of infobox
|
|
* If image element has invalid thumbnail, doesn't render this element at all.
|
|
*
|
|
* @param string $type
|
|
* @param array $data
|
|
*
|
|
* @return bool|string - HTML
|
|
*/
|
|
private function renderItem( $type, array $data ) {
|
|
$helper = new PortableInfoboxRenderServiceHelper();
|
|
|
|
if ( $type === 'image' ) {
|
|
$images = array();
|
|
for ( $i = 0; $i < count($data); $i++ ) {
|
|
$data[$i][ 'context' ] = self::MEDIA_CONTEXT_INFOBOX;
|
|
$data[$i] = $helper->extendImageData( $data[$i] );
|
|
if ( !!$data[$i] ) {
|
|
$images[] = $data[$i];
|
|
}
|
|
}
|
|
if ( count ( $images ) === 0 ) {
|
|
return false;
|
|
} else if ( count ( $images ) === 1 ) {
|
|
$data = $images[0];
|
|
$templateName = $type;
|
|
} else {
|
|
$data = array( 'images' => $images );
|
|
$templateName = 'image-collection';
|
|
}
|
|
if ( $helper->isWikiaMobile() ) {
|
|
$templateName = $templateName . self::MOBILE_TEMPLATE_POSTFIX;
|
|
}
|
|
} else {
|
|
$templateName = $type;
|
|
}
|
|
|
|
if ( $helper->isWikiaMobile() ) {
|
|
$data = $helper->sanitizeInfoboxTitle( $type, $data );
|
|
}
|
|
|
|
return $this->templateEngine->clearData()
|
|
->setData( $data )
|
|
->render( self::getTemplates()[ $templateName ] );
|
|
}
|
|
}
|