00001 <?php
00002
00012 class SMWTableResultPrinter extends SMWResultPrinter {
00013
00014 protected $mHTMLClass = '';
00015
00016 public function getName() {
00017 return wfMsg( 'smw_printername_' . $this->mFormat );
00018 }
00019
00028 protected function handleParameters( array $params, $outputmode ) {
00029 parent::handleParameters( $params, $outputmode );
00030 $this->mHTMLClass = $params['class'];
00031 }
00032
00033 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00034 $result = '';
00035
00036 $columnClasses = array();
00037
00038 if ( $this->mShowHeaders != SMW_HEADERS_HIDE ) {
00039 $headers = array();
00040
00041 foreach ( $res->getPrintRequests() as $pr ) {
00042 $attribs = array();
00043 $columnClass = str_replace( array( ' ', '_' ), '-', $pr->getText( SMW_OUTPUT_WIKI ) );
00044 $attribs['class'] = $columnClass;
00045
00046
00047 $columnClasses[] = $columnClass;
00048 $text = $pr->getText( $outputmode, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null : $this->mLinker ) );
00049
00050 $headers[] = Html::rawElement(
00051 'th',
00052 $attribs,
00053 $text === '' ? ' ' : $text
00054 );
00055 }
00056
00057 $headers = '<tr>' . implode( "\n", $headers ) . '</tr>';
00058
00059 if ( $outputmode == SMW_OUTPUT_HTML ) {
00060 $headers = '<thead>' . $headers . '</thead>';
00061 }
00062 $headers = "\n$headers\n";
00063
00064 $result .= $headers;
00065 }
00066
00067 $tableRows = array();
00068 $rowNum = 1;
00069 while ( $subject = $res->getNext() ) {
00070 $tableRows[] = $this->getRowForSubject( $subject, $outputmode, $columnClasses, $rowNum++ );
00071 }
00072
00073 $tableRows = implode( "\n", $tableRows );
00074
00075 if ( $outputmode == SMW_OUTPUT_HTML ) {
00076 $tableRows = '<tbody>' . $tableRows . '</tbody>';
00077 }
00078
00079 $result .= $tableRows;
00080
00081
00082 if ( $this->linkFurtherResults( $res ) ) {
00083 $link = $res->getQueryLink();
00084 if ( $this->getSearchLabel( $outputmode ) ) {
00085 $link->setCaption( $this->getSearchLabel( $outputmode ) );
00086 }
00087 $result .= "\t<tr class=\"smwfooter\"><td class=\"sortbottom\" colspan=\"" . $res->getColumnCount() . '"> ' . $link->getText( $outputmode, $this->mLinker ) . "</td></tr>\n";
00088 }
00089
00090
00091 $tableAttrs = array( 'class' => $this->mHTMLClass );
00092
00093 if ( $this->mFormat == 'broadtable' ) {
00094 $tableAttrs['width'] = '100%';
00095 }
00096
00097 $result = Xml::tags( 'table', $tableAttrs, $result );
00098
00099 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
00100
00101 return $result;
00102 }
00103
00114 protected function getRowForSubject( array $subject, $outputmode, $columnClasses, $rowNum ) {
00115 $cells = array();
00116
00117 foreach ( $subject as $i => $field ) {
00118
00119
00120 if ( array_key_exists( $i, $columnClasses ) ) {
00121 $columnClass = $columnClasses[$i];
00122 } else {
00123 $columnClass = null;
00124 }
00125 $cells[] = $this->getCellForPropVals( $field, $outputmode, $columnClass );
00126 }
00127
00128 $rowClass = ( $rowNum % 2 == 1 ) ? 'row-odd' : 'row-even';
00129 return "<tr class=\"$rowClass\">\n\t" . implode( "\n\t", $cells ) . "\n</tr>";
00130 }
00131
00142 protected function getCellForPropVals( SMWResultArray $resultArray, $outputmode, $columnClass ) {
00143 $dataValues = array();
00144
00145 while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
00146 $dataValues[] = $dv;
00147 }
00148
00149 $attribs = array();
00150 $content = null;
00151
00152 if ( count( $dataValues ) > 0 ) {
00153 $sortkey = $dataValues[0]->getDataItem()->getSortKey();
00154
00155 if ( is_numeric( $sortkey ) ) {
00156 $attribs['data-sort-value'] = $sortkey;
00157 }
00158
00159 $alignment = trim( $resultArray->getPrintRequest()->getParameter( 'align' ) );
00160
00161 if ( in_array( $alignment, array( 'right', 'left', 'center' ) ) ) {
00162 $attribs['style'] = "text-align:' . $alignment . ';";
00163 }
00164 $attribs['class'] = $columnClass;
00165
00166 $content = $this->getCellContent(
00167 $dataValues,
00168 $outputmode,
00169 $resultArray->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS
00170 );
00171 }
00172
00173 return Html::rawElement(
00174 'td',
00175 $attribs,
00176 $content
00177 );
00178 }
00179
00191 protected function getCellContent( array $dataValues, $outputmode, $isSubject ) {
00192 $values = array();
00193
00194 foreach ( $dataValues as $dv ) {
00195 $value = $dv->getShortText( $outputmode, $this->getLinker( $isSubject ) );
00196 $values[] = $value;
00197 }
00198
00199 return implode( '<br />', $values );
00200 }
00201
00202 public function getParameters() {
00203 $params = array_merge( parent::getParameters(), parent::textDisplayParameters() );
00204
00205 $params['class'] = new Parameter( 'class', Parameter::TYPE_STRING );
00206 $params['class']->setMessage( 'smw-paramdesc-table-class' );
00207 $params['class']->setDefault( 'sortable wikitable smwtable' );
00208
00209 return $params;
00210 }
00211
00212 }