forked from stevem/Connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect_lookup.php-original
452 lines (404 loc) · 14 KB
/
connect_lookup.php-original
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* interface function for returning lookup description or data
*/
function connect_target_lookup($op = 'lookup', $parent = NULL, $child = NULL, $type = '') {
$return = false;
if ( $parent && $type ) {
$lookup = connect_node_options( $parent->nid, $type.'_lookup_type' );
if ($lookup) {
eval('$return = '.$lookup.'($op, $parent, $child);');
}
}
return $return;
}
/*
* connect_lookup_NAME( $op = 'lookup', $child )
*
* @ param $op
* 'lookup' -> perform the lookup
* 'requires' -> get info to pass through via connect_action_hook function
* 'describe' -> returns a title to display on the node settings form
* 'cache' -> returns an array of descriptors for the cache table
*
* @ return
* if $op=='lookup': object( object->email object->name object->fax, etc )
* if $op=='requires' : array as per connect_action_* 'requires' op
* if $op=='describe' : string describing the function
* of $op=='cache' : array describing the connet_cache items this lookup can/may use
*/
/*
* function to look up contact info for Canadian MP -- basic lookup from Postal Code
*
* uses the OpenConcept Consulting web service at http://makethechange.ca/
* which requires a client key
*
*/
function connect_lookup_mp($op = 'lookup', &$parent, &$child) {
switch ($op) {
case 'describe' :
return 'Canadian MP (postal code)';
break;
case 'cache' :
return array(
'EDID2MP' => 'Federal riding to MP',
'postalcode2riding' => 'Postal code to Federal riding'
);
break;
case 'requires':
$return['variables'] = array(
'makethechange_key' => array(
'#type' => 'textfield',
'#title' => 'Makethechange web service key',
'#default_value' => connect_node_options($parent->nid, 'makethechange_key'),
'#required' => TRUE,
),
'mp_lookup_cache' => array(
'#type' => 'radios',
'#title' => 'Cache the lookup data?',
'#options' => array( 'no' => 'No', 'yes' => 'Yes'),
'#default_value' => connect_node_options($parent->nid, 'mp_lookup_cache'),
'#required' => TRUE,
),
);
$return['child'] = array(
'postal_code' => 'Postal code',
'lookup_mp_data_edid' => 'Riding',
);
return $return;
case 'validate' :
$code = connect_value( 'postal_code', $parent, $child, 'child' );
if ( !connect_is_postalcode($code) ) {
form_set_error('', 'Please enter a valid postal code.');
}
break;
case 'lookup':
$return = array();
$use_cache = (connect_node_options($parent->nid, 'mp_lookup_cache') == 'yes');
$mtc_key = connect_node_options($parent->nid, 'makethechange_key');
$postalcode = connect_value('postal_code', $parent, $child, 'child');
$edid = _connect_get_riding($mtc_key, $postalcode, 'federal', 'federal', $use_cache);
if ($edid) {
// save the riding in the child node
connect_value('lookup_mp_data_edid', $parent, $child, 'child', $edid[0]['id']);
/* Danger! in rare cases there may be > 1 matches: this
* just grabs the first one; if this is a problem,
* use the CCK MPautocomplete version to allow users to select a riding
* or write a two-stage version of the participation form
*/
$mp = _connect_get_MP_by_EDID($mtc_key, $edid[0]['id'], $use_cache);
if (!empty($mp)){
$return['name'] = $mp->mp_name;
$return['email'] = $mp->email;
$return['fax'] = $mp->fax;
}
else {
watchdog('connect', "connect_lookup_mp: MP not found for $edid[0][id]");
}
}
else {
watchdog('connect', "connect_lookup_mp: EDID not found for $postalcode");
}
return $return;
break;
}
}
/*
* function to look up contact info for Canadian MP -- Autocomplete version
* requires MP autocomplete and riding selection fields
*
* uses the OpenConcept Consulting web service at http://makethechange.ca/
* which requires a client key
*
*/
function connect_lookup_mp_autocomplete($op='lookup', &$parent, &$child) {
switch ($op) {
case 'describe' :
return 'Canadian MP (autocomplete + riding list)';
break;
case 'requires':
$return['variables'] = array(
'makethechange_key' => array(
'#type' => 'textfield',
'#title' => 'Makethechange web service key',
'#default_value' => connect_node_options( $parent->nid, 'makethechange_key' ),
'#required' => TRUE,
),
'mp_autocomplete_lookup_cache' => array(
'#type' => 'radios',
'#title' => 'Cache the lookup data?',
'#options' => array( 'no' => 'No', 'yes' => 'Yes'),
'#default_value' => connect_node_options( $parent->nid, 'mp_autocomplete_lookup_cache'),
'#required' => TRUE,
),
);
$return['child'] = array(
'riding_auto' => 'MP autocomplete field',
'riding_list' => 'Riding list (to be selected if autocomplete fails)',
);
return $return;
case 'cache' :
return array(
'EDID2MP' => 'Federal riding to MP',
'postalcode2riding' => 'Postal code to Federal riding'
);
break;
case 'validate' :
$map = connect_get_map($parent->nid);
$riding_auto = $child[$map['riding_auto']][0]['value'];
$riding_list = $child[$map['riding_list']]['key'];
if ( empty($riding_auto) && empty($riding_list) ) {
form_set_error('', 'Please enter your postal code or select your riding from the list.');
}
break;
case 'lookup':
$edid = 0;
$use_cache = (connect_node_options($parent->nid, 'mp_autocomplete_lookup_cache') == 'yes');
$riding_auto = connect_value( 'riding_auto', $parent, $child, 'child' );
$riding_list = connect_value( 'riding_list', $parent, $child, 'child' );
if ( !empty($riding_list) ) {
$edid = (int) $riding_list;
}
if ( $edid == 0 && !empty($riding_auto) ) {
list($id,$discard) = sscanf($riding_auto, "%d | %s");
$edid = (int) $id;
}
// do the lookup
if ( is_numeric($edid) ) {
$mtc_key = connect_node_options( $parent->nid, 'makethechange_key' );
$mp = _connect_get_MP_by_EDID( $mtc_key, $edid, $use_cache);
if ($mp ){
$return['name'] = $mp->mp_name;
$return['email'] = $mp->email;
$return['fax'] = $mp->fax;
return $return;
}
} else {
die('problem!');
}
return null;
}
}
/*
* function to look up contact info for Ontario MPP
*
* uses the OpenConcept Consulting web service at http://makethechange.ca/
* which requires a client key
*
*/
/*
function connect_lookup_on_mpp(&$participant) {
// validate postal code
$code = $participant->field_postal_code[0][value];
if (! connect_is_postalcode($code)) {
form_set_error('', t('There was a problem with the form data.'));
return false;
}
// lookup MPP info
$service_key = connect_get_service_key();
// cacheable
$cached_address = cache_get('on_mpp_' . $code, 'cache_connect');
if ($cached_address) {
return $cached_address;
}
$get_url = "http://makethechange.ca/provincial/on_riding.php?key=".urlencode($service_key)."&pc=".urlencode($code) . "&type=csv";
$fh = fopen($get_url, "r") or watchdog('debug','fopen failed: ' . $get_url );
$mpp_address = fread($fh, 200) or watchdog('debug','fread failed: ' . $get_url );
fclose($fh);
if (strpos($mpp_address, 'Error:')) {
$mpp_address = false;
}
cache_set('on_mpp_' . $code, 'cache_connect', $mpp_address, CACHE_PERMANENT);
return $mpp_address;
}
*/
/***** Makethechange functions *****/
// MP lookup using EDID
function _connect_get_MP_by_EDID($service_key = NULL, $edid = NULL, $use_cache = FALSE) {
if (is_numeric($edid)) {
// cached value?
if ($use_cache) {
$cached = _connect_cached_value('EDID2MP', $edid);
if (!empty($cached)) {
drupal_set_message( '_connect_get_MP_by_EDID cache hit' );
return $cached;
}
}
// do lookup
if ($service_key) {
_connect_json_support(); // make sure we have JSON support
$mp_url = 'http://makethechange.ca/federal/riding.php?type=json&key='.urlencode($service_key).'&edid='.$edid;
$fh = fopen($mp_url, "r") or watchdog('debug','fopen failed: ' . $mp_url );
$mp_info = fread($fh, 8192) or watchdog('debug','fread failed: ' . $mp_url );
fclose($fh);
if (!empty($mp_info)) {
if (!strpos($mp_info, 'Error:')) {
$return = json_decode(trim($mp_info));
if ($use_cache) {
$cached = _connect_cached_value('EDID2MP', $edid, $return);
drupal_set_message( '_connect_get_MP_by_EDID cache set' );
}
return $return;
}
else {
watchdog('connect', "get_MP_by_EDID $mp_info");
}
}
else {
watchdog('connect', "get_MP_by_EDID lookup returned empty MP data for $edid");
}
}
}
// parameter and lookup errors fall through here
watchdog('connect', "get_MP_by_EDID missing EDID or service key");
return NULL;
}
/*
* generic riding lookup
*
* @parameters
* postalcode (string)
* scope (string) : 'federal'|'provincial'
* detail (string) : 'ontario', 'ottawa', etc.
*
* @return
* (array) of riding info = array( 'id'=>'', 'en'=>'', 'fr'=>'' )
*
* TODO - fix the web service API.
*
*/
function _connect_get_riding($service_key = NULL, $postalcode = NULL, $scope='federal', $detail = 'federal', $use_cache = FALSE) {
// verify and normalize postalcode format
if (!connect_is_postalcode($postalcode)) {
return;
}
$postalcode = strtoupper(preg_replace('/\s/', '', $postalcode));
$return = FALSE;
// cached value?
if ($use_cache) {
$cached = _connect_cached_value('postalcode2riding', $postalcode);
if(!empty($cached)) {
drupal_set_message( '_connect_get_riding cache hit' );
return $cached;
}
}
// look up data, if not found in cache
// determine lookup URI based on type of lookup
$scope_function = array (
'federal' => array ( 'federal' => 'pc2csv/index' ),
'provincial' => array ( 'ontario' => 'provincial/on_riding' ),
'municipal' => array (),
);
if ( $service_key && isset($scope_function[$scope][$detail]) ) {
$riding_url = 'http://makethechange.ca/' .$scope_function[$scope][$detail]. '.php?key='.urlencode($service_key).'&pc='.urlencode($postalcode).'&type=riding';
$fh = fopen($riding_url, "r") or watchdog('debug','fopen failed: ' . $riding_url );
$riding_data = fread($fh, 200) or watchdog('debug','fread failed: ' . $riding_url );
fclose($fh);
}
// if there are results, walk through them and create an array of arrays
if (!empty($riding_data)) {
$row_token = strtok($riding_data, "\n");
while ($row_token != false) {
$return[] = connect_riding_data($row_token);
$row_token = strtok("\n");
}
// cache, if desired
if ($use_cache) {
$cached = _connect_cached_value('postalcode2riding', $postalcode, $return);
drupal_set_message( '_connect_get_riding cache set' );
}
}
return $return;
}
// turns riding data returned from web service into an indexed array
function connect_riding_data($data = NULL) {
$return = null;
$array_keys = array('id','en','fr');
$array_temp = explode(',',$data);
for ($i = 0; $i < count($array_temp); $i++) {
$return[$array_keys[$i]] = trim( $array_temp[$i] );
}
return $return;
}
/**** Utility functions ****/
/*
* Determine available target lookup types, return in select list format
*/
function connect_get_lookup_types($select = FALSE) {
static $return = array();
$empty = array();
if (empty($return) ) {
$functions = get_defined_functions();
$array = $functions['user'];
foreach ($array as $key=>$name ) {
if (strpos($name, 'connect_lookup_') !== FALSE ) {
eval( '$label = '.$name.'(\'describe\', $empty, $empty);' );
$return[$name] = $label;
}
}
$return[0] = '';
asort($return);
}
return $return;
}
/*
* Wrappers for JSON support if this version/install of PHP
* doesn't support it natively
*
* Thanks to http://abeautifulsite.net/notebook/71
* JSON library from http://mike.teczno.com/json.html
*/
function _connect_json_support() {
if (!function_exists('json_encode')) {
require_once('JSON.php');
function json_encode($data) {
$json = new Services_JSON();
return($json->encode($data));
}
}
if (!function_exists('json_decode')) {
require_once('JSON.php');
function json_decode($data) {
$json = new Services_JSON();
return($json->decode($data));
}
}
}
// check the connect cache for a saved value for a lookup
// if no third param = getter; third value = setter
function _connect_cached_value($type = NULL, $source = NULL, $value = NULL) {
$return = FALSE;
if ($type && $source) {
if (!$value) {
$sql = "SELECT target from {connect_cache} WHERE type='%s' AND source = '%s';";
$result = db_query($sql, $type, $source);
if ($row = db_fetch_object($result)) {
$return = unserialize($row->target);
}
}
else {
// delete
$sql = "DELETE from {connect_cache} WHERE type='%s' AND source = '%s';";
$result = db_query($sql, $type, $source);
//insert
$sql = "INSERT INTO {connect_cache} (type, source, target, created) VALUES('%s', '%s', '%s', %d);";
$result = db_query($sql, $type, $source, serialize($value), time());
$return = $result;
}
}
return $return;
}
// return names for cached items
function _connect_get_cache_names() {
$cache_names = array();
$lookup_types = connect_get_lookup_types(TRUE);
unset($lookup_types[0]);
$empty = array();
foreach ($lookup_types as $key=>$title) {
if (function_exists($key)) {
eval('$temp = ' . $key . '(\'cache\', $empty, $empty);');
$cache_names = array_merge($cache_names, $temp);
}
}
return array_unique($cache_names);
}