Commit 2d2ff2a9 authored by lyricnz's avatar lyricnz
Browse files

Issue #1094734: simplify configuration

parent fee9c7a7
......@@ -9,6 +9,39 @@
* Admin Settings Page
*/
function geonames_admin_settings() {
$form['geonames_server'] = array(
'#type' => 'textfield',
'#title' => t('URL to Geonames server to use.'),
'#description' => t('Example: http://ws.geonames.net (paid service) or http://api.geonames.org (free service) - without the trailing slash. You should always support developers of quality services. Increased performance is cheap, and you should afford it. Visit the !link page for more information', array('!link' => l(t('GeoNames Premium Webservices'), 'http://www.geonames.org/professional-webservices.html'))),
'#default_value' => variable_get('geonames_server', 'http://api.geonames.net'),
);
$form['geonames_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#description' => t('This is *required* for commercial and free services. You can !register.', array('!register' => l('register a GeoNames account', 'http://www.geonames.org/login'))),
'#default_value' => variable_get('geonames_username', ''),
);
$form['geonames_count_credits'] = array(
'#type' => 'radios',
'#title' => t('Count credits used when calling the service'),
'#default_value' => variable_get('geonames_count_credits', 0),
'#options' => array(1 => 'Enabled', 0 => 'Disabled'),
);
if (variable_get('geonames_count_credits', 0)) {
// Show the Status and Credits Counter (ick)
$since = variable_get('geonames_commercial_credits_since');
$tvalues = array(
'@credits' => variable_get('geonames_commercial_credits', 0),
'@date' => $since ? format_date($since) : 'unknown',
'!link' => l(t('Reset'), 'admin/settings/geonames/reset_credits'),
);
$form['counter'] = array(
'#type' => 'item',
'#title' => 'Credits',
'#markup' => t('You have used @credits credits since @date. [!link]', $tvalues),
);
}
$form['geonames_docs_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to GeoNames Documentation'),
......@@ -49,52 +82,6 @@ function geonames_admin_settings() {
'#options' => array(1 => 'Yes', 0 => 'No'),
);
$commercial_collapsed = (variable_get('geonames_commercial_active', FALSE) == TRUE) ? FALSE : TRUE;
$form['commercial'] = array(
'#type' => 'fieldset',
'#title' => t('GeoNames Commercial Webservices'),
'#collapsible' => TRUE,
'#collapsed' => $commercial_collapsed
);
$form['commercial']['leadingtext'] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#markup' => t('You should always support developers of quality services. Increased performance is cheap, and you should afford it. Visit the !link page for more information', array('!link' => l(t('GeoNames Commercial Webservices'), 'http://www.geonames.org/professional-webservices.html'))),
'#suffix' => '</div>'
);
if (variable_get('geonames_commercial_active', FALSE) == TRUE) {
// Show the Status and Credits Counter (ick)
$since = variable_get('geonames_commercial_credits_since', 0);
$form['commercial']['counter'] = array(
'#type' => 'markup',
'#prefix' => '<div><p>',
'#markup' => '<b>' . t('Status') . ':</b><br />' . t('You have used @credits credits since %date. [!link]', array(
'@credits' => variable_get('geonames_commercial_credits', 0),
'%date' => $since ? format_date($since) : t('Unknown date'),
'!link' => l(t('Reset'), 'admin/config/geonames/reset_credits'),
)),
'#suffix' => '</p></div>'
);
}
$form['commercial']['geonames_commercial_active'] = array(
'#type' => 'radios',
'#title' => t('GeoNames Commercial Webservices'),
'#default_value' => variable_get('geonames_commercial_active', 0),
'#options' => array(1 => 'Enabled', 0 => 'Disabled'),
);
$form['commercial']['geonames_commercial_server'] = array(
'#type' => 'textfield',
'#title' => t('URL to Commercial Service Server'),
'#description' => 'Example: http://ws.geonames.net - without the trailing slash',
'#default_value' => variable_get('geonames_commercial_server', 'http://ws.geonames.net'),
);
$form['commercial']['geonames_commercial_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('geonames_commercial_username', ''),
);
$updated = variable_get('geonames_metadata_updated', 0);
$values = array(
'!numcountries' => db_query('SELECT COUNT(*) FROM {geonames_countryinfo}')->fetchField(),
......@@ -151,45 +138,24 @@ function geonames_admin_settings() {
* Admin Settings Page : Validation
*/
function geonames_admin_settings_validate($form, &$form_state) {
$geonames_commercial_active = $form_state['values']['geonames_commercial_active'];
$geonames_commercial_server = $form_state['values']['geonames_commercial_server'];
$geonames_commercial_username = $form_state['values']['geonames_commercial_username'];
// have Commercial Services Changed?
$c = $form['commercial'];
$com_changed = $geonames_commercial_active != $c['geonames_commercial_active']['#default_value'];
$com_changed = $com_changed || ($geonames_commercial_server != $c['geonames_commercial_server']['#default_value']);
$com_changed = $com_changed || ($geonames_commercial_username != $c['geonames_commercial_username']['#default_value']);
// if active and changed, validate
if ($geonames_commercial_active && $com_changed) {
// verify that a username is set
if ($geonames_commercial_username == '') {
form_set_error('geonames_commercial_username', t('Commercial Webservices: You must provide a username.'));
// validate that the given settings work
$url = $form_state['values']['geonames_server'] .'/search?username='. $form_state['values']['geonames_username'] .'&name=nydalen';
$data = drupal_http_request($url);
if ($data->code != '200') {
form_set_error('', t('There is a problem with the response from the URL you have specified.') .'<br />'. t('The server returned errorcode %code: %error', array('%code' => $data->code, '%error' => $data->error)));
}
else {
$xml = new SimpleXMLElement($data->data);
if ($xml->status['message']) {
form_set_error('', t('GeoNames Service Response: %message (code: %code)', array('%message' => $xml->status['message'], '%code' => $xml->status['value'])));
}
else {
// verify if the service works
// Test using the search service
$url = $geonames_commercial_server . '/search?username=' . $geonames_commercial_username . '&name=nydalen';
$data = drupal_http_request($url);
if ($data->code != '200') {
form_set_error('geonames_commercial_server', t('There is a problem with the response from the URL you have specified.') . '<br />' . t('The server returned errorcode %code: %error', array('%code' => $data->code, '%error' => $data->error)));
geonames_credits_pay(1); // use one credit! ;)
if ($xml->geoname[0]->name == 'Nydalen') {
drupal_set_message(t('Your account has been successfully tested and is properly configured!'));
}
else {
$xml = new SimpleXMLElement($data->data);
if ($xml->status['message']) {
form_set_error('geonames_commercial_server', t('GeoNames Service Response: %message (code: %code)', array('%message' => $xml->status['message'], '%code' => $xml->status['value'])));
}
else {
geonames_credits_pay(1); // use one credit! ;)
if ($xml->geoname[0]->name == 'Nydalen') {
drupal_set_message(t('Your account has been successfully tested and is properly configured!'));
}
else {
form_set_error('', t('Unknown Error'));
}
}
form_set_error('', t('Unknown Error'));
}
}
}
......@@ -217,4 +183,3 @@ function geonames_credits_reset($adminpage = TRUE) {
drupal_goto('admin/config/geonames');
}
}
......@@ -258,4 +258,29 @@ function geonames_update_7000() {
drupal_set_message(t('Cache table %table may no longer be used', array('%table' => $geonames_cache_table)), 'warning');
}
variable_del('geonames_cache_table');
}
/**
* Simplify configuration relating to commercial web-services.
*/
function geonames_update_7001() {
// Now there are just explicit settings for the URL to use, whether
// to count credits, and which username to use. For the new servers
// at api.geonames.org username is *required*.
$commercial_active = variable_get('geonames_commercial_active', FALSE);
$server = $commercial_active ? variable_get('geonames_commercial_server', '') : GEONAMES_FREE_SERVER_URL;
$username = variable_get('geonames_commercial_username', '');
if ($username === '') {
$link = l('GeoNames settings', 'admin/config/geonames');
drupal_set_message('Geonames server username is not set. Please configure this at '. $link, 'warning');
}
variable_set('geonames_server', $server);
variable_set('geonames_count_credits', $commercial_active);
variable_set('geonames_username', $username);
variable_del('geonames_commercial_active');
variable_del('geonames_commercial_server');
variable_del('geonames_commercial_username');
}
\ No newline at end of file
......@@ -29,7 +29,7 @@
* Constants
* hrmpf...
*/
DEFINE('GEONAMES_FREE_SERVER_URL', 'http://ws.geonames.org');
DEFINE('GEONAMES_FREE_SERVER_URL', 'http://api.geonames.org');
// require DRUPAL_ROOT . '/' . "geonames_config.inc";
......@@ -417,7 +417,7 @@ function geonames_query($service, $query = array(), $options = NULL) {
}
$data = $response->data;
// increase credit counter if commercial server is used
if (variable_get('geonames_commercial_active', FALSE)) {
if (variable_get('geonames_count_credits', FALSE)) {
geonames_credits_pay($service);
}
if ($geonames_cache && $columns) {
......@@ -469,12 +469,9 @@ function geonames_query($service, $query = array(), $options = NULL) {
function geonames_service_url($service) {
$service_path = geonames_config($service, 'service_path');
if ($service_path) {
if (variable_get('geonames_commercial_active', FALSE)) {
return variable_get('geonames_commercial_server', '') . '/' . $service_path . '?username=' . variable_get('geonames_commercial_username', '') . '&';
}
else {
return GEONAMES_FREE_SERVER_URL . '/' . $service_path . '?';
}
$server = variable_get('geonames_server', GEONAMES_FREE_SERVER_URL);
$username = variable_get('geonames_username', '');
return $server .'/'. $service_path .'?username='. $username .'&';
}
return FALSE;
}
......@@ -760,8 +757,11 @@ function geonames_parse_elevation_xml($data) {
* Commercial Service: Increase the Credits Counter ;)
*/
function geonames_credits_pay($service) {
// TODO: using variables is an inefficient way to store the current credts -
// it causes invalidation of the big $variables blob
$cost = (is_numeric($service)) ? $service : geonames_config($service, 'credit_cost');
variable_set('geonames_commercial_credits', $cost + variable_get('geonames_commercial_credits', 0));
variable_set('geonames_commercial_credits_since', REQUEST_TIME);
}
/**
......
......@@ -10,7 +10,7 @@ class GeoNamesTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
public static function getInfo() {
return array(
'name' => t('GeoNames'),
'description' => t('Test basic functionality of GeoNames module - requires internet access.'),
......@@ -21,7 +21,7 @@ class GeoNamesTestCase extends DrupalWebTestCase {
/**
* Implementation of setUp().
*/
function setUp() {
protected function setUp() {
parent::setUp('geonames');
variable_set('geonames_commercial_active', FALSE);
variable_set('geonames_cache', FALSE);
......@@ -30,7 +30,7 @@ class GeoNamesTestCase extends DrupalWebTestCase {
/**
* Check geonames_query() with a simple search.
*/
function testGeoNamesQuery() {
protected function testGeoNamesQuery() {
// Setup configuration for the tests.
variable_set('geonames_cache', TRUE);
variable_set('geonames_reveal_cache', TRUE);
......@@ -81,7 +81,7 @@ class GeoNamesTestCase extends DrupalWebTestCase {
// error_log(print_r($result3, TRUE));
}
function testCacheSorting() {
protected function testCacheSorting() {
variable_set('geonames_cache', TRUE);
variable_set('geonames_reveal_cache', TRUE);
......@@ -117,7 +117,7 @@ class GeoNamesTestCase extends DrupalWebTestCase {
* Check that GeoNames loads country-info and feature-codes metadata from
* Geonames, and returns it to local queries.
*/
function testMetaData() {
protected function testMetaData() {
// Update the MetaData, and check it updated the last-updated value
variable_set('geonames_metadata_updated', 0);
geonames_metadata_update();
......@@ -148,7 +148,7 @@ class GeoNamesTestCase extends DrupalWebTestCase {
$this->assertEqual($info['name'], 'administrative division', 'Found ADMD:administrative division (featurecode)');
}
function testHelperFunctions() {
protected function testHelperFunctions() {
// Check Continent Codes
$codes = geonames_continent_codes();
$this->assertEqual(count($codes), 7, t('Seven continents found'));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment