00001 <?php
00017 class SRFTimeline extends SMWResultPrinter {
00018
00019 protected $m_tlstart = '';
00020 protected $m_tlend = '';
00021 protected $m_tlsize = '';
00022 protected $m_tlbands = '';
00023 protected $m_tlpos = '';
00024
00033 protected function handleParameters( array $params, $outputmode ) {
00034 parent::handleParameters( $params, $outputmode );
00035
00036 $this->m_tlstart = smwfNormalTitleDBKey( $params['timelinestart'] );
00037 $this->m_tlend = smwfNormalTitleDBKey( $params['timelineend'] );
00038 $this->m_tlbands = $params['timelinebands'];
00039 $this->m_tlpos = strtolower( trim( $params['timelineposition'] ) );
00040
00041
00042
00043 $this->m_tlsize = htmlspecialchars( str_replace( ';', ' ', strtolower( $params['timelinesize'] ) ) );
00044 }
00045
00046 public function getName() {
00047 return wfMsg( 'srf_printername_' . $this->mFormat );
00048 }
00049
00050 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00051 global $smwgIQRunningNumber;
00052
00053 $this->includeJS();
00054
00055 $isEventline = 'eventline' == $this->mFormat;
00056
00057 if ( !$isEventline && ( $this->m_tlstart == '' ) ) {
00058 foreach ( $res->getPrintRequests() as $pr ) {
00059 if ( ( $pr->getMode() == SMWPrintRequest::PRINT_PROP ) && ( $pr->getTypeID() == '_dat' ) ) {
00060 $dataValue = $pr->getData();
00061
00062 $date_value = $dataValue->getDataItem()->getLabel();
00063
00064 if ( ( $this->m_tlend == '' ) && ( $this->m_tlstart != '' ) &&
00065 ( $this->m_tlstart != $date_value ) ) {
00066 $this->m_tlend = $date_value;
00067 } elseif ( ( $this->m_tlstart == '' ) && ( $this->m_tlend != $date_value ) ) {
00068 $this->m_tlstart = $date_value;
00069 }
00070 }
00071 }
00072 }
00073
00074
00075 $link = $res->getQueryLink( wfMsgForContent( 'srf-timeline-allresults' ) );
00076 $result = "<div class=\"smwtimeline\" id=\"smwtimeline$smwgIQRunningNumber\" style=\"height: $this->m_tlsize\">";
00077 $result .= '<span class="smwtlcomment">' . wfMsgForContent( 'srf-timeline-nojs' ) . ' ' . $link->getText( $outputmode, $this->mLinker ) . '</span>';
00078
00079 foreach ( $this->m_tlbands as $band ) {
00080 $result .= '<span class="smwtlband" style="display:none;">' . htmlspecialchars( $band ) . '</span>';
00081
00082 }
00083
00084
00085 if ( ( $this->m_tlstart != '' ) || $isEventline ) {
00086 $result .= $this->getEventsHTML( $res, $outputmode, $isEventline );
00087 }
00088
00089
00090
00091 $result .= '</div>';
00092
00093
00094 $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
00095
00096 return $result;
00097 }
00098
00104 protected function includeJS() {
00105 SMWOutputs::requireHeadItem( SMW_HEADER_STYLE );
00106
00107
00108 $realFunction = array( 'SMWOutputs', 'requireResource' );
00109 if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) && is_callable( $realFunction ) ) {
00110 SMWOutputs::requireResource( 'ext.srf.timeline' );
00111 }
00112 else {
00113 global $srfgScriptPath;
00114 SMWOutputs::requireHeadItem(
00115 'smw_tlhelper',
00116 '<script type="text/javascript" src="' . $srfgScriptPath .
00117 '/Timeline/SRF_timeline.js"></script>'
00118 );
00119 SMWOutputs::requireHeadItem(
00120 'smw_tl',
00121 '<script type="text/javascript" src="' . $srfgScriptPath .
00122 '/Timeline/SimileTimeline/timeline-api.js"></script>'
00123 );
00124 }
00125 }
00126
00138 protected function getEventsHTML( SMWQueryResult $res, $outputmode, $isEventline ) {
00139 global $curarticle, $cururl;
00140
00141 $positions = array();
00142 $curcolor = 0;
00143
00144 $result = '';
00145
00146 $output = false;
00147 if ( $isEventline ) $events = array();
00148
00149 while ( $row = $res->getNext() ) {
00150 $hastime = false;
00151 $hastitle = false;
00152 $curdata = '';
00153 $curmeta = '';
00154 $cururl = '';
00155 $curarticle = '';
00156 $first_col = true;
00157
00158 foreach ( $row as $field ) {
00159 $first_value = true;
00160 $pr = $field->getPrintRequest();
00161 $dataValue = $pr->getData();
00162
00163 if ( $dataValue == '' ) {
00164 $date_value = null;
00165 }
00166 else {
00167 $date_value = $dataValue->getDataItem()->getLabel();
00168 }
00169
00170 while ( ( $object = $field->getNextDataValue() ) !== false ) {
00171 $event = $this->handlePropertyValue(
00172 $object, $outputmode, $pr, $first_col, $hastitle, $hastime,
00173 $first_value, $isEventline, $curmeta, $curdata, $date_value, $output, $positions
00174 );
00175
00176 if ( $event !== false ) {
00177 $events[] = $event;
00178 }
00179
00180 $first_value = false;
00181 }
00182
00183 if ( $output ) {
00184 $curdata .= '<br />';
00185 }
00186
00187 $output = false;
00188 $first_col = false;
00189 }
00190
00191 if ( $hastime ) {
00192 $result .= Html::rawElement(
00193 'span',
00194 array( 'class' => 'smwtlevent', 'style' => 'display:none;' ),
00195 $curmeta . Html::element(
00196 'span',
00197 array( 'class' => 'smwtlcoloricon' ),
00198 $curcolor
00199 ) . $curdata
00200 );
00201 }
00202
00203 if ( $isEventline ) {
00204 foreach ( $events as $event ) {
00205 $result .= '<span class="smwtlevent" style="display:none;" ><span class="smwtlstart">' . $event[0] . '</span><span class="smwtlurl">' . str_replace( ' ', '_', $curarticle ) . '</span><span class="smwtlcoloricon">' . $curcolor . '</span>';
00206 if ( $curarticle != '' ) $result .= '<span class="smwtlprefix">' . $curarticle . ' </span>';
00207 $result .= $curdata . '</span>';
00208 $positions[$event[2]] = $event[0];
00209 }
00210 $events = array();
00211 $curcolor = ( $curcolor + 1 ) % 10;
00212 }
00213 }
00214
00215 if ( count( $positions ) > 0 ) {
00216 ksort( $positions );
00217 $positions = array_values( $positions );
00218
00219 switch ( $this->m_tlpos ) {
00220 case 'start':
00221 $result .= '<span class="smwtlposition" style="display:none;" >' . $positions[0] . '</span>';
00222 break;
00223 case 'end':
00224 $result .= '<span class="smwtlposition" style="display:none;" >' . $positions[count( $positions ) - 1] . '</span>';
00225 break;
00226 case 'today': break;
00227 case 'middle': default:
00228 $result .= '<span class="smwtlposition" style="display:none;" >' . $positions[ceil( count( $positions ) / 2 ) - 1] . '</span>';
00229 break;
00230 }
00231 }
00232
00233 return $result;
00234 }
00235
00259 protected function handlePropertyValue( SMWDataValue $object, $outputmode, SMWPrintRequest $pr, $first_col,
00260 &$hastitle, &$hastime, $first_value, $isEventline, &$curmeta, &$curdata, $date_value, &$output, array &$positions ) {
00261 global $curarticle, $cururl;
00262
00263 $event = false;
00264
00265 $l = $this->getLinker( $first_col );
00266
00267 if ( !$hastitle && $object->getTypeID() != '_wpg' ) {
00268 $l = null;
00269 }
00270
00271 if ( $object->getTypeID() == '_wpg' ) {
00272 $objectlabel = $object->getLongText( $outputmode, $l );
00273 } else {
00274 $objectlabel = $object->getShortText( $outputmode, $l );
00275 }
00276
00277 $urlobject = ( $l !== null );
00278 $header = '';
00279
00280 if ( $first_value ) {
00281
00282 if ( $this->mShowHeaders && ( '' != $pr->getLabel() ) ) {
00283 $header = $pr->getText( $outputmode, $this->mLinker ) . ': ';
00284 }
00285
00286
00287 if ( ( $pr->getMode() == SMWPrintRequest::PRINT_PROP ) &&
00288 ( $date_value == $this->m_tlstart ) ) {
00289
00290
00291
00292 $curmeta .= Html::element(
00293 'span',
00294 array( 'class' => 'smwtlstart' ),
00295 $object->getXMLSchemaDate()
00296 );
00297 $positions[$object->getHash()] = $object->getXMLSchemaDate();
00298 $hastime = true;
00299 }
00300
00301
00302 if ( ( $pr->getMode() == SMWPrintRequest::PRINT_PROP ) &&
00303 ( $date_value == $this->m_tlend ) ) {
00304
00305 $curmeta .= Html::element(
00306 'span',
00307 array( 'class' => 'smwtlend' ),
00308 $object->getXMLSchemaDate( false )
00309 );
00310 }
00311
00312
00313 if ( !$hastitle ) {
00314 $curmeta .= Html::element(
00315 'span',
00316 array(
00317 'class' => $urlobject ? 'smwtlurl' : 'smwtltitle'
00318 ),
00319 $objectlabel
00320 );
00321
00322 if ( $pr->getMode() == SMWPrintRequest::PRINT_THIS ) {
00323 $curarticle = $object->getShortText( $outputmode, false );
00324 $cururl = $object->getTitle()->getFullUrl();
00325 }
00326
00327
00328 $hastitle = true;
00329 }
00330 } elseif ( $output ) {
00331
00332 $curdata .= ', ';
00333 }
00334
00335 if ( !$first_col || !$first_value || $isEventline ) {
00336 $curdata .= $header . $objectlabel;
00337 $output = true;
00338 }
00339
00340 if ( $isEventline && ( $pr->getMode() == SMWPrintRequest::PRINT_PROP ) && ( $pr->getTypeID() == '_dat' ) && ( '' != $pr->getLabel() ) && ( $date_value != $this->m_tlstart ) && ( $date_value != $this->m_tlend ) ) {
00341 $event = array(
00342 $object->getXMLSchemaDate(),
00343 $pr->getLabel(),
00344 $object->getDataItem()->getSortKey(),
00345 );
00346 }
00347
00348 return $event;
00349 }
00350
00358 public function getParameters() {
00359 $params = parent::getParameters();
00360
00361 $params['timelinesize'] = new Parameter( 'timelinesize' );
00362 $params['timelinesize']->setDefault( '300px' );
00363 $params['timelinesize']->setMessage( 'srf_paramdesc_timelinesize' );
00364
00365 $params['timelineposition'] = new Parameter( 'timelineposition' );
00366 $params['timelineposition']->setDefault( 'middle' );
00367 $params['timelineposition']->setMessage( 'srf_paramdesc_timelineposition' );
00368 $params['timelineposition']->addCriteria( new CriterionInArray( 'start', 'middle', 'end', 'today' ) );
00369
00370 $params['timelinestart'] = new Parameter( 'timelinestart' );
00371 $params['timelinestart']->setDefault( '' );
00372 $params['timelinestart']->setMessage( 'srf_paramdesc_timelinestart' );
00373
00374 $params['timelineend'] = new Parameter( 'timelineend' );
00375 $params['timelineend']->setDefault( '' );
00376 $params['timelineend']->setMessage( 'srf_paramdesc_timelineend' );
00377
00378 $params['timelinebands'] = new ListParameter( 'timelinebands' );
00379 $params['timelinebands']->setDefault( array( 'MONTH', 'YEAR' ) );
00380 $params['timelinebands']->setMessage( 'srf_paramdesc_timelinebands' );
00381 $params['timelinebands']->addCriteria( new CriterionInArray( 'DECADE', 'YEAR', 'MONTH', 'WEEK', 'DAY', 'HOUR', 'MINUTE' ) );
00382
00383 return $params;
00384 }
00385
00386 }