location.admin.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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(
  25. 'This will be the country that is automatically selected when a location form is served for a new location.'
  26. )
  27. );
  28. $form['location_display_location'] = array(
  29. '#type' => 'radios',
  30. '#title' => t('Toggle location display'),
  31. '#default_value' => variable_get('location_display_location', 1),
  32. '#options' => array(
  33. 0 => t('Disable the display of locations.'),
  34. 1 => t('Enable the display of locations.')
  35. ),
  36. '#description' => t(
  37. '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.'
  38. )
  39. );
  40. $form['location_use_province_abbreviation'] = array(
  41. '#type' => 'radios',
  42. '#title' => t('Province display'),
  43. '#default_value' => variable_get('location_use_province_abbreviation', 1),
  44. '#options' => array(
  45. 0 => t('Display full province name.'),
  46. 1 => t('Display province/state code.'),
  47. ),
  48. );
  49. $form['location_usegmap'] = array(
  50. '#type' => 'checkbox',
  51. '#title' => t('Use a Google Map to set latitude and longitude '),
  52. '#default_value' => variable_get('location_usegmap', FALSE),
  53. '#description' => t(
  54. '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>.',
  55. array(
  56. '@enabled' => url('admin/build/modules'),
  57. '@google_maps_api_key' => 'http://www.google.com/apis/maps',
  58. '@gmap_module_settings' => url('admin/config/content/gmap')
  59. )
  60. ),
  61. );
  62. $form['location_locpick_macro'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Location chooser macro'),
  65. '#size' => 50,
  66. '#maxlength' => 500,
  67. '#default_value' => variable_get('location_locpick_macro', '[gmap]'),
  68. '#description' => t(
  69. '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.'
  70. ),
  71. );
  72. $form['location_jit_geocoding'] = array(
  73. '#type' => 'checkbox',
  74. '#title' => t('Enable JIT geocoding'),
  75. '#default_value' => variable_get('location_jit_geocoding', FALSE),
  76. '#description' => t(
  77. '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.'
  78. ),
  79. );
  80. $form['maplink_external'] = array(
  81. '#type' => 'fieldset',
  82. '#title' => t('Map link'),
  83. );
  84. $form['maplink_external']['location_maplink_external'] = array(
  85. '#type' => 'checkbox',
  86. '#title' => t('Open map link in new window'),
  87. '#default_value' => variable_get('location_maplink_external', 0),
  88. '#description' => t('Select this if you want the map link to open in a separate window'),
  89. );
  90. $form['maplink_external']['location_maplink_external_method'] = array(
  91. '#type' => 'radios',
  92. '#title' => t('Open in new window method'),
  93. '#options' => array(
  94. 'target="_blank"' => 'target="_blank"',
  95. 'rel="external"' => 'rel="external"',
  96. ),
  97. '#default_value' => variable_get('location_maplink_external_method', 'target="_blank"'),
  98. '#description' => t(
  99. '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"'
  100. ),
  101. );
  102. return system_settings_form($form);
  103. }
  104. /**
  105. * Settings page for map links.
  106. */
  107. function location_map_link_options_form($form, &$form_state) {
  108. $form = array();
  109. $form['countries'] = array(
  110. '#type' => 'markup',
  111. '#markup' => ''
  112. );
  113. foreach (_location_supported_countries() as $country_iso => $country_name) {
  114. location_load_country($country_iso);
  115. $form['countries'][$country_iso] = array(
  116. '#type' => 'markup',
  117. '#markup' => ''
  118. );
  119. $form['countries'][$country_iso]['label_' . $country_iso] = array(
  120. '#type' => 'markup',
  121. '#markup' => $country_name
  122. );
  123. // Set up '#options' array for mapping providers for the current country.
  124. $mapping_options = array();
  125. $provider_function = 'location_map_link_' . $country_iso . '_providers';
  126. $default_provider_function = 'location_map_link_' . $country_iso . '_default_providers';
  127. // Default providers will be taken from the country specific default providers
  128. // function if it exists, otherwise it will use the global function.
  129. $checked = variable_get(
  130. 'location_map_link_' . $country_iso,
  131. function_exists($default_provider_function) ? $default_provider_function() : location_map_link_default_providers()
  132. );
  133. // Merge the global providers with country specific ones so that countries
  134. // can add to or override the defaults.
  135. $providers = function_exists($provider_function) ? array_merge(
  136. location_map_link_providers(),
  137. $provider_function()
  138. ) : location_map_link_providers();
  139. foreach ($providers as $name => $details) {
  140. $mapping_options[$name] = '<a href="' . $details['url'] . '">' . $details['name'] . '</a> (<a href="' . $details['tos'] . '">Terms of Use</a>)';
  141. }
  142. if (count($mapping_options)) {
  143. $form['countries'][$country_iso]['location_map_link_' . $country_iso] = array(
  144. '#title' => '',
  145. '#type' => 'checkboxes',
  146. '#default_value' => $checked,
  147. '#options' => $mapping_options,
  148. );
  149. }
  150. else {
  151. $form['countries'][$country_iso]['location_map_link_' . $country_iso] = array(
  152. '#type' => 'markup',
  153. '#markup' => t('None supported.'),
  154. );
  155. }
  156. }
  157. $form = system_settings_form($form);
  158. $form['#theme'] = 'location_map_link_options';
  159. return $form;
  160. }
  161. /**
  162. * Options form.
  163. */
  164. function location_geocoding_options_form($form, &$form_state) {
  165. $form = array();
  166. $form['location_geocode_google_minimum_accuracy'] = array(
  167. '#type' => 'select',
  168. '#title' => t('Google Maps geocoding minimum accuracy'),
  169. '#options' => location_google_geocode_accuracy_codes(),
  170. '#default_value' => variable_get('location_geocode_google_minimum_accuracy', '3'),
  171. '#description' => t(
  172. '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.',
  173. array('!accuracy_values_link' => '<a href="http://code.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy">description of these values</a>')
  174. )
  175. );
  176. $form['countries'] = array();
  177. // First, we build two arrays to help us figure out on the fly whether a specific country is covered by a multi-country geocoder,
  178. // and what the details of the multi-country geocoder are
  179. // (1) Get list of geocoders.
  180. $general_geocoders_list = location_get_general_geocoder_list();
  181. // (2) get data about each geocoder and the list of coutnries covered by each geocoder.
  182. $general_geocoders_data = array();
  183. $general_geocoders_countries = array();
  184. foreach ($general_geocoders_list as $geocoder_name) {
  185. location_load_geocoder($geocoder_name);
  186. $info_function = $geocoder_name . '_geocode_info';
  187. if (function_exists($info_function)) {
  188. $general_geocoders_data[$geocoder_name] = $info_function();
  189. }
  190. $countries_function = $geocoder_name . '_geocode_country_list';
  191. if (function_exists($countries_function)) {
  192. $general_geocoders_countries[$geocoder_name] = $countries_function();
  193. }
  194. }
  195. foreach (_location_supported_countries() as $country_iso => $country_name) {
  196. location_load_country($country_iso);
  197. $geocoding_options = array();
  198. $form['countries'][$country_iso] = array(
  199. '#type' => 'markup',
  200. '#markup' => '',
  201. );
  202. $form['countries'][$country_iso]['label_' . $country_iso] = array(
  203. '#type' => 'markup',
  204. '#markup' => '<div id="' . $country_iso . '">' . $country_name . '</div>',
  205. );
  206. // Next, we look for options presented by country specific providers.
  207. $country_specific_provider_function = 'location_geocode_' . $country_iso . '_providers';
  208. if (function_exists($country_specific_provider_function)) {
  209. foreach ($country_specific_provider_function() as $name => $details) {
  210. $geocoding_options[$name . '|' . $country_iso] = '<a href="' . $details['url'] . '">' . $details['name'] . '</a> (<a href="' . $details['tos'] . '">Terms of Use</a>)';
  211. }
  212. }
  213. foreach ($general_geocoders_list as $geocoder_name) {
  214. if (in_array($country_iso, $general_geocoders_countries[$geocoder_name])) {
  215. $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>)';
  216. }
  217. }
  218. if (count($geocoding_options)) {
  219. $geocoding_options = array_merge(array('none' => t('None')), $geocoding_options);
  220. $form['countries'][$country_iso]['location_geocode_' . $country_iso] = array(
  221. '#type' => 'radios',
  222. '#default_value' => variable_get('location_geocode_' . $country_iso, 'none'),
  223. '#options' => $geocoding_options,
  224. );
  225. }
  226. else {
  227. $form['countries'][$country_iso]['location_geocode_' . $country_iso] = array(
  228. '#type' => 'markup',
  229. '#markup' => t('None supported.'),
  230. );
  231. }
  232. $current_value = variable_get('location_geocode_' . $country_iso, 'none');
  233. if ($current_value == 'none') {
  234. $form['countries'][$country_iso]['location_geocode_config_link_' . $country_iso] = array(
  235. '#type' => 'markup',
  236. '#markup' => t('No service selected for country.'),
  237. );
  238. }
  239. else {
  240. $current_val_chopped = substr($current_value, 0, strpos($current_value, '|'));
  241. $geocode_settings_form_function_specific = 'location_geocode_' . $country_iso . '_' . $current_val_chopped . '_settings';
  242. $geocode_settings_form_function_general = $current_value . '_geocode_settings';
  243. if (function_exists($geocode_settings_form_function_specific)) {
  244. $form['countries'][$country_iso]['location_geocode_config_link_' . $country_iso] = array(
  245. '#type' => 'link',
  246. '#title' => t('Configure parameters'),
  247. '#href' => 'admin/config/content/location/geocoding/' . $country_iso . '/' . $current_val_chopped,
  248. );
  249. }
  250. elseif (function_exists($geocode_settings_form_function_general)) {
  251. $form['countries'][$country_iso]['location_geocode_config_link_' . $country_iso] = array(
  252. '#type' => 'link',
  253. '#title' => t('Configure parameters'),
  254. '#href' => 'admin/config/content/location/geocoding/' . $country_iso . '/' . $current_value,
  255. );
  256. }
  257. else {
  258. $form['countries'][$country_iso]['location_geocode_config_link_' . $country_iso] = array(
  259. '#type' => 'markup',
  260. '#markup' => t('No configuration necessary for selected service.'),
  261. );
  262. }
  263. }
  264. }
  265. $form = system_settings_form($form);
  266. $form['#theme'] = 'location_geocoding_options';
  267. array_unshift($form['#submit'], 'location_geocoding_options_form_submit');
  268. return $form;
  269. }
  270. /**
  271. * Submit callback.
  272. */
  273. function location_geocoding_options_form_submit($form, &$form_state) {
  274. $general_geocoders = location_get_general_geocoder_list();
  275. $general_geocoders_in_use = array();
  276. foreach ($form_state['values'] as $key => $value) {
  277. if (substr($key, 0, 17) == 'location_geocode_' && $key != 'location_geocode_google_minimum_accuracy') {
  278. if (in_array($value, $general_geocoders)) {
  279. $general_geocoders_in_use[$value] = $value;
  280. variable_set($key, $value);
  281. }
  282. }
  283. }
  284. variable_set(
  285. 'location_geocode_google_minimum_accuracy',
  286. $form_state['values']['location_geocode_google_minimum_accuracy']
  287. );
  288. variable_set('location_general_geocoders_in_use', $general_geocoders_in_use);
  289. }
  290. /**
  291. * Theme callback.
  292. */
  293. function theme_location_map_link_options($variables) {
  294. $form = $variables['form'];
  295. $header = array(
  296. array('align' => 'center', 'data' => '<center>' . t('Country') . '</center>'),
  297. array('align' => 'center', 'data' => '<center>' . t('Options') . '</center>')
  298. );
  299. $rows = array();
  300. foreach (element_children($form['countries']) as $country_iso) {
  301. $row = array();
  302. $row[] = array(
  303. 'data' => drupal_render($form['countries'][$country_iso]['label_' . $country_iso]),
  304. );
  305. $row[] = array(
  306. 'data' => drupal_render($form['countries'][$country_iso]['location_map_link_' . $country_iso]),
  307. );
  308. $rows[] = $row;
  309. }
  310. $output = theme('table', array('header' => $header, 'rows' => $rows));
  311. $output .= drupal_render_children($form);
  312. return $output;
  313. }
  314. /**
  315. * Theme callback.
  316. */
  317. function theme_location_geocoding_options($variables) {
  318. $form = $variables['form'];
  319. $output = drupal_render($form['location_geocode_google_minimum_accuracy']);
  320. $header = array(
  321. array('align' => 'center', 'data' => '<center>' . t('Country') . '</center>'),
  322. array('align' => 'center', 'data' => '<center>' . t('Options') . '</center>'),
  323. array('align' => 'center', 'data' => '<center>' . t('Configure') . '</center>')
  324. );
  325. $rows = array();
  326. foreach (element_children($form['countries']) as $country_iso) {
  327. $row = array();
  328. $row[] = array(
  329. 'data' => drupal_render($form['countries'][$country_iso]['label_' . $country_iso])
  330. );
  331. $row[] = array(
  332. 'data' => drupal_render($form['countries'][$country_iso]['location_geocode_' . $country_iso])
  333. );
  334. $row[] = array(
  335. 'data' => drupal_render($form['countries'][$country_iso]['location_geocode_config_link_' . $country_iso])
  336. );
  337. $rows[] = $row;
  338. }
  339. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  340. $output .= drupal_render_children($form);
  341. return $output;
  342. }
  343. /**
  344. * Location Utilities form.
  345. */
  346. function location_util_form($form, &$form_state) {
  347. $form['province_clear'] = array(
  348. '#type' => 'fieldset',
  349. '#title' => t('Clear province cache'),
  350. '#description' => t(
  351. 'If you have modified location.xx.inc files, you will need to clear the province cache to get Location to recognize the modifications.'
  352. ),
  353. );
  354. $form['supported_countries_clear'] = array(
  355. '#type' => 'fieldset',
  356. '#title' => t('Clear supported country list'),
  357. '#description' => t(
  358. '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.'
  359. ),
  360. );
  361. $form['actions'] = array(
  362. '#type' => 'actions',
  363. );
  364. $form['actions']['province_clear_submit'] = array(
  365. '#type' => 'submit',
  366. '#value' => t('Clear province cache'),
  367. '#submit' => array('location_util_form_clear_province_cache_submit'),
  368. );
  369. $form['actions']['supported_countries_clear_submit'] = array(
  370. '#type' => 'submit',
  371. '#value' => t('Clear supported country list'),
  372. '#submit' => array('location_util_form_clear_supported_countries_submit'),
  373. );
  374. return $form;
  375. }
  376. /**
  377. * Location utilities form: Clear province cache.
  378. */
  379. function location_util_form_clear_province_cache_submit() {
  380. drupal_set_message(t('Location province cache cleared.'));
  381. cache_clear_all('provinces:', 'cache_location', TRUE);
  382. }
  383. /**
  384. * Location utilities form: Clear supported countries cache.
  385. */
  386. function location_util_form_clear_supported_countries_submit() {
  387. drupal_set_message(t('Location supported country list cleared.'));
  388. cache_clear_all('location:supported-countries', 'cache_location');
  389. }