You are here

function flickr_block_view in Flickr 7

Implements hook_block_view().

File

block/flickr_block.module, line 916
The Flickr block module

Code

function flickr_block_view($delta = '') {
  $settings = variable_get('flickr_block_' . $delta, array(
    'user_id' => '',
    'show_n' => variable_get('flickr_photos_per_page', 6),
    'size' => variable_get('flickr_default_size_album', 's'),
    'tag' => '',
    'group_id' => '',
    'photoset_id' => '',
    'sort' => 'unsorted',
    'filter' => '',
    'media' => 'photos',
    'vocab' => '',
    'geo' => '',
    'date' => '',
    'extend' => variable_get('flickr_extend', 1),
    'tag_mode' => 'context',
    'min_title' => variable_get('flickr_title_suppress_on_small', '100'),
    'min_metadata' => variable_get('flickr_metadata_suppress_on_small', '150'),
  ));
  if (!isset($settings['tag'])) {
    $settings['tag'] = '';
  }
  $default_userid = variable_get('flickr_default_userid', '');
  if (isset($settings['user_id'])) {
    if ($settings['user_id'] == 'public' || (!isset($settings['user_id']) || empty($settings['user_id'])) && empty($default_userid)) {
      $settings['user_id'] = '39873962@N08';
    }
    elseif (!isset($settings['user_id']) || empty($settings['user_id'])) {

      // Get the default user id as a fallback.
      $settings['user_id'] = variable_get('flickr_default_userid', '');
    }
    else {
      $settings['user_id'] = flickr_user_find_by_identifier($settings['user_id']);
    }
  }
  if (!isset($settings['group_id'])) {
    $settings['group_id'] = '';
  }
  if (!isset($settings['photoset_id'])) {
    $settings['photoset_id'] = '';
  }
  if (!isset($settings['sort'])) {
    $settings['sort'] = 'unsorted';
  }
  if (!isset($settings['filter'])) {
    $settings['filter'] = '';
  }
  if (!isset($settings['vocab'])) {
    $settings['vocab'] = '';
  }
  if (!isset($settings['extend'])) {
    $settings['extend'] = variable_get('flickr_extend', 1);
  }
  if (!isset($settings['tag_mode'])) {
    $settings['tag_mode'] = 'context';
  }
  if (!isset($settings['geo'])) {
    $settings['geo'] = '';
  }
  if (!isset($settings['date'])) {
    $settings['date'] = '';
  }
  if (!isset($settings['min_title'])) {
    $settings['min_title'] = variable_get('flickr_title_suppress_on_small', '100');
  }
  if (!isset($settings['min_metadata'])) {
    $settings['min_metadata'] = variable_get('flickr_metadata_suppress_on_small', '150');
  }
  $block = array();
  switch ($delta) {

    // User Flickr photos.
    case 0:
    case 1:
    case 2:
    case 13:
    case 14:
    case 15:
    case 16:

      // Get per user nsid if necessary.
      switch (arg(0)) {
        case 'user':
          if ($uid = (int) arg(1)) {
            $user = user_load($uid);
            if (function_exists('taxonomy_vocabulary_load') && $settings['vocab'] != 0) {
              $taxonomy = taxonomy_vocabulary_load($settings['vocab']);
              if (property_exists($user, 'field_' . $taxonomy->machine_name)) {
                $terms_user = !empty($settings['vocab']) ? field_view_field('user', $user, 'field_' . $taxonomy->machine_name) : '';
              }
              else {
                $terms_user = '';
                drupal_set_message(t("It is not required, but you can add the 'Term reference' field !vocab_name to user profiles at <a href='#overlay=admin/config/people/accounts/fields%3Fdestination%3D@current_path'>admin/config/people/accounts/fields</a> to give users the option to grab only certain tagged photos from their Flickr account (e.g. 'website' or 'blog') or select a vocabulary in your <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>Flickr block configuration</a> that does exist also on a user profile.", array(
                  '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
                  '@current_path' => current_path(),
                  '@delta' => $delta,
                )), 'warning', FALSE);
              }
              if (!empty($terms_user)) {
                $i = 0;
                foreach ($terms_user['#items'] as $item) {
                  $tags[$i] = $terms_user['#items'][$i]['taxonomy_term']->name;
                  $i++;
                }
                $tags_user = implode(",", $tags);
              }
              else {
                $tags_user = '';
              }
            }
            else {
              $tags_user = '';
            }
            $tags = 'from_user_page';

            // To display photos near a user on their profile.
            if (module_exists('geofield') && !is_numeric($settings['geo']) && !empty($settings['geo'])) {
              $value = field_get_items('user', $user, $settings['geo']);
              $lat = $value[0]['lat'];
              $lon = $value[0]['lon'];
            }
            elseif (module_exists('location_node') && $settings['geo'] == 1 && !empty($user->locations)) {
              $lat = $user->locations[0]['latitude'];
              $lon = $user->locations[0]['longitude'];
            }
            else {
              $lat = NULL;
              $lon = NULL;
            }

            // It does not make sense having a date field on a user profile for
            // the display of Flickr photos. Set to avoid undefined errors.
            $datefrom = NULL;
            $dateto = NULL;
          }
          break;
        case 'node':
          if (($nid = (int) arg(1)) && (arg(2) == "" || arg(2) == 'view')) {
            $node = node_load($nid);
            $user = user_load($node->uid);
            if (function_exists('taxonomy_vocabulary_load') && $settings['vocab'] != 0) {
              $taxonomy = taxonomy_vocabulary_load($settings['vocab']);
              if (property_exists($user, 'field_' . $taxonomy->machine_name)) {
                $terms_user = !empty($settings['vocab']) && !empty($taxonomy->machine_name) ? field_view_field('user', $user, 'field_' . $taxonomy->machine_name) : '';
              }
              else {
                $terms_user = '';
                drupal_set_message(t("It is not required, but you can add the 'Term reference' field !vocab_name to user profiles at <a href='#overlay=admin/config/people/accounts/fields%3Fdestination%3D@current_path'>admin/config/people/accounts/fields</a> to give users the option to grab only certain tagged photos from their Flickr account (e.g. 'website' or 'blog') or select a vocabulary in your <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>Flickr block configuration</a> that does exist also on a user profile.", array(
                  '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
                  '@current_path' => current_path(),
                  '@delta' => $delta,
                )), 'warning', FALSE);
              }
              if (!empty($terms_user)) {
                $i = 0;
                foreach ($terms_user['#items'] as $item) {
                  $tags_user[$i] = $terms_user['#items'][$i]['taxonomy_term']->name;
                  $i++;
                }
              }
              else {
                $tags_user = '';
              }
              if (property_exists($node, 'field_' . $taxonomy->machine_name)) {
                $terms_node = !empty($settings['vocab']) && !empty($taxonomy->machine_name) ? field_view_field('node', $node, 'field_' . $taxonomy->machine_name) : '';
              }
              else {
                drupal_set_message(t("Add the 'Term reference' field !vocab_name to the current content type at <a href='#overlay=admin/structure/types/manage/@type/fields%3Fdestination%3D@current_path'>admin/structure/types/manage/@type/fields</a> or <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>exclude the Flickr block</a> in the block 'Visibility settings' for this content type or select another vocabulary to use.", array(
                  '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
                  '@type' => $node->type,
                  '@current_path' => current_path(),
                  '@delta' => $delta,
                )), 'error', FALSE);
                $terms_node = '';
              }
              if (!empty($terms_node)) {
                $i = 0;
                foreach ($terms_node['#items'] as $item) {
                  $tags[$i] = $terms_node['#items'][$i]['taxonomy_term']->name;
                  $i++;
                }
                $tags = implode(",", $tags);
              }
              else {
                $tags = '';
              }
            }
            else {
              $tags_user = '';
              $tags = '';
            }
            if (module_exists('geofield') && !is_numeric($settings['geo']) && !empty($settings['geo'])) {
              $value = field_get_items('node', $node, $settings['geo']);
              $lat = $value[0]['lat'];
              $lon = $value[0]['lon'];
            }
            elseif (module_exists('location_node') && $settings['geo'] == 1 && !empty($node->locations)) {
              $lat = $node->locations[0]['latitude'];
              $lon = $node->locations[0]['longitude'];
            }
            else {
              $lat = NULL;
              $lon = NULL;
            }
            if (module_exists('date') && !is_numeric($settings['date']) && !empty($settings['date'])) {
              $value = field_get_items('node', $node, $settings['date']);
              $datefrom = substr($value[0]['value'], 0, strrpos($value[0]['value'], ' '));
              $dateto = substr($value[0]['value2'], 0, strrpos($value[0]['value2'], ' '));
            }
            else {
              $datefrom = NULL;
              $dateto = NULL;
            }
          }
          break;
        default:
          return;
      }
      if (!empty($user->flickr['nsid'])) {
        switch ($delta) {

          // User recent Flickr photosets.
          case 1:
            $sort = 'recent';
            $people = flickr_get_user_info($user->flickr['nsid']);
            $recent_sets = l(t('Recent Flickr sets'), $people['photosurl'] . 'sets/', array(
              'attributes' => array(
                'title' => t('View sets on Flickr.'),
                'target' => '_blank',
              ),
            ));

            // 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, $people, $sort, variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
            break;

          // User Flickr photos.
          case 0:
          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 : '';
            $block['subject'] = NULL;
            $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'], $settings['filter'], $lat, $lon, NULL, $datefrom, $dateto, $settings['extend'], $settings['tag_mode']);
            break;

          // User favorite Flickr photos.
          case 13:
          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 : '';
            $block['subject'] = NULL;
            $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'], NULL, NULL, NULL, NULL, NULL, NULL, $settings['extend']);
            break;

          // User gallery Flickr photos.
          case 15:
          case 16:

            // Only if on a node page use it's terms.
            $tags = ($nid = (int) arg(1)) && arg(0) == 'node' && (arg(2) == "" || arg(2) == 'view') ? $tags : '';
            $galleries = flickr_galleries_getlist($user->flickr['nsid']);
            if (empty($galleries)) {
              break;
            }
            $terms = is_array($tags) ? $tags : explode(",", $tags);
            $block['subject'] = NULL;
            $block['content'] = '';
            foreach ($galleries as $gallery) {
              foreach ($terms as $term) {
                if (stripos($gallery['title']['_content'], $term) !== FALSE || empty($term) && !isset($taxonomy)) {
                  $block['content'] .= flickr_album('gallery', $gallery['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'], NULL, NULL, NULL, NULL, NULL, NULL, $settings['extend']);
                }
              }
            }
            break;
        }
      }
      break;

    // Photos from a Flickr user.
    case 3:
    case 5:
      if (($nid = (int) arg(1)) && arg(0) == 'node' && (arg(2) == "" || arg(2) == 'view')) {
        $node = node_load($nid);
        if (function_exists('taxonomy_vocabulary_load') && $settings['vocab'] != 0) {
          $taxonomy = taxonomy_vocabulary_load($settings['vocab']);
          if (property_exists($node, 'field_' . $taxonomy->machine_name)) {
            $terms = !empty($settings['vocab']) && !empty($taxonomy->machine_name) ? field_view_field('node', $node, 'field_' . $taxonomy->machine_name) : '';
          }
          else {
            drupal_set_message(t("Add the 'Term reference' field !vocab_name to the current content type at <a href='#overlay=admin/structure/types/manage/@type/fields%3Fdestination%3D@current_path'>admin/structure/types/manage/@type/fields</a> or <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>exclude the Flickr block</a> in the block 'Visibility settings' for this content type or select another vocabulary to use.", array(
              '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
              '@type' => $node->type,
              '@current_path' => current_path(),
              '@delta' => $delta,
            )), 'error', FALSE);
            $terms = '';
          }
          if (!empty($terms)) {
            $i = 0;
            foreach ($terms['#items'] as $item) {
              $tags[$i] = $terms['#items'][$i]['taxonomy_term']->name;
              $i++;
            }
            $tags = implode(",", $tags);
          }
          else {
            $tags = '';
          }
        }
        else {
          $tags = '';
        }
        if (module_exists('geofield') && !is_numeric($settings['geo']) && !empty($settings['geo'])) {
          $value = field_get_items('node', $node, $settings['geo']);
          $lat = $value[0]['lat'];
          $lon = $value[0]['lon'];
        }
        elseif (module_exists('location_node') && $settings['geo'] == 1 && !empty($node->locations)) {
          $lat = $node->locations[0]['latitude'];
          $lon = $node->locations[0]['longitude'];
        }
        else {
          $lat = NULL;
          $lon = NULL;
        }
        if (module_exists('date') && !is_numeric($settings['date']) && !empty($settings['date'])) {
          $value = field_get_items('node', $node, $settings['date']);
          $datefrom = substr($value[0]['value'], 0, strrpos($value[0]['value'], ' '));
          $dateto = substr($value[0]['value2'], 0, strrpos($value[0]['value2'], ' '));
        }
        else {
          $datefrom = NULL;
          $dateto = NULL;
        }
      }
      else {
        $tags = '';
        $lat = NULL;
        $lon = NULL;
        $datefrom = NULL;
        $dateto = NULL;
      }
      $block['subject'] = NULL;
      $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'], $settings['filter'], $lat, $lon, NULL, $datefrom, $dateto, $settings['extend'], $settings['tag_mode']);
      break;

    // Recent photosets from a Flickr user.
    case 4:
      $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(
        'attributes' => array(
          'title' => t('View sets on Flickr.'),
          'target' => '_blank',
        ),
      ));
      $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, $settings['sort'], variable_get('flickr_block_heading', 'h2'), $settings['min_title'], $settings['min_metadata']);
      break;

    // Photos from a Flickr group.
    case 6:
    case 10:
      if (($nid = (int) arg(1)) && arg(0) == 'node' && (arg(2) == "" || arg(2) == 'view')) {
        $node = node_load($nid);
        if (function_exists('taxonomy_vocabulary_load') && $settings['vocab'] != 0) {
          $taxonomy = taxonomy_vocabulary_load($settings['vocab']);
          if (property_exists($node, 'field_' . $taxonomy->machine_name)) {
            $terms = !empty($settings['vocab']) && !empty($taxonomy->machine_name) ? field_view_field('node', $node, 'field_' . $taxonomy->machine_name) : '';
          }
          else {
            drupal_set_message(t("Add the 'Term reference' field !vocab_name to the current content type at <a href='#overlay=admin/structure/types/manage/@type/fields%3Fdestination%3D@current_path'>admin/structure/types/manage/@type/fields</a> or <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>exclude the Flickr block</a> in the block 'Visibility settings' for this content type or select another vocabulary to use.", array(
              '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
              '@type' => $node->type,
              '@current_path' => current_path(),
              '@delta' => $delta,
            )), 'error', FALSE);
            $terms = '';
          }
          if (!empty($terms)) {
            $i = 0;
            foreach ($terms['#items'] as $item) {
              $tags[$i] = $terms['#items'][$i]['taxonomy_term']->name;
              $i++;
            }
            $tags = implode(",", $tags);
          }
          else {
            $tags = '';
          }
        }
        else {
          $tags = '';
        }
        if (module_exists('geofield') && !is_numeric($settings['geo']) && !empty($settings['geo'])) {
          $value = field_get_items('node', $node, $settings['geo']);
          $lat = $value[0]['lat'];
          $lon = $value[0]['lon'];
        }
        elseif (module_exists('location_node') && $settings['geo'] == 1 && !empty($node->locations)) {
          $lat = $node->locations[0]['latitude'];
          $lon = $node->locations[0]['longitude'];
        }
        else {
          $lat = NULL;
          $lon = NULL;
        }
        if (module_exists('date') && !is_numeric($settings['date']) && !empty($settings['date'])) {
          $value = field_get_items('node', $node, $settings['date']);
          $datefrom = substr($value[0]['value'], 0, strrpos($value[0]['value'], ' '));
          $dateto = substr($value[0]['value2'], 0, strrpos($value[0]['value2'], ' '));
        }
        else {
          $datefrom = NULL;
          $dateto = NULL;
        }
      }
      else {
        $tags = '';
        $lat = NULL;
        $lon = NULL;
        $datefrom = NULL;
        $dateto = NULL;
      }
      $block['subject'] = NULL;
      $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'], $settings['filter'], $lat, $lon, NULL, $datefrom, $dateto, $settings['extend'], $settings['tag_mode']);
      break;

    // Photos from a Flickr photoset.
    case 7:
    case 8:
      if (($nid = (int) arg(1)) && arg(0) == 'node' && (arg(2) == "" || arg(2) == 'view')) {
        $node = node_load($nid);
        if (function_exists('taxonomy_vocabulary_load') && $settings['vocab'] != 0) {
          $taxonomy = taxonomy_vocabulary_load($settings['vocab']);
          if (property_exists($node, 'field_' . $taxonomy->machine_name)) {
            $terms = !empty($settings['vocab']) && !empty($taxonomy->machine_name) ? field_view_field('node', $node, 'field_' . $taxonomy->machine_name) : '';
          }
          else {
            drupal_set_message(t("Add the 'Term reference' field !vocab_name to the current content type at <a href='#overlay=admin/structure/types/manage/@type/fields%3Fdestination%3D@current_path'>admin/structure/types/manage/@type/fields</a> or <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>exclude the Flickr block</a> in the block 'Visibility settings' for this content type or select another vocabulary to use.", array(
              '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
              '@type' => $node->type,
              '@current_path' => current_path(),
              '@delta' => $delta,
            )), 'error', FALSE);
            $terms = '';
          }
          if (!empty($terms)) {
            $i = 0;
            foreach ($terms['#items'] as $item) {
              $tags[$i] = $terms['#items'][$i]['taxonomy_term']->name;
              $i++;
            }
            $tags = implode(",", $tags);
          }
          else {
            $tags = '';
          }
        }
        else {
          $tags = '';
        }
      }
      else {
        $tags = '';
      }
      $block['subject'] = NULL;
      $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'], NULL, NULL, NULL, NULL, NULL, NULL, $settings['extend']);
      break;

    // Favorite public photos from a Flickr user.
    case 9:
    case 12:
      $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);
        if (function_exists('taxonomy_vocabulary_load') && $settings['vocab'] != 0) {
          $taxonomy = taxonomy_vocabulary_load($settings['vocab']);
          if (property_exists($node, 'field_' . $taxonomy->machine_name)) {
            $terms = !empty($settings['vocab']) && !empty($taxonomy->machine_name) ? field_view_field('node', $node, 'field_' . $taxonomy->machine_name) : '';
          }
          else {
            $terms = '';
            drupal_set_message(t("Add the 'Term reference' field !vocab_name to the current content type at <a href='#overlay=admin/structure/types/manage/@type/fields%3Fdestination%3D@current_path'>admin/structure/types/manage/@type/fields</a> or <a href='#overlay=admin/structure/block/manage/flickr/@delta/configure%3Fdestination%3D@current_path'>exclude the Flickr block</a> in the block 'Visibility settings' for this content type or select another vocabulary to use.", array(
              '!vocab_name' => '<em>' . $taxonomy->name . '</em>',
              '@type' => $node->type,
              '@current_path' => current_path(),
              '@delta' => $delta,
            )), 'error', FALSE);
          }
          if (!empty($terms)) {
            $i = 0;
            foreach ($terms['#items'] as $item) {
              $tags[$i] = $terms['#items'][$i]['taxonomy_term']->name;
              $i++;
            }
            $tags = implode(",", $tags);
          }
          else {
            $tags = '';
          }
        }
        else {
          $tags = '';
        }
      }
      else {
        $tags = '';
      }
      $block['subject'] = NULL;
      $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'], NULL, NULL, NULL, NULL, NULL, NULL, $settings['extend']);
      break;

    // Photos with a specific tag from a Flickr user.
    case 11:
      $block['subject'] = NULL;
      $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'], 0, $settings['filter'], NULL, NULL, NULL, NULL, NULL, $settings['extend'], $settings['tag_mode']);
      break;
  }
  return $block;
}