-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextension.driver.php
executable file
·192 lines (152 loc) · 4.92 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
class Extension_GeocodingField extends Extension {
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
protected static $fields = array();
public function uninstall() {
if(parent::uninstall() == true){
Symphony::Database()->query("DROP TABLE `tbl_fields_geocoding`");
return true;
}
return false;
}
public function install() {
try {
Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_fields_geocoding` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`field_id` INT(11) UNSIGNED NOT NULL,
`expression` VARCHAR(255) DEFAULT NULL,
`hide` ENUM('yes', 'no') DEFAULT 'no',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
} catch (Exception $e) {
return false;
}
return true;
}
public function update($previousVersion){
if (version_compare($previousVersion, '0.7', '<')) {
Symphony::Configuration()->remove('geocoding-field');
Administration::instance()->saveConfig();
}
return true;
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'compileBackendFields'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'compileBackendFields'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'compileFrontendFields'
),
);
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public function getXPath($entry) {
$entry_xml = new XMLElement('entry');
$data = $entry->getData();
$fields = array();
$entry_xml->setAttribute('id', $entry->get('id'));
$associated = $entry->fetchAllAssociatedEntryCounts();
if (is_array($associated) and !empty($associated)) {
foreach ($associated as $section => $count) {
$handle = Symphony::Database()->fetchVar('handle', 0, "
SELECT
s.handle
FROM
`tbl_sections` AS s
WHERE
s.id = '{$section}'
LIMIT 1
");
$entry_xml->setAttribute($handle, (string)$count);
}
}
// Add fields:
foreach ($data as $field_id => $values) {
if (empty($field_id)) continue;
$field = FieldManager::fetch($field_id);
$field->appendFormattedElement($entry_xml, $values, false, null);
}
$xml = new XMLElement('data');
$xml->appendChild($entry_xml);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
$dom->loadXML($xml->generate(true));
$xpath = new DOMXPath($dom);
if (version_compare(phpversion(), '5.3', '>=')) {
$xpath->registerPhpFunctions();
}
return $xpath;
}
/*
Modified from:
http://www.kevinbradwick.co.uk/developer/php/free-to-script-to-calculate-the-radius-of-a-coordinate-using-latitude-and-longitude
*/
public static function geoRadius($lat, $lng, $rad, $kilometers=false) {
$radius = ($kilometers) ? ($rad * 0.621371192) : $rad;
(float)$dpmLAT = 1 / 69.1703234283616;
// Latitude calculation
(float)$usrRLAT = $dpmLAT * $radius;
(float)$latMIN = $lat - $usrRLAT;
(float)$latMAX = $lat + $usrRLAT;
// Longitude calculation
(float)$mpdLON = 69.1703234283616 * cos($lat * (pi/180));
(float)$dpmLON = 1 / $mpdLON; // degrees per mile longintude
$usrRLON = $dpmLON * $radius;
$lonMIN = $lng - $usrRLON;
$lonMAX = $lng + $usrRLON;
return array("lonMIN" => $lonMIN, "lonMAX" => $lonMAX, "latMIN" => $latMIN, "latMAX" => $latMAX);
}
/*
Calculate distance between two lat/long pairs
*/
public static function geoDistance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtolower($unit);
$distance = 0;
if ($unit == "k") {
$distance = ($miles * 1.609344);
} else if ($unit == "n") {
$distance = ($miles * 0.8684);
} else {
$distance = $miles;
}
return round($distance, 1);
}
public function registerField($field) {
self::$fields[] = $field;
}
/*-------------------------------------------------------------------------
Delegates:
-------------------------------------------------------------------------*/
public function compileBackendFields($context) {
foreach (self::$fields as $field) {
$field->compile($context['entry']);
}
}
public function compileFrontendFields($context) {
foreach (self::$fields as $field) {
$field->compile($context['entry']);
}
}
}