You are here

function flickr_block in Flickr 6

Same name and namespace in other branches
  1. 5 block/flickr_block.module \flickr_block()

Implements hook_block().

File

block/flickr_block.module, line 17

Code

function flickr_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':

      // User profile page blocks.
      $blocks[0]['info'] = t("User profile page recent Flickr photos");
      $blocks[1]['info'] = t("User profile page recent Flickr photosets");
      $blocks[2]['info'] = t("User profile page random Flickr photos");

      // Site wide blocks.
      $blocks[3]['info'] = t('Recent photos from a Flickr user');
      $blocks[4]['info'] = t('Recent photosets from a Flickr user');
      $blocks[5]['info'] = t('Random photos from a Flickr user');
      $blocks[6]['info'] = t('Recent photosfrom a Flickr group');
      $blocks[7]['info'] = t('Random photos from a Flickr photoset');
      $blocks[8]['info'] = t('Recent photos from a Flickr photoset');

      // Returns a list of favorite public photos for the given user.
      $blocks[9]['info'] = t("Favorite public photos from a Flickr user");
      $blocks[10]['info'] = t("Random photos from a Flickr group");
      $blocks[11]['info'] = t("Random photos with a specific tag from a Flickr user");
      return $blocks;
    case 'configure':

      // Remove the large and original sizes.
      $size_options = array();
      foreach (flickr_photo_sizes() as $size => $info) {
        $size_options[$size] = $info['label'] . ' - ' . $info['description'];
      }
      unset($size_options['b']);
      unset($size_options['o']);

      // Define all of the form element variables and retrieve existing values.
      $settings = variable_get('flickr_block_' . $delta, array(
        'user_id' => '',
        'show_n' => 4,
        'size' => 's',
        'tag' => '',
        'group_id' => '',
        'photoset_id' => '',
        'media' => 'all',
      ));
      if (!isset($settings['tag'])) {
        $settings['tag'] = '';
      }
      if (!isset($settings['group_id'])) {
        $settings['group_id'] = '';
      }
      if (!isset($settings['photoset_id'])) {
        $settings['photoset_id'] = '';
      }

      // Define all elements of the block parameters form.
      $form = array();
      $default_userid = variable_get('flickr_default_userid', '');
      $user_id = array_key_exists('user_id', $settings) ? $settings['user_id'] : '';
      $default = $user_id;
      if (!empty($default)) {
        $info = flickr_people_get_info($default);
        $default = $info['username']['_content'];
      }

      // Require a user id if the site-wide default user has not been set.
      if (empty($default_userid)) {
        $form["flickr_block_{$delta}_user_id"] = array(
          '#type' => 'textfield',
          '#title' => t('Flickr User ID'),
          '#default_value' => $default,
          '#required' => TRUE,
          '#description' => t("The user id of a Flickr user (ID number, username or email). Note that the sites's default Flickr user id has not been set."),
          '#element_validate' => array(
            'flickr_userid_block_validate',
          ),
        );
      }
      else {
        $info = flickr_people_get_info(variable_get('flickr_default_userid', ''));
        $form["flickr_block_{$delta}_user_id"] = array(
          '#type' => 'textfield',
          '#title' => t('Flickr User Id'),
          '#default_value' => $default,
          '#description' => t("A Flickr user ID (ID number, username or email). If this is left blank, the sites's default user will be used. The current default Flickr user is") . " '<em>" . $info['username']['_content'] . "</em>'.",
          '#element_validate' => array(
            'flickr_userid_block_validate',
          ),
        );
      }
      $form["flickr_block_{$delta}_show_n"] = array(
        '#type' => 'textfield',
        '#options' => $count_options,
        '#title' => t('Show <em>n</em> photos'),
        '#required' => TRUE,
        '#default_value' => $settings['show_n'],
        '#element_validate' => array(
          'flickr_block_show_n_validate',
        ),
        '#description' => t("The block will display this many photos."),
        '#size' => 2,
        '#maxlength' => 2,
      );
      $form["flickr_block_{$delta}_size"] = array(
        '#type' => 'select',
        '#options' => $size_options,
        '#title' => t('Size of photos'),
        '#default_value' => $settings['size'],
        '#description' => t("Select the size of photos you'd like to display in the block."),
      );
      $form["flickr_block_{$delta}_group_id"] = array(
        '#type' => 'textfield',
        '#title' => t('Flickr Group id'),
        '#default_value' => $settings['group_id'],
        '#description' => t("The numerical group id."),
      );
      $form["flickr_block_{$delta}_tag"] = array(
        '#type' => 'textfield',
        '#title' => t('Flickr tag'),
        '#default_value' => $settings['tag'],
        '#description' => t("The tag you want to search for."),
      );
      $form["flickr_block_{$delta}_media"] = array(
        '#type' => 'select',
        '#options' => array(
          'all' => t('all'),
          'photos' => t('photos'),
          'videos' => t('videos'),
        ),
        '#title' => t('Media type'),
        '#default_value' => $settings['media'],
        '#description' => t("Filter results by media type."),
      );

      // Build the block form - use unset to hide unwanted form elements.
      switch ($delta) {

        // User page, recent.
        case 0:
          unset($form["flickr_block_{$delta}_user_id"]);
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          break;

        // User page, photosets.
        case 1:
          unset($form["flickr_block_{$delta}_user_id"]);
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);

          // Photoset, not photos.
          $form["flickr_block_{$delta}_show_n"]['#title'] = t('Show the last <em>n</em> photosets');
          $form["flickr_block_{$delta}_show_n"]['#description'] = t("The block will show this many of the user's photosets.");
          unset($form["flickr_block_{$delta}_media"]);
          break;

        // User page, random.
        case 2:
          unset($form["flickr_block_{$delta}_user_id"]);
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          break;

        // Sitewide, recent.
        case 3:
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          break;

        // Sitewide photoset, not photos.
        case 4:
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          $form["flickr_block_{$delta}_show_n"]['#title'] = t('Show the last <em>n</em> photosets');
          $form["flickr_block_{$delta}_show_n"]['#description'] = t("The block will show this many of the user's photosets.");
          unset($form["flickr_block_{$delta}_media"]);
          break;

        // Sitewide, random.
        case 5:
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          break;

        // Sitewide, group.
        case 6:
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          $form["flickr_block_{$delta}_user_id"]['#title'] = t('Show photos from this Group ID');
          $form["flickr_block_{$delta}_user_id"]['#description'] = t('Will select photos from this group pool');
          $form["flickr_block_{$delta}_user_id"]['#required'] = TRUE;
          break;

        // Sitewide, random.
        case 7:
          unset($form["flickr_block_{$delta}_user_id"]);
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          $form["flickr_block_{$delta}_photoset"] = array(
            '#type' => 'textfield',
            '#title' => t('Flickr Photoset ID'),
            '#default_value' => $settings['photoset_id'],
            '#description' => t("The id of a Flickr photoset."),
            '#required' => TRUE,
          );
          break;

        // Sitewide, recent.
        case 8:
          unset($form["flickr_block_{$delta}_user_id"]);
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          $form["flickr_block_{$delta}_photoset"] = array(
            '#type' => 'textfield',
            '#title' => t('Flickr Photoset Id'),
            '#default_value' => $settings['photoset_id'],
            '#description' => t("The id of a Flickr photoset."),
            '#required' => TRUE,
          );
          break;

        // List of favorite public photos for the given user.
        case 9:
          unset($form["flickr_block_{$delta}_group_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          break;

        // Group, random.
        case 10:
          unset($form["flickr_block_{$delta}_user_id"]);
          unset($form["flickr_block_{$delta}_tag"]);
          $form["flickr_block_{$delta}_group_id"]['#title'] = t('Show photos from this Group ID');
          $form["flickr_block_{$delta}_group_id"]['#description'] = t('Will select random photos from this group pool');
          $form["flickr_block_{$delta}_group_id"]['#required'] = TRUE;
          break;

        // Tag, random.
        case 11:
          unset($form["flickr_block_{$delta}_group_id"]);
          $form["flickr_block_{$delta}_tag"]['#title'] = t('Show photos having this tag');
          $form["flickr_block_{$delta}_tag"]['#description'] = t("Will select random photos from the user's photos having this tag");
          $form["flickr_block_{$delta}_tag"]['#required'] = TRUE;
          break;
      }
      return $form;
    case 'save':
      switch ($delta) {
        case 0:
        case 2:
          variable_set('flickr_block_' . $delta, array(
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
            'media' => $edit["flickr_block_{$delta}_media"],
          ));
          break;
        case 1:
          variable_set('flickr_block_' . $delta, array(
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
          ));
          break;
        case 3:
        case 4:
        case 5:
        case 6:
        case 9:
          $userid = $edit["flickr_block_{$delta}_user_id"];
          if (!empty($userid)) {
            if (!flickr_is_nsid($edit["flickr_block_{$delta}_user_id"])) {
              if ($user = flickr_user_find_by_identifier($userid)) {
                drupal_set_message(t("The Flickr user associated with '%userid' has internally been replaced with the corresponding Flickr ID '%uid'.", array(
                  '%userid' => $edit["flickr_block_{$delta}_user_id"],
                  '%uid' => $user,
                )));
                $edit["flickr_block_{$delta}_user_id"] = $user;
              }
            }
            else {
              $info = flickr_people_get_info($edit["flickr_block_{$delta}_user_id"]);
              drupal_set_message(t("The Flickr user associated with '%uid' will be shown to you as Flickr user '%userid'.", array(
                '%uid' => $edit["flickr_block_{$delta}_user_id"],
                '%userid' => $info['username']['_content'],
              )));
            }
          }
          variable_set('flickr_block_' . $delta, array(
            'user_id' => $edit["flickr_block_{$delta}_user_id"],
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
            'media' => $edit["flickr_block_{$delta}_media"],
          ));
          break;
        case 7:
          variable_set('flickr_block_' . $delta, array(
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
            'media' => $edit["flickr_block_{$delta}_media"],
            'photoset_id' => $edit["flickr_block_{$delta}_photoset"],
          ));
          break;
        case 8:
          variable_set('flickr_block_' . $delta, array(
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
            'media' => $edit["flickr_block_{$delta}_media"],
            'photoset_id' => $edit["flickr_block_{$delta}_photoset"],
          ));
          break;
        case 10:
          variable_set('flickr_block_' . $delta, array(
            'group_id' => $edit["flickr_block_{$delta}_group_id"],
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
            'media' => $edit["flickr_block_{$delta}_media"],
          ));
          break;
        case 11:
          $userid = $edit["flickr_block_{$delta}_user_id"];
          if (!empty($userid)) {
            if (!flickr_is_nsid($edit["flickr_block_{$delta}_user_id"])) {
              if ($user = flickr_user_find_by_identifier($userid)) {
                drupal_set_message(t("The Flickr user associated with '%userid' has internally been replaced with the corresponding Flickr ID '%uid'.", array(
                  '%userid' => $edit["flickr_block_{$delta}_user_id"],
                  '%uid' => $user,
                )));
                $edit["flickr_block_{$delta}_user_id"] = $user;
              }
            }
            else {
              $info = flickr_people_get_info($edit["flickr_block_{$delta}_user_id"]);
              drupal_set_message(t("The Flickr user associated with '%uid' will be shown to you as Flickr user '%userid'.", array(
                '%uid' => $edit["flickr_block_{$delta}_user_id"],
                '%userid' => $info['username']['_content'],
              )));
            }
          }
          variable_set('flickr_block_' . $delta, array(
            'user_id' => $edit["flickr_block_{$delta}_user_id"],
            'tag' => $edit["flickr_block_{$delta}_tag"],
            'show_n' => (int) $edit["flickr_block_{$delta}_show_n"],
            'size' => $edit["flickr_block_{$delta}_size"],
            'media' => $edit["flickr_block_{$delta}_media"],
          ));
          break;
      }
    case 'view':
    default:
      if (variable_get('flickr_css', 1)) {
        drupal_add_css(drupal_get_path('module', 'flickr') . '/flickr.css');
      }
      $settings = variable_get('flickr_block_' . $delta, array(
        'user_id' => '',
        'show_n' => 4,
        'size' => 's',
        'tag' => '',
        'group_id' => '',
        'photoset_id' => '',
        'media' => 'all',
      ));
      if (!isset($settings['tag'])) {
        $settings['tag'] = '';
      }
      if (!isset($settings['group_id'])) {
        $settings['group_id'] = '';
      }
      if (!isset($settings['photoset_id'])) {
        $settings['photoset_id'] = '';
      }

      // Get the default user id as a fallback.
      if ($settings['user_id'] == '') {
        $settings['user_id'] = variable_get('flickr_default_userid', '');

        // TODO: better name would be flickr_default_identifier.
      }
      $settings['user_id'] = flickr_user_find_by_identifier($settings['user_id']);
      $block = array();
      switch ($delta) {
        case 0:
        case 1:
        case 2:

          // Get per user nsid if necessary.
          if (arg(0) == 'user' && ($uid = (int) arg(1))) {
            if ($user = user_load($uid)) {
              if (!empty($user->flickr['nsid'])) {
                if ($delta == 0) {
                  $block['subject'] = t("%username's recent Flickr photos", array(
                    '%username' => $user->name,
                  ));
                  $block['content'] = _flickr_block_recent($user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media']);
                }
                elseif ($delta == 1) {
                  $block['subject'] = t("%username's recent Flickr photosets", array(
                    '%username' => $user->name,
                  ));
                  $block['content'] = _flickr_block_photosets($user->flickr['nsid'], $settings['show_n'], $settings['size']);
                }
                elseif ($delta == 2) {
                  $block['subject'] = t("%username's random Flickr photos", array(
                    '%username' => $user->name,
                  ));
                  $block['content'] = _flickr_block_random($user->flickr['nsid'], $settings['show_n'], $settings['size'], $settings['media']);
                }
              }
            }
          }
          break;
        case 3:
          $block['subject'] = t('Flickr recent photos');
          $block['content'] = _flickr_block_recent($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media']);
          break;
        case 4:
          $block['subject'] = t('Flickr recent photosets');
          $block['content'] = _flickr_block_photosets($settings['user_id'], $settings['show_n'], $settings['size']);
          break;
        case 5:
          $block['subject'] = t('Flickr random photos');
          $block['content'] = _flickr_block_random($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media']);
          break;
        case 6:
          $block['subject'] = t('Flickr recent group photos');
          $block['content'] = _flickr_block_group_recent($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media']);
          break;
        case 7:
          $block['subject'] = t('Flickr random photoset photos');
          $block['content'] = _flickr_block_photoset_random($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $settings['photoset_id']);
          break;
        case 8:
          $block['subject'] = t('Flickr recent photoset photos');
          $block['content'] = _flickr_block_photoset_recent($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $settings['photoset_id']);
          break;
        case 9:
          $block['subject'] = t('Flickr favorite public photos');
          $block['content'] = _flickr_block_favorite_public($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media']);
          break;
        case 10:
          $block['subject'] = t('Flickr group random photos');
          $block['content'] = _flickr_block_group_random($settings['group_id'], $settings['show_n'], $settings['size'], $settings['media']);
          break;
        case 11:
          $block['subject'] = t('Flickr tag random photos');
          $block['content'] = _flickr_block_tag_random($settings['user_id'], $settings['show_n'], $settings['size'], $settings['media'], $settings['tag']);
          break;
      }
      return $block;
  }
}