geocoder.admin.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * Settings form.
  5. */
  6. /**
  7. * Module settings page.
  8. */
  9. function geocoder_admin_settings($form, &$form_state) {
  10. $geocoder_settings= variable_get("geocoder_settings", array());
  11. $form['geocoder_apikey_yahoo'] = array(
  12. '#type' => 'textfield',
  13. '#title' => t('Yahoo PlaceFinder API Key'),
  14. '#description' => t('You can obtain a Yahoo PlaceFinder consumer key at') . ' ' . 'http://developer.yahoo.com/geo/placefinder/',
  15. '#default_value' => empty($geocoder_settings['geocoder_apikey_yahoo']) ? '' : $geocoder_settings['geocoder_apikey_yahoo'],
  16. '#required' => FALSE,
  17. );
  18. $form['geocoder_apikey_yandex'] = array(
  19. '#type' => 'textfield',
  20. '#title' => t('Yandex Maps API Key'),
  21. '#description' => t('You can obtain a Yandex API Key at ') . 'http://api.yandex.ru/maps/getkey.xml',
  22. '#default_value' => empty($geocoder_settings['geocoder_apikey_yandex']) ? '' : $geocoder_settings['geocoder_apikey_yandex'],
  23. '#required' => FALSE,
  24. );
  25. $form['geocoder_apikey_bing'] = array(
  26. '#type' => 'textfield',
  27. '#title' => t('Bing API Key'),
  28. '#description' => t('You can obtain a Bing API Key at ') . 'http://msdn.microsoft.com/en-us/library/ff428642.aspx',
  29. '#default_value' => empty($geocoder_settings['geocoder_apikey_bing']) ? '' : $geocoder_settings['geocoder_apikey_bing'],
  30. '#required' => FALSE,
  31. );
  32. $form['geocoder_google_auth_method'] = array(
  33. '#type' => 'select',
  34. '#title' => t('Google API Authorization Method'),
  35. '#description' => t("If your website runs on shared hosting, you'll want to authenticate requests to the Google Geocoding API to reduce the likelihood of being rate limited (2500 requests per day / 5 requests per second). Alternatively, Google Maps for Work customers may use their Client ID and Signing Key to authenticate."),
  36. '#default_value' => variable_get('geocoder_google_auth_method', GEOCODER_GOOGLE_AUTH_NONE),
  37. '#options' => array(
  38. GEOCODER_GOOGLE_AUTH_NONE => 'None',
  39. GEOCODER_GOOGLE_AUTH_KEY => 'API Key (free)',
  40. GEOCODER_GOOGLE_AUTH_WORK => 'Google Maps API for Work',
  41. ),
  42. );
  43. $form['geocoder_apikey_google'] = array(
  44. '#type' => 'textfield',
  45. '#title' => t('Google Maps API Key'),
  46. '#description' => t('Obtain a free Google Geocoding API Key at <a href="@link">@link</a>', array(
  47. '@link' => 'https://developers.google.com/maps/documentation/geocoding/#api_key',
  48. )),
  49. '#default_value' => empty($geocoder_settings['geocoder_apikey_google']) ? '' : $geocoder_settings['geocoder_apikey_google'],
  50. '#required' => FALSE,
  51. '#states' => array(
  52. 'visible' => array(
  53. ':input[name="geocoder_google_auth_method"]' => array('value' => GEOCODER_GOOGLE_AUTH_KEY),
  54. ),
  55. ),
  56. );
  57. $form['geocoder_google_client_id'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('Google Maps API for Work: Client ID'),
  60. '#description' => t('For more information, visit <a href="@link">@link</a>', array(
  61. '@link' => 'https://developers.google.com/maps/documentation/business/webservices/auth#business-specific_parameters',
  62. )),
  63. '#default_value' => variable_get('geocoder_google_client_id'),
  64. '#required' => FALSE,
  65. '#states' => array(
  66. 'visible' => array(
  67. ':input[name="geocoder_google_auth_method"]' => array(
  68. 'value' => GEOCODER_GOOGLE_AUTH_WORK,
  69. ),
  70. ),
  71. ),
  72. );
  73. $form['geocoder_google_private_key'] = array(
  74. '#type' => 'textfield',
  75. '#title' => t('Google Maps API for Work: Private/Signing Key'),
  76. '#description' => t('For more information, visit <a href="@link">@link</a>', array(
  77. '@link' => 'https://developers.google.com/maps/documentation/business/webservices/auth#how_do_i_get_my_signing_key',
  78. )),
  79. '#default_value' => variable_get('geocoder_google_private_key'),
  80. '#required' => FALSE,
  81. '#states' => array(
  82. 'visible' => array(
  83. ':input[name="geocoder_google_auth_method"]' => array(
  84. 'value' => GEOCODER_GOOGLE_AUTH_WORK,
  85. ),
  86. ),
  87. ),
  88. );
  89. $form['geocoder_google_delay'] = array(
  90. '#type' => 'textfield',
  91. '#title' => t('Delay between Google geocoding requests (in milliseconds)'),
  92. '#description' => t('Adds a delay between geocoding requests, to avoid OVER_QUERY_LIMIT errors from Google. 200ms is recommended.'),
  93. '#default_value' => variable_get('geocoder_google_delay', 0),
  94. '#size' => 10,
  95. );
  96. $form['geocoder_cache_empty_results'] = array(
  97. '#type' => 'checkbox',
  98. '#title' => t('Cache empty results'),
  99. '#default_value' => variable_get('geocoder_cache_empty_results', TRUE),
  100. '#description' => t('Geocoder caches all queries by default. Do you want that to include the ones with no results? If not, it will be checked every time, probably with the same no-result.'),
  101. );
  102. $form['#submit'][] = 'geocoder_admin_settings_submit';
  103. return system_settings_form($form);
  104. }
  105. function geocoder_admin_settings_validate($form_id, $form_values) {
  106. if (!empty($form_values['values']['geocoder_apikey_yahoo']) && preg_match("/[^A-Za-z0-9\\-]/", trim($form_values['values']['geocoder_apikey_yahoo']))) {
  107. form_set_error('geocoder_apikey_yahoo', t('Yahoo API keys are alpha numeric and dashes only.'));
  108. }
  109. }
  110. function geocoder_admin_settings_submit($form, &$form_state) {
  111. $geocoder_settings= variable_get("geocoder_settings", array());
  112. $geocoder_settings['geocoder_apikey_yahoo'] = trim($form_state['values']['geocoder_apikey_yahoo']);
  113. $geocoder_settings['geocoder_apikey_yandex'] = trim($form_state['values']['geocoder_apikey_yandex']);
  114. $geocoder_settings['geocoder_apikey_bing'] = trim($form_state['values']['geocoder_apikey_bing']);
  115. $geocoder_settings['geocoder_apikey_google'] = trim($form_state['values']['geocoder_apikey_google']);
  116. variable_set("geocoder_settings", $geocoder_settings);
  117. }