You are here

function subscriptions_page in Subscriptions 5

Same name and namespace in other branches
  1. 5.2 subscriptions.admin.inc \subscriptions_page()
  2. 6 subscriptions.admin.inc \subscriptions_page()

displays subscribed content data on user and subuscription pages @ TODO clean up all of these parts

1 string reference to 'subscriptions_page'
subscriptions_menu in ./subscriptions.module
Implementation of hook_menu().

File

./subscriptions.module, line 1338

Code

function subscriptions_page($uid, $display_type = NULL) {
  $account = user_load(array(
    'uid' => $uid,
  ));
  $subscribed = FALSE;
  if (!arg(2)) {
    $sid = arg(1);
    $nid = $sid;
    $op = arg(0);
  }
  else {
    $op = arg(1);
    $stype = arg(2);
    $sid = arg(3);
    $nid = arg(4);
    if ($stype == 'node') {
      $nid = $sid;
    }
  }

  //  determine return location
  if (is_null($_SERVER['HTTP_REFERER'])) {
    $rtnloc = "node/{$node->nid}";
  }
  else {
    if (variable_get('clean_url', 0) == 1) {

      // clean URLs on
      global $base_url;

      // extract $base_url from $_SERVER['HTTP_REFERER']
      $istart = strlen($base_url) + 1;
      $rtnloc = substr($_SERVER['HTTP_REFERER'], $istart);
    }
    else {

      // split $_SERVER['HTTP_REFERER'] at "q="
      if (strpos($_SERVER['HTTP_REFERER'], 'q=') > 0) {
        $istart = strpos($_SERVER['HTTP_REFERER'], 'q=' + 2);
      }
      else {
        $istart = 0;
      }
      $rtnloc = substr($_SERVER['HTTP_REFERER'], $istart);
      $rtnloc = urldecode($rtnloc);
    }
    $return = $_SERVER['HTTP_REFERER'];
  }

  // end determine return location
  $message = "";
  switch ($op) {

    // inserts a new subscription into the subscriptions_nodes table
    case 'add':
      subscriptions_add($sid, $uid, $stype);
      $message = t('Your subscription was activated.');
      drupal_set_message($message);
      drupal_goto($rtnloc);
      break;

    // removes a subscription from the subscriptions_nodes table
    case 'del':
      db_query('DELETE FROM {subscriptions} WHERE sid = %d AND uid = %d AND stype = \'%s\'', $sid, $uid, $stype);
      $message = t('Your subscription was deactivated.');
      drupal_set_message($message);
      drupal_goto($rtnloc);
      break;

    // Base report for admin functions
    case 'admin':

      // get all subscriptions for all users
      $subscriptions = subscriptions_get_summary();

      // build node rows
      foreach ($subscriptions['node'] as $nsub) {
        $subrowsn[] = array(
          t('thread'),
          l($nsub->title, 'node/' . $nsub->nid),
          $nsub->ncount,
        );
      }

      // build blog rows
      foreach ($subscriptions['blog'] as $bsub) {
        $subrowsb[] = array(
          t('blog'),
          l($bsub->name, 'blog/' . $bsub->uid),
          $bsub->ncount,
        );
      }

      // traverse the taxonomy tree
      $taxa = subscriptions_get_taxa_count();

      // omit undesired vocabularies from listing
      $vocabularies = taxonomy_get_vocabularies();
      $omits = variable_get('subscriptions_omitted_taxa', array());
      foreach ($omits as $omit) {
        unset($vocabularies[$omit]);
      }
      foreach ($vocabularies as $vocab) {
        $tree = taxonomy_get_tree($vocab->vid);
        foreach ($tree as $term) {
          $subrowst[] = array(
            t('category'),
            $vocab->name . ': ' . l($term->name, 'taxonomy/term/' . $term->tid),
            is_null($taxa[$term->tid]) ? '0' : $taxa[$term->tid],
          );
        }
      }

      // build content type rows
      $tree = node_get_types();
      foreach ($tree as $ntype => $nname) {
        $count = 0;
        foreach ($subscriptions['type'] as $tpsub) {
          if (substr($tpsub->stype, 4) == $ntype) {
            $count = $tpsub->ncount;
          }
        }
        $subrowstp[] = array(
          t('content type'),
          l($nname, $ntype),
          $count,
        );
      }

      // concatentate the arrays
      $headers = array(
        t('type'),
        t('title'),
        t('subscribers'),
      );
      $subrows = array_merge((array) $subrowsn, (array) $subrowsb, (array) $subrowst, (array) $subrowstp);

      // assemble output
      if (!$subrows) {
        $message .= t('<p>No threads or categories are currently subscribed.</p>');
      }
      else {
        $message .= theme('table', $headers, $subrows, array(
          'id' => 'subscriptions',
        ));
      }
      drupal_set_title(t('Subscriptions Summary'));
      return $message;
      break;

    // determines the user's subscription status and displays the right option to change it
    default:

      // set output by type
      switch ($display_type) {
        case 'blogs':
          $output = subscriptions_blogs($account);
          break;
        case 'taxonomy':
          $output = subscriptions_taxa($account);
          break;
        case 'content':
          $output = subscriptions_nodes($account);
          break;
        case 'type':
          $output = subscriptions_type($account);
          break;
      }
      $message .= theme('box', '', $output);
      $message .= theme('xml_icon', url("subscriptions/feed"));
      drupal_add_link(array(
        'rel' => 'alternate',
        'type' => 'application/rss+xml',
        'title' => t("!name Subscriptions", array(
          '!name' => $user->name,
        )),
        'href' => url('subscriptions/feed'),
      ));
      return $message;
  }
}