Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoDisplayMap
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
380
0.00% covered (danger)
0.00%
0 / 1
 run
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
380
1<?php
2/**
3 * CargoDisplayMap - class for the #cargo_display_map parser function.
4 *
5 * @author Yaron Koren
6 * @ingroup Cargo
7 */
8
9class CargoDisplayMap {
10
11    /**
12     * Handles the #cargo_display_map parser function - displays a
13     * map showing a single point.
14     *
15     * This function is based conceptually on the #display_map
16     * parser function defined by the Maps extension.
17     *
18     * @param Parser $parser
19     * @return string|array
20     */
21    public static function run( $parser ) {
22        $params = func_get_args();
23        array_shift( $params ); // we already know the $parser...
24
25        $pointStr = null;
26        $serviceStr = null;
27        $heightStr = null;
28        $widthStr = null;
29        $zoomStr = null;
30        $imageStr = null;
31
32        foreach ( $params as $param ) {
33            $parts = explode( '=', $param, 2 );
34            if ( count( $parts ) != 2 ) {
35                continue;
36            }
37            $key = trim( $parts[0] );
38            $value = trim( $parts[1] );
39            if ( $key == 'point' ) {
40                $pointStr = $value;
41            } elseif ( $key == 'service' ) {
42                $serviceStr = $value;
43            } elseif ( $key == 'height' ) {
44                $heightStr = $value;
45            } elseif ( $key == 'width' ) {
46                $widthStr = $value;
47            } elseif ( $key == 'zoom' ) {
48                $zoomStr = $value;
49            } elseif ( $key == 'image' ) {
50                $imageStr = $value;
51            }
52        }
53
54        // If $pointStr is null, it means that the 'point' parameter
55        // was not set for #cargo_display_map - display an error
56        // message. If, however, it is blank, it means that it was set,
57        // but to a blank value, most likely via a template call. In
58        // that case, just don't display anything.
59        if ( $pointStr === null ) {
60            return CargoUtils::formatError( "Error: 'point' parameter must be set." );
61        }
62        if ( $pointStr === '' ) {
63            return '';
64        }
65
66        // Simulate a query with the appropriate mapping format.
67        // Ideally, both this code and the #cargo_query code would
68        // call some separate mapping code, but that's not the case
69        // yet.
70        if ( $serviceStr == 'googlemaps' ) {
71            $mappingFormat = new CargoGoogleMapsFormat( $parser->getOutput() );
72        } elseif ( $serviceStr == 'leaflet' ) {
73            $mappingFormat = new CargoLeafletFormat( $parser->getOutput() );
74        } else {
75            $mappingFormat = new CargoOpenLayersFormat( $parser->getOutput() );
76        }
77
78        try {
79            [ $lat, $lon ] = CargoUtils::parseCoordinatesString( $pointStr );
80        } catch ( MWException $e ) {
81            return CargoUtils::formatError( "Cannot display map: " . $e->getMessage() );
82        }
83        $valuesTable = [ [ 'Coords  lat' => $lat, 'Coords  lon' => $lon ] ];
84        $formattedValuesTable = $valuesTable;
85        $coordsDesc = new CargoFieldDescription();
86        $coordsDesc->mType = 'Coordinates';
87        $fieldDescriptions = [ 'Coords' => $coordsDesc ];
88        $displayParams = [];
89        if ( $heightStr != null ) {
90            $displayParams['height'] = $heightStr;
91        }
92        if ( $widthStr != null ) {
93            $displayParams['width'] = $widthStr;
94        }
95        if ( $zoomStr != null ) {
96            $displayParams['zoom'] = $zoomStr;
97        }
98        if ( $imageStr != null ) {
99            $displayParams['image'] = $imageStr;
100        }
101
102        try {
103            $text = $mappingFormat->display( $valuesTable,
104                $formattedValuesTable, $fieldDescriptions,
105                $displayParams );
106        } catch ( MWException $e ) {
107            return CargoUtils::formatError( $e->getMessage() );
108        }
109
110        return [ $text, 'noparse' => true, 'isHTML' => true ];
111    }
112
113}