location.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // Functionality depends on User Location.
  3. if (module_exists('location_user')) {
  4. /**
  5. * Implementation of hook_user_import_form_field_match(). Add supported Location fields into our dropdown list.
  6. */
  7. function location_user_import_form_field_match() {
  8. $options = array();
  9. $options['location'] = location_field_names();
  10. return $options;
  11. }
  12. /**
  13. * Implementation of hook_user_import_form_update_user().
  14. */
  15. function location_user_import_form_update_user() {
  16. $form['location'] = array(
  17. 'title' => t('Location'),
  18. 'description' => t('Affected: fields in user Location.'),
  19. );
  20. return $form;
  21. }
  22. /**
  23. * Implementation of hook_user_import_data().
  24. */
  25. function location_user_import_data($settings, $update_setting, $column_settings, $module, $field_id, $data, $column_id) {
  26. if ($module != 'location') {
  27. return;
  28. }
  29. if (empty($data[$column_id])) {
  30. $defaults = location_locationapi($data, 'defaults');
  31. $data[$column_id] = $defaults[$field_id]['default'];
  32. }
  33. switch ($field_id) {
  34. case 'country':
  35. $country = location_country_name($data[$column_id]);
  36. if ($country != '') {
  37. return trim($data[$column_id]);
  38. }
  39. else {
  40. $countries = location_get_iso3166_list();
  41. if ($country = array_search($data[$column_id], $countries)) {
  42. if ($country !== FALSE) {
  43. return $country;
  44. }
  45. }
  46. }
  47. user_import_errors(t('Invalid country'));
  48. return trim($data[$column_id]);
  49. case 'province':
  50. if (!empty($data['country'])) {
  51. if (!empty($data[$column_id])) {
  52. $provinces = location_get_provinces($data['country']);
  53. $found = FALSE;
  54. $province = strtoupper($data['province']);
  55. foreach ($provinces as $key => $value) {
  56. if ($province == strtoupper($key) || $province == strtoupper($value)) {
  57. $found = TRUE;
  58. break;
  59. }
  60. }
  61. if (!$found) {
  62. user_import_errors(t('Province not found'));
  63. }
  64. return trim($data[$column_id]);
  65. }
  66. }
  67. return trim($data[$column_id]);
  68. default:
  69. return trim($data[$column_id]);
  70. }
  71. }
  72. /**
  73. * Implementation of hook_user_import_after_save().
  74. */
  75. function location_user_import_after_save($settings, $account, $password, $fields, $updated, $update_setting_per_module) {
  76. if (!is_array($fields['location'])) {
  77. return;
  78. }
  79. // check if it's an existing user and if location is to be updated
  80. if ($updated && $update_setting_per_module['location'] == UPDATE_NONE) {
  81. return;
  82. }
  83. // Arrange values for location array
  84. $location = array();
  85. foreach ($fields['location'] as $column_id => $column_data) {
  86. $location[0][$column_id] = $column_data[0];
  87. }
  88. // Merge defaults in
  89. $dummy = array();
  90. $defaults = location_invoke_locationapi($dummy, 'defaults');
  91. foreach ($defaults as $key => $value) {
  92. if (!isset($location[0][$key])) {
  93. $location[0][$key] = $value['default'];
  94. }
  95. }
  96. location_save_locations($location, array('uid' => $account->uid));
  97. return;
  98. }
  99. }