Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoPieChartFormat
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 allowedParameters
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 queryAndDisplay
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3 * @author Kris Field
4 * @ingroup Cargo
5 */
6
7use MediaWiki\Html\Html;
8
9class CargoPieChartFormat extends CargoDeferredFormat {
10
11    public static function allowedParameters() {
12        return [
13            'height' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-heightparam' )->parse() ],
14            'width' => [ 'type' => 'int', 'label' => wfMessage( 'cargo-viewdata-widthparam' )->parse() ],
15            'colors' => [ 'type' => 'string', 'label' => wfMessage( 'cargo-viewdata-colorsparam' )->parse() ],
16            'hide legend' => [ 'type' => 'boolean', 'label' => wfMessage( 'cargo-viewdata-hidelegendparam' )->parse() ]
17        ];
18    }
19
20    /**
21     * @param array $sqlQueries
22     * @param array $displayParams
23     * @param array|null $querySpecificParams Unused
24     * @return string HTML
25     */
26    public function queryAndDisplay( $sqlQueries, $displayParams, $querySpecificParams = null ) {
27        $this->mOutput->addModules( [ 'ext.cargo.nvd3' ] );
28        $ce = SpecialPage::getTitleFor( 'CargoExport' );
29        $queryParams = $this->sqlQueriesToQueryParams( $sqlQueries );
30        $queryParams['format'] = 'nvd3chart';
31
32        $svgAttrs = [];
33        if ( array_key_exists( 'width', $displayParams ) && $displayParams['width'] != '' ) {
34            $width = $displayParams['width'];
35            // Add on "px", if no unit is defined.
36            if ( is_numeric( $width ) ) {
37                $width .= "px";
38            }
39            $svgAttrs['width'] = $width;
40        } else {
41            $svgAttrs['width'] = '700px';
42        }
43        if ( array_key_exists( 'height', $displayParams ) && $displayParams['height'] != '' ) {
44            $height = $displayParams['height'];
45            // Add on "px", if no unit is defined.
46            if ( is_numeric( $height ) ) {
47                $height .= "px";
48            }
49            $svgAttrs['height'] = $height;
50        } else {
51            $svgAttrs['height'] = '400px';
52        }
53
54        $svgText = Html::element( 'svg', $svgAttrs, '' );
55
56        $divAttrs = [
57            'class' => 'cargoPieChart',
58            'dataurl' => $ce->getFullURL( $queryParams ),
59        ];
60        if ( array_key_exists( 'colors', $displayParams ) && $displayParams['colors'] != '' ) {
61            $divAttrs['data-colors'] = json_encode( explode( ',', $displayParams['colors'] ) );
62        }
63        if ( array_key_exists( 'hide legend', $displayParams ) ) {
64            $divAttrs['data-hide-legend'] = ( $displayParams['hide legend'] == 'yes' ) ? 1 : 0;
65        }
66        $text = Html::rawElement( 'div', $divAttrs, $svgText );
67
68        return $text;
69    }
70
71}