Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CargoCategoryFormat | |
0.00% |
0 / 42 |
|
0.00% |
0 / 2 |
272 | |
0.00% |
0 / 1 |
allowedParameters | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
display | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
240 |
1 | <?php |
2 | /** |
3 | * @author Yaron Koren |
4 | * @ingroup Cargo |
5 | */ |
6 | |
7 | class CargoCategoryFormat extends CargoListFormat { |
8 | |
9 | public static function allowedParameters() { |
10 | return [ |
11 | 'columns' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-columnsparam' )->parse() ] |
12 | ]; |
13 | } |
14 | |
15 | /** |
16 | * @param array $valuesTable |
17 | * @param array $formattedValuesTable |
18 | * @param array $fieldDescriptions |
19 | * @param array $displayParams |
20 | * @return string |
21 | */ |
22 | public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) { |
23 | $contLang = CargoUtils::getContentLang(); |
24 | |
25 | if ( array_key_exists( 'columns', $displayParams ) && $displayParams['columns'] != '' ) { |
26 | $numColumns = max( $displayParams['columns'], 1 ); |
27 | } else { |
28 | $numColumns = 3; |
29 | } |
30 | if ( array_key_exists( 'header field', $displayParams ) ) { |
31 | $headerField = str_replace( '_', ' ', $displayParams['header field'] ); |
32 | if ( count( $valuesTable ) > 0 && !array_key_exists( $headerField, $valuesTable[0] ) ) { |
33 | throw new MWException( "Error: the header field \"$headerField\" must be among this query's fields." ); |
34 | } |
35 | $this->undisplayedFields[] = $headerField; |
36 | } else { |
37 | $headerField = null; |
38 | } |
39 | |
40 | $result = ''; |
41 | $num = count( $valuesTable ); |
42 | |
43 | $prev_first_char = ""; |
44 | $rows_per_column = ceil( $num / $numColumns ); |
45 | // Column width is a percentage. |
46 | $column_width = floor( 100 / $numColumns ); |
47 | |
48 | // Print all result rows: |
49 | $rowindex = 0; |
50 | |
51 | foreach ( $formattedValuesTable as $i => $row ) { |
52 | if ( $headerField == null ) { |
53 | $curValue = reset( $valuesTable[$i] ); |
54 | } else { |
55 | $curValue = $valuesTable[$i][$headerField]; |
56 | } |
57 | // Ignore the namespace when setting the index character. |
58 | if ( array_key_exists( 'namespace', $row ) ) { |
59 | $curValue = str_replace( $row['namespace'] . ':', '', $curValue ); |
60 | } |
61 | $cur_first_char = $contLang->firstChar( $curValue ); |
62 | |
63 | if ( $rowindex % $rows_per_column == 0 ) { |
64 | $result .= "\n\t\t\t<div style=\"float: left; width: $column_width%;\">\n"; |
65 | if ( $cur_first_char == $prev_first_char ) { |
66 | $result .= "\t\t\t\t<ul>\n"; |
67 | } |
68 | } |
69 | |
70 | // If we're at a new first letter, end |
71 | // the last list and start a new one. |
72 | if ( $cur_first_char != $prev_first_char ) { |
73 | if ( $rowindex % $rows_per_column > 0 ) { |
74 | $result .= " </ul>\n"; |
75 | } |
76 | $result .= "\t\t\t\t<h3>$cur_first_char</h3>\n <ul>\n"; |
77 | } |
78 | $prev_first_char = $cur_first_char; |
79 | |
80 | $result .= '<li>' . $this->displayRow( $row, $fieldDescriptions ) . "</li>\n"; |
81 | |
82 | // end list if we're at the end of the column |
83 | // or the page |
84 | if ( ( $rowindex + 1 ) % $rows_per_column == 0 && ( $rowindex + 1 ) < $num ) { |
85 | $result .= "\t\t\t\t</ul>\n\t\t\t</div> <!-- end column -->"; |
86 | } |
87 | |
88 | $rowindex++; |
89 | } |
90 | |
91 | $result .= "</ul>\n</div> <!-- end column -->"; |
92 | // clear all the CSS floats |
93 | $result .= "\n" . '<br style="clear: both;"/>'; |
94 | |
95 | // <H3> will generate TOC entries otherwise. Probably need another way |
96 | // to accomplish this -- user might still want TOC for other page content. |
97 | // $result .= '__NOTOC__'; |
98 | return Html::rawElement( 'div', [ 'class' => 'cargo-category-format-results' ], $result ); |
99 | } |
100 | |
101 | } |