1 | <?php |
||
20 | class CategoryResultPrinter extends ResultPrinter { |
||
21 | |||
22 | protected $mDelim; |
||
23 | protected $mTemplate; |
||
24 | protected $mUserParam; |
||
25 | protected $mNumColumns; |
||
26 | |||
27 | /** |
||
28 | * @see SMWResultPrinter::handleParameters |
||
29 | * |
||
30 | * @since 1.6.2 |
||
31 | * |
||
32 | * @param array $params |
||
33 | * @param $outputmode |
||
34 | */ |
||
35 | 3 | protected function handleParameters( array $params, $outputmode ) { |
|
43 | |||
44 | 1 | public function getName() { |
|
47 | |||
48 | 3 | protected function getResultText( SMWQueryResult $res, $outputMode ) { |
|
49 | 3 | $contentsByIndex = array(); |
|
50 | |||
51 | // Print all result rows: |
||
52 | 3 | $rowindex = 0; |
|
53 | 3 | $row = $res->getNext(); |
|
54 | |||
55 | 3 | $mwCollaboratorFactory = ApplicationFactory::getInstance()->newMwCollaboratorFactory(); |
|
56 | |||
57 | 3 | $htmlColumnListRenderer = $mwCollaboratorFactory->newHtmlColumnListRenderer(); |
|
58 | 3 | $templateRenderer = $mwCollaboratorFactory->newWikitextTemplateRenderer(); |
|
59 | |||
60 | 3 | while ( $row !== false ) { |
|
61 | 3 | $nextrow = $res->getNext(); // look ahead |
|
62 | |||
63 | 3 | if ( !isset( $row[0] ) ) { |
|
64 | $row = $nextrow; |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | 3 | $content = $row[0]->getContent(); |
|
69 | |||
70 | 3 | if ( !isset( $content[0] ) || !( $content[0] instanceof SMWDataItem ) ) { |
|
71 | 1 | $row = $nextrow; |
|
72 | 1 | continue; |
|
73 | } |
||
74 | |||
75 | 3 | $columnIndex = $this->getFirstLetterForCategory( $res, $content[0] ); |
|
76 | |||
77 | 3 | if ( !isset( $contentsByIndex[$columnIndex] ) ) { |
|
78 | 3 | $contentsByIndex[$columnIndex] = array(); |
|
79 | 3 | $lastColumnIndex = $columnIndex; |
|
80 | } |
||
81 | |||
82 | 3 | if ( $this->mTemplate !== '' ) { // build template code |
|
83 | |||
84 | 2 | $first_col = true; |
|
85 | 2 | $this->hasTemplates = true; |
|
86 | |||
87 | 2 | if ( $this->mUserParam ) { |
|
88 | $templateRenderer->addField( 'userparam', $this->mUserParam ); |
||
89 | } |
||
90 | |||
91 | 2 | $this->addRowFieldsToTemplate( |
|
92 | $res, |
||
93 | $row, |
||
94 | $first_col, |
||
95 | $templateRenderer |
||
96 | ); |
||
97 | |||
98 | 2 | $templateRenderer->addField( '#', $rowindex ); |
|
99 | 2 | $templateRenderer->packFieldsForTemplate( $this->mTemplate ); |
|
100 | |||
101 | // str_replace('|', '|', // encode '|' for use in templates (templates fail otherwise) -- |
||
|
|||
102 | // this is not the place for doing this, since even DV-Wikitexts contain proper "|"! |
||
103 | 2 | $contentsByIndex[$columnIndex][] = $templateRenderer->render(); |
|
104 | } else { // build simple list |
||
105 | 3 | $first_col = true; |
|
106 | 3 | $found_values = false; // has anything but the first column been printed? |
|
107 | 3 | $result = ''; |
|
108 | |||
109 | 3 | foreach ( $row as $field ) { |
|
110 | 3 | $first_value = true; |
|
111 | 3 | $fieldValues = array(); |
|
112 | |||
113 | 3 | while ( ( $text = $field->getNextText( SMW_OUTPUT_WIKI, $this->getLinker( $first_col ) ) ) !== false ) { |
|
114 | |||
115 | 3 | if ( !$first_col && !$found_values ) { // first values after first column |
|
116 | 3 | $result .= '('; |
|
117 | 3 | $found_values = true; |
|
118 | } |
||
119 | |||
120 | 3 | if ( $first_value ) { // first value in any column, print header |
|
121 | 3 | $first_value = false; |
|
122 | |||
123 | 3 | if ( $this->mShowHeaders && ( $field->getPrintRequest()->getLabel() !== '' ) ) { |
|
124 | 2 | $result .= $field->getPrintRequest()->getText( SMW_OUTPUT_WIKI, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null : $this->mLinker ) ) . ' '; |
|
125 | } |
||
126 | } |
||
127 | |||
128 | 3 | $fieldValues[] = $text; |
|
129 | } |
||
130 | |||
131 | 3 | $first_col = false; |
|
132 | |||
133 | // Always sort the column value list in the same order |
||
134 | 3 | natsort( $fieldValues ); |
|
135 | 3 | $result .= implode( ( $this->mDelim ? $this->mDelim : ',' ) . ' ', $fieldValues ) . ' '; |
|
136 | } |
||
137 | |||
138 | 3 | if ( $found_values ) { |
|
139 | 3 | $result = trim( $result ) . ')'; |
|
140 | } |
||
141 | |||
142 | 3 | $contentsByIndex[$columnIndex][] = $result; |
|
143 | } |
||
144 | |||
145 | 3 | $row = $nextrow; |
|
146 | 3 | $rowindex++; |
|
147 | } |
||
148 | |||
149 | 3 | if ( $contentsByIndex === array() ) { |
|
150 | |||
151 | 1 | $res->addErrors( array( |
|
152 | 1 | $this->msg( 'smw-qp-empty-data' )->inContentLanguage()->text() |
|
153 | ) ); |
||
154 | |||
155 | 1 | return ''; |
|
156 | } |
||
157 | |||
158 | // Make label for finding further results |
||
159 | 3 | if ( $this->linkFurtherResults( $res ) ) { |
|
160 | 1 | $contentsByIndex[$lastColumnIndex][] = $this->getFurtherResultsLink( $res, $outputMode )->getText( SMW_OUTPUT_WIKI, $this->mLinker ); |
|
161 | } |
||
162 | |||
163 | 3 | $htmlColumnListRenderer->setNumberOfColumns( $this->mNumColumns ); |
|
164 | 3 | $htmlColumnListRenderer->addContentsByIndex( $contentsByIndex ); |
|
165 | |||
166 | // Per convention, an explicit 0 setting forces the columns to behave responsive |
||
167 | 3 | if ( $this->params['columns'] == 0 ) { |
|
168 | $htmlColumnListRenderer->setColumnClass( 'smw-column-responsive' ); |
||
169 | $htmlColumnListRenderer->setNumberOfColumns( 1 ); |
||
170 | } |
||
171 | |||
172 | 3 | return $htmlColumnListRenderer->getHtml(); |
|
173 | } |
||
174 | |||
175 | 3 | public function getParameters() { |
|
214 | |||
215 | 3 | private function getFirstLetterForCategory( SMWQueryResult $res, SMWDataItem $dataItem ) { |
|
226 | |||
227 | 2 | private function addRowFieldsToTemplate( $res, $row, &$first_col, $templateRenderer ) { |
|
257 | |||
258 | } |
||
259 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.