| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* interface function for returning lookup description or data
|
| 5 |
*/
|
| 6 |
function connect_target_lookup($op = 'lookup', $parent = NULL, $child = NULL, $type = '') {
|
| 7 |
$return = false;
|
| 8 |
if ( $parent && $type ) {
|
| 9 |
$lookup = connect_node_options( $parent->nid, $type.'_lookup_type' );
|
| 10 |
if ($lookup) {
|
| 11 |
eval('$return = '.$lookup.'($op, $parent, $child);');
|
| 12 |
}
|
| 13 |
}
|
| 14 |
return $return;
|
| 15 |
}
|
| 16 |
|
| 17 |
|
| 18 |
/*
|
| 19 |
* connect_lookup_NAME( $op = 'lookup', $child )
|
| 20 |
*
|
| 21 |
* @ param $op
|
| 22 |
* 'lookup' -> perform the lookup
|
| 23 |
* 'requires' -> get info to pass through via connect_action_hook function
|
| 24 |
* 'describe' -> returns a title to display on the node settings form
|
| 25 |
* 'cache' -> returns an array of descriptors for the cache table
|
| 26 |
*
|
| 27 |
* @ return
|
| 28 |
* if $op=='lookup': object( object->email object->name object->fax, etc )
|
| 29 |
* if $op=='requires' : array as per connect_action_* 'requires' op
|
| 30 |
* if $op=='describe' : string describing the function
|
| 31 |
* of $op=='cache' : array describing the connet_cache items this lookup can/may use
|
| 32 |
*/
|
| 33 |
|
| 34 |
|
| 35 |
/*
|
| 36 |
* function to look up contact info for Canadian MP -- basic lookup from Postal Code
|
| 37 |
*
|
| 38 |
* uses the OpenConcept Consulting web service at http://makethechange.ca/
|
| 39 |
* which requires a client key
|
| 40 |
*
|
| 41 |
*/
|
| 42 |
function connect_lookup_mp($op = 'lookup', &$parent, &$child) {
|
| 43 |
switch ($op) {
|
| 44 |
case 'describe' :
|
| 45 |
return 'Canadian MP (postal code)';
|
| 46 |
break;
|
| 47 |
|
| 48 |
case 'cache' :
|
| 49 |
return array(
|
| 50 |
'EDID2MP' => 'Federal riding to MP',
|
| 51 |
'postalcode2riding' => 'Postal code to Federal riding'
|
| 52 |
);
|
| 53 |
break;
|
| 54 |
|
| 55 |
case 'requires':
|
| 56 |
$return['variables'] = array(
|
| 57 |
'makethechange_key' => array(
|
| 58 |
'#type' => 'textfield',
|
| 59 |
'#title' => 'Makethechange web service key',
|
| 60 |
'#default_value' => connect_node_options($parent->nid, 'makethechange_key'),
|
| 61 |
'#required' => TRUE,
|
| 62 |
),
|
| 63 |
'mp_lookup_cache' => array(
|
| 64 |
'#type' => 'radios',
|
| 65 |
'#title' => 'Cache the lookup data?',
|
| 66 |
'#options' => array( 'no' => 'No', 'yes' => 'Yes'),
|
| 67 |
'#default_value' => connect_node_options($parent->nid, 'mp_lookup_cache'),
|
| 68 |
'#required' => TRUE,
|
| 69 |
),
|
| 70 |
);
|
| 71 |
|
| 72 |
$return['child'] = array(
|
| 73 |
'postal_code' => 'Postal code',
|
| 74 |
'lookup_mp_data_edid' => 'Riding',
|
| 75 |
);
|
| 76 |
return $return;
|
| 77 |
|
| 78 |
case 'validate' :
|
| 79 |
$code = connect_value( 'postal_code', $parent, $child, 'child' );
|
| 80 |
if ( !connect_is_postalcode($code) ) {
|
| 81 |
form_set_error('', 'Please enter a valid postal code.');
|
| 82 |
}
|
| 83 |
break;
|
| 84 |
|
| 85 |
case 'lookup':
|
| 86 |
$return = array();
|
| 87 |
$use_cache = (connect_node_options($parent->nid, 'mp_lookup_cache') == 'yes');
|
| 88 |
$mtc_key = connect_node_options($parent->nid, 'makethechange_key');
|
| 89 |
$postalcode = connect_value('postal_code', $parent, $child, 'child');
|
| 90 |
$edid = _connect_get_riding($mtc_key, $postalcode, 'federal', 'federal', $use_cache);
|
| 91 |
if ($edid) {
|
| 92 |
// save the riding in the child node
|
| 93 |
connect_value('lookup_mp_data_edid', $parent, $child, 'child', $edid[0]['id']);
|
| 94 |
|
| 95 |
/* Danger! in rare cases there may be > 1 matches: this
|
| 96 |
* just grabs the first one; if this is a problem,
|
| 97 |
* use the CCK MPautocomplete version to allow users to select a riding
|
| 98 |
* or write a two-stage version of the participation form
|
| 99 |
*/
|
| 100 |
$mp = _connect_get_MP_by_EDID($mtc_key, $edid[0]['id'], $use_cache);
|
| 101 |
if (!empty($mp)){
|
| 102 |
$return['name'] = $mp->mp_name;
|
| 103 |
$return['email'] = $mp->email;
|
| 104 |
$return['fax'] = $mp->fax;
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
watchdog('connect', "connect_lookup_mp: MP not found for $edid[0][id]");
|
| 108 |
}
|
| 109 |
}
|
| 110 |
else {
|
| 111 |
watchdog('connect', "connect_lookup_mp: EDID not found for $postalcode");
|
| 112 |
}
|
| 113 |
|
| 114 |
return $return;
|
| 115 |
break;
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
/*
|
| 120 |
* function to look up contact info for Canadian MP -- Autocomplete version
|
| 121 |
* requires MP autocomplete and riding selection fields
|
| 122 |
*
|
| 123 |
* uses the OpenConcept Consulting web service at http://makethechange.ca/
|
| 124 |
* which requires a client key
|
| 125 |
*
|
| 126 |
*/
|
| 127 |
function connect_lookup_mp_autocomplete($op='lookup', &$parent, &$child) {
|
| 128 |
switch ($op) {
|
| 129 |
case 'describe' :
|
| 130 |
return 'Canadian MP (autocomplete + riding list)';
|
| 131 |
break;
|
| 132 |
|
| 133 |
case 'requires':
|
| 134 |
$return['variables'] = array(
|
| 135 |
'makethechange_key' => array(
|
| 136 |
'#type' => 'textfield',
|
| 137 |
'#title' => 'Makethechange web service key',
|
| 138 |
'#default_value' => connect_node_options( $parent->nid, 'makethechange_key' ),
|
| 139 |
'#required' => TRUE,
|
| 140 |
),
|
| 141 |
'mp_autocomplete_lookup_cache' => array(
|
| 142 |
'#type' => 'radios',
|
| 143 |
'#title' => 'Cache the lookup data?',
|
| 144 |
'#options' => array( 'no' => 'No', 'yes' => 'Yes'),
|
| 145 |
'#default_value' => connect_node_options( $parent->nid, 'mp_autocomplete_lookup_cache'),
|
| 146 |
'#required' => TRUE,
|
| 147 |
),
|
| 148 |
);
|
| 149 |
$return['child'] = array(
|
| 150 |
'riding_auto' => 'MP autocomplete field',
|
| 151 |
'riding_list' => 'Riding list (to be selected if autocomplete fails)',
|
| 152 |
);
|
| 153 |
return $return;
|
| 154 |
|
| 155 |
case 'cache' :
|
| 156 |
return array(
|
| 157 |
'EDID2MP' => 'Federal riding to MP',
|
| 158 |
'postalcode2riding' => 'Postal code to Federal riding'
|
| 159 |
);
|
| 160 |
break;
|
| 161 |
|
| 162 |
case 'validate' :
|
| 163 |
$map = connect_get_map($parent->nid);
|
| 164 |
$riding_auto = $child[$map['riding_auto']][0]['value'];
|
| 165 |
$riding_list = $child[$map['riding_list']]['key'];
|
| 166 |
if ( empty($riding_auto) && empty($riding_list) ) {
|
| 167 |
form_set_error('', 'Please enter your postal code or select your riding from the list.');
|
| 168 |
}
|
| 169 |
break;
|
| 170 |
|
| 171 |
case 'lookup':
|
| 172 |
$edid = 0;
|
| 173 |
$use_cache = (connect_node_options($parent->nid, 'mp_autocomplete_lookup_cache') == 'yes');
|
| 174 |
$riding_auto = connect_value( 'riding_auto', $parent, $child, 'child' );
|
| 175 |
$riding_list = connect_value( 'riding_list', $parent, $child, 'child' );
|
| 176 |
if ( !empty($riding_list) ) {
|
| 177 |
$edid = (int) $riding_list;
|
| 178 |
}
|
| 179 |
if ( $edid == 0 && !empty($riding_auto) ) {
|
| 180 |
list($id,$discard) = sscanf($riding_auto, "%d | %s");
|
| 181 |
$edid = (int) $id;
|
| 182 |
}
|
| 183 |
|
| 184 |
// do the lookup
|
| 185 |
if ( is_numeric($edid) ) {
|
| 186 |
$mtc_key = connect_node_options( $parent->nid, 'makethechange_key' );
|
| 187 |
$mp = _connect_get_MP_by_EDID( $mtc_key, $edid, $use_cache);
|
| 188 |
if ($mp ){
|
| 189 |
$return['name'] = $mp->mp_name;
|
| 190 |
$return['email'] = $mp->email;
|
| 191 |
$return['fax'] = $mp->fax;
|
| 192 |
return $return;
|
| 193 |
}
|
| 194 |
} else {
|
| 195 |
die('problem!');
|
| 196 |
}
|
| 197 |
return null;
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
/*
|
| 202 |
* function to look up contact info for Ontario MPP
|
| 203 |
*
|
| 204 |
* uses the OpenConcept Consulting web service at http://makethechange.ca/
|
| 205 |
* which requires a client key
|
| 206 |
*
|
| 207 |
*/
|
| 208 |
/*
|
| 209 |
function connect_lookup_on_mpp(&$participant) {
|
| 210 |
// validate postal code
|
| 211 |
$code = $participant->field_postal_code[0][value];
|
| 212 |
if (! connect_is_postalcode($code)) {
|
| 213 |
form_set_error('', t('There was a problem with the form data.'));
|
| 214 |
return false;
|
| 215 |
}
|
| 216 |
|
| 217 |
// lookup MPP info
|
| 218 |
$service_key = connect_get_service_key();
|
| 219 |
|
| 220 |
// cacheable
|
| 221 |
$cached_address = cache_get('on_mpp_' . $code, 'cache_connect');
|
| 222 |
if ($cached_address) {
|
| 223 |
return $cached_address;
|
| 224 |
}
|
| 225 |
|
| 226 |
$get_url = "http://makethechange.ca/provincial/on_riding.php?key=".urlencode($service_key)."&pc=".urlencode($code) . "&type=csv";
|
| 227 |
|
| 228 |
$fh = fopen($get_url, "r") or watchdog('debug','fopen failed: ' . $get_url );
|
| 229 |
$mpp_address = fread($fh, 200) or watchdog('debug','fread failed: ' . $get_url );
|
| 230 |
fclose($fh);
|
| 231 |
|
| 232 |
if (strpos($mpp_address, 'Error:')) {
|
| 233 |
$mpp_address = false;
|
| 234 |
}
|
| 235 |
cache_set('on_mpp_' . $code, 'cache_connect', $mpp_address, CACHE_PERMANENT);
|
| 236 |
return $mpp_address;
|
| 237 |
}
|
| 238 |
*/
|
| 239 |
|
| 240 |
|
| 241 |
/***** Makethechange functions *****/
|
| 242 |
|
| 243 |
// MP lookup using EDID
|
| 244 |
function _connect_get_MP_by_EDID($service_key = NULL, $edid = NULL, $use_cache = FALSE) {
|
| 245 |
if (is_numeric($edid)) {
|
| 246 |
// cached value?
|
| 247 |
if ($use_cache) {
|
| 248 |
$cached = _connect_cached_value('EDID2MP', $edid);
|
| 249 |
if (!empty($cached)) {
|
| 250 |
drupal_set_message( '_connect_get_MP_by_EDID cache hit' );
|
| 251 |
return $cached;
|
| 252 |
}
|
| 253 |
}
|
| 254 |
|
| 255 |
// do lookup
|
| 256 |
if ($service_key) {
|
| 257 |
_connect_json_support(); // make sure we have JSON support
|
| 258 |
$mp_url = 'http://makethechange.ca/federal/riding.php?type=json&key='.urlencode($service_key).'&edid='.$edid;
|
| 259 |
$fh = fopen($mp_url, "r") or watchdog('debug','fopen failed: ' . $mp_url );
|
| 260 |
$mp_info = fread($fh, 8192) or watchdog('debug','fread failed: ' . $mp_url );
|
| 261 |
fclose($fh);
|
| 262 |
if (!empty($mp_info)) {
|
| 263 |
if (!strpos($mp_info, 'Error:')) {
|
| 264 |
$return = json_decode(trim($mp_info));
|
| 265 |
if ($use_cache) {
|
| 266 |
$cached = _connect_cached_value('EDID2MP', $edid, $return);
|
| 267 |
drupal_set_message( '_connect_get_MP_by_EDID cache set' );
|
| 268 |
}
|
| 269 |
return $return;
|
| 270 |
}
|
| 271 |
else {
|
| 272 |
watchdog('connect', "get_MP_by_EDID $mp_info");
|
| 273 |
}
|
| 274 |
}
|
| 275 |
else {
|
| 276 |
watchdog('connect', "get_MP_by_EDID lookup returned empty MP data for $edid");
|
| 277 |
}
|
| 278 |
}
|
| 279 |
}
|
| 280 |
|
| 281 |
// parameter and lookup errors fall through here
|
| 282 |
watchdog('connect', "get_MP_by_EDID missing EDID or service key");
|
| 283 |
return NULL;
|
| 284 |
}
|
| 285 |
|
| 286 |
|
| 287 |
/*
|
| 288 |
* generic riding lookup
|
| 289 |
*
|
| 290 |
* @parameters
|
| 291 |
* postalcode (string)
|
| 292 |
* scope (string) : 'federal'|'provincial'
|
| 293 |
* detail (string) : 'ontario', 'ottawa', etc.
|
| 294 |
*
|
| 295 |
* @return
|
| 296 |
* (array) of riding info = array( 'id'=>'', 'en'=>'', 'fr'=>'' )
|
| 297 |
*
|
| 298 |
* TODO - fix the web service API.
|
| 299 |
*
|
| 300 |
*/
|
| 301 |
function _connect_get_riding($service_key = NULL, $postalcode = NULL, $scope='federal', $detail = 'federal', $use_cache = FALSE) {
|
| 302 |
// verify and normalize postalcode format
|
| 303 |
if (!connect_is_postalcode($postalcode)) {
|
| 304 |
return;
|
| 305 |
}
|
| 306 |
$postalcode = strtoupper(preg_replace('/\s/', '', $postalcode));
|
| 307 |
$return = FALSE;
|
| 308 |
|
| 309 |
// cached value?
|
| 310 |
if ($use_cache) {
|
| 311 |
$cached = _connect_cached_value('postalcode2riding', $postalcode);
|
| 312 |
if(!empty($cached)) {
|
| 313 |
drupal_set_message( '_connect_get_riding cache hit' );
|
| 314 |
return $cached;
|
| 315 |
}
|
| 316 |
}
|
| 317 |
|
| 318 |
// look up data, if not found in cache
|
| 319 |
// determine lookup URI based on type of lookup
|
| 320 |
$scope_function = array (
|
| 321 |
'federal' => array ( 'federal' => 'pc2csv/index' ),
|
| 322 |
'provincial' => array ( 'ontario' => 'provincial/on_riding' ),
|
| 323 |
'municipal' => array (),
|
| 324 |
);
|
| 325 |
|
| 326 |
if ( $service_key && isset($scope_function[$scope][$detail]) ) {
|
| 327 |
$riding_url = 'http://makethechange.ca/' .$scope_function[$scope][$detail]. '.php?key='.urlencode($service_key).'&pc='.urlencode($postalcode).'&type=riding';
|
| 328 |
$fh = fopen($riding_url, "r") or watchdog('debug','fopen failed: ' . $riding_url );
|
| 329 |
$riding_data = fread($fh, 200) or watchdog('debug','fread failed: ' . $riding_url );
|
| 330 |
fclose($fh);
|
| 331 |
}
|
| 332 |
|
| 333 |
// if there are results, walk through them and create an array of arrays
|
| 334 |
if (!empty($riding_data)) {
|
| 335 |
$row_token = strtok($riding_data, "\n");
|
| 336 |
while ($row_token != false) {
|
| 337 |
$return[] = connect_riding_data($row_token);
|
| 338 |
$row_token = strtok("\n");
|
| 339 |
}
|
| 340 |
|
| 341 |
// cache, if desired
|
| 342 |
if ($use_cache) {
|
| 343 |
$cached = _connect_cached_value('postalcode2riding', $postalcode, $return);
|
| 344 |
drupal_set_message( '_connect_get_riding cache set' );
|
| 345 |
}
|
| 346 |
}
|
| 347 |
|
| 348 |
return $return;
|
| 349 |
}
|
| 350 |
|
| 351 |
|
| 352 |
// turns riding data returned from web service into an indexed array
|
| 353 |
function connect_riding_data($data = NULL) {
|
| 354 |
$return = null;
|
| 355 |
$array_keys = array('id','en','fr');
|
| 356 |
$array_temp = explode(',',$data);
|
| 357 |
for ($i = 0; $i < count($array_temp); $i++) {
|
| 358 |
$return[$array_keys[$i]] = trim( $array_temp[$i] );
|
| 359 |
}
|
| 360 |
return $return;
|
| 361 |
}
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
/**** Utility functions ****/
|
| 366 |
|
| 367 |
|
| 368 |
/*
|
| 369 |
* Determine available target lookup types, return in select list format
|
| 370 |
*/
|
| 371 |
function connect_get_lookup_types($select = FALSE) {
|
| 372 |
static $return = array();
|
| 373 |
$empty = array();
|
| 374 |
if (empty($return) ) {
|
| 375 |
$functions = get_defined_functions();
|
| 376 |
$array = $functions['user'];
|
| 377 |
foreach ($array as $key=>$name ) {
|
| 378 |
if (strpos($name, 'connect_lookup_') !== FALSE ) {
|
| 379 |
eval( '$label = '.$name.'(\'describe\', $empty, $empty);' );
|
| 380 |
$return[$name] = $label;
|
| 381 |
}
|
| 382 |
}
|
| 383 |
$return[0] = '';
|
| 384 |
asort($return);
|
| 385 |
}
|
| 386 |
return $return;
|
| 387 |
}
|
| 388 |
|
| 389 |
/*
|
| 390 |
* Wrappers for JSON support if this version/install of PHP
|
| 391 |
* doesn't support it natively
|
| 392 |
*
|
| 393 |
* Thanks to http://abeautifulsite.net/notebook/71
|
| 394 |
* JSON library from http://mike.teczno.com/json.html
|
| 395 |
*/
|
| 396 |
function _connect_json_support() {
|
| 397 |
if (!function_exists('json_encode')) {
|
| 398 |
require_once('JSON.php');
|
| 399 |
function json_encode($data) {
|
| 400 |
$json = new Services_JSON();
|
| 401 |
return($json->encode($data));
|
| 402 |
}
|
| 403 |
}
|
| 404 |
|
| 405 |
if (!function_exists('json_decode')) {
|
| 406 |
require_once('JSON.php');
|
| 407 |
function json_decode($data) {
|
| 408 |
$json = new Services_JSON();
|
| 409 |
return($json->decode($data));
|
| 410 |
}
|
| 411 |
}
|
| 412 |
}
|
| 413 |
|
| 414 |
// check the connect cache for a saved value for a lookup
|
| 415 |
// if no third param = getter; third value = setter
|
| 416 |
function _connect_cached_value($type = NULL, $source = NULL, $value = NULL) {
|
| 417 |
$return = FALSE;
|
| 418 |
if ($type && $source) {
|
| 419 |
if (!$value) {
|
| 420 |
$sql = "SELECT target from {connect_cache} WHERE type='%s' AND source = '%s';";
|
| 421 |
$result = db_query($sql, $type, $source);
|
| 422 |
if ($row = db_fetch_object($result)) {
|
| 423 |
$return = unserialize($row->target);
|
| 424 |
}
|
| 425 |
}
|
| 426 |
else {
|
| 427 |
// delete
|
| 428 |
$sql = "DELETE from {connect_cache} WHERE type='%s' AND source = '%s';";
|
| 429 |
$result = db_query($sql, $type, $source);
|
| 430 |
//insert
|
| 431 |
$sql = "INSERT INTO {connect_cache} (type, source, target, created) VALUES('%s', '%s', '%s', %d);";
|
| 432 |
$result = db_query($sql, $type, $source, serialize($value), time());
|
| 433 |
$return = $result;
|
| 434 |
}
|
| 435 |
}
|
| 436 |
return $return;
|
| 437 |
}
|
| 438 |
|
| 439 |
// return names for cached items
|
| 440 |
function _connect_get_cache_names() {
|
| 441 |
$cache_names = array();
|
| 442 |
$lookup_types = connect_get_lookup_types(TRUE);
|
| 443 |
unset($lookup_types[0]);
|
| 444 |
$empty = array();
|
| 445 |
foreach ($lookup_types as $key=>$title) {
|
| 446 |
if (function_exists($key)) {
|
| 447 |
eval('$temp = ' . $key . '(\'cache\', $empty, $empty);');
|
| 448 |
$cache_names = array_merge($cache_names, $temp);
|
| 449 |
}
|
| 450 |
}
|
| 451 |
return array_unique($cache_names);
|
| 452 |
}
|