You are here

function user_relationships_block in User Relationships 5

File

./user_relationships_hooks.inc, line 301

Code

function user_relationships_block($op = 'list', $delta = 0, $edit = array()) {
  $all_types = 'ALL';
  if (0 != strcmp($op, 'list')) {
    $block_rtype = explode('|', $delta);
    $block = $block_rtype[0];

    // 'MY' or 'USER'
    $rtype = $block_rtype[1];

    // valid rtid or $all_types
  }
  $which_rels_options = array(
    1 => 'Newest',
    2 => 'Oldest',
    3 => 'Random',
  );
  $default_option = 1;
  switch ($op) {
    case 'list':

      // return list of all blocks defined by the module
      $relationships = user_relationships_relationship_types_load();
      $block = array();
      foreach ($relationships as $relationship) {
        $block["MY|{$relationship->rtid}"]['info'] = t('My Relationships: @type', array(
          '@type' => drupal_ucfirst($relationship->plural_name),
        ));
        $block["USER|{$relationship->rtid}"]['info'] = t('User Relationships: @type', array(
          '@type' => drupal_ucfirst($relationship->plural_name),
        ));
      }
      $block["MY|{$all_types}"]['info'] = t('My Relationships: All relationships');
      $block["USER|{$all_types}"]['info'] = t('User Relationships: All relationships');
      return $block;
      break;
    case 'configure':
      $form = array();
      if ($block == 'MY') {
        $block_desc = t('NOTE: This block displays relationships of the user who is currently logged in.');
      }
      else {
        $block_desc = t('NOTE: This block displays the relationships of the user whose node is being viewed.');
        $page_types = array();
        $node_types = node_get_types();
        foreach ($node_types as $ntype) {
          $page_types[$ntype->type] = $ntype->name;
        }
        asort($page_types);
        $page_types[USER_PAGES] = t('user pages (/user/<uid>)');
        $default_pages = variable_get("user_relationships_block_{$block}_t_{$rtype}_which_pages", array(
          USER_PAGES,
        ));
        $form['user_relationships_block_display_pages'] = array(
          '#type' => 'select',
          '#multiple' => TRUE,
          '#options' => $page_types,
          '#default_value' => $default_pages,
          '#title' => t('Pages types on which to display this block'),
          '#description' => t('Select the page types on which you want to display this block.'),
          '#required' => TRUE,
          '#weight' => 5,
        );
      }
      $form['user_relationships_block_num_rels'] = array(
        '#type' => 'textfield',
        '#title' => t('Number of relationships to display in block'),
        '#description' => t('Enter the maximum number of relationships to display in this block.'),
        '#size' => 4,
        '#default_value' => variable_get("user_relationships_block_{$block}_t_{$rtype}_num_rels", 8),
        '#weight' => 1,
        '#required' => TRUE,
        '#validate' => array(
          'user_relationships_block_num_rels_validate' => array(),
        ),
      );
      $form['user_relationships_block_which_rels'] = array(
        '#type' => 'radios',
        '#title' => t('Which relationships should be displayed'),
        '#options' => $which_rels_options,
        '#default_value' => variable_get("user_relationships_block_{$block}_t_{$rtype}_which_rels", $default_option),
        '#required' => TRUE,
        '#weight' => 3,
        '#suffix' => $block_desc,
      );
      return $form;
      break;
    case 'save':
      variable_set("user_relationships_block_{$block}_t_{$rtype}_num_rels", (int) $edit['user_relationships_block_num_rels']);
      variable_set("user_relationships_block_{$block}_t_{$rtype}_which_rels", (int) $edit['user_relationships_block_which_rels']);
      variable_set("user_relationships_block_{$block}_t_{$rtype}_which_pages", array_keys($edit['user_relationships_block_display_pages']));
      break;
    case 'view':
      global $user;

      // determine which user's relationships to display
      if ('MY' == $block) {
        if ($user->uid) {
          $viewing_user =& $user;
        }
      }
      else {
        $page_types = variable_get("user_relationships_block_{$block}_t_{$rtype}_which_pages", array());
        if (arg(0) == 'node' && is_numeric(arg(1))) {
          $node = node_load(arg(1));
          if (in_array($node->type, $page_types)) {
            $viewing_user = user_load(array(
              'uid' => $node->uid,
            ));
          }
        }
        elseif (arg(0) == 'user' && is_numeric(arg(1)) && in_array(USER_PAGES, $page_types)) {
          $viewing_user = user_load(array(
            'uid' => arg(1),
          ));
        }
      }

      // if we don't have a valid user to report on, there's nothing to display
      if (!isset($viewing_user)) {
        return NULL;
      }

      // select the appropriate set of relationships based on admin's configuration settings
      $relationships = user_relationships_block_select_relationships($viewing_user, $rtype, variable_get("user_relationships_block_{$block}_t_{$rtype}_num_rels", 8), $which_rels_options, variable_get("user_relationships_block_{$block}_t_{$rtype}_which_rels", $default_option));

      // get the content of the block
      $blk['subject'] = theme('user_relationships_relationships_block_subject', $viewing_user, $relationships, $rtype, $rtype == $all_types ? TRUE : FALSE);
      if ($relationships != NULL) {
        $blk['content'] = theme('user_relationships_relationships_block_content', $viewing_user, $relationships, $rtype, $rtype == $all_types ? TRUE : FALSE);
      }
      else {
        $blk['content'] = theme('user_relationships_relationships_block_empty', $viewing_user, $rtype, $rtype == $all_types ? TRUE : FALSE);
      }
      return $blk;
      break;
  }
}