-
Notifications
You must be signed in to change notification settings - Fork 0
/
btm_geonames.module
388 lines (308 loc) · 10 KB
/
btm_geonames.module
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
define('BTM_GEONAMES_PATH_TO_MODULE', drupal_get_path('module', 'btm_geonames'));
require_once(BTM_GEONAMES_PATH_TO_MODULE . '/btm_geonames.fapi.inc');
function btm_geonames_example_form($form_state){
btm_geonames_add_js();
drupal_add_js("Drupal.behaviors.bindStateProvince = function(context){
BTM.GeoNames.bindCountryProvinceSelectSet('body','#edit-country-province-country','#edit-country-province-state-province');
}", "inline");
$form = array();
$form['country_province'] = array(
'#type' => 'country_province_select',
'#title' => 'Country/Province Select',
'#default_value' => array()
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit'
);
return $form;
}
function btm_geonames_example_form_submit($form, &$form_state){
print '<pre>'; var_dump($form_state['values']);exit;
}
/**
* Include the necessary JS to activate the elements
*/
function btm_geonames_add_js(){
drupal_add_js(BTM_GEONAMES_PATH_TO_MODULE . '/BTM.GeoNames.js');
}
/**
* Menu callback to build the confirm import form.
*
* @return string
*/
function btm_geonames_import_places() {
return drupal_get_form('btm_geonames_import_places_confirm');
}
/**
* Make sure they really want to import
*/
function btm_geonames_import_places_confirm() {
$form = array();
$form['intro_text'] = array(
'#value' => "<p>Click the 'Import Place Data' button below will remove all current country,
state and province info and replace it with fresh data from the GeoNames.org web service. It will probably
take a few minutes, so please be patient.</p>"
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Import Place Data',
);
return $form;
}
/* * *
* May need validation later
*/
function btm_geonames_import_places_confirm_validate() {
}
/* * *
* Imports countries and provinces into the drupal db
*
* @todo investigate Duplicate primary key entry warnings in provinces table
*/
function btm_geonames_import_places_confirm_submit() {
//include the Service class
require_once(BTM_GEONAMES_PATH_TO_MODULE . '/Service_GeoNames.class.php');
//New geo names service
$Service_GeoNames = new Service_GeoNames();
//get the countries
$response_countries = $Service_GeoNames->getCountriesData();
//if the service is working, we can safely truncate the tables
if (count($response_countries->country) > 0) {
db_query("TRUNCATE TABLE {btm_countries}");
db_query("TRUNCATE TABLE {btm_states_provinces}");
} else {
drupal_set_message("Geonames service is not functioning, try again later", "warning");
return false;
}
//Loop through the countries. Insert country enetries
foreach ($response_countries->country as $country) {
$sql = "INSERT INTO {btm_countries}
SET
country_code = \"%s\",
country_id = %d,
geoname_id = %d,
country_name = \"%s\"";
$result = db_query($sql, $country->countryCode, $country->isoNumeric, $country->geonameId, $country->countryName);
drupal_set_message("Added country <strong>{$country->countryName}</strong> to the database.", 'status');
//Get top level administrative areas for each country, store them
$response_provinces = $Service_GeoNames->getStatesProvinces($country->geonameId);
if ($response_provinces->totalResultsCount < 1) {
drupal_set_message(
"Could not retrieve any states/provinces for country: {$country->countryName}.
There was either an error retrieving them or no data is available.",
"warning"
);
continue;
}
//store each province
foreach ($response_provinces->geoname as $province) {
$sql = "INSERT INTO {btm_states_provinces}
SET
state_province_id = %d,
parent_geoname_id = %d,
state_province_name = \"%s\"
";
$result = db_query($sql, $province->geonameId, $country->geonameId, $province->name);
drupal_set_message(
"Added state/province/region <em>{$province->name}</em> of <strong>{$country->countryName}</strong> to the database.",
"status"
);
}
}
}
/**
* Return an array of countries suitable for FAPI select boxes
*
* @param string $default
* @return array
*/
function btm_geonames_get_countries($default = "US") {
$countries = array();
$result = db_query("SELECT * FROM {btm_countries} ORDER BY country_name");
$countries = array('0' => '----');
while ($country = db_fetch_object($result)) {
$countries[$country->geoname_id] = $country->country_name;
}
return $countries;
}
/**
* Return an array of provinces suitable for FAPI select boxes
*
*
* @param string $parent_geoname_id
* The geoname_id of the country
*
* @param string $default
* @return arrat
*/
function btm_geonames_get_states_provinces($parent_geoname_id, $default = "", $is_for_js = FALSE) {
$provinces = array();
$url_is_for_js = arg(4);
if($url_is_for_js == 'TRUE') {
$is_for_js = TRUE;
}
//var_dump($parent_geoname_id, $default, $is_for_js);
// var_dump($dev_parent, $dev_default,$dev_is_for_js);die;
if ($parent_geoname_id < 1) {
throw new Exception("No country geoname id given.");
}
$result = db_query("SELECT * FROM {btm_states_provinces}
WHERE parent_geoname_id = %d
ORDER BY state_province_name ASC", $parent_geoname_id);
$provinces = array('----' => '0');
while ($province = db_fetch_object($result)) {
$provinces[$province->state_province_name] = $province->state_province_id;
}
if(!$is_for_js) {
$provinces = array_flip($provinces);
}
return $provinces;
}
/**
* Get a country by the geoname id
*
* @param string country_id
* @return string
*/
function btm_geonames_get_country($country_id) {
if ($country_id < 1) {
//throw new Exception("Invalid country id");
return false;
}
$sql = "SELECT country_name FROM {btm_countries} WHERE geoname_id = %d";
$result = db_query($sql, $country_id);
$country = db_fetch_object($result);
return $country->country_name;
}
/**
* Get an ID by Name. There's a few synonyms for 'United States' attached.
*
* @param string $country_name
* @return int
*/
function btm_geonames_get_country_id($country_name) {
$united_states_synonyms = array('usa', 'us', 'united states of america', 'u.s.', 'u.s.a.');
$country_name = trim(strtolower($country_name));
if(in_array($country_name, $united_states_synonyms)) {
$country_name = 'United States';
}
$sql = "SELECT * FROM {btm_countries} WHERE `country_name` LIKE \"%s\"";
$result = db_query($sql, $country_name);
$country = db_fetch_object($result);
return $country->geoname_id;
}
/**
* Get a state/province name by geoname id
*
* @param string $province_id
* @return string
*/
function btm_geonames_get_province($province_id) {
if ($province_id < 1) {
//throw new Exception("Invalid province ID given.");
return false;
}
$sql = "SELECT state_province_name FROM {btm_states_provinces} WHERE state_province_id = %d";
$result = db_query($sql, $province_id);
$province = db_fetch_object($result);
return $province->state_province_name;
}
/**
* Lookup an ID by Name
*
* @param string $province_name
* @return int
*/
function btm_geonames_get_province_id($province_name) {
$province_name = trim($province_name);
$sql = "SELECT state_province_id FROM {btm_states_provinces} WHERE state_province_name LIKE \"%s\"";
$result = db_query($sql, $province_name);
$province = db_fetch_object($result);
return $province->state_province_id;
}
/**
* Ajax callback to get provinces for a country. the $country_id is after the final slash
*
* @param int $country_id
* @return null
*/
function btm_geonames_ajax_get_states_provinces($country_id){
if(!$country_id) {
return array(
'success' => FALSE,
'states_provinces'=>array(),
);
}
$provinces = btm_geonames_get_states_provinces($country_id);
$response = array(
'success' => TRUE,
'states_provinces'=> $provinces,
);
drupal_json($response);
exit;
}
/**
* Tell drupal about the theme function for our select boxes
*
* @return array
*/
function btm_geonames_theme(){
return array(
'country_province_select' => array(
'arguments' => array('element' => NULL)
)
);
}
/**
* We have one menu callback for importing data and one for the ajax callback
*
* @return array
*/
function btm_geonames_menu() {
$items = array();
$items['admin/settings/import-geo-data'] = array(
'title' => 'Import Geographic Data',
'description' => 'Import Geographic Data',
'page callback' => 'btm_geonames_import_places',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['btm_geonames/get_states_provinces'] = array(
'title' => 'AJAX callback to get states/provinces',
'description' => 'ditto',
'page callback' => 'btm_geonames_ajax_get_states_provinces',
'access arguments' => array('access content'),
'page arguments' => array(2, 3, 4),
'type' => MENU_CALLBACK,
);
$items['btm_geonames/example'] = array(
'title' => 'Example of using the FAPI field',
'description' => 'ditto',
'page callback' => 'drupal_get_form',
'page arguments'=> array('btm_geonames_example_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_help
*
* @param string $path
* @param string $arg
* @return string
*/
function btm_geonames_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#btm_geonames":
$output = '<p>' . t("Country & Province data import from GeoNames webservice and a custom FAPI element to handle it, with necessary javascript.") . '</p>';
break;
}
return $output;
}
function btm_geonames_views_api(){
return array('api' => 2, 'path' => BTM_GEONAMES_PATH_TO_MODULE . '/views');
}