00001 <?php
00002
00013 class SRFGallery extends SMWResultPrinter {
00014
00015 public function getName() {
00016 return wfMsg( 'srf_printername_gallery' );
00017 }
00018
00019 public function getResult( SMWQueryResult $results, array $params, $outputmode ) {
00020
00021 $this->handleParameters( $params, $outputmode );
00022 return $this->getResultText( $results, SMW_OUTPUT_HTML );
00023 }
00024
00025 public function getResultText( SMWQueryResult $results, $outputmode ) {
00026 global $wgUser, $wgParser;
00027
00028 $ig = new ImageGallery();
00029 $ig->setShowBytes( false );
00030 $ig->setShowFilename( false );
00031 $ig->setParser( $wgParser );
00032 $ig->setCaption( $this->mIntro );
00033
00034 if ( $this->params['galleryformat'] == 'carousel' ) {
00035 static $carouselNr = 0;
00036
00037
00038 $dataAttribs = array(
00039 'wrap' => 'both',
00040 'vertical' => 'false',
00041 'rtl' => 'false',
00042 );
00043
00044
00045 if ( empty( $this->params['perrow'] ) ) {
00046 $dataAttribs['scroll'] = 1;
00047 } else {
00048 $dataAttribs['scroll'] = $this->params['perrow'];
00049 $dataAttribs['visible'] = $this->params['perrow'];
00050 }
00051
00052 $attribs = array(
00053 'id' => 'carousel' . ++$carouselNr,
00054 'class' => 'jcarousel jcarousel-skin-smw',
00055 'style' => 'display:none;',
00056 );
00057
00058 foreach ( $dataAttribs as $name => $value ) {
00059 $attribs['data-' . $name] = $value;
00060 }
00061
00062 $ig->setAttributes( $attribs );
00063
00064
00065 SMWOutputs::requireResource( 'ext.srf.jcarousel' );
00066 }
00067
00068
00069 if ( $this->params['perrow'] !== '' && $this->params['galleryformat'] !== 'carousel' ) {
00070 $ig->setPerRow( $this->params['perrow'] );
00071 }
00072
00073 if ( $this->params['widths'] !== '' ) {
00074 $ig->setWidths( $this->params['widths'] );
00075 }
00076
00077 if ( $this->params['heights'] !== '' ) {
00078 $ig->setHeights( $this->params['heights'] );
00079 }
00080
00081 $printReqLabels = array();
00082
00083 foreach ( $results->getPrintRequests() as $printReq ) {
00084 $printReqLabels[] = $printReq->getLabel();
00085 }
00086
00087 if ( $this->params['imageproperty'] !== '' && in_array( $this->params['imageproperty'], $printReqLabels ) ) {
00088 $this->addImageProperties( $results, $ig, $this->params['imageproperty'], $this->params['captionproperty'] );
00089 }
00090 else {
00091 $this->addImagePages( $results, $ig );
00092 }
00093
00094 return array( $ig->toHTML(), 'nowiki' => true, 'isHTML' => true );
00095 }
00096
00107 protected function addImageProperties( SMWQueryResult $results, ImageGallery &$ig, $imageProperty, $captionProperty ) {
00108 while ( $row = $results->getNext() ) {
00109 $images = array();
00110 $captions = array();
00111
00112 for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) {
00113 if ( $row[$i]->getPrintRequest()->getLabel() == $imageProperty ) {
00114 while ( ( $obj = $row[$i]->getNextDataValue() ) !== false ) {
00115 if ( $obj->getTypeID() == '_wpg' ) {
00116 $images[] = $obj->getTitle();
00117 }
00118 }
00119 }
00120 elseif ( $row[$i]->getPrintRequest()->getLabel() == $captionProperty ) {
00121 while ( ( $obj = $row[$i]->getNextDataValue() ) !== false ) {
00122 $captions[] = $obj->getShortText( SMW_OUTPUT_HTML, $this->getLinker( true ) );
00123 }
00124 }
00125 }
00126
00127 $amountMatches = count( $captions ) == count( $images );
00128 $hasCaption = $amountMatches || count( $captions ) > 0;
00129
00130 foreach ( $images as $imgTitle ) {
00131 if ( $imgTitle->exists() ) {
00132 $imgCaption = $hasCaption ? ( $amountMatches ? array_shift( $captions ) : $captions[0] ) : '';
00133 $this->addImageToGallery( $ig, $imgTitle, $imgCaption );
00134 }
00135 }
00136 }
00137 }
00138
00147 protected function addImagePages( SMWQueryResult $results, ImageGallery &$ig ) {
00148 while ( $row = $results->getNext() ) {
00149 $firstField = $row[0];
00150 $nextObject = $firstField->getNextDataValue();
00151
00152 if ( $nextObject !== false ) {
00153 $imgTitle = $nextObject->getTitle();
00154 $imgCaption = '';
00155
00156
00157 if ( isset( $row[1] ) ) {
00158 $imgCaption = $row[1]->getNextDataValue();
00159 if ( is_object( $imgCaption ) ) {
00160 $imgCaption = $imgCaption->getShortText( SMW_OUTPUT_HTML, $this->getLinker( true ) );
00161 }
00162 }
00163
00164 $this->addImageToGallery( $ig, $imgTitle, $imgCaption );
00165 }
00166 }
00167 }
00168
00179 protected function addImageToGallery( ImageGallery &$ig, Title $imgTitle, $imgCaption ) {
00180 global $wgParser;
00181
00182 if ( empty( $imgCaption ) ) {
00183 if ( $this->m_params['autocaptions'] ) {
00184 $imgCaption = $imgTitle->getBaseText();
00185
00186 if ( !$this->m_params['fileextensions'] ) {
00187 $imgCaption = preg_replace( '#\.[^.]+$#', '', $imgCaption );
00188 }
00189 }
00190 else {
00191 $imgCaption = '';
00192 }
00193 }
00194 else {
00195 $imgCaption = $wgParser->recursiveTagParse( $imgCaption );
00196
00197
00198
00199 }
00200
00201 $ig->add( $imgTitle, $imgCaption );
00202
00203
00204 if ( $imgTitle->getNamespace() == NS_IMAGE && !is_null( $imgTitle->getDBkey() ) ) {
00205 $wgParser->mOutput->addImage( $imgTitle->getDBkey() );
00206 }
00207 }
00208
00216 public function getParameters() {
00217 $params = parent::getParameters();
00218
00219 if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
00220
00221 $params['galleryformat'] = new Parameter( 'galleryformat', Parameter::TYPE_STRING, '' );
00222 $params['galleryformat']->setMessage( 'srf_paramdesc_galleryformat' );
00223 $params['galleryformat']->addCriteria( new CriterionInArray( 'carousel' ) );
00224 }
00225
00226 $params['perrow'] = new Parameter( 'perrow', Parameter::TYPE_INTEGER );
00227 $params['perrow']->setMessage( 'srf_paramdesc_perrow' );
00228 $params['perrow']->setDefault( '', false );
00229
00230 $params['widths'] = new Parameter( 'widths', Parameter::TYPE_INTEGER );
00231 $params['widths']->setMessage( 'srf_paramdesc_widths' );
00232 $params['widths']->setDefault( '', false );
00233
00234 $params['heights'] = new Parameter( 'heights', Parameter::TYPE_INTEGER );
00235 $params['heights']->setMessage( 'srf_paramdesc_heights' );
00236 $params['heights']->setDefault( '', false );
00237
00238 $params['autocaptions'] = new Parameter( 'autocaptions', Parameter::TYPE_BOOLEAN );
00239 $params['autocaptions']->setMessage( 'srf_paramdesc_autocaptions' );
00240 $params['autocaptions']->setDefault( true );
00241
00242 $params['fileextensions'] = new Parameter( 'fileextensions', Parameter::TYPE_BOOLEAN );
00243 $params['fileextensions']->setMessage( 'srf_paramdesc_fileextensions' );
00244 $params['fileextensions']->setDefault( false );
00245
00246 $params['captionproperty'] = new Parameter( 'captionproperty' );
00247 $params['captionproperty']->setMessage( 'srf_paramdesc_captionproperty' );
00248 $params['captionproperty']->setDefault( '' );
00249
00250 $params['imageproperty'] = new Parameter( 'imageproperty' );
00251 $params['imageproperty']->setMessage( 'srf_paramdesc_imageproperty' );
00252 $params['imageproperty']->setDefault( '' );
00253
00254 return $params;
00255 }
00256
00257 }