@link . To create a flickr entry click here", array('@link' => url('http://www.flickr.com/services/api/keys/apply/'),'@link2' => url('admin/flickr/entry/create')));
case 'admin/help#flickr':
return t('The flickr module uses XML-RPC to connect to Flickr\'s API and retreive photo information.');
}
}
function flickr_perm () {
return array(
'view own flickr photos',
'view all flickr photos',
'administer flickr',
);
}
/**
* Implementation of hook_menu().
*/
function flickr_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/flickr', 'title' => t('Flickr'),
'callback' => 'flickr_admin',
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Flickr'));
$items[] = array(
'path' => 'admin/flickr/entries', 'title' => t('Flickr entries'),
'callback' => 'flickr_admin_entries',
'access' => user_access('administer site configuration'),
'type' => MENU_CALLBACK,
'description' => t('Flickr entries.'));
$items[] = array(
'path' => 'admin/flickr/entry/create', 'title' => t('Create a Flickr entry'),
'callback' => 'flickr_admin_entry_create',
'access' => user_access('administer site configuration'),
'type' => MENU_CALLBACK,
'description' => t('Create a Flickr entry.'));
$items[] = array(
'path' => 'admin/settings/flickr', 'title' => t('Flickr'),
'callback' => 'drupal_get_form',
'callback arguments' => array('flickr_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Change settings for the flickr module.'));
$items[] = array(
'path' => 'flickr',
'title' => t('Flickr photos'),
'access' => TRUE,
'type' => MENU_CALLBACK,
'callback' => 'flickr_photos',
'description' => t('Flickr photos of default user id.'));
$items[] = array(
'path' => 'flickr/auth',
'access' => TRUE,
'type' => MENU_CALLBACK,
'callback' => 'flickr_auth_callback');
}
else {
if ( arg(0) == 'admin' && arg(1) == 'flickr' && arg(2) == 'entry' && arg(3) == 'delete' && is_numeric(arg(4)) ) {
$items[] = array(
'path' => 'admin/flickr/entry/delete/'. arg(4),
'title' => t('Are you sure you want to delete this flickr entry?'),
'callback' => 'flickr_admin_entry_delete',
'access' => user_access('administer site configuration'),
'callback arguments' => array(arg(4)),
'type' => MENU_CALLBACK);
}
if ( arg(0) == 'admin' && arg(1) == 'flickr' && arg(2) == 'entry' && arg(3) == 'edit' && is_numeric(arg(4)) ) {
$items[] = array(
'path' => 'admin/flickr/entry/edit/'. arg(4),
'title' => t('Edit a flickr entry.'),
'callback' => 'flickr_admin_entry_edit',
'access' => user_access('administer site configuration'),
'callback arguments' => array(arg(4)),
'type' => MENU_CALLBACK);
}
if (arg(0) == 'flickr' && is_numeric(arg(1)) && arg(1) > 0) {
$account = user_load(array('uid' => arg(1)));
if ($account !== FALSE && isset($account->flickr['nsid'])) {
$nsid = $account->flickr['nsid'];
$admin_access = user_access('administer flickr');
// let a user view their own account or all if they have permission
$view_access |= (user_access('view own flickr photos') && ($user->uid == arg(1))) ||
user_access('view all flickr photos');
// Only admins can view blocked accounts
$view_access &= $account->status || $admin_access;
//main flickr user page(photos)
$items[] = array(
'path' => 'flickr/' . arg(1),
'title' => t("@user's Flickr", array('@user' => $account->name)),
'type' => MENU_CALLBACK,
'callback' => 'flickr_photos',
'callback arguments' => array(arg(1)),
'access' => $view_access,
);
$items[] = array(
'path' => 'flickr/'. arg(1) .'/photos',
'title' => t('Photos'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'access' => $view_access,
);
}
elseif ($account !== FALSE && !isset($account->flickr['nsid'])) {
drupal_set_message(t('%user does not have a Flickr account', array('%user'=>$account->name)), 'error');
}
}
}
return $items;
}
/**
* Implementation of hook_settings
*/
function flickr_admin_settings() {
$form['#validate'] = array('flickr_admin_settings_validate' => array());
$form['flickr_api_key'] = array(
'#type' => 'textfield',
'#title' => t('API Key'),
'#required' => TRUE,
'#default_value' => variable_get('flickr_api_key', ''),
'#description' => t('API Key from Flickr'),
);
$form['flickr_api_secret'] = array(
'#type' => 'textfield',
'#title' => t('API Shared Secret'),
'#required' => TRUE,
'#default_value' => variable_get('flickr_api_secret', ''),
'#description' => t("API key's secret from Flickr."),
);
$form['flickr_default_userid'] = array(
'#type' => 'textfield',
'#title' => t('Default Flickr User Id'),
'#default_value' => variable_get('flickr_default_userid', ''),
'#description' => t("An, optional, default Flickr username or user id. This will be used when no user is specified."),
);
$times = array(900, 1800, 2700, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 86400);
$ageoptions = drupal_map_assoc($times, 'format_interval');
$form['flickr_cache_duration'] = array(
'#type' => 'select',
'#title' => t('Update interval'),
'#options' => $ageoptions,
'#default_value' => variable_get('flickr_cache_duration', 3600),
'#description' => t("The refresh interval indicating how often you want to check cached Flickr API calls are up to date."),
);
// we need an api key before we can verify usernames
if (!$form['flickr_api_key']['#default_value']) {
$form['flickr_default_userid']['#disabled'] = TRUE;
$form['flickr_default_userid']['#description'] .= t(" Disabled until a valid API Key is set.");
}
return system_settings_form($form);
}
function flickr_admin_settings_validate($form_id, $form) {
$key = trim($form['flickr_api_key']);
$sec = trim($form['flickr_api_secret']);
$uid = trim($form['flickr_default_userid']);
if ($key && (preg_match('/^[A-Fa-f\d]{32}$/', $key) != 1)) {
form_set_error('flickr_api_key', t('This does not appear to be a Flickr API key.'));
}
if ($sec && (preg_match('/^[A-Fa-f\d]{16}$/', $sec) != 1)) {
form_set_error('flickr_api_secret', t('This does not appear to be a Flickr API secret.'));
}
if ($uid) {
if (flickr_is_nsid($uid)) {
// it's already a uid
}
else {
$user = flickr_user_find_by_username($uid);
if (!$user) {
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)));
}
}
}
}
function flickr_admin_settings_submit($form_id, $form) {
// clean up the data ...
$form['flickr_api_key'] = trim($form['flickr_api_key']);
$form['flickr_api_secret'] = trim($form['flickr_api_secret']);
$form['flickr_default_userid'] = trim($form['flickr_default_userid']);
// ... replace the usernames with a user id ...
if (!flickr_is_nsid($form['flickr_default_userid'])) {
$username = $form['flickr_default_userid'];
if ($user = flickr_user_find_by_username($username)) {
drupal_set_message(t("The Flickr username %username has been replaced with the corresponding user id %uid.", array('%username' => $form['flickr_default_userid'], '%uid' => $user['id'])));
$form['flickr_default_userid'] = $user['id'];
}
}
// ... and save the settings
system_settings_form_submit($form_id, $form);
}
function theme_flickr_photo($p, $size = NULL, $format = NULL, $attribs = NULL) {
$img = flickr_img($p, $size, $attribs);
$photo_url = flickr_photo_page_url($p['owner'], $p['id']);
$title = is_array($p['title']) ? $p['title']['_content'] : $p['title'];
return l($img, $photo_url, array('title' => $title), NULL, NULL, TRUE, TRUE);
}
function theme_flickr_photo_box($p, $size = NULL, $format = NULL, $attribs = NULL) {
$img = flickr_img($p, $size, $attribs);
$title = is_array($p['title']) ? $p['title']['_content'] : $p['title'];
$photo_url = flickr_photo_page_url($p['owner'], $p['id']);
$output .= "
\n";
return $output;
}
/**
* Implimentation of the hook_user()
* Add an extra field for the user to enter his flickr identifier.
*/
function flickr_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'form') {
if ($category == 'account') {
$user = user_load(array('uid' => $account->uid));
$form['flickr'] = array(
'#type' => 'fieldset',
'#title' => t('Flickr settings'),
'#collapsible' => FALSE,
'#weight' => 4,
'#tree' => FALSE,
);
$form['flickr']['flickr_identifier'] = array(
'#type' => 'textfield',
'#title' => t('Flickr identifier'),
'#default_value' => $user->flickr['identifier'],
'#description' => t('Enter either your Flickr username, the email address associated with your Flickr account, or your Flickr NSID. Leave this box empty to delete your Flickr page on this site.'),
'#maxlength' => 64,
);
return $form;
}
}
elseif ($op == 'validate') {
if (!empty($edit['flickr_identifier'])) {
if (!flickr_user_find_by_identifier($edit['flickr_identifier'])) {
form_set_error('flickr_identifier', t('%ident is not a valid Flickr username, email, or NSID.', array('%ident' => $edit['flickr_identifier'])));
}
}
}
elseif ($op == 'insert' || $op == 'update') {
if (isset($edit['flickr_identifier'])) {
db_query('DELETE FROM {flickr_users} WHERE uid=%d', $account->uid);
if (!empty($edit['flickr_identifier'])) {
db_query("INSERT INTO {flickr_users} (uid, nsid, identifier) VALUES (%d, '%s', '%s')", $account->uid, flickr_user_find_by_identifier($edit['flickr_identifier']), $edit['flickr_identifier']);
}
else {
//flickr account deleted
drupal_set_message(t('%username\'s Flickr page has been deleted.', array('%username'=> $account->name)));
}
}
$edit['flickr_identifier'] = NULL;
}
elseif ($op == 'load') {
$result = db_query('SELECT * FROM {flickr_users} WHERE uid=%d', $account->uid);
if (db_num_rows($result) > 0) {
$flickr_info = db_fetch_object($result);
$account->flickr['identifier'] = $flickr_info->identifier;
$account->flickr['nsid'] = $flickr_info->nsid;
}
}
elseif ($op == 'delete') {
db_query('DELETE FROM {flickr_users} WHERE uid=%d', $account->uid);
}
}
function flickr_photos($uid = NULL) {
drupal_add_css(drupal_get_path('module', 'flickr') .'/flickr.css');
global $pager_page_array, $pager_total, $pager_total_items;
//set this to something else if you want multiple pagers
$element = 0;
$pager_page_array[$element] = $_GET['page'] ? $_GET['page'] : '';
if ($uid === NULL) {
$nsid = variable_get('flickr_default_userid', '');
if (!$nsid) {
drupal_set_message(t('No default Flickr user id has been set.'));
return FALSE;
}
}
else {
$account = user_load(array('uid' => $uid));
if ($account->flickr['nsid']) {
$nsid = $account->flickr['nsid'];
}
else {
drupal_set_message(t('%user does not have a Flickr account', array('%user'=>$account->name)), 'error');
return FALSE;
}
}
$photos = flickr_photos_search($nsid, $pager_page_array[$element]+1);
if (!$photos) {
drupal_set_message(t('No accessible photos found for Flickr %userid', array('%userid' => $nsid)));
return FALSE;
}
//set pager information we just acquired
$pager_total[$element] = $photos['pages'];
$pager_total_items[$element] = $photos['total'];
return theme('flickr_photos', $uid, $photos);
}
function theme_flickr_photos($uid, $photos){
$output = theme('pager', NULL, variable_get('flickr_photos_per_page', 20));
$output .= "\n";
foreach($photos['photo'] as $photo) {
$output .= theme('flickr_photo_box', $photo, 'm');
}
$output .= '
';
$output .= theme('pager', NULL, variable_get('flickr_photos_per_page', 20));
return $output;
}
function theme_flickr_photoset($ps, $owner, $size, $attribs = NULL) {
$img = flickr_img($ps, $size, $attribs);
$photo_url = flickr_photoset_page_url($owner, $ps['id']);
$title = is_array($ps['title']) ? $ps['title']['_content'] : $ps['title'];
return l($img, $photo_url, array('title' => $title), NULL, NULL, TRUE, TRUE);
}
function flickr_admin_entry_create() {
$output = drupal_get_form('flickr_admin_entry_create_form');
return $output;
}
function flickr_admin_entry_create_form() {
$form['#validate'] = array('flickr_admin_entry_create_form_validate' => array());
$form['flickr_entry_name'] = array(
'#type' => 'textfield',
'#title' => t('Flickr entry name'),
'#required' => TRUE,
'#description' => t("Flickr entry name eg . Birthday party. "),
);
$form['flickr_entry_tags'] = array(
'#type' => 'textfield',
'#title' => t('Flickr tags'),
'#required' => FALSE,
'#description' => t("Flickr tags eg birthday,party "),
);
$form['flickr_entry_genre'] = array(
'#type' => 'radios',
'#title' => t('Flickr photos that are'),
'#default_value' => 're',
'#options' => array('re' =>'recently added','ra' =>'randomly selected'),
'#description' => t("Flickr photos that are recently added or randomly selected "),
);
$form['flickr_entry_scope'] = array(
'#type' => 'radios',
'#title' => t('Flickr photos that are'),
'#default_value' => 'all',
'#options' => array('all' =>'selected from all flickr accounts','you' =>'selected from an individual flickr account'),
'#description' => t("Flickr photos that are selected for all or individula accounts ")
);
$form['flickr_entry_submit'] = array(
'#type' => 'submit',
'#value' => t('Create an flickr entry'),
);
return $form ;
}
function flickr_admin_entry_create_form_submit($form_id, $form_values) {
$flickr_entries = variable_get('flickr_entries', array());
$flickr_entry = array(
'name' => $form_values['flickr_entry_name'] ,
'tags' => $form_values['flickr_entry_tags'] ,
'genre' => $form_values['flickr_entry_genre'],
'scope' => $form_values['flickr_entry_scope'],
);
array_push($flickr_entries , $flickr_entry);
variable_set('flickr_entries' , $flickr_entries);
drupal_set_message("Flickr entry ".$form_values['flickr_entry_name'] ." has been posted");
}
function flickr_admin_entry_edit($flickr_entry) {
$output = drupal_get_form('flickr_admin_entry_edit_form',$flickr_entry);
return $output;
}
function flickr_admin_entry_edit_form($flickr_entry) {
$flickr_entries = variable_get('flickr_entries' , array());
$flickr_entries[$flickr_entry]['name'];
$form['#validate'] = array('flickr_admin_update_entry_form_validate' => array());
$form['flickr_entry_name'] = array(
'#type' => 'textfield',
'#default_value' => $flickr_entries[$flickr_entry]['name'],
'#title' => t('Flickr entry name'),
'#required' => TRUE,
'#description' => t("Flickr entry name eg . Birthday party. "),
);
$form['flickr_entry_tags'] = array(
'#type' => 'textfield',
'#default_value' => $flickr_entries[$flickr_entry]['tags'],
'#title' => t('Flickr tags'),
'#required' => FALSE,
'#description' => t("Flickr tags eg birthday,party "),
);
$form['flickr_entry_genre'] = array(
'#type' => 'radios',
'#title' => t('Flickr photos that are'),
'#default_value' => $flickr_entries[$flickr_entry]['genre'],
'#options' => array('re' =>'recently added','ra' =>'randomly selected'),
'#description' => t("Flickr photos that are recently added or randomly selected "),
);
$form['flickr_entry_scope'] = array(
'#type' => 'radios',
'#title' => t('Flickr photos that are'),
'#default_value' => $flickr_entries[$flickr_entry]['scope'],
'#options' => array('all' =>'selected from all flickr accounts','you' =>'selected from an individual flickr account'),
'#description' => t("Flickr photos that are selected for all or individula accounts ")
);
$form['flickr_entry'] = array(
'#type' => 'hidden',
'#value' => $flickr_entry,
);
$form['flickr_entry_submit'] = array(
'#type' => 'submit',
'#value' => t('Edit flickr entry'),
);
return $form ;
}
function flickr_admin_entry_edit_form_submit($form_id, $form_values) {
$key = $form_values['flickr_entry'];
$flickr_entry = array(
'name' => $form_values['flickr_entry_name'] ,
'tags' => $form_values['flickr_entry_tags'] ,
'genre' => $form_values['flickr_entry_genre'],
'scope' => $form_values['flickr_entry_scope'],
);
$flickr_entries = variable_get('flickr_entries', array());
$flickr_entries[$key] = $flickr_entry;
variable_set('flickr_entries',$flickr_entries);
drupal_set_message("Flickr entry ".$form_values['flickr_entry_name'] ." has been updated");
}
function flickr_admin_entries() {
$output = drupal_get_form('flickr_admin_entries_form');
print theme('page', $output);
}
function flickr_admin_entries_form() {
$form['flickr-admin-entries']['header'] = array('#type' => 'value', '#value' => array(
array('data' => t('Name')),
array('data' => t('Tags')),
array('data' => t('Select')),
array('data' => t('From')),
array('data' => t('Edit')),
array('data' => t('Delete'))
));
$flickr_entries = variable_get('flickr_entries', array());
foreach ($flickr_entries as $key=>$flickr_entry){
$form['flickr-admin-entries']['name'][$key] = array('#value' => $flickr_entry['name']);
$form['flickr-admin-entries']['tags'][$key] = array('#value' => $flickr_entry['tags']);
$form['flickr-admin-entries']['select'][$key] = array('#value' => $flickr_entry['genre'] == 're' ? 'recent' : 'at random');
$form['flickr-admin-entries']['from'][$key] = array('#value' => $flickr_entry['scope'] == 'you' ? 'your account' : 'all accounts' );
$form['flickr-admin-entries']['edit'][$key] = array('#value' => l('edit', "admin/flickr/entry/edit/".$key));
$form['flickr-admin-entries']['delete'][$key] = array('#value' => l('delete', "admin/flickr/entry/delete/".$key)) ;
}
return $form['flickr-admin-entries'];
}
function theme_flickr_admin_entries_form($form) {
if (isset($form['name']) && is_array($form['name'])) {
foreach (element_children($form['name']) as $key) {
$row = array();
$row[] = drupal_render($form['name'][$key]);
$row[] = drupal_render($form['tags'][$key]);
$row[] = drupal_render($form['select'][$key]);
$row[] = drupal_render($form['from'][$key]);
$row[] = drupal_render($form['edit'][$key]);
$row[] = drupal_render($form['delete'][$key]);
$rows[] = $row;
}
}
else {
$rows[] = array(array('data' => t('No flickr entries'), 'colspan' => '6'));
}
$output .= theme('table', $form['header']['#value'], $rows);
if ($form['pager']['#value']) {
$output .= drupal_render($form['pager']);
}
$output .= drupal_render($form);
return $output;
}
function flickr_admin_entry_delete_form($flickr_entry) {
$flickr_entries = variable_get('flickr_entries',NULL);
$name = $flickr_entries[$flickr_entry]['name'] ;
$form['flickr_entry'] = array('#type' => 'hidden', '#value' => $flickr_entry);
$form['name'] = array('#type' => 'value', '#value' => $name);
return confirm_form($form,
t('Are you sure you want to delete the flickr entry "%name" ?', array('%name' => $name)),
'admin/flickr/entries',
'This action cannot be undone.',
t('Confirm'),
t('Cancel'));
}
function flickr_admin_entry_delete($flickr_entry) {
$output = drupal_get_form('flickr_admin_entry_delete_form',$flickr_entry);
print theme('page', $output);
}
function flickr_admin_entry_delete_form_submit($form_id, $form_values) {
$flickr_entry = $form_values['flickr_entry'];
$name = $form_values['name'];
$flickr_entries = variable_get('flickr_entries',array());
unset($flickr_entries[$flickr_entry]);
variable_set('flickr_entries',$flickr_entries);
db_query("DELETE FROM {blocks} WHERE module = 'flickr' AND delta = %d", $flickr_entry);
cache_clear_all();
drupal_set_message(t('Flickr entry "%name" has been deleted', array('%name' => $name)));
return "admin/flickr/entries";
}
function flickr_admin() {
$output = l('Create a flickr entry', "admin/flickr/entry/create").' | '.l('Flickr entries', "admin/flickr/entries") ;
print theme('page', $output);
}