PortableInfobox/services/PortableInfoboxRenderService.class.php

169 lines
4.3 KiB
PHP
Raw Normal View History

<?php
class PortableInfoboxRenderService extends WikiaService {
const LOGGER_LABEL = 'portable-infobox-render-not-supported-type';
const DESKTOP_THUMBNAIL_WIDTH = 270;
const MOBILE_THUMBNAIL_WIDTH = 360;
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',
'navigation' => 'PortableInfoboxItemNavigation.mustache'
];
private $templateEngine;
function __construct() {
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
->setPrefix( dirname( __FILE__ ) . '/../templates' );
}
/**
* renders infobox
*
* @param array $infoboxdata
* @return string - infobox HTML
*/
2015-06-03 13:48:44 +00:00
public function renderInfobox( array $infoboxdata, $theme, $layout ) {
wfProfileIn( __METHOD__ );
$infoboxHtmlContent = '';
foreach ( $infoboxdata as $item ) {
$data = $item[ 'data' ];
$type = $item[ 'type' ];
switch ( $type ) {
case 'group':
2015-06-09 11:37:03 +00:00
$infoboxHtmlContent .= $this->renderGroup( $data );
break;
case 'navigation':
$infoboxHtmlContent .= $this->renderItem( 'navigation', $data );
break;
default:
if ( $this->validateType( $type ) ) {
$infoboxHtmlContent .= $this->renderItem( $type, $data );
};
}
}
if(!empty($infoboxHtmlContent)) {
2015-06-03 13:48:44 +00:00
$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 ) {
$groupHTMLContent = '';
2015-06-09 11:37:03 +00:00
$dataItems = $groupData['value'];
2015-06-10 15:24:49 +00:00
$layout = $groupData['layout'];
2015-06-09 11:37:03 +00:00
foreach ( $dataItems as $item ) {
$type = $item['type'];
if ( $this->validateType( $type ) ) {
$groupHTMLContent .= $this->renderItem( $type, $item['data'] );
}
}
2015-06-10 15:24:49 +00:00
return $this->renderItem( 'group', [ 'content' => $groupHTMLContent, 'layout' => $layout] );
}
/**
* renders part of infobox
2015-07-10 12:10:03 +00:00
* If image element has invalid thumbnail, doesn't render this element at all.
*
* @param string $type
* @param array $data
2015-07-11 08:59:56 +00:00
* @return bool|string - HTML
*/
private function renderItem( $type, array $data ) {
//TODO: with validated the performance of render Service and in the next phase we want to refactor it (make
// it modular) While doing this we also need to move this logic to appropriate image render class
2015-05-06 14:02:44 +00:00
if ( $type === 'image' ) {
$data[ 'key' ] = urlencode( $data[ 'key' ] );
2015-07-08 16:31:56 +00:00
$thumbnail = $this->getThumbnail( $data['name'] );
2015-07-08 11:22:12 +00:00
2015-07-10 12:10:03 +00:00
if (!$thumbnail) {
return false;
2015-07-08 11:22:12 +00:00
}
2015-05-12 11:32:42 +00:00
2015-07-10 12:10:03 +00:00
$data[ 'height' ] = $thumbnail->getHeight();
$data[ 'width' ] = $thumbnail->getWidth();
$data[ 'thumbnail' ] = $thumbnail->getUrl();
if ( $this->isWikiaMobile() ) {
2015-05-12 11:32:42 +00:00
$type = $type . self::MOBILE_TEMPLATE_POSTFIX;
}
2015-05-06 14:02:44 +00:00
}
return $this->templateEngine->clearData()
->setData( $data )
->render( $this->templates[ $type ] );
}
2015-05-22 15:27:20 +00:00
/**
2015-07-08 16:31:56 +00:00
* @desc create a thumb of the image from file title
* @param $title
2015-07-11 08:59:56 +00:00
* @return bool|MediaTransformOutput
2015-05-22 15:27:20 +00:00
*/
2015-07-08 16:31:56 +00:00
protected function getThumbnail( $title ) {
2015-07-07 14:28:53 +00:00
$file = \WikiaFileHelper::getFileFromTitle( $title );
if ( $file ) {
$width = $this->isWikiaMobile() ?
self::MOBILE_THUMBNAIL_WIDTH :
self::DESKTOP_THUMBNAIL_WIDTH;
2015-07-08 16:31:56 +00:00
$thumb = $file->transform( ['width' => $width] );
if (!is_null($thumb) && !$thumb->isError()) {
return $thumb;
}
2015-07-07 14:28:53 +00:00
}
2015-07-08 11:22:12 +00:00
return false;
2015-07-07 14:28:53 +00:00
}
2015-05-26 10:57:00 +00:00
/**
* required for testing mobile template rendering
* @return bool
*/
protected function isWikiaMobile() {
return F::app()->checkSkin( 'wikiamobile' );
2015-05-07 12:36:30 +00:00
}
/**
* check if item type is supported and logs unsupported types
*
* @param string $type - template type
* @return bool
*/
private function validateType( $type ) {
$isValid = true;
if ( !isset( $this->templates[ $type ] ) ) {
Wikia\Logger\WikiaLogger::instance()->info( self::LOGGER_LABEL, [
'type' => $type
] );
$isValid = false;
}
return $isValid;
}
}