1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\Filtered\Filter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File holding the SRF_FF_Distance class |
7
|
|
|
* |
8
|
|
|
* @author Stephan Gambke |
9
|
|
|
* @file |
10
|
|
|
* @ingroup SemanticResultFormats |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use DataValues\Geo\Parsers\LatLongParser; |
14
|
|
|
use Exception; |
15
|
|
|
use SMWPropertyValue; |
16
|
|
|
use SRF\Filtered\ResultItem; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The SRF_FF_Distance class. |
20
|
|
|
* |
21
|
|
|
* Available parameters for this filter: |
22
|
|
|
* distance filter origin: the point from which the distance is measured (address or geo coordinate) |
23
|
|
|
* distance filter property: the property containing the point to which distance is measured - not implemented yet |
24
|
|
|
* distance filter unit: the unit in which the distance is measured |
25
|
|
|
* |
26
|
|
|
* @ingroup SemanticResultFormats |
27
|
|
|
*/ |
28
|
|
|
class DistanceFilter extends Filter { |
29
|
|
|
|
30
|
|
|
private $jsConfig; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the name (string) or names (array of strings) of the resource |
34
|
|
|
* modules to load. |
35
|
|
|
* |
36
|
|
|
* @return string|array |
37
|
|
|
*/ |
38
|
1 |
|
public function getResourceModules() { |
39
|
1 |
|
return 'ext.srf.filtered.distance-filter'; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
protected function buildJsConfig() { |
43
|
|
|
|
44
|
1 |
|
parent::buildJsConfig(); |
45
|
|
|
|
46
|
1 |
|
if ( !array_key_exists( 'distance filter origin', $this->getActualParameters() ) ) { |
47
|
1 |
|
$label = $this->getPrintRequest()->getLabel(); |
48
|
1 |
|
$this->getQueryPrinter()->addError( "Missing origin for distance filter on '$label'." ); |
49
|
1 |
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
|
54
|
1 |
|
$geoCoordinateParser = new LatLongParser(); |
55
|
|
|
|
56
|
1 |
|
$callback = function ( $value ) use ( $geoCoordinateParser ) { |
57
|
1 |
|
$latlng = $geoCoordinateParser->parse( $value ); |
58
|
1 |
|
return [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ]; |
59
|
1 |
|
}; |
60
|
|
|
|
61
|
1 |
|
$this->addValueToJsConfig( 'distance filter origin', 'origin', null, $callback ); |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
catch ( Exception $exception ) { |
65
|
|
|
$label = $this->getPrintRequest()->getLabel(); |
66
|
|
|
$this->getQueryPrinter()->addError( "Distance filter on $label: " . $exception->getMessage() ); |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$this->addValueToJsConfig( 'distance filter collapsible', 'collapsible' ); |
71
|
1 |
|
$this->addValueToJsConfig( 'distance filter initial value', 'initial value' ); |
72
|
1 |
|
$this->addValueToJsConfig( 'distance filter max distance', 'max' ); |
73
|
1 |
|
$this->addValueToJsConfig( 'distance filter unit', 'unit' ); |
74
|
1 |
|
$this->addValueListToJsConfig( 'distance filter switches', 'switches' ); |
75
|
|
|
|
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param ResultItem $row |
80
|
|
|
* |
81
|
|
|
* @return array|null |
82
|
|
|
*/ |
83
|
1 |
|
public function getJsDataForRow( ResultItem $row ) { |
84
|
|
|
|
85
|
1 |
|
$markerPositionPropertyName = $this->getPrintRequest()->getData()->getInceptiveProperty()->getKey(); |
86
|
|
|
|
87
|
1 |
|
foreach ( $row->getValue() as $field ) { |
88
|
|
|
|
89
|
1 |
|
$printRequest = $field->getPrintRequest(); |
90
|
1 |
|
$field->reset(); |
91
|
|
|
|
92
|
1 |
|
$value = $field->getNextDataItem(); |
93
|
1 |
|
if ( $printRequest->getData() instanceof SMWPropertyValue && |
|
|
|
|
94
|
1 |
|
$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName && |
95
|
1 |
|
( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob ) |
96
|
|
|
) { |
97
|
|
|
$values = []; // contains plain text |
98
|
|
|
|
99
|
|
|
if ( $value instanceof \SMWDIGeoCoord ) { |
100
|
|
|
|
101
|
|
|
while ( $value instanceof \SMWDIGeoCoord ) { |
102
|
|
|
$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ]; |
103
|
|
|
$value = $field->getNextDataItem(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} else { |
107
|
|
|
|
108
|
|
|
$coordParser = new LatLongParser(); |
109
|
|
|
while ( $value instanceof \SMWDataItem ) { |
110
|
|
|
try { |
111
|
|
|
$latlng = $coordParser->parse( $value->getSerialization() ); |
112
|
|
|
$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ]; |
113
|
|
|
} |
114
|
|
|
catch ( \Exception $exception ) { |
115
|
|
|
$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() ); |
116
|
|
|
} |
117
|
|
|
$value = $field->getNextDataItem(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return [ 'positions' => $values, ]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
1 |
|
public function isValidFilterForPropertyType() { |
133
|
1 |
|
return $this->getPrintRequest()->getTypeID() === '_geo' || $this->getPrintRequest()->getTypeID() === '_txt'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|