View source
<?php
require_once drupal_get_path('module', 'flickr') . '/flickr.inc';
function flickr_help($path, $arg) {
switch ($path) {
case 'admin/help#flickr':
return _filter_autop(file_get_contents(dirname(__FILE__) . '/README.txt'));
}
}
function flickr_init() {
if (variable_get('flickr_css', 1)) {
drupal_add_css(drupal_get_path('module', 'flickr') . '/flickr.css', $type = 'module', $media = 'all', $preprocess = TRUE);
}
}
function flickr_perm() {
return array(
'view own flickr photos',
'view all flickr photos',
'administer flickr',
);
}
function flickr_theme() {
return array(
'flickr_photo' => array(
'arguments' => array(
'size' => NULL,
'format' => NULL,
'attribs' => NULL,
),
),
'flickr_photo_box' => array(
'arguments' => array(
'p',
'size' => NULL,
'format' => NULL,
'attribs' => NULL,
),
),
'flickr_photos' => array(
'arguments' => array(
'uid' => NULL,
'photos' => NULL,
),
),
'flickr_photoset' => array(
'arguments' => array(
'ps',
'owner',
'size',
'attribs' => NULL,
),
),
);
}
function flickr_menu() {
$items['admin/settings/flickr'] = array(
'title' => 'Flickr',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flickr_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'description' => 'Change settings for the flickr module.',
'file' => 'flickr.admin.inc',
);
$items['flickr'] = array(
'title' => 'Flickr photos',
'type' => MENU_CALLBACK,
'page callback' => 'flickr_photos',
'access callback' => TRUE,
'description' => 'Flickr photos of default user id.',
);
$items['flickr/%user'] = array(
'title callback' => 'flickr_page_title',
'title arguments' => array(
1,
),
'type' => MENU_CALLBACK,
'page callback' => 'flickr_photos',
'page arguments' => array(
1,
),
'access callback' => 'flickr_photos_access',
'access arguments' => array(
1,
),
'description' => 'Flickr photos of specified user.',
);
$items['flickr/auth'] = array(
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'flickr_auth_callback',
);
return $items;
}
function flickr_photos_access($account) {
global $user;
$photo_access = FALSE;
if (!empty($account) && !empty($account->uid)) {
if (isset($account->flickr['nsid'])) {
$photo_access = user_access('administer flickr') || $account->status && (user_access('view all flickr photos') || user_access('view own flickr photos') && $user->uid == $account->uid);
}
else {
drupal_set_message(t('%user does not have a Flickr account', array(
'%user' => $account->name,
)), 'error');
}
}
return $photo_access;
}
function flickr_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'form' && (user_access('view own flickr photos') || user_access('administer flickr'))) {
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' => empty($user->flickr['identifier']) ? '' : $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);
$user_affected = db_affected_rows();
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']);
}
elseif ($user_affected) {
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 ($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($user = NULL) {
global $pager_page_array, $pager_total, $pager_total_items;
$element = 0;
$pager_page_array[$element] = empty($_GET['page']) ? 0 : (int) $_GET['page'];
if ($user === NULL) {
$nsid = variable_get('flickr_default_userid', '');
if (!$nsid) {
drupal_set_message(t('No default Flickr user id has been set.'));
return FALSE;
}
$uid = 0;
}
else {
$account = $user;
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;
}
$uid = $account->uid;
}
$nsid = flickr_user_find_by_identifier($nsid);
$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;
}
$pager_total[$element] = $photos['pages'];
$pager_total_items[$element] = $photos['total'];
return theme('flickr_photos', $uid, $photos);
}
function theme_flickr_photo($photo, $size = NULL, $format = NULL, $attributes = NULL) {
$class = variable_get('flickr_class', '');
$rel = variable_get('flickr_rel', '');
$url = flickr_photo_img($photo, variable_get('flickr_opening_size', ''), $format);
$img = flickr_img($photo, $size, $attributes);
$img_url = flickr_photo_img($photo, $size);
switch ($size) {
case 's':
$width = '75';
break;
case 'q':
$width = '150';
break;
}
if (!isset($width)) {
list($width) = getimagesize($img_url);
}
$photo_url = flickr_photo_page_url($photo['owner'], $photo['id']);
$title = is_array($photo['title']) ? str_replace('"', "'", strip_tags($photo['title']['_content'])) : $photo['title'];
$info = flickr_photo_get_info($photo['id']);
$description = !empty($info['description']['_content']) ? str_replace('"', "'", htmlspecialchars_decode(strip_tags($info['description']['_content']))) : $title;
$username = !empty($info['owner']['realname']) ? l($info['owner']['realname'], 'https://www.flickr.com/photos/' . $info['owner']['nsid'], array(
'attributes' => array(
'title' => t('View user on Flickr.'),
'target' => '_blank',
),
)) : l($info['owner']['username'], 'https://www.flickr.com/photos/' . $info['owner']['nsid'], array(
'attributes' => array(
'title' => t('View user on Flickr.'),
'target' => '_blank',
),
));
$taken = isset($info['dates']['taken']) ? format_interval(time() - strtotime($info['dates']['taken']), 1) . ' ' . t('ago') : '';
$neighbourhood = isset($info['location']['neighbourhood']['_content']) ? strip_tags($info['location']['neighbourhood']['_content']) . ', ' : '';
$locality = isset($info['location']['locality']['_content']) ? strip_tags($info['location']['locality']['_content']) . ', ' : '';
$region = isset($info['location']['region']['_content']) ? strip_tags($info['location']['region']['_content']) . ', ' : '';
$country = isset($info['location']['country']['_content']) ? strip_tags($info['location']['country']['_content']) : '';
$location = !empty($country) ? ' ' . t('at') . ' ' . $neighbourhood . $locality . $region . $country : '';
$metadata = '<br />' . $taken . $location . ' ' . t('by') . ' ' . $username;
$credit = $width < variable_get('flickr_title_suppress_on_small', '100') ? t('Flickr') : $title;
$metadatacaption = $width < variable_get('flickr_metadata_suppress_on_small', '150') ? '' : $metadata;
$overlay = variable_get('flickr_info_overlay', array(
'title' => 'title',
'metadata' => 'metadata',
'description' => 'description',
));
$overlaytitle = gettype($overlay['title']) == 'integer' ? '' : $title . ' - ';
$metadata = gettype($overlay['metadata']) == 'integer' ? '' : $metadata . ' - ';
$description = gettype($overlay['description']) == 'integer' || $overlaytitle == $description . ' - ' ? '' : $description;
$overlayhtml = $overlaytitle . $metadata . $description;
if (variable_get('flickr_class', '') == NULL && variable_get('flickr_rel', '') == NULL) {
return l($img, $photo_url, array(
'attributes' => array(
'title' => $title,
),
'absolute' => TRUE,
'html' => TRUE,
));
}
else {
return '<span class="flickr-wrap">' . l($img, $url, array(
'attributes' => array(
'title' => $overlayhtml,
'class' => $class,
'rel' => $rel,
),
'html' => TRUE,
)) . '<span class="flickr-credit" style="width: ' . ($width - variable_get('flickr_caption_padding', '0')) . 'px;">' . l($credit, $photo_url, array(
'attributes' => array(
'title' => t('View on Flickr. To enlarge click image.'),
'target' => '_blank',
),
'html' => TRUE,
)) . $metadatacaption . '</span></span>';
}
}
function theme_flickr_photo_box($photo, $size = NULL, $format = NULL, $attributes = NULL) {
$img = flickr_img($photo, $size, $attributes);
$title = is_array($photo['title']) ? $photo['title']['_content'] : $photo['title'];
$photo_url = flickr_photo_page_url($photo['owner'], $photo['id']);
$output = "<div class='flickr-photo-box'>\n";
$output .= "<a href='{$photo_url}'>{$img}</a>";
$output .= "<a href='{$photo_url}'>";
$output .= '<div class="flickr-photo-title">' . strip_tags($title) . "</div>\n";
$output .= "</a>";
$output .= "</div>\n";
return $output;
}
function theme_flickr_photos($uid, $photos) {
$output = theme('pager', NULL, variable_get('flickr_photos_per_page', 20));
$output .= "<div class='flickr-photoset'>\n";
foreach ($photos['photo'] as $photo) {
$output .= theme('flickr_photo_box', $photo, 'm');
}
$output .= '</div>';
$output .= theme('pager', NULL, variable_get('flickr_photos_per_page', 20));
return $output;
}
function theme_flickr_photoset($photoset, $owner, $size, $attributes = NULL) {
if (module_exists('flickr_sets')) {
$output .= "<div class='flickr-photoset'>\n";
$photos = flickr_set_load($photoset['id']);
foreach ((array) $photos['photoset']['photo'] as $photo) {
$photo['owner'] = $owner;
$output .= theme('flickr_photo', $photo, $size);
}
$output .= '</div>';
return $output;
}
else {
$img = flickr_img($photoset, $size, $attributes);
$output = theme('pager', NULL, variable_get('flickr_photos_per_page', 20));
$photo_url = flickr_photoset_page_url($owner, $photoset['id']);
$output .= "<div class='flickr-photoset'>\n";
$title = is_array($photoset['title']) ? $photoset['title']['_content'] : $photoset['title'];
return l($img, $photo_url, array(
'attributes' => array(
'title' => $title,
),
'absolute' => TRUE,
'html' => TRUE,
));
}
}
function flickr_page_title($user) {
return 'Flickr photos - ' . $user->name;
}