| 1 |
<?php |
<?php |
| 2 |
// $Id: location.module,v 1.246 2009/07/30 17:25:04 bdragon Exp $ |
// $Id: location.module,v 1.247 2009/07/30 20:53:48 bdragon Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 95 |
*/ |
*/ |
| 96 |
function location_perm() { |
function location_perm() { |
| 97 |
return array( |
return array( |
| 98 |
'submit latitude/longitude', |
'submit latitude/longitude' => array( |
| 99 |
|
'title' => t('Submit Latitude / Longitude'), |
| 100 |
|
'description' => t('Allow the user to submit raw coordinates.'), |
| 101 |
|
), |
| 102 |
); |
); |
| 103 |
} |
} |
| 104 |
|
|
| 134 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 135 |
'#location_settings' => array(), |
'#location_settings' => array(), |
| 136 |
'#required' => FALSE, |
'#required' => FALSE, |
| 137 |
'#attributes' => array('class' => 'location'), |
'#attributes' => array('class' => array('location')), |
| 138 |
// Element level validation. |
// Element level validation. |
| 139 |
'#element_validate' => array('location_element_validate'), |
'#element_validate' => array('location_element_validate'), |
| 140 |
|
'#theme' => 'theme_fieldset', // @@@TODO 7.x -- verify that this is sane. |
| 141 |
), |
), |
| 142 |
'location_settings' => array( |
'location_settings' => array( |
| 143 |
'#input' => TRUE, |
'#input' => TRUE, |
| 145 |
'#collapsible' => TRUE, |
'#collapsible' => TRUE, |
| 146 |
'#collapsed' => TRUE, |
'#collapsed' => TRUE, |
| 147 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 148 |
|
'#theme' => 'location_settings', |
| 149 |
), |
), |
| 150 |
); |
); |
| 151 |
} |
} |
| 152 |
|
|
| 153 |
/** |
/** |
|
* Theme function to fixup location elements. |
|
|
* @ingroup themable |
|
|
*/ |
|
|
function theme_location_element($element) { |
|
|
// Prevent spurious "Array" from appearing. |
|
|
unset($element['#value']); |
|
|
return theme('fieldset', $element); |
|
|
} |
|
|
|
|
|
/** |
|
| 154 |
* Implementation of hook_theme(). |
* Implementation of hook_theme(). |
| 155 |
*/ |
*/ |
| 156 |
function location_theme() { |
function location_theme() { |
| 304 |
'#title' => t('Latitude'), |
'#title' => t('Latitude'), |
| 305 |
'#default_value' => isset($element['#default_value']['locpick']['user_latitude']) ? $element['#default_value']['locpick']['user_latitude'] : '', |
'#default_value' => isset($element['#default_value']['locpick']['user_latitude']) ? $element['#default_value']['locpick']['user_latitude'] : '', |
| 306 |
'#size' => 16, |
'#size' => 16, |
| 307 |
'#attributes' => array('class' => 'container-inline'), |
'#attributes' => array('class' => array('container-inline')), |
| 308 |
'#maxlength' => 20, |
'#maxlength' => 20, |
| 309 |
); |
); |
| 310 |
$element['locpick']['user_longitude'] = array( |
$element['locpick']['user_longitude'] = array( |
| 319 |
'#type' => 'markup', |
'#type' => 'markup', |
| 320 |
'#weight' => 1, |
'#weight' => 1, |
| 321 |
'#prefix' => '<div class=\'description\'>', |
'#prefix' => '<div class=\'description\'>', |
| 322 |
'#value' => '<br /><br />' . t('If you wish to supply your own latitude and longitude, you may enter them above. If you leave these fields blank, the system will attempt to determine a latitude and longitude for you from the entered address. To have the system recalculate your location from the address, for example if you change the address, delete the values for these fields.'), |
'#markup' => '<br /><br />' . t('If you wish to supply your own latitude and longitude, you may enter them above. If you leave these fields blank, the system will attempt to determine a latitude and longitude for you from the entered address. To have the system recalculate your location from the address, for example if you change the address, delete the values for these fields.'), |
| 323 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 324 |
); |
); |
| 325 |
if (function_exists('gmap_get_auto_mapid') && variable_get('location_usegmap', FALSE)) { |
if (function_exists('gmap_get_auto_mapid') && variable_get('location_usegmap', FALSE)) { |
| 362 |
'#type' => 'markup', |
'#type' => 'markup', |
| 363 |
'#weight' => 2, |
'#weight' => 2, |
| 364 |
'#prefix' => '<div class=\'description\'>', |
'#prefix' => '<div class=\'description\'>', |
| 365 |
'#value' => t('You may set the location by clicking on the map, or dragging the location marker. To clear the location and cause it to be recalculated, click on the marker.'), |
'#markup' => t('You may set the location by clicking on the map, or dragging the location marker. To clear the location and cause it to be recalculated, click on the marker.'), |
| 366 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 367 |
); |
); |
| 368 |
} |
} |
| 377 |
); |
); |
| 378 |
} |
} |
| 379 |
|
|
| 380 |
$element += _element_info('fieldset'); |
$element += element_info('fieldset'); |
| 381 |
return $element; |
return $element; |
| 382 |
} |
} |
| 383 |
|
|
| 384 |
function _location_expand_location_settings(&$element) { |
function _location_expand_location_settings($element) { |
| 385 |
// Set a value for the fieldset that doesn't interfere with rendering and doesn't generate a warning. |
// Set a value for the fieldset that doesn't interfere with rendering and doesn't generate a warning. |
| 386 |
$element['#tree'] = TRUE; |
$element['#tree'] = TRUE; |
| 387 |
$element['#theme'] = 'location_settings'; |
$element['#theme'] = 'location_settings'; |
| 468 |
unset($element['country']['collect']['#options'][0]); |
unset($element['country']['collect']['#options'][0]); |
| 469 |
|
|
| 470 |
foreach (element_children($element) as $key) { |
foreach (element_children($element) as $key) { |
| 471 |
$element[$key]['weight']['#attributes']['class'] = 'location-settings-weight'; |
$element[$key]['weight']['#attributes']['class'] = array('location-settings-weight'); |
| 472 |
unset($element[$key]['default']['#title']); |
unset($element[$key]['default']['#title']); |
| 473 |
$row = array(); |
$row = array(); |
| 474 |
$row[] = array('data' => '', 'class' => 'location-settings-drag'); |
$row[] = array('data' => '', 'class' => array('location-settings-drag')); |
| 475 |
$row[] = drupal_render($element[$key]['name']); |
$row[] = drupal_render($element[$key]['name']); |
| 476 |
$row[] = drupal_render($element[$key]['collect']); |
$row[] = drupal_render($element[$key]['collect']); |
| 477 |
$row[] = drupal_render($element[$key]['default']); |
$row[] = drupal_render($element[$key]['default']); |
| 478 |
$row[] = array('data' => drupal_render($element[$key]['weight']), 'class' => 'delta-order'); |
$row[] = array('data' => drupal_render($element[$key]['weight']), 'class' => array('delta-order')); |
| 479 |
|
|
| 480 |
$rows[] = array( |
$rows[] = array( |
| 481 |
'#weight' => (int)$element[$key]['weight']['#value'], |
'#weight' => (int)$element[$key]['weight']['#value'], |
| 482 |
'data' => $row, |
'data' => $row, |
| 483 |
'class' => 'draggable', |
'class' => array('draggable'), |
| 484 |
); |
); |
| 485 |
} |
} |
| 486 |
|
|
| 537 |
'city' => array('default' => '', 'collect' => 0, 'weight' => 8), |
'city' => array('default' => '', 'collect' => 0, 'weight' => 8), |
| 538 |
'province' => array('default' => '', 'collect' => 0, 'weight' => 10), |
'province' => array('default' => '', 'collect' => 0, 'weight' => 10), |
| 539 |
'postal_code' => array('default' => '', 'collect' => 0, 'weight' => 12), |
'postal_code' => array('default' => '', 'collect' => 0, 'weight' => 12), |
| 540 |
'country' => array('default' => variable_get('location_default_country', 'us'), 'collect' => 1, 'weight' => 14), // @@@ Fix weight? |
'country' => array('default' => variable_get('site_default_country', 'us'), 'collect' => 1, 'weight' => 14), // @@@ Fix weight? |
| 541 |
'locpick' => array('default' => FALSE, 'collect' => 1, 'weight' => 20, 'nodiff' => TRUE), |
'locpick' => array('default' => FALSE, 'collect' => 1, 'weight' => 20, 'nodiff' => TRUE), |
| 542 |
'latitude' => array('default' => 0), |
'latitude' => array('default' => 0), |
| 543 |
'longitude' => array('default' => 0), |
'longitude' => array('default' => 0), |
| 626 |
|
|
| 627 |
case 'province': |
case 'province': |
| 628 |
drupal_add_js(drupal_get_path('module', 'location') .'/location_autocomplete.js'); |
drupal_add_js(drupal_get_path('module', 'location') .'/location_autocomplete.js'); |
| 629 |
$country = $a5['country'] ? $a5['country'] : variable_get('location_default_country', 'us'); |
$country = $a5['country'] ? $a5['country'] : variable_get('site_default_country', 'us'); |
| 630 |
return array( |
return array( |
| 631 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 632 |
'#title' => t('State/Province'), |
'#title' => t('State/Province'), |
| 636 |
'#maxlength' => 64, |
'#maxlength' => 64, |
| 637 |
'#description' => NULL, |
'#description' => NULL, |
| 638 |
// Used by province autocompletion js. |
// Used by province autocompletion js. |
| 639 |
'#attributes' => array('class' => 'location_auto_province'), |
'#attributes' => array('class' => array('location_auto_province')), |
| 640 |
'#required' => ($a4 == 2), |
'#required' => ($a4 == 2), |
| 641 |
); |
); |
| 642 |
|
|
| 658 |
'#description' => NULL, |
'#description' => NULL, |
| 659 |
'#required' => ($a4 == 2), |
'#required' => ($a4 == 2), |
| 660 |
// Used by province autocompletion js. |
// Used by province autocompletion js. |
| 661 |
'#attributes' => array('class' => 'location_auto_country'), |
'#attributes' => array('class' => array('location_auto_country')), |
| 662 |
); |
); |
| 663 |
} |
} |
| 664 |
break; |
break; |
| 738 |
else { |
else { |
| 739 |
return system_settings_form(array( |
return system_settings_form(array( |
| 740 |
'#type' => 'markup', |
'#type' => 'markup', |
| 741 |
'#value' => t('No configuration parameters are necessary, or a form to take such paramters has not been implemented.') |
'#markup' => t('No configuration parameters are necessary, or a form to take such paramters has not been implemented.') |
| 742 |
)); |
)); |
| 743 |
} |
} |
| 744 |
} |
} |
| 757 |
// by accident. 0 and '' are reserved for "not applicable." |
// by accident. 0 and '' are reserved for "not applicable." |
| 758 |
return array(); |
return array(); |
| 759 |
} |
} |
| 760 |
if ($key == 'genid') { |
$query = db_select('location_instance', 'l'); |
| 761 |
$result = db_query('SELECT lid FROM {location_instance} WHERE '. db_escape_table($key) ." = '%s'", $id); |
$lid_field = $query->addField('l', 'lid'); |
| 762 |
} |
$query->condition($key, $id); |
| 763 |
else { |
$result = $query->execute(); |
|
$result = db_query('SELECT lid FROM {location_instance} WHERE '. db_escape_table($key) .' = %d', $id); |
|
|
} |
|
| 764 |
$locations = array(); |
$locations = array(); |
| 765 |
while ($lid = db_fetch_object($result)) { |
foreach ($result as $lid) { |
| 766 |
$locations[] = location_load_location($lid->lid); |
$locations[] = location_load_location($lid->{$lid_field}); |
| 767 |
} |
} |
| 768 |
return $locations; |
return $locations; |
| 769 |
} |
} |
| 784 |
foreach (array_keys($locations) as $key) { |
foreach (array_keys($locations) as $key) { |
| 785 |
location_save($locations[$key], TRUE, $criteria); |
location_save($locations[$key], TRUE, $criteria); |
| 786 |
} |
} |
|
$columns = array(); |
|
|
$placeholders = array(); |
|
|
$qfrags = array(); |
|
|
$args = array(); |
|
|
foreach (array('nid' => '%d', 'vid' => '%d', 'uid' => '%d', 'genid' => "'%s'") as $key => $placeholder) { |
|
|
if (isset($criteria[$key])) { |
|
|
$columns[] = $key; |
|
|
$placeholders[] = $placeholder; |
|
|
$args[] = $criteria[$key]; |
|
|
$qfrags[] = "$key = $placeholder"; |
|
|
} |
|
|
} |
|
|
$querybase = 'FROM {location_instance} WHERE '. implode(' AND ', $qfrags); |
|
| 787 |
|
|
|
$oldlids = array(); |
|
|
$newlids = array(); |
|
| 788 |
// Find affected lids. |
// Find affected lids. |
| 789 |
$query = "SELECT lid $querybase"; |
$query = db_select('location_instance', 'l'); |
| 790 |
$result = db_query($query, $args); |
$lid_field = $query->addField('l', 'lid'); |
| 791 |
while ($t = db_fetch_object($result)) { |
foreach ($criteria as $key => $value) { |
| 792 |
$oldlids[] = $t->lid; |
$query->condition($key, $value); |
| 793 |
|
} |
| 794 |
|
$oldlids = $query->execute()->fetchCol(); |
| 795 |
|
|
| 796 |
|
// Delete current set of instances. |
| 797 |
|
$query = db_delete('location_instance'); |
| 798 |
|
foreach ($criteria as $key => $value) { |
| 799 |
|
$query->condition($key, $value); |
| 800 |
} |
} |
| 801 |
|
|
| 802 |
$query = "DELETE $querybase"; |
$newlids = array(); |
| 803 |
db_query($query, $args); |
$query = db_insert('location_instance')->fields(array('nid', 'vid', 'uid', 'genid', 'lid')); |
|
// Tack on the lid. |
|
|
$columns[] = 'lid'; |
|
|
$placeholders[] = '%d'; |
|
|
|
|
| 804 |
foreach ($locations as $location) { |
foreach ($locations as $location) { |
| 805 |
// Don't save "empty" locations. |
// Don't save "empty" locations. |
| 806 |
// location_save() explicitly returns FALSE for empty locations, |
// location_save() explicitly returns FALSE for empty locations, |
| 807 |
// so it should be ok to rely on the data type. |
// so it should be ok to rely on the data type. |
| 808 |
if ($location['lid'] !== FALSE) { |
if ($location['lid'] !== FALSE) { |
|
$args[] = $location['lid']; |
|
| 809 |
$newlids[] = $location['lid']; |
$newlids[] = $location['lid']; |
| 810 |
db_query('INSERT INTO {location_instance} ('. implode(', ', $columns) .') VALUES ('. implode(', ', $placeholders) .')', $args); |
$instance = array( |
| 811 |
array_pop($args); |
'nid' => 0, |
| 812 |
|
'vid' => 0, |
| 813 |
|
'uid' => 0, |
| 814 |
|
'genid' => '', |
| 815 |
|
'lid' => $location['lid'], |
| 816 |
|
); |
| 817 |
|
foreach ($criteria as $k => $v) { |
| 818 |
|
$instance[$k] = $v; |
| 819 |
|
} |
| 820 |
|
$query->values($instance); |
| 821 |
} |
} |
| 822 |
} |
} |
| 823 |
|
$query->execute(); |
| 824 |
|
|
| 825 |
// Check anything that dropped a reference during this operation. |
// Check anything that dropped a reference during this operation. |
| 826 |
foreach (array_diff($oldlids, $newlids) as $check) { |
foreach (array_diff($oldlids, $newlids) as $check) { |
| 827 |
// An instance may have been deleted. Check reference count. |
// An instance may have been deleted. Check reference count. |
| 828 |
$count = db_result(db_query('SELECT COUNT(*) FROM {location_instance} WHERE lid = %d', $check)); |
$count = db_query('SELECT COUNT(*) FROM {location_instance} WHERE lid = :lid', array(':lid' => $check))->fetchField(); |
| 829 |
if ($count !== FALSE && $count == 0) { |
if ($count !== FALSE && $count == 0) { |
| 830 |
watchdog('location', t('Deleting unreferenced location with LID %lid.', array('%lid' => $check))); |
watchdog('location', t('Deleting unreferenced location with LID %lid.', array('%lid' => $check))); |
| 831 |
$location = array('lid' => $check); |
$location = array('lid' => $check); |
| 832 |
location_invoke_locationapi($location, 'delete'); |
location_invoke_locationapi($location, 'delete'); |
| 833 |
db_query('DELETE FROM {location} WHERE lid = %d', $location['lid']); |
db_delete('location') |
| 834 |
|
->condition('lid', $location['lid']) |
| 835 |
|
->execute(); |
| 836 |
} |
} |
| 837 |
} |
} |
| 838 |
} |
} |
| 845 |
* @return A location array. |
* @return A location array. |
| 846 |
*/ |
*/ |
| 847 |
function location_load_location($lid) { |
function location_load_location($lid) { |
| 848 |
$location = db_fetch_array(db_query('SELECT * FROM {location} WHERE lid = %d', $lid)); |
$location = db_query('SELECT * FROM {location} WHERE lid = :lid', array(':lid' => $lid))->fetchAssoc(); |
| 849 |
// @@@ Just thought of this, but I am not certain it is a good idea... |
// @@@ Just thought of this, but I am not certain it is a good idea... |
| 850 |
if (empty($location)) { |
if (empty($location)) { |
| 851 |
$location = array('lid' => $lid); |
$location = array('lid' => $lid); |
| 863 |
if (isset($location['source']) && $location['source'] == LOCATION_LATLON_JIT_GEOCODING) { |
if (isset($location['source']) && $location['source'] == LOCATION_LATLON_JIT_GEOCODING) { |
| 864 |
if (variable_get('location_jit_geocoding', FALSE)) { |
if (variable_get('location_jit_geocoding', FALSE)) { |
| 865 |
_location_geo_logic($location, array('street' => 1), array()); |
_location_geo_logic($location, array('street' => 1), array()); |
| 866 |
db_query("UPDATE {location} SET latitude = '%f', longitude = '%f', source = %d WHERE lid = %d", |
db_update('location') |
| 867 |
$location['latitude'], |
->fields(array('latitude', 'longitude', 'source')) |
| 868 |
$location['longitude'], |
->values($location) |
| 869 |
$location['source'], |
->condition('lid', $location['lid']) |
| 870 |
$location['lid']); |
->execute(); |
| 871 |
} |
} |
| 872 |
} |
} |
| 873 |
|
|
| 1088 |
if (!empty($criteria)) { |
if (!empty($criteria)) { |
| 1089 |
// Check for other instances. |
// Check for other instances. |
| 1090 |
// See #306171 for more information. |
// See #306171 for more information. |
| 1091 |
$columns = array(); |
$query = db_select('location_instance', 'l'); |
| 1092 |
$placeholders = array(); |
foreach ($criteria as $key => $value) { |
| 1093 |
$qfrags = array(); |
$query->condition($key, $value); |
|
$args = array(); |
|
|
foreach (array('nid' => '%d', 'vid' => '%d', 'uid' => '%d', 'genid' => "'%s'") as $key => $placeholder) { |
|
|
if (isset($criteria[$key])) { |
|
|
$columns[] = $key; |
|
|
$placeholders[] = $placeholder; |
|
|
$args[] = $criteria[$key]; |
|
|
$qfrags[] = "$key = $placeholder"; |
|
|
} |
|
| 1094 |
} |
} |
| 1095 |
$querybase = 'FROM {location_instance} WHERE '. implode(' AND ', $qfrags); |
$associated = $query->countQuery()->execute()->fetchField(); |
| 1096 |
|
$all = db_query("SELECT COUNT(*) FROM {location_instance} WHERE lid = :lid", array(':lid', $location['lid']))->fetchField(); |
|
$associated = db_result(db_query("SELECT COUNT(*) $querybase", $args)); |
|
|
$all = db_result(db_query("SELECT COUNT(*) FROM {location_instance} WHERE lid = %d", $location['lid'])); |
|
| 1097 |
if ($associated != $all) { |
if ($associated != $all) { |
| 1098 |
// If there were a different number of instances than instances matching the criteria, |
// If there were a different number of instances than instances matching the criteria, |
| 1099 |
// we need a new LID. |
// we need a new LID. |
| 1652 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 1653 |
'#title' => format_plural($numforms, 'Location', 'Locations'), |
'#title' => format_plural($numforms, 'Location', 'Locations'), |
| 1654 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 1655 |
'#attributes' => array('class' => 'locations'), |
'#attributes' => array('class' => array('locations')), |
| 1656 |
'#weight' => $settings['form']['weight'], |
'#weight' => $settings['form']['weight'], |
| 1657 |
'#collapsible' => $settings['form']['collapsible'], |
'#collapsible' => $settings['form']['collapsible'], |
| 1658 |
'#collapsed' => $settings['form']['collapsed'], |
'#collapsed' => $settings['form']['collapsed'], |
| 1699 |
// Show all locations |
// Show all locations |
| 1700 |
return array( |
return array( |
| 1701 |
'#type' => 'markup', |
'#type' => 'markup', |
| 1702 |
'#value' => theme('locations', $locations, $hide), |
'#markup' => theme('locations', $locations, $hide), |
| 1703 |
'#weight' => $settings['display']['weight'], |
'#weight' => $settings['display']['weight'], |
| 1704 |
); |
); |
| 1705 |
} |
} |
|
|
|
|
function location_rss_item($location, $mode = 'simple') { |
|
|
require_once(drupal_get_path('module', 'location') .'/location.georss.inc'); |
|
|
return _location_rss_item($location, $mode); |
|
|
} |
|