1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WNBY; |
4
|
|
|
|
5
|
|
|
use Parser; |
6
|
|
|
use MapsMappingServices; |
7
|
|
|
use Html; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @since 1.0 |
12
|
|
|
* |
13
|
|
|
* @author mwjames |
14
|
|
|
*/ |
15
|
|
|
class NearbyParserFunction { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Parser |
19
|
|
|
*/ |
20
|
|
|
private $parser; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @return Parser $parser |
|
|
|
|
26
|
|
|
*/ |
27
|
5 |
|
public function __construct( Parser $parser ) { |
28
|
5 |
|
$this->parser = $parser; |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @since 1.0 |
33
|
|
|
* |
34
|
|
|
* @return array $params |
35
|
|
|
*/ |
36
|
4 |
|
public function parse( array $params ) { |
37
|
|
|
|
38
|
4 |
|
$parameters = array(); |
39
|
4 |
|
$class = 'whats-nearby'; |
40
|
|
|
|
41
|
4 |
|
if( isset( $params[0] ) && $params[0] instanceof \Parser ) { |
|
|
|
|
42
|
|
|
array_shift( $params ); |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
foreach ( $params as $key => $value ) { |
46
|
4 |
|
$this->doValidateKeyValueForParameterMatch( $key, $value, $class, $parameters ); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
4 |
|
$this->addMapServicesToOutput( $parameters ); |
50
|
|
|
|
51
|
4 |
|
$this->parser->getOutput()->addModules( |
52
|
|
|
'ext.whats.nearby' |
53
|
4 |
|
); |
54
|
|
|
|
55
|
4 |
|
return Html::rawElement( |
56
|
4 |
|
'div', |
57
|
|
|
array( |
58
|
4 |
|
'class' => $class . ( isset( $parameters['class'] ) ? ' ' . $parameters['class'] : '' ), |
59
|
4 |
|
'data-parameters' => json_encode( $parameters ) |
60
|
4 |
|
), |
61
|
4 |
|
Html::rawElement( 'div', array( 'id' => 'controls', 'style' => 'display:none' ) ) . |
62
|
4 |
|
Html::rawElement( 'div', array( 'id' => 'selection', 'style' => 'display:none' ) ) . |
63
|
4 |
|
Html::rawElement( 'div', array( 'id' => 'status' ), |
64
|
4 |
|
Html::rawElement( 'span', array( 'class' => 'geolocation' ) ) . |
65
|
4 |
|
Html::rawElement( 'span', array( 'class' => 'localcache' ) ) . |
66
|
4 |
|
Html::rawElement( 'span', array( 'class' => 'error' ) ) |
67
|
4 |
|
) . |
68
|
4 |
|
Html::rawElement( 'div', array( 'id' => 'output', 'style' => 'opacity: 0.5;' ), wfMessage( 'wnby-loading' )->text() ) |
69
|
4 |
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
private function doValidateKeyValueForParameterMatch( $key, $value, &$class, &$parameters ) { |
|
|
|
|
73
|
|
|
|
74
|
4 |
|
if ( $key == 0 && strpos( $value, '=' ) === false ) { |
75
|
|
|
$parameters['condition'] = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Build printrequest identifier for a template |
79
|
|
|
// extending argument |
80
|
4 |
|
if ( $value !== '' && $value{0} === '?' ) { |
81
|
1 |
|
$parameters['pr-' . $key] = $value; |
82
|
1 |
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
if ( strpos( $value, '=' ) === false ) { |
86
|
4 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
list( $k, $v ) = explode( '=', $value ); |
90
|
4 |
|
$parameters[strtolower( $k )] = $v; |
91
|
4 |
|
} |
92
|
|
|
|
93
|
4 |
|
private function addMapServicesToOutput( &$parameters ) { |
94
|
|
|
|
95
|
4 |
|
if ( isset( $parameters['format'] ) && |
96
|
4 |
|
in_array( $parameters['format'], array( 'openlayers', 'leaflet', 'googlemaps', 'googlemaps3', 'maps', 'google' ) ) ) { |
97
|
|
|
$parameters['maps'] = $parameters['format']; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
if ( !isset( $parameters['maps'] ) || !class_exists( 'MapsMappingServices' ) ) { |
101
|
2 |
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if ( $parameters['maps'] === 'openlayers' ) { |
105
|
1 |
|
$mapsOpenLayers = MapsMappingServices::getServiceInstance( 'openlayers' ); |
106
|
1 |
|
$mapsOpenLayers->addDependencies( $this->parser ); |
107
|
1 |
|
$this->parser->getOutput()->addJsConfigVars( $mapsOpenLayers->getConfigVariables() ); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
if ( |
111
|
2 |
|
$parameters['maps'] === 'googlemaps' || |
112
|
1 |
|
$parameters['maps'] === 'googlemaps3' || |
113
|
1 |
|
$parameters['maps'] === 'maps' || |
114
|
2 |
|
$parameters['maps'] === 'google' ) { |
115
|
1 |
|
$mapsGoogleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' ); |
116
|
1 |
|
$mapsGoogleMaps->addDependencies( $this->parser ); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
2 |
|
if ( $parameters['maps'] === 'leaflet' || $parameters['maps'] === 'leafletmaps' ) { |
120
|
|
|
$mapsLeaflet = MapsMappingServices::getServiceInstance( 'leaflet' ); |
121
|
|
|
$mapsLeaflet->addDependencies( $this->parser ); |
122
|
|
|
} |
123
|
2 |
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.