Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
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 / 44
0.00% covered (danger)
0.00%
0 / 2
306
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 / 41
0.00% covered (danger)
0.00%
0 / 1
272
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 = $displayParams['header field'];
34            if ( $headerField[0] != '_' ) {
35                $headerField = str_replace( '_', ' ', $headerField );
36            }
37            if ( count( $valuesTable ) > 0 && !array_key_exists( $headerField, $valuesTable[0] ) ) {
38                throw new MWException( "Error: the header field \"$headerField\" must be among this query's fields." );
39            }
40            $this->undisplayedFields[] = $headerField;
41        } else {
42            $headerField = null;
43        }
44
45        $result = '';
46        $num = count( $valuesTable );
47
48        $prev_first_char = "";
49        $rows_per_column = ceil( $num / $numColumns );
50        // Column width is a percentage.
51        $column_width = floor( 100 / $numColumns );
52
53        // Print all result rows:
54        $rowindex = 0;
55
56        foreach ( $formattedValuesTable as $i => $row ) {
57            if ( $headerField == null ) {
58                $curValue = reset( $valuesTable[$i] );
59            } else {
60                $curValue = $valuesTable[$i][$headerField];
61            }
62            // Ignore the namespace when setting the index character.
63            if ( array_key_exists( 'namespace', $row ) ) {
64                $curValue = str_replace( $row['namespace'] . ':', '', $curValue );
65            }
66            $cur_first_char = $contLang->firstChar( $curValue );
67
68            if ( $rowindex % $rows_per_column == 0 ) {
69                $result .= "\n\t\t\t<div style=\"float: left; width: $column_width%;\">\n";
70                if ( $cur_first_char == $prev_first_char ) {
71                    $result .= "\t\t\t\t<ul>\n";
72                }
73            }
74
75            // If we're at a new first letter, end
76            // the last list and start a new one.
77            if ( $cur_first_char != $prev_first_char ) {
78                if ( $rowindex % $rows_per_column > 0 ) {
79                    $result .= "                </ul>\n";
80                }
81                $result .= "\t\t\t\t<h3>$cur_first_char</h3>\n                <ul>\n";
82            }
83            $prev_first_char = $cur_first_char;
84
85            $result .= '<li>' . $this->displayRow( $row, $fieldDescriptions ) . "</li>\n";
86
87            // end list if we're at the end of the column
88            // or the page
89            if ( ( $rowindex + 1 ) % $rows_per_column == 0 && ( $rowindex + 1 ) < $num ) {
90                $result .= "\t\t\t\t</ul>\n\t\t\t</div> <!-- end column -->";
91            }
92
93            $rowindex++;
94        }
95
96        $result .= "</ul>\n</div> <!-- end column -->";
97        // clear all the CSS floats
98        $result .= "\n" . '<br style="clear: both;"/>';
99
100        // <H3> will generate TOC entries otherwise. Probably need another way
101        // to accomplish this -- user might still want TOC for other page content.
102        // $result .= '__NOTOC__';
103        return Html::rawElement( 'div', [ 'class' => 'cargo-category-format-results' ], $result );
104    }
105
106}