PortableInfobox/services/PortableInfoboxRenderService.class.php

161 lines
4.3 KiB
PHP
Raw Normal View History

<?php
use \Wikia\PortableInfobox\Helpers\PortableInfoboxRenderServiceHelper;
2015-07-02 11:55:39 +00:00
class PortableInfoboxRenderService extends WikiaService {
2015-05-12 11:32:42 +00:00
const MOBILE_TEMPLATE_POSTFIX = '-mobile';
private $templates = [
'wrapper' => 'PortableInfoboxWrapper.mustache',
'title' => 'PortableInfoboxItemTitle.mustache',
'header' => 'PortableInfoboxItemHeader.mustache',
'image' => 'PortableInfoboxItemImage.mustache',
2015-05-12 11:32:42 +00:00
'image-mobile' => 'PortableInfoboxItemImageMobile.mustache',
'data' => 'PortableInfoboxItemData.mustache',
'group' => 'PortableInfoboxItemGroup.mustache',
'horizontal-group-content' => 'PortableInfoboxHorizontalGroupContent.mustache',
2015-07-03 10:05:59 +00:00
'navigation' => 'PortableInfoboxItemNavigation.mustache',
'hero-mobile' => 'PortableInfoboxItemHeroMobile.mustache'
];
private $templateEngine;
2015-07-02 11:55:39 +00:00
function __construct() {
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
->setPrefix( dirname( __FILE__ ) . '/../templates' );
}
/**
* renders infobox
*
* @param array $infoboxdata
2015-07-02 11:55:39 +00:00
*
* @return string - infobox HTML
*/
2015-07-02 11:55:39 +00:00
public function renderInfobox( array $infoboxdata, $theme, $layout ) {
wfProfileIn( __METHOD__ );
$helper = PortableInfoboxRenderServiceHelper::getInstance();
$infoboxHtmlContent = '';
2015-07-02 11:55:39 +00:00
$heroData = [ ];
2015-07-02 11:55:39 +00:00
foreach ( $infoboxdata as $item ) {
$data = $item[ 'data' ];
$type = $item[ 'type' ];
2015-07-02 11:55:39 +00:00
switch ( $type ) {
case 'group':
2015-07-02 11:55:39 +00:00
$infoboxHtmlContent .= $this->renderGroup( $data );
break;
case 'navigation':
$infoboxHtmlContent .= $this->renderItem( 'navigation', $data );
break;
default:
if ( $helper->isInfoboxHeroEnabled() &&
$helper->isValidHeroDataItem( $item, $heroData ) ) {
2015-07-02 11:55:39 +00:00
$heroData[ $type ] = $data;
continue;
}
if ( $helper->validateType( $type, $this->templates ) ) {
2015-07-02 11:55:39 +00:00
$infoboxHtmlContent .= $this->renderItem( $type, $data );
};
}
}
2015-07-02 11:55:39 +00:00
if ( !empty( $heroData ) ) {
$infoboxHtmlContent = $this->renderInfoboxHero( $heroData ) . $infoboxHtmlContent;
}
2015-07-02 11:55:39 +00:00
if ( !empty( $infoboxHtmlContent ) ) {
$output = $this->renderItem( 'wrapper', [ 'content' => $infoboxHtmlContent, 'theme' => $theme,
'layout' => $layout ] );
} else {
$output = '';
}
2015-07-02 11:55:39 +00:00
wfProfileOut( __METHOD__ );
return $output;
}
/**
* renders group infobox component
*
* @param array $groupData
2015-07-02 11:55:39 +00:00
*
* @return string - group HTML markup
*/
2015-07-02 11:55:39 +00:00
private function renderGroup( $groupData ) {
$helper = PortableInfoboxRenderServiceHelper::getInstance();
$groupHTMLContent = '';
2015-07-02 11:55:39 +00:00
$dataItems = $groupData[ 'value' ];
$layout = $groupData[ 'layout' ];
if ( $layout === 'horizontal' && !$helper->isWikiaMobile() ) {
$groupHTMLContent .= $this->renderItem(
'horizontal-group-content',
$helper->createHorizontalGroupData( $dataItems )
);
} else {
foreach ( $dataItems as $item ) {
$type = $item[ 'type' ];
if ( $helper->validateType( $type, $this->templates ) ) {
$groupHTMLContent .= $this->renderItem( $type, $item[ 'data' ] );
}
}
}
2015-07-02 11:55:39 +00:00
return $this->renderItem( 'group', [ 'content' => $groupHTMLContent, 'layout' => $layout ] );
}
/**
* renders infobox hero component
*
* @param array $data - infobox hero component data
2015-07-02 11:55:39 +00:00
*
* @return string
*/
private function renderInfoboxHero( $data ) {
if ( array_key_exists( 'image', $data ) ) {
$data[ 'image' ] = PortableInfoboxRenderServiceHelper::getInstance()->extendImageData( $data[ 'image' ] );
$markup = $this->renderItem( 'hero-mobile', $data );
} else {
$markup = $this->renderItem( 'title', $data[ 'title' ] );
}
2015-07-02 11:55:39 +00:00
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 = PortableInfoboxRenderServiceHelper::getInstance();
2015-07-16 11:21:20 +00:00
if ( $type === 'image' ) {
$data = $helper->extendImageData( $data );
if ( !$data ) {
return false;
}
if ( $helper->isWikiaMobile() ) {
$type = $type . self::MOBILE_TEMPLATE_POSTFIX;
}
}
if ( $helper->isWikiaMobile() ) {
$data = $helper->sanitizeInfoboxTitle( $type, $data );
}
return $this->templateEngine->clearData()
->setData( $data )
->render( $this->templates[ $type ] );
}
}