Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php |
— | — | @@ -13,6 +13,8 @@ |
14 | 14 | */ |
15 | 15 | class SMWTableResultPrinter extends SMWResultPrinter { |
16 | 16 | |
| 17 | + protected $columnsWithSortKey = array(); |
| 18 | + |
17 | 19 | public function getName() { |
18 | 20 | smwfLoadExtensionMessages( 'SemanticMediaWiki' ); |
19 | 21 | return wfMsg( 'smw_printername_' . $this->mFormat ); |
— | — | @@ -22,58 +24,39 @@ |
23 | 25 | global $smwgIQRunningNumber; |
24 | 26 | SMWOutputs::requireHeadItem( SMW_HEADER_SORTTABLE ); |
25 | 27 | |
| 28 | + $tableRows = array(); |
| 29 | + |
| 30 | + while ( $record = $res->getNext() ) { |
| 31 | + $tableRows[] = $this->getRowForRecord( $record, $outputmode ); |
| 32 | + } |
| 33 | + |
26 | 34 | // print header |
27 | 35 | $result = '<table class="smwtable"' . |
28 | 36 | ( $this->mFormat == 'broadtable' ? ' width="100%"' : '' ) . |
29 | 37 | " id=\"querytable$smwgIQRunningNumber\">\n"; |
30 | 38 | |
31 | 39 | if ( $this->mShowHeaders != SMW_HEADERS_HIDE ) { // building headers |
32 | | - $result .= "\t<tr>\n"; |
| 40 | + $headers = array(); |
33 | 41 | |
34 | 42 | foreach ( $res->getPrintRequests() as $pr ) { |
35 | | - $result .= "\t\t<th>" . $pr->getText( $outputmode, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker ) ) . "</th>\n"; |
| 43 | + $attribs = array(); |
| 44 | + |
| 45 | + if ( array_key_exists( $pr->getHash(), $this->columnsWithSortKey ) ) { |
| 46 | + $attribs['class'] = 'numericsort'; |
| 47 | + } |
| 48 | + |
| 49 | + $headers[] = Html::rawElement( |
| 50 | + 'th', |
| 51 | + $attribs, |
| 52 | + $pr->getText( $outputmode, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker ) ) |
| 53 | + ); |
36 | 54 | } |
37 | 55 | |
38 | | - $result .= "\t</tr>\n"; |
| 56 | + array_unshift( $tableRows, '<tr>' . implode( "\n", $headers ) . '</tr>' ); |
39 | 57 | } |
40 | 58 | |
41 | | - // print all result rows |
42 | | - while ( $row = $res->getNext() ) { |
43 | | - $result .= "\t<tr>\n"; |
44 | | - $firstcol = true; |
45 | | - $fieldcount = - 1; |
46 | | - foreach ( $row as $field ) { |
47 | | - $fieldcount = $fieldcount + 1; |
48 | | - |
49 | | - $result .= "\t\t<td"; |
50 | | - $alignment = trim( $field->getPrintRequest()->getParameter( 'align' ) ); |
51 | | - if ( ( $alignment == 'right' ) || ( $alignment == 'left' ) || ( $alignment == 'center' ) ) { |
52 | | - $result .= ' style="text-align:' . $alignment . ';"'; |
53 | | - } |
54 | | - $result .= ">"; |
55 | | - |
56 | | - $first = true; |
57 | | - while ( ( $dv = $field->getNextDataValue() ) !== false ) { |
58 | | - if ( $first ) { |
59 | | - $sortkey = $dv->getDataItem()->getSortKey(); |
60 | | - if ( is_numeric( $sortkey ) ) { // additional hidden sortkey for numeric entries |
61 | | - $result .= '<span class="smwsortkey">' . $sortkey . '</span>'; |
62 | | - } |
63 | | - $first = false; |
64 | | - } else { |
65 | | - $result .= '<br />'; |
66 | | - } |
67 | | - // use shorter "LongText" for wikipage |
68 | | - $result .= ( ( $dv->getTypeID() == '_wpg' ) || ( $dv->getTypeID() == '__sin' ) ) ? |
69 | | - $dv->getLongText( $outputmode, $this->getLinker( $firstcol ) ) : |
70 | | - $dv->getShortText( $outputmode, $this->getLinker( $firstcol ) ); |
71 | | - } |
72 | | - $result .= "</td>\n"; |
73 | | - $firstcol = false; |
74 | | - } |
75 | | - $result .= "\t</tr>\n"; |
76 | | - } |
77 | | - |
| 59 | + $result .= implode( "\n", $tableRows ); |
| 60 | + |
78 | 61 | // print further results footer |
79 | 62 | if ( $this->linkFurtherResults( $res ) ) { |
80 | 63 | $link = $res->getQueryLink(); |
— | — | @@ -82,11 +65,67 @@ |
83 | 66 | } |
84 | 67 | $result .= "\t<tr class=\"smwfooter\"><td class=\"sortbottom\" colspan=\"" . $res->getColumnCount() . '"> ' . $link->getText( $outputmode, $this->mLinker ) . "</td></tr>\n"; |
85 | 68 | } |
| 69 | + |
86 | 70 | $result .= "</table>\n"; // print footer |
87 | 71 | $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed |
| 72 | + |
88 | 73 | return $result; |
89 | 74 | } |
90 | 75 | |
| 76 | + protected function getRowForRecord( array /* of SMWResultArray */ $record, $outputmode ) { |
| 77 | + $cells = array(); |
| 78 | + |
| 79 | + foreach ( $record as $field ) { |
| 80 | + $cells[] = $this->getCellForPropVals( $field, $outputmode ); |
| 81 | + } |
| 82 | + |
| 83 | + return "<tr>\n\t" . implode( "\n\t", $cells ) . "\n</tr>"; |
| 84 | + } |
| 85 | + |
| 86 | + protected function getCellForPropVals( SMWResultArray $resultArray, $outputmode ) { |
| 87 | + $attribs = array(); |
| 88 | + |
| 89 | + $alignment = trim( $resultArray->getPrintRequest()->getParameter( 'align' ) ); |
| 90 | + |
| 91 | + if ( in_array( $alignment, array( 'right', 'left', 'center' ) ) ) { |
| 92 | + $attribs['style'] = "text-align:' . $alignment . ';"; |
| 93 | + } |
| 94 | + |
| 95 | + return Html::rawElement( |
| 96 | + 'td', |
| 97 | + $attribs, |
| 98 | + $this->getCellContent( $resultArray, $outputmode ) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + protected function getCellContent( SMWResultArray $resultArray, $outputmode ) { |
| 103 | + $values = array(); |
| 104 | + $isFirst = true; |
| 105 | + |
| 106 | + while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) { |
| 107 | + $sortKey = ''; |
| 108 | + |
| 109 | + if ( $isFirst ) { |
| 110 | + $isFirst = false; |
| 111 | + $sortkey = $dv->getDataItem()->getSortKey(); |
| 112 | + |
| 113 | + if ( is_numeric( $sortkey ) ) { // additional hidden sortkey for numeric entries |
| 114 | + $this->columnsWithSortKey[$resultArray->getPrintRequest()->getHash()] = true; |
| 115 | + $sortKey .= '<span class="smwsortkey">' . $sortkey . '</span>'; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + $isSubject = $resultArray->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS; |
| 120 | + $value = ( ( $dv->getTypeID() == '_wpg' ) || ( $dv->getTypeID() == '__sin' ) ) ? |
| 121 | + $dv->getLongText( $outputmode, $this->getLinker( $isSubject ) ) : |
| 122 | + $dv->getShortText( $outputmode, $this->getLinker( $isSubject ) ); |
| 123 | + |
| 124 | + $values[] = $sortKey . $value; |
| 125 | + } |
| 126 | + |
| 127 | + return implode( '<br />', $values ); |
| 128 | + } |
| 129 | + |
91 | 130 | public function getParameters() { |
92 | 131 | return array_merge( parent::getParameters(), parent::textDisplayParameters() ); |
93 | 132 | } |
Index: trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js |
— | — | @@ -126,17 +126,12 @@ |
127 | 127 | return; |
128 | 128 | } |
129 | 129 | |
130 | | - sortfn = smw_sort_caseinsensitive; // sorting w/o keys |
131 | | - // check for sorting keys and change sorting function |
132 | | - var itm = table.rows[1].cells[column]; |
133 | | - var spans = itm.getElementsByTagName( 'span' ); |
134 | | - if( spans.length > 0 ) { |
135 | | - for ( var i = 0; i < spans.length; i++ ) { |
136 | | - if( spans[i].className == 'smwsortkey' ) { |
137 | | - sortfn = smw_sort_numeric; // sorting with keys |
138 | | - } |
139 | | - } |
| 130 | + if ( table.rows[0].cells[column].className == 'numericsort' ) { |
| 131 | + sortfn = smw_sort_numeric; // sorting with keys |
140 | 132 | } |
| 133 | + else { |
| 134 | + sortfn = smw_sort_caseinsensitive; |
| 135 | + } |
141 | 136 | |
142 | 137 | SORT_COLUMN_INDEX = column; |
143 | 138 | var firstRow = new Array(); |