Completed
Push — master ( 118804...eea786 )
by Stephan
01:58
created

MapView::setMapProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SRF\Filtered\View;
4
5
use DataValues\Geo\Parsers\GeoCoordinateParser;
6
use Exception;
7
use SMWPropertyValue;
8
use SRF\Filtered\ResultItem;
9
10
class MapView extends View {
11
12
	private static $viewParams = null;
13
14
	private $mapProvider = null;
15
16
17
	/**
18
	 * @param null $mapProvider
19
	 */
20
	public function setMapProvider( $mapProvider ) {
21
		$this->mapProvider = $mapProvider;
22
	}
23
24
	/**
25
	 * @return null
26
	 */
27
	public function getMapProvider() {
0 ignored issues
show
Coding Style introduced by
getMapProvider uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
28
		if ( $this->mapProvider === null ) {
29
			$this->setMapProvider( isset( $GLOBALS[ 'srfgMapProvider' ] ) ? $GLOBALS[ 'srfgMapProvider' ] : '' );
30
		}
31
32
		return $this->mapProvider;
33
	}
34
35
	/**
36
	 * @param ResultItem $row
37
	 * @return array|null
38
	 */
39
	public function getJsDataForRow( ResultItem $row ) {
40
41
		$markerPositionPropertyName = $this->getActualParameters()[ 'map view marker position property' ];
42
43
		foreach ( $row->getValue() as $field ) {
44
45
			$printRequest = $field->getPrintRequest();
46
			$field->reset();
47
48
			$value = $field->getNextDataItem();
49
			if ( $printRequest->getData() instanceof SMWPropertyValue &&
50
				$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName &&
51
				( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob )
52
			) {
53
				$values = []; // contains plain text
54
55
				if ( $value instanceof \SMWDIGeoCoord ) {
56
57
					while ( $value instanceof \SMWDIGeoCoord ) {
58
						$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ];
59
						$value = $field->getNextDataItem();
60
					}
61
62
				} else {
63
64
					$coordParser = new GeoCoordinateParser();
65
					while ( $value instanceof \SMWDataItem ) {
66
						try {
67
							$latlng = $coordParser->parse( $value->getSerialization() );
68
							$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ];
69
							$value = $field->getNextDataItem();
70
						} catch ( Exception $exception ) {
71
							$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() );
72
						}
73
					}
74
75
				}
76
77
				return [ 'positions' => $values, ];
78
			}
79
		}
80
81
		return null;
82
	}
83
84
	/**
85
	 * Returns an array of config data for this view to be stored in the JS
86
	 * @return array
87
	 */
88
	public function getJsConfig() {
89
		$config = parent::getJsConfig();
90
91
		foreach ( [ 'height', 'zoom', 'min zoom', 'max zoom' ] as $key ) {
92
			$this->addToConfig( $config, $key );
93
		}
94
95
		$config[ 'map provider' ] = $this->getMapProvider();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config['map provider'] is correct as $this->getMapProvider() (which targets SRF\Filtered\View\MapView::getMapProvider()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
97
		return $config;
98
	}
99
100
	/**
101
	 * A function to describe the allowed parameters of a query for this view.
102
	 *
103
	 * @return array of Parameter
104
	 */
105
	public static function getParameters() {
106
107
		if ( self::$viewParams === null ) {
108
109
			$params = parent::getParameters();
110
111
			$params[ 'marker position property' ] = [
112
				// 'type' => 'string',
113
				'name' => 'map view marker position property',
114
				'message' => 'srf-paramdesc-filtered-map-position',
115
				'default' => '',
116
				// 'islist' => false,
117
			];
118
119
			$params[ 'height' ] = [
120
				'type' => 'dimension',
121
				'name' => 'map view height',
122
				'message' => 'srf-paramdesc-filtered-map-height',
123
				'default' => 'auto',
124
				// 'islist' => false,
125
			];
126
127
			$params[ 'zoom' ] = [
128
				'type' => 'integer',
129
				'name' => 'map view zoom',
130
				'message' => 'srf-paramdesc-filtered-map-zoom',
131
				'default' => 14,
132
				// 'islist' => false,
133
			];
134
135
			$params[ 'min zoom' ] = [
136
				'type' => 'integer',
137
				'name' => 'map view min zoom',
138
				'message' => 'srf-paramdesc-filtered-map-zoom',
139
				'default' => -1,
140
				// 'islist' => false,
141
			];
142
143
			$params[ 'max zoom' ] = [
144
				'type' => 'integer',
145
				'name' => 'map view max zoom',
146
				'message' => 'srf-paramdesc-filtered-map-zoom',
147
				'default' => -1,
148
				// 'islist' => false,
149
			];
150
151
			self::$viewParams = $params;
152
		}
153
154
		return self::$viewParams;
155
	}
156
157
	/**
158
	 * Returns the name of the resource module to load.
159
	 *
160
	 * @return string
161
	 */
162
	public function getResourceModules() {
163
		return 'ext.srf.filtered.map-view';
164
	}
165
166
	/**
167
	 * @param array $config
168
	 * @param string $key
169
	 */
170
	private function addToConfig( &$config, $key ) {
171
172
		$paramDefinition = self::getParameters()[ $key ];
173
174
		$param = $this->getActualParameters()[ $paramDefinition[ 'name' ] ];
175
176
		if ( $param !== $paramDefinition[ 'default' ] ) {
177
			$config[ $key ] = $param;
178
		}
179
180
	}
181
182
	/**
183
	 * @return bool
184
	 */
185
	public function getInitError() {
186
		return $this->getMapProvider() === ''? 'srf-filtered-map-provider-missing-error' : null;
187
	}
188
189
}