| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_settings
|
| 6 |
*/
|
| 7 |
function flickr_admin_settings() {
|
| 8 |
$form['flickr_api_key'] = array(
|
| 9 |
'#type' => 'textfield',
|
| 10 |
'#title' => t('API Key'),
|
| 11 |
'#required' => TRUE,
|
| 12 |
'#default_value' => variable_get('flickr_api_key', ''),
|
| 13 |
'#description' => t('API Key from Flickr'),
|
| 14 |
);
|
| 15 |
$form['flickr_api_secret'] = array(
|
| 16 |
'#type' => 'textfield',
|
| 17 |
'#title' => t('API Shared Secret'),
|
| 18 |
'#required' => TRUE,
|
| 19 |
'#default_value' => variable_get('flickr_api_secret', ''),
|
| 20 |
'#description' => t("API key's secret from Flickr."),
|
| 21 |
);
|
| 22 |
$form['flickr_default_userid'] = array(
|
| 23 |
'#type' => 'textfield',
|
| 24 |
'#title' => t('Default Flickr User Id'),
|
| 25 |
'#default_value' => variable_get('flickr_default_userid', ''),
|
| 26 |
'#description' => t("An, optional, default Flickr username or user id. This will be used when no user is specified."),
|
| 27 |
);
|
| 28 |
$times = array(900, 1800, 2700, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 86400);
|
| 29 |
$ageoptions = drupal_map_assoc($times, 'format_interval');
|
| 30 |
$form['flickr_cache_duration'] = array(
|
| 31 |
'#type' => 'select',
|
| 32 |
'#title' => t('Update interval'),
|
| 33 |
'#options' => $ageoptions,
|
| 34 |
'#default_value' => variable_get('flickr_cache_duration', 3600),
|
| 35 |
'#description' => t("The refresh interval indicating how often you want to check cached Flickr API calls are up to date."),
|
| 36 |
);
|
| 37 |
|
| 38 |
// we need an api key before we can verify usernames
|
| 39 |
if (!$form['flickr_api_key']['#default_value']) {
|
| 40 |
$form['flickr_default_userid']['#disabled'] = TRUE;
|
| 41 |
$form['flickr_default_userid']['#description'] .= t(" Disabled until a valid API Key is set.");
|
| 42 |
}
|
| 43 |
|
| 44 |
return system_settings_form($form);
|
| 45 |
}
|
| 46 |
|
| 47 |
function flickr_admin_settings_validate($form, &$form_state) {
|
| 48 |
$key = trim($form_state['values']['flickr_api_key']);
|
| 49 |
$sec = trim($form_state['values']['flickr_api_secret']);
|
| 50 |
$uid = trim($form_state['values']['flickr_default_userid']);
|
| 51 |
|
| 52 |
if ($key && (preg_match('/^[A-Fa-f\d]{32}$/', $key) != 1)) {
|
| 53 |
form_set_error('flickr_api_key', t('This does not appear to be a Flickr API key.'));
|
| 54 |
}
|
| 55 |
if ($sec && (preg_match('/^[A-Fa-f\d]{16}$/', $sec) != 1)) {
|
| 56 |
form_set_error('flickr_api_secret', t('This does not appear to be a Flickr API secret.'));
|
| 57 |
}
|
| 58 |
if ($uid) {
|
| 59 |
if (flickr_is_nsid($uid)) {
|
| 60 |
// it's already a uid
|
| 61 |
}
|
| 62 |
else {
|
| 63 |
$user = flickr_user_find_by_username($uid);
|
| 64 |
if (!$user) {
|
| 65 |
form_set_error('flickr_default_userid', t('%uid is not a Flickr user id and it does not appear to be a valid user name.', array('%uid' => $uid)));
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
function flickr_admin_settings_submit($form, &$form_state) {
|
| 72 |
// clean up the data ...
|
| 73 |
$form_state['values']['flickr_api_key'] = trim($form_state['values']['flickr_api_key']);
|
| 74 |
$form_state['values']['flickr_api_secret'] = trim($form_state['values']['flickr_api_secret']);
|
| 75 |
$form_state['values']['flickr_default_userid'] = trim($form_state['values']['flickr_default_userid']);
|
| 76 |
|
| 77 |
// ... replace the usernames with a user id ...
|
| 78 |
if (!flickr_is_nsid($form_state['values']['flickr_default_userid'])) {
|
| 79 |
$username = $form_state['values']['flickr_default_userid'];
|
| 80 |
if ($user = flickr_user_find_by_username($username)) {
|
| 81 |
drupal_set_message(t("The Flickr username %username has been replaced with the corresponding user id %uid.", array('%username' => $form_state['values']['flickr_default_userid'], '%uid' => $user['id'])));
|
| 82 |
$form_state['values']['flickr_default_userid'] = $user['id'];
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
// ... and save the settings
|
| 87 |
system_settings_form_submit($form, &$form_state);
|
| 88 |
}
|
| 89 |
|