location.admin.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * @file
  4. * Admin forms for Location.
  5. */
  6. /**
  7. * Admin settings form.
  8. *
  9. * @ingroup form
  10. */
  11. function location_admin_settings($form, &$form_state) {
  12. // Recalculate the supported countries.
  13. cache_clear_all('location:supported-countries', 'cache');
  14. _location_supported_countries();
  15. $iso_list_sorted = location_get_iso3166_list();
  16. array_multisort($iso_list_sorted);
  17. $iso_list_sorted = array_merge(array('' => ''), $iso_list_sorted);
  18. $form = array();
  19. $form['location_default_country'] = array(
  20. '#type' => 'select',
  21. '#title' => t('Default country selection'),
  22. '#default_value' => variable_get('location_default_country', 'us'),
  23. '#options' => $iso_list_sorted,
  24. '#description' => t('This will be the country that is automatically selected when a location form is served for a new location.')
  25. );
  26. $form['location_display_location'] = array(
  27. '#type' => 'radios',
  28. '#title' => t('Toggle location display'),
  29. '#default_value' => variable_get('location_display_location', 1),
  30. '#options' => array(
  31. 0 => t('Disable the display of locations.'),
  32. 1 => t('Enable the display of locations.')
  33. ),
  34. '#description' => t('If you are interested in turning off locations and having a custom theme control their display, you may want to disable the display of locations so your theme can take that function.')
  35. );
  36. $form['location_use_province_abbreviation'] = array(
  37. '#type' => 'radios',
  38. '#title' => t('Province display'),
  39. '#default_value' => variable_get('location_use_province_abbreviation', 1),
  40. '#options' => array(
  41. 0 => t('Display full province name.'),
  42. 1 => t('Display province/state code.'),
  43. ),
  44. );
  45. $form['location_usegmap'] = array(
  46. '#type' => 'checkbox',
  47. '#title' => t('Use a Google Map to set latitude and longitude '),
  48. '#default_value' => variable_get('location_usegmap', FALSE),
  49. '#description' => t('If the gmap.module is installed and <a href="@enabled">enabled</a>, and this setting is also turned on, users that are allowed to manually enter latitude/longitude coordinates will be able to do so with an interactive Google Map. You should also make sure you have entered a <a href="@google_maps_api_key">Google Maps API key</a> into your <a href="@gmap_module_settings">gmap module settings</a>.', array('@enabled' => url('admin/build/modules'), '@google_maps_api_key' => 'http://www.google.com/apis/maps', '@gmap_module_settings' => url('admin/config/content/gmap'))),
  50. // @@@ megapatch This is an idea, but I'd opt more for a warning here...
  51. // '#disabled' => !module_exists('gmap'),
  52. );
  53. $form['location_locpick_macro'] = array(
  54. '#type' => 'textfield',
  55. '#title' => t('Location chooser macro'),
  56. '#size' => 50,
  57. '#maxlength' => 500,
  58. '#default_value' => variable_get('location_locpick_macro', '[gmap]'),
  59. '#description' => t('If you would like to change the macro used to generate the location chooser map, you can do so here. Note: Behaviors <em>locpick</em> and <em>collapsehack</em> are forced to be enabled and cannot be changed.'),
  60. );
  61. $form['location_jit_geocoding'] = array(
  62. '#type' => 'checkbox',
  63. '#title' => t('Enable JIT geocoding'),
  64. '#default_value' => variable_get('location_jit_geocoding', FALSE),
  65. '#description' => t('If you are going to be importing locations in bulk directly into the database, you may wish to enable JIT geocoding and load the locations with source set to 4 (LOCATION_LATLON_JIT_GEOCODING). The system will automatically geocode locations as they are loaded.'),
  66. );
  67. $form['maplink_external'] = array(
  68. '#type' => 'fieldset',
  69. '#title' => t('Map link'),
  70. );
  71. $form['maplink_external']['location_maplink_external'] = array(
  72. '#type' => 'checkbox',
  73. '#title' => t('Open map link in new window'),
  74. '#default_value' => variable_get('location_maplink_external', 0),
  75. '#description' => t('Select this if you want the map link to open in a separate window'),
  76. );
  77. $form['maplink_external']['location_maplink_external_method'] = array(
  78. '#type' => 'radios',
  79. '#title' => t('Open in new window method'),
  80. '#options' => array(
  81. 'target="_blank"' => 'target="_blank"',
  82. 'rel="external"' => 'rel="external"',
  83. ),
  84. '#default_value' => variable_get('location_maplink_external_method', 'target="_blank"'),
  85. '#description' => t('If you have selected to open map in a new window this controls the method used to open in a new window. target="_blank" will just work but is not XTHML Strict compliant. rel="external" is XHTML Strict compliant but will not open in a new window unless you add some jQuery to your site to add the target attribute. If you are unsure leave set to target="_blank"'),
  86. );
  87. return system_settings_form($form);
  88. }
  89. /**
  90. * Settings page for map links.
  91. */
  92. function location_map_link_options_form($form, &$form_state) {
  93. $form = array();
  94. $form['countries'] = array(
  95. '#type' => 'markup',
  96. '#markup' => ''
  97. );
  98. foreach (_location_supported_countries() as $country_iso => $country_name) {
  99. location_load_country($country_iso);
  100. $form['countries'][$country_iso] = array(
  101. '#type' => 'markup',
  102. '#markup' => ''
  103. );
  104. $form['countries'][$country_iso]['label_'. $country_iso] = array(
  105. '#type' => 'markup',
  106. '#markup' => $country_name
  107. );
  108. // Set up '#options' array for mapping providers for the current country
  109. $mapping_options = array();
  110. $provider_function = 'location_map_link_'. $country_iso .'_providers';
  111. $default_provider_function = 'location_map_link_'. $country_iso .'_default_providers';
  112. $checked = variable_get('location_map_link_'. $country_iso, function_exists($default_provider_function) ? $default_provider_function() : array());
  113. //print "Calling provider function $provider_function";
  114. if (function_exists($provider_function)) {
  115. foreach ($provider_function() as $name => $details) {
  116. $mapping_options[$name] = '<a href="'. $details['url'] .'">'. $details['name'] .'</a> (<a href="'. $details['tos'] .'">Terms of Use</a>)';
  117. }
  118. }
  119. if (count($mapping_options)) {
  120. $form['countries'][$country_iso]['location_map_link_'. $country_iso] = array(
  121. '#title' => '',
  122. '#type' => 'checkboxes',
  123. '#default_value' => $checked,
  124. '#options' => $mapping_options
  125. );
  126. }
  127. else {
  128. $form['countries'][$country_iso]['location_map_link_'. $country_iso] = array(
  129. '#type' => 'markup',
  130. '#markup' => t('None supported.')
  131. );
  132. }
  133. }
  134. $form = system_settings_form($form);
  135. $form['#theme'] = 'location_map_link_options';
  136. return $form;
  137. }
  138. function location_geocoding_options_form($form, &$form_state) {
  139. $form = array();
  140. $form['location_geocode_google_minimum_accuracy'] = array(
  141. '#type' => 'select',
  142. '#title' => t('Google Maps geocoding minimum accuracy'),
  143. '#options' => location_google_geocode_accuracy_codes(),
  144. '#default_value' => variable_get('location_geocode_google_minimum_accuracy', '3'),
  145. '#description' => t('The Google Maps geocoding API returns results with a given accuracy. Any responses below this minimum accuracy will be ignored. See a !accuracy_values_link.', array('!accuracy_values_link' => '<a href="http://code.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy">description of these values</a>'))
  146. );
  147. $form['countries'] = array();
  148. // First, we build two arrays to help us figure out on the fly whether a specific country is covered by a multi-country geocoder,
  149. // and what the details of the multi-country geocoder are
  150. // (1) Get list of geocoders
  151. $general_geocoders_list = location_get_general_geocoder_list();
  152. // (2) get data about each geocoder and the list of coutnries covered by each geocoder
  153. $general_geocoders_data = array();
  154. $general_geocoders_countries = array();
  155. foreach ($general_geocoders_list as $geocoder_name) {
  156. location_load_geocoder($geocoder_name);
  157. $info_function = $geocoder_name .'_geocode_info';
  158. if (function_exists($info_function)) {
  159. $general_geocoders_data[$geocoder_name] = $info_function();
  160. }
  161. $countries_function = $geocoder_name .'_geocode_country_list';
  162. if (function_exists($countries_function)) {
  163. $general_geocoders_countries[$geocoder_name] = $countries_function();
  164. }
  165. }
  166. foreach (_location_supported_countries() as $country_iso => $country_name) {
  167. location_load_country($country_iso);
  168. $geocoding_options = array();
  169. $form['countries'][$country_iso] = array(
  170. '#type' => 'markup',
  171. '#markup' => ''
  172. );
  173. $form['countries'][$country_iso]['label_'. $country_iso] = array(
  174. '#type' => 'markup',
  175. '#markup' => '<div id="'. $country_iso .'">'. $country_name .'</div>'
  176. );
  177. // Next, we look for options presented by country specific providers
  178. $country_specific_provider_function = 'location_geocode_'. $country_iso .'_providers';
  179. if (function_exists($country_specific_provider_function)) {
  180. foreach ($country_specific_provider_function() as $name => $details) {
  181. $geocoding_options[$name .'|'. $country_iso] = '<a href="'. $details['url'] .'">'. $details['name'] .'</a> (<a href="'. $details['tos'] .'">Terms of Use</a>)';
  182. }
  183. }
  184. foreach ($general_geocoders_list as $geocoder_name) {
  185. if (in_array($country_iso, $general_geocoders_countries[$geocoder_name])) {
  186. $geocoding_options[$geocoder_name] = '<a href="'. $general_geocoders_data[$geocoder_name]['url'] .'">'. $general_geocoders_data[$geocoder_name]['name'] .'</a> (<a href="'. $general_geocoders_data[$geocoder_name]['tos'] .'">Terms of Use</a>)';
  187. }
  188. }
  189. if (count($geocoding_options)) {
  190. $geocoding_options = array_merge(array('none' => t('None')), $geocoding_options);
  191. $form['countries'][$country_iso]['location_geocode_'. $country_iso] = array(
  192. '#type' => 'radios',
  193. '#default_value' => variable_get('location_geocode_'. $country_iso, 'none'),
  194. '#options' => $geocoding_options
  195. );
  196. }
  197. else {
  198. $form['countries'][$country_iso]['location_geocode_'. $country_iso] = array(
  199. '#type' => 'markup',
  200. '#markup' => t('None supported.')
  201. );
  202. }
  203. $current_value = variable_get('location_geocode_'. $country_iso, 'none');
  204. if ($current_value == 'none') {
  205. $form['countries'][$country_iso]['location_geocode_config_link_'. $country_iso] = array(
  206. '#type' => 'markup',
  207. '#markup' => t('No service selected for country.')
  208. );
  209. }
  210. else {
  211. $current_val_chopped = substr($current_value, 0, strpos($current_value, '|'));
  212. $geocode_settings_form_function_specific = 'location_geocode_'. $country_iso .'_'. $current_val_chopped .'_settings';
  213. $geocode_settings_form_function_general = $current_value .'_geocode_settings';
  214. if (function_exists($geocode_settings_form_function_specific)) {
  215. $form['countries'][$country_iso]['location_geocode_config_link_'. $country_iso] = array(
  216. '#type' => 'link',
  217. '#title' => t('Configure parameters'),
  218. '#href' => 'admin/config/content/location/geocoding/' . $country_iso . '/' . $current_val_chopped,
  219. );
  220. }
  221. elseif (function_exists($geocode_settings_form_function_general)) {
  222. $form['countries'][$country_iso]['location_geocode_config_link_'. $country_iso] = array(
  223. '#type' => 'link',
  224. '#title' => t('Configure parameters'),
  225. '#href' => 'admin/config/content/location/geocoding/' . $country_iso . '/' . $current_value,
  226. );
  227. }
  228. else {
  229. $form['countries'][$country_iso]['location_geocode_config_link_'. $country_iso] = array(
  230. '#type' => 'markup',
  231. '#markup' => t('No configuration necessary for selected service.')
  232. );
  233. }
  234. }
  235. }
  236. $form = system_settings_form($form);
  237. $form['#theme'] = 'location_geocoding_options';
  238. array_unshift($form['#submit'], 'location_geocoding_options_form_submit');
  239. return $form;
  240. }
  241. function location_geocoding_options_form_submit($form, &$form_state) {
  242. $general_geocoders = location_get_general_geocoder_list();
  243. $general_geocoders_in_use = array();
  244. foreach ($form_state['values'] as $key => $value) {
  245. if (substr($key, 0, 17) == 'location_geocode_' && $key != 'location_geocode_google_minimum_accuracy') {
  246. if (in_array($value, $general_geocoders)) {
  247. $general_geocoders_in_use[$value] = $value;
  248. variable_set($key, $value);
  249. }
  250. }
  251. }
  252. variable_set('location_geocode_google_minimum_accuracy', $form_state['values']['location_geocode_google_minimum_accuracy']);
  253. variable_set('location_general_geocoders_in_use', $general_geocoders_in_use);
  254. }
  255. function theme_location_map_link_options($variables) {
  256. $form = $variables['form'];
  257. $header = array(array('align' => 'center', 'data' => '<center>'. t('Country') .'</center>'), array('align' => 'center', 'data' => '<center>'. t('Options') .'</center>'));
  258. $rows = array();
  259. foreach (element_children($form['countries']) as $country_iso) {
  260. $row = array();
  261. $row[] = array(
  262. 'data' => drupal_render($form['countries'][$country_iso]['label_'. $country_iso])
  263. );
  264. $row[] = array(
  265. 'data' => drupal_render($form['countries'][$country_iso]['location_map_link_'. $country_iso])
  266. );
  267. $rows[] = $row;
  268. }
  269. $output = theme('table', array('header' => $header, 'rows' => $rows));
  270. $output .= drupal_render_children($form);
  271. return $output;
  272. }
  273. function theme_location_geocoding_options($variables) {
  274. $form = $variables['form'];
  275. $output = drupal_render($form['location_geocode_google_minimum_accuracy']);
  276. $header = array(
  277. array('align' => 'center', 'data' => '<center>'. t('Country') .'</center>'),
  278. array('align' => 'center', 'data' => '<center>'. t('Options') .'</center>'),
  279. array('align' => 'center', 'data' => '<center>'. t('Configure') .'</center>')
  280. );
  281. $rows = array();
  282. foreach (element_children($form['countries']) as $country_iso) {
  283. $row = array();
  284. $row[] = array(
  285. 'data' => drupal_render($form['countries'][$country_iso]['label_'. $country_iso])
  286. );
  287. $row[] = array(
  288. 'data' => drupal_render($form['countries'][$country_iso]['location_geocode_'. $country_iso])
  289. );
  290. $row[] = array(
  291. 'data' => drupal_render($form['countries'][$country_iso]['location_geocode_config_link_'. $country_iso])
  292. );
  293. $rows[] = $row;
  294. }
  295. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  296. $output .= drupal_render_children($form);
  297. return $output;
  298. }
  299. /**
  300. * Location Utilities form.
  301. */
  302. function location_util_form($form, &$form_State) {
  303. $form['province_clear'] = array(
  304. '#type' => 'fieldset',
  305. '#title' => t('Clear province cache'),
  306. '#description' => t('If you have modified location.xx.inc files, you will need to clear the province cache to get Location to recognize the modifications.'),
  307. );
  308. $form['supported_countries_clear'] = array(
  309. '#type' => 'fieldset',
  310. '#title' => t('Clear supported country list'),
  311. '#description' => t('If you have added support for a new country, you will need to clear the supported country list to get Location to recognize the modifications.'),
  312. );
  313. $form['actions'] = array(
  314. '#type' => 'actions',
  315. );
  316. $form['actions']['province_clear_submit'] = array(
  317. '#type' => 'submit',
  318. '#value' => t('Clear province cache'),
  319. '#submit' => array('location_util_form_clear_province_cache_submit'),
  320. );
  321. $form['actions']['supported_countries_clear_submit'] = array(
  322. '#type' => 'submit',
  323. '#value' => t('Clear supported country list'),
  324. '#submit' => array('location_util_form_clear_supported_countries_submit'),
  325. );
  326. return $form;
  327. }
  328. /**
  329. * Location utilities form: Clear province cache.
  330. */
  331. function location_util_form_clear_province_cache_submit() {
  332. drupal_set_message(t('Location province cache cleared.'));
  333. cache_clear_all('provinces:', 'cache_location', TRUE);
  334. }
  335. /**
  336. * Location utilities form: Clear supported countries cache.
  337. */
  338. function location_util_form_clear_supported_countries_submit() {
  339. drupal_set_message(t('Location supported country list cleared.'));
  340. cache_clear_all('location:supported-countries', 'cache_location');
  341. }