diff --git a/browscap.admin.inc b/browscap.admin.inc index 9ff0e13..3c66cc0 100644 --- a/browscap.admin.inc +++ b/browscap.admin.inc @@ -5,12 +5,21 @@ */ /** - * Callback for settings form. - * Turn monitoring on or off and refresh the reference data. + * Implementation of hook_form(). */ -function browscap_settings() { +function browscap_settings_form(&$form_state) { + $version = variable_get('browscap_version', 0); $form['browscap_data_status'] = array( - '#markup' => '
' . t('Current browscap data version: %fileversion.', array('%fileversion' => $version ? $version : t('Never fetched'))) . '
', + '#value' => '' . t('Current browscap data version: %fileversion.', array('%fileversion' => $version ? $version : t('Never fetched'))) . '
', + ); + $form['browscap_update_interval'] = array( + '#type' => 'textfield', + '#title' => t('Update interval'), + '#default_value' => variable_get('browscap_update_interval', 7), + '#description' => t('Control how often Browscap checks for new browser information. Enter 0 to disable updating.'), + '#field_suffix' => t('days'), + '#size' => 3, + '#maxlength' => 3, ); $form['browscap_monitor'] = array( '#type' => 'checkbox', @@ -18,27 +27,36 @@ function browscap_settings() { '#default_value' => variable_get('browscap_monitor', FALSE), '#description' => t('Monitor all user agents visiting the site. View the reports in the Browscap reports area.', array('!reports' => url('admin/reports/browscap'))), ); - $form['save'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - $form['refresh'] = array( + $form['buttons']['browscap_refresh'] = array( '#type' => 'submit', '#value' => t('Refresh browscap data'), + '#submit' => array('browscap_refresh_submit'), + '#weight' => 10, ); - return $form; + return system_settings_form($form); } /** - * Submit handler for settings page. + * Validate the settings form. */ -function browscap_settings_submit($form, &$form_state) { - switch ($form_state['clicked_button']['#id']) { - case 'edit-save': - variable_set('browscap_monitor', $form_state['values']['browscap_monitor']); - break; - case 'edit-refresh': - _browscap_import(FALSE); - break; +function browscap_settings_form_validate($form, &$form_state) { + $update_interval = $form_state['values']['browscap_update_interval']; + + // Ensure that the update interval is a number + if (!is_numeric($update_interval)) { + form_set_error('browscap_update_interval', t('The update interval must be a number.')); + } + + // Ensure that the update interval is a non-negative number + if ($update_interval < 0) { + form_set_error('browscap_update_interval', t('The update interval must be non-negative.')); } } + +/** + * Submit handler for the refresh browscap button. + */ +function browscap_refresh_submit($form, &$form_state) { + _browscap_import(FALSE); + variable_set('browscap_imported', REQUEST_TIME); +} diff --git a/browscap.install b/browscap.install index 653d08f..1d37baf 100644 --- a/browscap.install +++ b/browscap.install @@ -106,8 +106,9 @@ function browscap_schema() { */ function browscap_uninstall() { drupal_uninstall_schema('browscap'); - variable_del('browscap_monitor'); variable_del('browscap_imported'); + variable_del('browscap_monitor'); + variable_del('browscap_update_interval'); variable_del('browscap_version'); } diff --git a/browscap.module b/browscap.module index 44377fc..e6e6e7c 100644 --- a/browscap.module +++ b/browscap.module @@ -55,7 +55,7 @@ function browscap_menu() { 'title' => 'Browscap', 'description' => 'Enable browscap site statistics.', 'page callback' => 'drupal_get_form', - 'page arguments' => array('browscap_settings'), + 'page arguments' => array('browscap_settings_form'), 'access arguments' => array('administer browscap'), 'file' => 'browscap.admin.inc', ); @@ -100,9 +100,15 @@ function browscap_exit() { * Implementation of hook_cron(). */ function browscap_cron() { - // Has it been a week since the last (attempt to) import? - $last_imported = variable_get('browscap_imported', 0); - if (($last_imported + 60*60*24*7) < time()) { + // How often should a check for updates occur (in days)? + $update_interval = variable_get('browscap_update_interval', 7); + + // Convert the update interval from days to seconds + $update_interval = $update_interval * 7 * 24 * 60 * 60; + + + // Has it been long enough since the last update was attempted? + if (($last_imported + $update_interval) < REQUEST_TIME) { _browscap_import(); variable_set('browscap_imported', time()); }