00001 <?php
00019 class SMWListResultPrinter extends SMWResultPrinter {
00020
00021 protected $mSep;
00022 protected $mTemplate;
00023 protected $mNamedArgs;
00024 protected $mUserParam;
00025 protected $mColumns;
00026 protected $mIntroTemplate;
00027 protected $mOutroTemplate;
00028
00037 protected function handleParameters( array $params, $outputmode ) {
00038 parent::handleParameters( $params, $outputmode );
00039
00040 $this->mSep = $params['sep'];
00041 $this->mTemplate = trim( $params['template'] );
00042 $this->mNamedArgs = $params['named args'];
00043 $this->mUserParam = trim( $params['userparam'] );
00044 $this->mColumns = !$this->isPlainlist() ? $params['columns'] : 1;
00045 $this->mIntroTemplate = $params['introtemplate'];
00046 $this->mOutroTemplate = $params['outrotemplate'];
00047 }
00048
00049 public function getName() {
00050 return wfMsg( 'smw_printername_' . $this->mFormat );
00051 }
00052
00053 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00054 if ( ( $this->mFormat == 'template' ) && ( $this->mTemplate == false ) ) {
00055 $res->addErrors( array( wfMsgForContent( 'smw_notemplategiven' ) ) );
00056 return '';
00057 }
00058
00059
00060 if ( ( $this->mFormat == 'ul' ) || ( $this->mFormat == 'ol' ) ) {
00061 $header = "<" . $this->mFormat . ">\n";
00062 $footer = "</" . $this->mFormat . ">\n";
00063 $rowstart = "\t<li>";
00064 $rowend = "</li>\n";
00065 $plainlist = false;
00066 } else {
00067 $header = '';
00068 $footer = '';
00069 $rowstart = '';
00070 $rowend = '';
00071 $plainlist = true;
00072
00073 }
00074
00075 if ( $this->mSep !== '' ) {
00076 $listsep = $this->mSep;
00077 $finallistsep = $listsep;
00078 } elseif ( $this->mFormat == 'list' ) {
00079
00080
00081
00082
00083
00084 $listsep = ', ';
00085 $finallistsep = wfMsgForContent( 'smw_finallistconjunct' ) . ' ';
00086 } else {
00087 $listsep = '';
00088 $finallistsep = '';
00089 }
00090
00091
00092 $result = '';
00093 $column_width = 0;
00094 $rows_per_column = -1;
00095 $rows_in_cur_column = -1;
00096
00097
00098 if ( $this->mColumns > 1 ) {
00099 $column_width = floor( 100 / $this->mColumns );
00100 $result .= '<div style="float: left; width: ' . $column_width . '%">' . "\n";
00101 $rows_per_column = ceil( $res->getCount() / $this->mColumns );
00102 $rows_in_cur_column = 0;
00103 }
00104
00105 if ( $header !== '' ) {
00106 $result .= $header;
00107 }
00108
00109 if ( $this->mIntroTemplate !== '' ) {
00110 $result .= "{{" . $this->mIntroTemplate . "}}";
00111 }
00112
00113
00114 $rownum = -1;
00115 while ( $row = $res->getNext() ) {
00116 $this->printRow( $row, $rownum, $rows_in_cur_column,
00117 $rows_per_column, $this->mFormat, $plainlist,
00118 $header, $footer, $rowstart, $rowend, $result,
00119 $column_width, $res, $listsep, $finallistsep );
00120 }
00121
00122 if ( $this->mOutroTemplate !== '' ) {
00123 $result .= "{{" . $this->mOutroTemplate . "}}";
00124 }
00125
00126
00127 if ( $this->linkFurtherResults( $res ) && ( ( $this->mFormat != 'ol' ) || ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) ) ) {
00128 $this->showFurtherResults( $result, $res, $rowstart, $rowend );
00129 }
00130
00131
00132 if ( $footer !== '' ) {
00133 $result .= $footer;
00134 }
00135
00136 if ( $this->mColumns > 1 ) {
00137 $result .= "</div>\n";
00138 }
00139
00140 if ( $this->mColumns > 1 ) {
00141 $result .= '<br style="clear: both" />' . "\n";
00142 }
00143
00144 return $result;
00145 }
00146
00147 protected function printRow( $row, &$rownum, &$rows_in_cur_column,
00148 $rows_per_column, $format, $plainlist, $header, $footer,
00149 $rowstart, $rowend, &$result, $column_width, $res, $listsep,
00150 $finallistsep ) {
00151
00152 $rownum++;
00153
00154 if ( $this->mColumns > 1 ) {
00155 if ( $rows_in_cur_column == $rows_per_column ) {
00156
00157
00158
00159
00160
00161
00162 if ( $format == 'ol' ) {
00163 $header = "<ol start=\"" . ( $rownum + 1 ) . "\">";
00164 }
00165 $result .= <<<END
00166
00167 $footer
00168 </div>
00169 <div style="float: left; width: $column_width%">
00170 $header
00171
00172 END;
00173 $rows_in_cur_column = 0;
00174 }
00175
00176 $rows_in_cur_column++;
00177 }
00178
00179 if ( $rownum > 0 && $plainlist ) {
00180 $result .= ( $rownum <= $res->getCount() ) ? $listsep : $finallistsep;
00181 } else {
00182 $result .= $rowstart;
00183 }
00184
00185 if ( $this->mTemplate !== '' ) {
00186 $this->hasTemplates = true;
00187 $wikitext = ( $this->mUserParam ) ? "|userparam=$this->mUserParam" : '';
00188
00189 foreach ( $row as $i => $field ) {
00190 $wikitext .= '|' . ( $this->mNamedArgs ? '?' . $field->getPrintRequest()->getLabel() : $i + 1 ) . '=';
00191 $first_value = true;
00192
00193 while ( ( $text = $field->getNextText( SMW_OUTPUT_WIKI, $this->getLinker( $i == 0 ) ) ) !== false ) {
00194 if ( $first_value ) $first_value = false; else $wikitext .= ', ';
00195 $wikitext .= $text;
00196 }
00197 }
00198
00199 $wikitext .= "|#=$rownum";
00200 $result .= '{{' . $this->mTemplate . $wikitext . '}}';
00201
00202 } else {
00203 $first_col = true;
00204 $found_values = false;
00205
00206 foreach ( $row as $field ) {
00207 $first_value = true;
00208
00209 while ( ( $text = $field->getNextText( SMW_OUTPUT_WIKI, $this->getLinker( $first_col ) ) ) !== false ) {
00210 if ( !$first_col && !$found_values ) {
00211 $result .= ' (';
00212 $found_values = true;
00213 } elseif ( $found_values || !$first_value ) {
00214
00215 $result .= "$listsep ";
00216 }
00217
00218 if ( $first_value ) {
00219 $first_value = false;
00220
00221 if ( ( $this->mShowHeaders != SMW_HEADERS_HIDE ) && ( $field->getPrintRequest()->getLabel() !== '' ) ) {
00222 $result .= $field->getPrintRequest()->getText( SMW_OUTPUT_WIKI, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker ) ) . ' ';
00223 }
00224 }
00225
00226 $result .= $text;
00227 }
00228
00229 $first_col = false;
00230 }
00231
00232 if ( $found_values ) $result .= ')';
00233 }
00234
00235 $result .= $rowend;
00236 }
00237
00238 protected function showFurtherResults( &$result, $res, $rowstart, $rowend ) {
00239 $link = $res->getQueryLink();
00240
00241 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) {
00242 $link->setCaption( $this->getSearchLabel( SMW_OUTPUT_WIKI ) );
00243 }
00244
00245 if ( $this->mSep !== '' ) {
00246 $link->setParameter( $this->mSep, 'sep' );
00247 }
00248
00249 $link->setParameter( $this->mFormat, 'format' );
00250
00251 if ( $this->mTemplate !== '' ) {
00252 $link->setParameter( $this->mTemplate, 'template' );
00253 if ( array_key_exists( 'link', $this->params ) ) {
00254 $link->setParameter( $this->params['link'], 'link' );
00255 }
00256 }
00257
00258 if ( $this->mUserParam !== '' ) {
00259 $link->setParameter( $this->mUserParam, 'userparam' );
00260 }
00261
00262 if ( $this->mColumns !== '' ) {
00263 $link->setParameter( $this->mColumns, 'columns' );
00264 }
00265
00266 if ( $this->mIntro !== '' ) {
00267 $link->setParameter( $this->mIntro, 'intro' );
00268 }
00269
00270 if ( $this->mOutro !== '' ) {
00271 $link->setParameter( $this->mOutro, 'outro' );
00272 }
00273
00274 if ( $this->mIntroTemplate !== '' ) {
00275 $link->setParameter( $this->mIntroTemplate, 'introtemplate' );
00276 }
00277
00278 if ( $this->mOutroTemplate !== '' ) {
00279 $link->setParameter( $this->mOutroTemplate, 'outrotemplate' );
00280 }
00281
00282 $result .= $rowstart . ' '. $link->getText( SMW_OUTPUT_WIKI, $this->mLinker ) . $rowend;
00283 }
00284
00285 protected function isPlainlist() {
00286 return $this->mFormat != 'ul' && $this->mFormat != 'ol';
00287 }
00288
00289 public function getParameters() {
00290 $params = array_merge( parent::getParameters(), parent::textDisplayParameters() );
00291
00292 $params['sep'] = new Parameter( 'sep' );
00293 $params['sep']->setMessage( 'smw_paramdesc_sep' );
00294 $params['sep']->setDefault( '' );
00295
00296 $params['template'] = new Parameter( 'template' );
00297 $params['template']->setMessage( 'smw_paramdesc_template' );
00298 $params['template']->setDefault( '' );
00299
00300 $params['named args'] = new Parameter( 'named args', Parameter::TYPE_BOOLEAN, false );
00301 $params['named args']->setMessage( 'smw_paramdesc_named_args' );
00302
00303 if ( !$this->isPlainlist() ) {
00304 $params['columns'] = new Parameter( 'columns', Parameter::TYPE_INTEGER );
00305 $params['columns']->setMessage( 'smw_paramdesc_columns', 1 );
00306 $params['columns']->setDefault( 1, false );
00307 $params['columns']->addCriteria( new CriterionInRange( 1, 10 ) );
00308 }
00309
00310 $params['userparam'] = new Parameter( 'userparam' );
00311 $params['userparam']->setMessage( 'smw_paramdesc_userparam' );
00312 $params['userparam']->setDefault( '' );
00313
00314 $params['introtemplate'] = new Parameter( 'introtemplate' );
00315 $params['introtemplate']->setMessage( 'smw_paramdesc_introtemplate' );
00316 $params['introtemplate']->setDefault( '' );
00317
00318 $params['outrotemplate'] = new Parameter( 'outrotemplate' );
00319 $params['outrotemplate']->setMessage( 'smw_paramdesc_outrotemplate' );
00320 $params['outrotemplate']->setDefault( '' );
00321
00322 return $params;
00323 }
00324
00325 }