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