|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class BrowseCitiesPage extends BrowseAbstractPage |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Standard SS static |
|
8
|
|
|
**/ |
|
9
|
|
|
public static $icon = "geobrowser/images/treeicons/BrowseCitiesPage"; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Standard SS static |
|
13
|
|
|
**/ |
|
14
|
|
|
public static $default_parent = "BrowseRegionsPage"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Standard SS static |
|
18
|
|
|
**/ |
|
19
|
|
|
public static $can_be_root = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Standard SS static |
|
23
|
|
|
**/ |
|
24
|
|
|
public static $db = array( |
|
25
|
|
|
"Latitude" => "Double", |
|
26
|
|
|
"Longitude" => "Double", |
|
27
|
|
|
"TimeZone"=> "Varchar(10)", |
|
28
|
|
|
"County" => "Varchar(25)", |
|
29
|
|
|
"Code" => "Varchar(4)", |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Standard SS Static |
|
34
|
|
|
**/ |
|
35
|
|
|
public static $defaults = array( |
|
36
|
|
|
"ShowInMenus" => false |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Array - $googleMapAddressArray: an array of geographic data provided by google maps |
|
41
|
|
|
* @param Int - $maxRadius: maximum number of kilometers (as the bird flies) between search point defined in $googleMapAddressArray and city found. |
|
42
|
|
|
* @return Object | false : returns a BrowseCitiesPage or false if nothing was found |
|
|
|
|
|
|
43
|
|
|
**/ |
|
44
|
|
|
public static function get_clostest_city_page($googleMapAddressArray, $maxRadius = 500) |
|
45
|
|
|
{ |
|
46
|
|
|
$cityPage = null; |
|
47
|
|
|
$suburbPage = null; |
|
48
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
|
49
|
|
|
$existingDistance = $maxRadius+1; |
|
50
|
|
|
$newDistance = $maxRadius+1; |
|
51
|
|
|
$existingPage = null; |
|
52
|
|
|
$newPage = null; |
|
53
|
|
|
$radiusSelectionSQL = self::radiusDefinitionOtherTable($googleMapAddressArray[0], $googleMapAddressArray[1], "BrowseCitiesPage", "Latitude", "Longitude"); |
|
54
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
|
|
55
|
|
|
$sqlQuery->select = array("{$bt}BrowseCitiesPage{$bt}.{$bt}ID{$bt}, ". $radiusSelectionSQL." as distance"); |
|
56
|
|
|
$sqlQuery->from[] = "{$bt}BrowseCitiesPage{$bt}"; |
|
57
|
|
|
$sqlQuery->where[] = $radiusSelectionSQL . " < ".$maxRadius; |
|
58
|
|
|
$sqlQuery->orderby = " distance "; |
|
59
|
|
|
$sqlQuery->limit = "1"; |
|
|
|
|
|
|
60
|
|
|
$result = $sqlQuery->execute(); |
|
61
|
|
|
$page = null; |
|
|
|
|
|
|
62
|
|
|
foreach ($result as $row) { |
|
63
|
|
|
$existingDistance = $row["distance"]; |
|
64
|
|
|
$existingPage = DataObject::get_by_id("BrowseCitiesPage", $row["ID"]); |
|
65
|
|
|
} |
|
66
|
|
|
$radiusSelectionSQL = self::radiusDefinitionOtherTable($googleMapAddressArray[0], $googleMapAddressArray[1], "cities", "Latitude", "Longitude"); |
|
67
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
|
|
68
|
|
|
$sqlQuery->select = array("cities.CityID", $radiusSelectionSQL." as distance"); |
|
69
|
|
|
$sqlQuery->from[] = "{$bt}cities{$bt}"; |
|
70
|
|
|
$sqlQuery->where[] = $radiusSelectionSQL . " < ".$maxRadius; |
|
71
|
|
|
$sqlQuery->orderby = " distance "; |
|
72
|
|
|
$sqlQuery->limit = "1"; |
|
73
|
|
|
$result = $sqlQuery->execute(); |
|
74
|
|
|
foreach ($result as $row) { |
|
75
|
|
|
$sameOne = false; |
|
76
|
|
|
if ($existingPage) { |
|
77
|
|
|
if ($row["CityID"] == $existingPage->HiddenDataID) { |
|
78
|
|
|
$sameOne = true; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
if (!$sameOne) { |
|
82
|
|
|
$newPage = self::create_city_and_parents($row["CityID"]); |
|
83
|
|
|
$newDistance = $row["distance"]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
if (($newPage) && ($newDistance < $existingDistance) && ($newDistance < $maxRadius)) { |
|
87
|
|
|
$cityPage = $newPage; |
|
88
|
|
|
} elseif ($existingPage && $existingDistance < $maxRadius) { |
|
89
|
|
|
$cityPage = $existingPage; |
|
90
|
|
|
} |
|
91
|
|
|
if ($cityPage) { |
|
92
|
|
|
if ($cityPage->allowBrowseChildren()) { |
|
|
|
|
|
|
93
|
|
|
$suburbPage = BrowseSuburbPage::create_suburb($googleMapAddressArray, $cityPage); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
if ($suburbPage) { |
|
97
|
|
|
return $suburbPage; |
|
98
|
|
|
} |
|
99
|
|
|
return $cityPage; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* formulae for working out distance |
|
104
|
|
|
**/ |
|
105
|
|
|
protected static function radiusDefinitionOtherTable($lon, $lat, $table, $latitudeField, $longitudeField) |
|
106
|
|
|
{ |
|
107
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
|
108
|
|
|
return "(6378.137 * ACOS( ( SIN( PI( ) * ".$lat." /180 ) * SIN( PI( ) * {$bt}".$table."{$bt}.{$bt}".$latitudeField."{$bt} /180 ) ) + ( COS( PI( ) * ".$lat." /180 ) * cos( PI( ) * {$bt}".$table."{$bt}.{$bt}".$latitudeField."{$bt} /180 ) * COS( (PI( ) * {$bt}".$table."{$bt}.{$bt}".$longitudeField."{$bt} /180 ) - ( PI( ) *".$lon." /180 ) ) ) ) ) "; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* name for page level. |
|
113
|
|
|
**/ |
|
114
|
|
|
public function GeoLevelName() |
|
115
|
|
|
{ |
|
116
|
|
|
return "Cities"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* number for page level. |
|
121
|
|
|
**/ |
|
122
|
|
|
public function GeoLevelNumber() |
|
123
|
|
|
{ |
|
124
|
|
|
return 3; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* This static method creates a city page and all the required parent pages... |
|
129
|
|
|
*@param Int - $CityID: the ID for the city to create |
|
130
|
|
|
**/ |
|
131
|
|
|
public static function create_city_and_parents($CityID) |
|
132
|
|
|
{ |
|
133
|
|
|
$cityPage = null; |
|
134
|
|
|
//check if the city exists at all |
|
135
|
|
|
$sql = ' |
|
136
|
|
|
SELECT cities.RegionID, regions.CountryID, countries.ContinentID From cities, regions, countries, continents |
|
137
|
|
|
WHERE |
|
138
|
|
|
cities.RegionID = regions.RegionID AND |
|
139
|
|
|
regions.CountryID = countries.CountryID AND |
|
140
|
|
|
countries.ContinentID = continents.ContinentID AND |
|
141
|
|
|
cities.CityID = '.$CityID.' |
|
142
|
|
|
LIMIT 1;'; |
|
143
|
|
|
$result = DB::query($sql); |
|
144
|
|
|
|
|
145
|
|
|
foreach ($result as $row) { |
|
146
|
|
|
break; |
|
147
|
|
|
} |
|
148
|
|
|
$abstractHelpPage = new BrowseAbstractPage(); |
|
149
|
|
|
if ($row) { |
|
|
|
|
|
|
150
|
|
|
//1 check if world exists |
|
151
|
|
|
if ($worldPage = DataObject::get_one("BrowseWorldPage")) { |
|
|
|
|
|
|
152
|
|
|
//do nothing |
|
153
|
|
|
} else { |
|
154
|
|
|
$worldPage = new BrowseWorldPage(); |
|
155
|
|
|
$name = "Find"; |
|
156
|
|
|
$worldPage->Title = $name; |
|
157
|
|
|
$worldPage->MetaTitle = $name; |
|
158
|
|
|
$worldPage->MenuTitle = $name; |
|
159
|
|
|
$worldPage->writeToStage('Stage'); |
|
160
|
|
|
$worldPage->publish('Stage', 'Live'); |
|
161
|
|
|
$worldPage->flushCache(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
//2 check if continent exists |
|
165
|
|
|
$ContinentID = $row["ContinentID"]; |
|
|
|
|
|
|
166
|
|
View Code Duplication |
if ($continentPage = DataObject::get_one("BrowseContinentsPage", 'HiddenDataID = '.$ContinentID)) { |
|
|
|
|
|
|
167
|
|
|
//debug::show("continent exists"); |
|
|
|
|
|
|
168
|
|
|
} else { |
|
169
|
|
|
//create continent |
|
170
|
|
|
$continents = $abstractHelpPage->getDataFromTable("continents", "ContinentID = ".$ContinentID, null); |
|
171
|
|
|
foreach ($continents as $continentData) { |
|
172
|
|
|
$continentPage = new BrowseContinentsPage(); |
|
173
|
|
|
$continentPage->CreateContinent($continentData, $worldPage); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
//3 check if country exists |
|
178
|
|
|
$CountryID = $row["CountryID"]; |
|
179
|
|
View Code Duplication |
if ($countryPage = DataObject::get_one("BrowseCountriesPage", 'HiddenDataID = '.$CountryID)) { |
|
|
|
|
|
|
180
|
|
|
//debug::show("country exists"); |
|
|
|
|
|
|
181
|
|
|
} else { |
|
182
|
|
|
//create Country |
|
183
|
|
|
$countries = $abstractHelpPage->getDataFromTable("countries", "CountryID = ".$CountryID, null); |
|
184
|
|
|
foreach ($countries as $countryData) { |
|
185
|
|
|
$countryPage = new BrowseCountriesPage(); |
|
186
|
|
|
$countryPage->CreateCountry($countryData, $continentPage); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
//4 check if region exists |
|
191
|
|
|
$RegionID = $row["RegionID"]; |
|
192
|
|
View Code Duplication |
if ($regionPage = DataObject::get_one("BrowseRegionsPage", 'HiddenDataID = '.$RegionID)) { |
|
|
|
|
|
|
193
|
|
|
//debug::show("region exists"); |
|
|
|
|
|
|
194
|
|
|
} else { |
|
195
|
|
|
//create region |
|
196
|
|
|
$regions = $abstractHelpPage->getDataFromTable("regions", "RegionID = ".$RegionID, null); |
|
197
|
|
|
foreach ($regions as $regionData) { |
|
198
|
|
|
$regionPage = new BrowseRegionsPage(); |
|
199
|
|
|
$regionPage->CreateRegion($regionData, $countryPage); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
View Code Duplication |
if ($cityPage = DataObject::get_one("BrowseCitiesPage", 'HiddenDataID = '.$CityID)) { |
|
|
|
|
|
|
203
|
|
|
//debug::show("city exists"); |
|
|
|
|
|
|
204
|
|
|
} else { |
|
205
|
|
|
//create region |
|
206
|
|
|
$cities = $abstractHelpPage->getDataFromTable("cities", "CityID = ".$CityID, null); |
|
207
|
|
|
foreach ($cities as $city) { |
|
208
|
|
|
$cityPage = new BrowseCitiesPage(); |
|
209
|
|
|
$cityPage->CreateCity($city, $regionPage); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
return $cityPage; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* fix URLS |
|
218
|
|
|
* NOTE: you must set get variables: urls, from and to.... |
|
219
|
|
|
**/ |
|
220
|
|
|
public function requireDefaultRecords() |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
parent::requireDefaultRecords(); |
|
223
|
|
|
if (isset($_GET["urls"]) && isset($_GET["from"]) && isset($_GET["to"])) { |
|
224
|
|
|
$dos = DataObject::get("SiteTree", null, null, null, $_GET["from"].','.$_GET["to"]); |
|
225
|
|
|
foreach ($dos as $page) { |
|
226
|
|
|
if (isset($page)) { |
|
227
|
|
|
echo "<li>fixing ".$page->Title."</li>"; |
|
228
|
|
|
$page->URLSegment = $this->generateURLSegment($page->Title); |
|
229
|
|
|
$page->writeToStage('Stage'); |
|
230
|
|
|
$page->publish('Stage', 'Live'); |
|
231
|
|
|
$page->flushCache(); |
|
232
|
|
|
$page->detroy(); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Create a page |
|
240
|
|
|
* @param Array - $city: the data for the city |
|
241
|
|
|
* @param Object $parent: BrowseRegionsPage |
|
|
|
|
|
|
242
|
|
|
**/ |
|
243
|
|
|
public function CreateCity(array $city, BrowseRegionsPage $parent) |
|
|
|
|
|
|
244
|
|
|
{ |
|
245
|
|
|
if ($parent && isset($city["City"])) { |
|
246
|
|
|
$name = htmlentities($city["City"]); |
|
247
|
|
|
if (isset($name)) { |
|
248
|
|
|
if (isset($_GET["geobuild"])) { |
|
249
|
|
|
echo "<li>creating ".$name."</li>"; |
|
250
|
|
|
} |
|
251
|
|
|
$this->ParentID = $parent->ID; |
|
252
|
|
|
$this->Title = $name; |
|
253
|
|
|
$this->MetaTitle = $name; |
|
254
|
|
|
$this->MenuTitle = $name; |
|
255
|
|
|
$this->HiddenDataID = $city["CityID"]; |
|
256
|
|
|
|
|
257
|
|
|
$this->Code = $city["Code"]; |
|
258
|
|
|
$this->Latitude = $city["Latitude"]; |
|
259
|
|
|
$this->Longitude = $city["Longitude"]; |
|
260
|
|
|
$this->TimeZone = $city["TimeZone"]; |
|
261
|
|
|
$this->County = htmlentities($city["County"]); |
|
262
|
|
|
$this->Code = $city["Code"]; |
|
263
|
|
|
|
|
264
|
|
|
$this->CreateChildren = $parent->CreateAllChildren; |
|
265
|
|
|
$this->CreateAllChildren = $parent->CreateAllChildren; |
|
266
|
|
|
|
|
267
|
|
|
$this->URLSegment = $this->generateURLSegment($this->Title); |
|
268
|
|
|
|
|
269
|
|
|
$this->writeToStage('Stage'); |
|
270
|
|
|
$this->publish('Stage', 'Live'); |
|
271
|
|
|
$this->flushCache(); |
|
272
|
|
|
} else { |
|
273
|
|
|
if (isset($_GET["geobuild"])) { |
|
274
|
|
|
debug::show("No name can be found"); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
} else { |
|
278
|
|
|
if (isset($_GET["geobuild"])) { |
|
279
|
|
|
debug::show("Parent does not exist"); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
class BrowseCitiesPage_Controller extends BrowseAbstractPage_Controller |
|
|
|
|
|
|
286
|
|
|
{ |
|
287
|
|
|
} |
|
288
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.