You are here

flickr.module in Flickr 6

Same filename and directory in other branches
  1. 8 flickr.module
  2. 5 flickr.module
  3. 7 flickr.module

File

flickr.module
View source
<?php

require_once drupal_get_path('module', 'flickr') . '/flickr.inc';

/**
 * Implements hook_help().
 */
function flickr_help($path, $arg) {
  switch ($path) {
    case 'admin/help#flickr':

      // Return a line-break version of the README.txt.
      return _filter_autop(file_get_contents(dirname(__FILE__) . '/README.txt'));
  }
}

/**
 * Implements hook_init().
 */
function flickr_init() {

  // Determine the setting to use flickr.css or not.
  if (variable_get('flickr_css', 1)) {
    drupal_add_css(drupal_get_path('module', 'flickr') . '/flickr.css', $type = 'module', $media = 'all', $preprocess = TRUE);
  }
}

/**
 * Implements hook_perm().
 */
function flickr_perm() {
  return array(
    'view own flickr photos',
    'view all flickr photos',
    'administer flickr',
  );
}

/**
 * Implements hook_theme().
 */
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,
      ),
    ),
  );
}

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements 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' && (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) {

        // 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 ($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;

  // Set this to something else if you want multiple pagers.
  $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;

    // TODO: We can just use $account as the argument
    if ($account->flickr['nsid']) {

      // TODO: Were actually storing the "identifier" which is potentially the $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;
  }

  // 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_photo($photo, $size = NULL, $format = NULL, $attributes = NULL) {

  // Get 'class' and 'rel' link attributes to trigger an overlay browser.
  $class = variable_get('flickr_class', '');
  $rel = variable_get('flickr_rel', '');

  // URL of the image to open to enlarge.
  $url = flickr_photo_img($photo, variable_get('flickr_opening_size', ''), $format);

  // The image as HTML to render.
  $img = flickr_img($photo, $size, $attributes);

  // URL of the image to display.
  $img_url = flickr_photo_img($photo, $size);

  // If the image is a square we know the width (avoids to make a request).
  switch ($size) {
    case 's':
      $width = '75';
      break;
    case 'q':
      $width = '150';
      break;
  }

  // If it is not a square.
  if (!isset($width)) {

    // Get the real width of the image.
    list($width) = getimagesize($img_url);
  }

  // URL of the photo page on Flickr.
  $photo_url = flickr_photo_page_url($photo['owner'], $photo['id']);

  // Get the Flickr image title for set data or the photo itself. Sanatize.
  $title = is_array($photo['title']) ? str_replace('"', "'", strip_tags($photo['title']['_content'])) : $photo['title'];
  $info = flickr_photo_get_info($photo['id']);

  // Use title if description does not exist, else sanitize the description.
  $description = !empty($info['description']['_content']) ? str_replace('"', "'", htmlspecialchars_decode(strip_tags($info['description']['_content']))) : $title;

  // Real name if it exists or go with the username. Link to Flickr user page.
  $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',
    ),
  ));

  // The date an image was taken formatted as 'time ago'.
  $taken = isset($info['dates']['taken']) ? format_interval(time() - strtotime($info['dates']['taken']), 1) . ' ' . t('ago') : '';

  // A bunch of geo data.
  $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 : '';

  // Compose extra Flickr info as HTML to render.
  $metadata = '<br />' . $taken . $location . ' ' . t('by') . ' ' . $username;

  // Image width < 100 px is too small for most titles. Can be set differently.
  $credit = $width < variable_get('flickr_title_suppress_on_small', '100') ? t('Flickr') : $title;

  // Image width < 150 px is too small for extra info. Can be set differently.
  $metadatacaption = $width < variable_get('flickr_metadata_suppress_on_small', '150') ? '' : $metadata;

  // Determine what info goes with the enlarged version of the image.
  $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 'class' or 'rel' attribute are defined (overlay browser), use a caption.
  if (variable_get('flickr_class', '') == NULL && variable_get('flickr_rel', '') == NULL) {

    // Final step when NO overlay browser is used that generates the image that
    // links to the Flickr page to comply with the TOS of Flickr.
    return l($img, $photo_url, array(
      'attributes' => array(
        'title' => $title,
      ),
      'absolute' => TRUE,
      'html' => TRUE,
    ));
  }
  else {

    // Final step when an overlay browser is used that generates the image with
    // a link to the bigger version and a link to the Flickr page under it to
    // comply with the TOS of Flickr.
    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";

  // TODO: is this correct ?
  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) {

      // Insert owner into $photo because theme_flickr_photo needs it.
      $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;
}

Functions

Namesort descending Description
flickr_help Implements hook_help().
flickr_init Implements hook_init().
flickr_menu Implements hook_menu().
flickr_page_title
flickr_perm Implements hook_perm().
flickr_photos
flickr_photos_access
flickr_theme Implements hook_theme().
flickr_user Implements hook_user(). Add an extra field for the user to enter his flickr identifier.
theme_flickr_photo
theme_flickr_photos
theme_flickr_photoset
theme_flickr_photo_box