Commit 3d6ca411 authored by Martin Postma's avatar Martin Postma
Browse files

Issue #2257689 by lolandese: Added sort option 'number of views' (popularity)...

Issue #2257689 by lolandese: Added sort option 'number of views' (popularity) and added sort options to block config, 'random', 'recent' and 'views'.
parent f87906c3
Loading
Loading
Loading
Loading
+53 −33
Changes for block/flickr_block.module: 53 added lines, 33 removed lines.
Original line number Diff line number Diff line
@@ -20,29 +20,29 @@ function flickr_block_help($path, $arg) {
 */
function flickr_block_info() {
  // User blocks.
  $blocks[0]['info'] = t("User (node author) recent Flickr photos tagged as term from node");
  $blocks[0]['info'] = t("User (node author) Flickr photos tagged as term from node");
  $blocks[1]['info'] = t("User recent Flickr photosets");
  $blocks[2]['info'] = t("User (node author) random Flickr photos tagged as term from node");
  $blocks[2]['info'] = t("User (node author) Flickr photos tagged as term from node");

  // Site wide blocks.
  $blocks[3]['info'] = t('Recent photos from a Flickr user tagged as term from node');
  $blocks[3]['info'] = t('Photos from a Flickr user tagged as term from node');
  $blocks[4]['info'] = t('Recent photosets from a Flickr user');
  $blocks[5]['info'] = t('Random photos from a Flickr user tagged as term from node');
  $blocks[6]['info'] = t('Recent photos from a Flickr group tagged as term from node');
  $blocks[5]['info'] = t('Photos from a Flickr user tagged as term from node');
  $blocks[6]['info'] = t('Photos from a Flickr group tagged as term from node');

  // Photoset blocks.
  $blocks[7]['info'] = t('Random photos tagged as term from node from a Flickr photoset');
  $blocks[8]['info'] = t('Recent photos tagged as term from node from a Flickr photoset');
  $blocks[7]['info'] = t('Photos tagged as term from node from a Flickr photoset');
  $blocks[8]['info'] = t('Photos tagged as term from node from a Flickr photoset');

  // Returns a list of favorite public photos for the given user.
  $blocks[9]['info'] = t("Recent favorite public photos from a Flickr user tagged as term from node");
  $blocks[9]['info'] = t("Favorite public photos from a Flickr user tagged as term from node");

  $blocks[10]['info'] = t("Random photos from a Flickr group tagged as term from node");
  $blocks[11]['info'] = t("Random photos with a specific tags from a Flickr user");
  $blocks[12]['info'] = t("Random favorite public photos from a Flickr user tagged as term from node");
  $blocks[10]['info'] = t("Photos from a Flickr group tagged as term from node");
  $blocks[11]['info'] = t("Photos with specific tags from a Flickr user");
  $blocks[12]['info'] = t("Favorite public photos from a Flickr user tagged as term from node");

  $blocks[13]['info'] = t("User (node author) recent Flickr favorites tagged as term from node");
  $blocks[14]['info'] = t("User (node author) random Flickr favorites tagged as term from node");
  $blocks[13]['info'] = t("User (node author) Flickr favorites tagged as term from node");
  $blocks[14]['info'] = t("User (node author) Flickr favorites tagged as term from node");

  for ($i = 0; $i < 15; $i++) {
    $blocks[$i]['cache'] = DRUPAL_CACHE_GLOBAL;
@@ -83,6 +83,9 @@ function flickr_block_configure($delta = '') {
  if (!isset($settings['photoset_id'])) {
    $settings['photoset_id'] = '';
  }
  if (!isset($settings['sort'])) {
    $settings['sort'] = 'random';
  }
  if (!isset($settings['media'])) {
    $settings['media'] = 'photos';
  }
@@ -162,6 +165,17 @@ function flickr_block_configure($delta = '') {
    '#default_value' => $settings['size'],
    '#description' => t("Select the size of photos you'd like to display in the block."),
  );
  $form["flickr_block_{$delta}_sort"] = array(
    '#type' => 'select',
    '#options' => array(
      'random' => t('Random'),
      'recent' => t('Recent (newest first)'),
      'views' => t('Number of photo views on Flickr (popularity)'),
    ),
    '#title' => t('Sort order'),
    '#default_value' => $settings['sort'],
    '#description' => t("Gets ignored for slideshows (always 'recent')."),
  );
  $form["flickr_block_{$delta}_media"] = array(
    '#type' => 'select',
    '#options' => array(
@@ -223,7 +237,7 @@ function flickr_block_configure($delta = '') {
  );
  // A note for dynamic user blocks.
  $form['flickr_block_note'] = array(
    '#markup' => t("<h3>NOTE</h3><strong>The block visibility setting for 'user' blocks are not only restricted by the user in the block configuration but also by the Flickr Block module.</strong> The block only gets displayed when the first argument of the path is 'user' or 'node' and a specific uid is available.<br /><br />Below you can change the block settings to exclude 'node/*' pages or show them only on 'user/*' pages. You can do it also the other way around to show a block only on a node page. Fine-grain it even more by selecting only a certain content type you want to show them on, for example blog posts."),
    '#markup' => t("<h3>NOTE</h3><strong>The block visibility setting for 'user' (node author) blocks are not only restricted in the block configuration but also by the Flickr Block module.</strong> The block only gets displayed on user and node pages. Below you can change the block settings to exclude 'user/*' pages or show them only on 'node/*' pages or the other way around. Fine-grain even more by selecting a content type, for example blog posts."),
    '#weight' => 10,
  );

@@ -234,6 +248,7 @@ function flickr_block_configure($delta = '') {
      unset($form["flickr_block_{$delta}_user_id"]);
      unset($form["flickr_block_{$delta}_group_id"]);
      unset($form["flickr_block_{$delta}_photoset"]);
      unset($form["flickr_block_{$delta}_sort"]);
      if (module_exists('taxonomy')) {
        unset($form["flickr_block_{$delta}_vocab"]);
      }
@@ -285,6 +300,7 @@ function flickr_block_configure($delta = '') {
      unset($form["flickr_block_{$delta}_group_id"]);
      unset($form["flickr_block_{$delta}_photoset"]);
      unset($form['flickr_block_note']);
      unset($form["flickr_block_{$delta}_sort"]);
      if (module_exists('taxonomy')) {
        unset($form["flickr_block_{$delta}_vocab"]);
      }
@@ -409,6 +425,7 @@ function flickr_block_save($delta = '', $edit = array()) {
      variable_set('flickr_block_' . $delta, array(
        'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
        'size' => $edit["flickr_block_{$delta}_size"],
        'sort' => $edit["flickr_block_{$delta}_sort"],
        'media' => $edit["flickr_block_{$delta}_media"],
        'vocab' => $vocab,
        'min_title' => $edit["flickr_block_{$delta}_min_title"],
@@ -441,6 +458,7 @@ function flickr_block_save($delta = '', $edit = array()) {
        'user_id' => $edit["flickr_block_{$delta}_user_id"],
        'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
        'size' => $edit["flickr_block_{$delta}_size"],
        'sort' => $edit["flickr_block_{$delta}_sort"],
        'media' => $edit["flickr_block_{$delta}_media"],
        'vocab' => $vocab,
        'min_title' => $edit["flickr_block_{$delta}_min_title"],
@@ -486,6 +504,7 @@ function flickr_block_save($delta = '', $edit = array()) {
        'group_id' => $edit["flickr_block_{$delta}_group_id"],
        'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
        'size' => $edit["flickr_block_{$delta}_size"],
        'sort' => $edit["flickr_block_{$delta}_sort"],
        'media' => $edit["flickr_block_{$delta}_media"],
        'vocab' => $vocab,
        'min_title' => $edit["flickr_block_{$delta}_min_title"],
@@ -501,6 +520,7 @@ function flickr_block_save($delta = '', $edit = array()) {
      variable_set('flickr_block_' . $delta, array(
        'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
        'size' => $edit["flickr_block_{$delta}_size"],
        'sort' => $edit["flickr_block_{$delta}_sort"],
        'media' => $edit["flickr_block_{$delta}_media"],
        'photoset_id' => $edit["flickr_block_{$delta}_photoset"],
        'vocab' => (int) $edit["flickr_block_{$delta}_vocab"],
@@ -532,6 +552,7 @@ function flickr_block_save($delta = '', $edit = array()) {
        'tag' => preg_replace('/\s+/', '', $edit["flickr_block_{$delta}_tag"]),
        'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
        'size' => $edit["flickr_block_{$delta}_size"],
        'sort' => $edit["flickr_block_{$delta}_sort"],
        'media' => $edit["flickr_block_{$delta}_media"],
        'min_title' => $edit["flickr_block_{$delta}_min_title"],
        'min_metadata' => $edit["flickr_block_{$delta}_min_metadata"],
@@ -553,6 +574,7 @@ function flickr_block_view($delta = '') {
    'tag' => '',
    'group_id' => '',
    'photoset_id' => '',
    'sort' => 'random',
    'media' => 'photos',
    'vocab' => '',
    'min_title' => variable_get('flickr_title_suppress_on_small', '100'),
@@ -664,7 +686,7 @@ function flickr_block_view($delta = '') {
        switch ($delta) {
          // User recent Flickr photosets.
          case 1:
            $random = 0;
            $sort = 'recent';
            $people = flickr_get_user_info($user->flickr['nsid']);
            $recent_sets = l(t('Recent Flickr sets'), $people['photosurl'] . 'sets/', array(
              'attributes' => array(
@@ -675,7 +697,7 @@ function flickr_block_view($delta = '') {
            // Only if on a node page use it's terms.
            $tags = (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) ? $tags : '';
            $block['subject'] = t("!recent_sets by !username", array('!recent_sets' => $recent_sets, '!username' => $people['name']));
            $block['content'] = _flickr_block_photosets($user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media'], $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
            $block['content'] = _flickr_block_photosets($user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media'], $delta, $sort, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
            break;

          // User Flickr photos.
@@ -683,9 +705,8 @@ function flickr_block_view($delta = '') {
          case 2:
            // Only if on a node page use it's terms.
            $tags = (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) ? $tags : '';
            $random = $delta == 2 ? 1 : 0;
            $block['subject'] = NULL;
            $block['content'] = _flickr_album('user', $user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media'], $tags, $tags_user, $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
            $block['content'] = _flickr_album('user', $user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media'], $tags, $tags_user, $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
            break;

          // User favorite Flickr photos.
@@ -693,9 +714,8 @@ function flickr_block_view($delta = '') {
          case 14:
            // Only if on a node page use it's terms.
            $tags = (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) ? $tags : '';
            $random = $delta == 14 ? 1 : 0;
            $block['subject'] = NULL;
            $block['content'] = _flickr_album('favorites', $user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
            $block['content'] = _flickr_album('favorites', $user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
            break;
        }
      }
@@ -704,7 +724,7 @@ function flickr_block_view($delta = '') {
    // Photos from a Flickr user.
    case 3:
    case 5:
      $random = $delta == 5 ? 1 : 0;
      $sort = $delta == 5 ? 'random' : 'recent';
      if (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) {
        $node = node_load($nid);
        if (module_exists('taxonomy')) {
@@ -732,12 +752,12 @@ function flickr_block_view($delta = '') {
      // The used tags with a space forced after the comma and the last
      // one replaced by 'or'.
      $block['subject'] = NULL;
      $block['content'] = _flickr_album('user', $settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      $block['content'] = _flickr_album('user', $settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      break;

    // Recent photosets from a Flickr user.
    case 4:
      $random = 0;
      $sort = 'recent';
      $people = flickr_get_user_info($settings['user_id']);
      $media = $settings['media'] == 'videos' ? ' ' . t('(shows videos only)') : '';
      $recent_sets = l(t('Recent Flickr sets@media', array('@media' => $media)), $people['photosurl'] . 'sets/', array(
@@ -747,13 +767,13 @@ function flickr_block_view($delta = '') {
        ),
      ));
      $block['subject'] = t("!recent_sets by !username", array('!recent_sets' => $recent_sets, '!username' => $people['name']));
      $block['content'] = _flickr_block_photosets($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $delta, $people, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
      $block['content'] = _flickr_block_photosets($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $delta, $people, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
      break;

    // Photos from a Flickr group.
    case 6:
    case 10:
      $random = $delta == 10 ? 1 : 0;
      $sort = $delta == 10 ? 'random' : 'recent';
      if (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) {
        $node = node_load($nid);
        if (module_exists('taxonomy')) {
@@ -779,13 +799,13 @@ function flickr_block_view($delta = '') {
        $tags = '';
      }
      $block['subject'] = NULL;
      $block['content'] = _flickr_album('group', $settings['group_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      $block['content'] = _flickr_album('group', $settings['group_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      break;

    // Photos from a Flickr photoset.
    case 7:
    case 8:
      $random = $delta == 7 ? 1 : 0;
      $sort = $delta == 7 ? 'random' : 'recent';
      if (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) {
        $node = node_load($nid);
        if (module_exists('taxonomy')) {
@@ -811,13 +831,13 @@ function flickr_block_view($delta = '') {
        $tags = '';
      }
      $block['subject'] = NULL;
      $block['content'] = _flickr_album('photoset', $settings['photoset_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      $block['content'] = _flickr_album('photoset', $settings['photoset_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      break;

    // Favorite public photos from a Flickr user.
    case 9:
    case 12:
      $random = $delta == 12 ? 1 : 0;
      $sort = $delta == 12 ? 'random' : 'recent';
      $media = $settings['media'] == 'videos' ? t('videos') : t('photos');
      if (($nid = (int) arg(1)) && arg(0) == 'node' && ((arg(2) == "" || arg(2) == 'view'))) {
        $node = node_load($nid);
@@ -844,14 +864,14 @@ function flickr_block_view($delta = '') {
        $tags = '';
      }
      $block['subject'] = NULL;
      $block['content'] = _flickr_album('favorites', $settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      $block['content'] = _flickr_album('favorites', $settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $tags, '', $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata'], $settings['vocab']);
      break;

    // Random photos with a specific tag from a Flickr user.
    case 11:
      $random = 1;
      $sort = 'random';
      $block['subject'] = NULL;
      $block['content'] = _flickr_album('user', $settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $settings['tag'], '', $delta, $random, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
      $block['content'] = _flickr_album('user', $settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $settings['tag'], '', $delta, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
      break;
  }
  return $block;
@@ -860,7 +880,7 @@ function flickr_block_view($delta = '') {
/**
 * Photoset block. Block delta: 1 and 4.
 */
function _flickr_block_photosets($nsid, $show_n, $size, $media, $delta, $people = NULL, $random = 0, $heading = 'h2', $min_title = 100, $min_metadata = 150) {
function _flickr_block_photosets($nsid, $show_n, $size, $media, $delta, $people = NULL, $sort = 0, $heading = 'h2', $min_title = 100, $min_metadata = 150) {
  if ($cache = cache_get('flickr_block_' . $delta . '_' . $nsid)) {
    $output = $cache->data;
  }
+15 −20

File changed.

Preview size limit exceeded, changes collapsed.

+37 −5
Changes for flickr.inc: 37 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -493,7 +493,7 @@ function flickr_get_id_type($id) {
/**
 * Render multiple photos as an album.
 */
function _flickr_album($type = 'user', $id = '39873962@N08', $show_n = NULL, $size = NULL, $media = 'photos', $tags = '', $tags_user = '', $delta = 'true', $random = 0, $heading = 'p', $min_title = NULL, $min_metadata = NULL, $vocab_isset = 0) {
function _flickr_album($type = 'user', $id = '39873962@N08', $show_n = NULL, $size = NULL, $media = 'photos', $tags = '', $tags_user = '', $delta = 'true', $sort = 'random', $heading = 'p', $min_title = NULL, $min_metadata = NULL, $vocab_isset = 0) {
  // drupal_is_front_page() is added because it also can have arg(0) == 'node'.
  $cache = cache_get('flickr_block_' . $delta . '_' . $id . '_' . $tags . '_' . arg(0) . '_' . drupal_is_front_page());
  // If $delta is a number it is a block that could be cached.
@@ -514,7 +514,19 @@ function _flickr_album($type = 'user', $id = '39873962@N08', $show_n = NULL, $si
    else {
      $output = '<span class="flickr-photoset-single">';
    }
    $recent = ($random == 1 || $id == '39873962@N08') ? '' : t('Recent') . ' ';
    switch ($sort) {
      case 'recent':
        $order = t('Recent') . ' ';
        break;

      case 'views':
        $order = t('Most viewed') . ' ';
        break;

      default:
        $order = t('Random') . ' ';
        break;
    }
    // Merge the user profile terms with the terms from the node.
    $tags_user_string = is_array($tags_user) ? implode(",", $tags_user) : $tags_user;
    $tags_combined = trim($tags . ',' . $tags_user_string, ',');
@@ -549,7 +561,7 @@ function _flickr_album($type = 'user', $id = '39873962@N08', $show_n = NULL, $si
    }
    $people['photosurl'] = isset($people['photosurl']) ? $people['photosurl'] : 'https://www.flickr.com/groups/' . $id . '/';
    $people['photosurl'] = $id == '39873962@N08' ? 'https://www.flickr.com/search/?sort=relevance&text=' : $people['photosurl'];
    $photos = l(t('@recentFlickr @media', array('@recent' => $recent, '@media' => $media_type)), $people['photosurl'], array(
    $photos = l(t('@orderFlickr @media', array('@order' => $order, '@media' => $media_type)), $people['photosurl'], array(
      'attributes' => array(
        'title' => t('View Flickr photostream.'),
        'target' => '_blank',
@@ -800,8 +812,17 @@ function _flickr_album($type = 'user', $id = '39873962@N08', $show_n = NULL, $si
          default:
            return;
        }
        if ($random == 1) {
        switch ($sort) {
          case 'recent':
            break;

          case 'views':
            usort($photos, 'sortByPopularity');
            break;

          default:
            shuffle($photos);
            break;
        }
        // We shouldn't try to return more than the total number of photos.
        $to = min($show_n, count($photos));
@@ -991,7 +1012,7 @@ function _flickr_album($type = 'user', $id = '39873962@N08', $show_n = NULL, $si
      $output .= '</span>';
    }
    if (is_numeric($delta) && module_exists('flickr_block')) {
      $refresh = $random == 1 ? variable_get('flickr_block_refresh_random', 23) : variable_get('flickr_block_refresh_others', 31);
      $refresh = $sort == 'random' ? variable_get('flickr_block_refresh_random', 23) : variable_get('flickr_block_refresh_others', 31);
      // drupal_is_front_page() is added because it also can have
      // arg(0) == 'node'.
      cache_set('flickr_block_' . $delta . '_' . $id . '_' . $tags . '_' . arg(0) . '_' . drupal_is_front_page(), $output, 'cache', time() + $refresh * 60 * 60);
@@ -1024,3 +1045,14 @@ function flickr_in_array_r($needle, $haystack, $strict = FALSE) {
  }
  return FALSE;
}

/**
 * Puts an array in descending order on the key 'views' which in this case is
 * the number of views on Flickr (used as a measure for popularity).
 *
 * Call this function with e.g. 'usort($photos, 'sortByPopularity')'.
 * See http://stackoverflow.com/a/2699159.
 */
function sortByPopularity($a, $b) {
  return $b['views'] - $a['views'];
}