You are here

function subscriptions_taxonomy_taxa_form in Subscriptions 6

Same name and namespace in other branches
  1. 5.2 subscriptions_taxonomy.module \subscriptions_taxonomy_taxa_form()

Build the Categories subscription form at user/UID/subscriptions/taxa.

1 string reference to 'subscriptions_taxonomy_taxa_form'
subscriptions_taxonomy_page_taxa in ./subscriptions_taxonomy.module
Returns a list of taxonomy subscriptions.

File

./subscriptions_taxonomy.module, line 211
Subscriptions to taxonomy terms.

Code

function subscriptions_taxonomy_taxa_form(&$form_state, $vocabularies, $account, $form) {
  $subscriptions = array();
  $bulk_op = empty($_SESSION['subscriptions']['bulk_op']) ? '' : $_SESSION['subscriptions']['bulk_op'];
  if ($bulk_op) {

    // No initialization for bulk subscription.
    $uid = -DRUPAL_AUTHENTICATED_RID;
  }
  else {
    $arg5 = subscriptions_arg(5);
    $uid = isset($account) ? $account->uid : (is_numeric($arg5) ? -$arg5 : -DRUPAL_AUTHENTICATED_RID);
    $sql = db_rewrite_sql("\n      SELECT s.value, s.send_interval, s.author_uid, s.send_comments, s.send_updates, t.tid, t.vid, t.name\n      FROM {term_data} t\n      INNER JOIN {subscriptions} s ON " . ($GLOBALS['db_type'] == 'pgsql' ? 'CAST(' : '') . "t.tid" . ($GLOBALS['db_type'] == 'pgsql' ? ' AS VARCHAR)' : '') . " = s.value\n      WHERE s.module = 'node' AND s.field = 'tid' AND s.recipient_uid = %d\n      ORDER BY s.author_uid", 't', 'tid');
    $result = db_query($sql, $uid);
    while ($s = db_fetch_array($result)) {
      $subscriptions[$s['vid']][$s['value']][$s['author_uid']] = $s;
      $subscriptions_terms_by_vid[$s['vid']][$s['value']] = $s;
    }
  }
  foreach ($vocabularies as $vocab) {

    // display vocabulary name and group terms together
    $form[$vocab->vid] = array(
      '#type' => 'fieldset',
      '#title' => check_plain($vocab->name),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form[$vocab->vid][0] = array(
      '#tree' => TRUE,
      '#theme' => 'subscriptions_form_table',
    );
    $subscriptions_vid = isset($subscriptions[$vocab->vid]) ? $subscriptions[$vocab->vid] : NULL;
    $vids_to_restrict = variable_get('subscriptions_restricted_taxa', array());
    $tree = NULL;
    if (!in_array($vocab->vid, $vids_to_restrict)) {

      // @ TODO create mechanism to allow users to
      //        subscribe to all terms under this vocabulary
      $tree = taxonomy_get_tree($vocab->vid);
    }
    else {
      if (isset($subscriptions_terms_by_vid[$vocab->vid])) {
        $tree = $subscriptions_terms_by_vid[$vocab->vid];
        $form[$vocab->vid][1] = array(
          '#type' => 'item',
          '#description' => '<div>' . t('This is a restricted category group; deactivated subscriptions will be removed from the list. To subscribe to an unlisted category, go to a post in that category and subscribe from there.') . '</div>',
        );
      }
    }
    $forum_containers = NULL;
    if (module_exists('forum') && $vocab->vid == variable_get('forum_nav_vocabulary', '')) {
      $forum_containers = variable_get('forum_containers', array());
    }
    $defaults = array();
    if (isset($tree)) {
      foreach ($tree as $term) {
        if (is_array($term)) {
          $term = (object) $term;
        }
        $depth = empty($term->depth) ? 0 : $term->depth;
        $title = '<span class="indenter">' . str_repeat('--&nbsp;', $depth) . '</span>' . l($term->name, 'taxonomy/term/' . $term->tid, array(
          'attributes' => array(
            'class' => 'depth-' . $depth,
          ),
        ));
        if (!isset($subscriptions_vid[$term->tid][-1])) {

          // author-less item is missing -- add it here:
          $subscriptions_vid[$term->tid][-1] = array(
            'send_interval' => _subscriptions_get_setting('send_interval', $uid < 0 ? $uid : $account),
            'send_comments' => _subscriptions_get_setting('send_comments', $uid < 0 ? $uid : $account),
            'send_updates' => _subscriptions_get_setting('send_updates', $uid < 0 ? $uid : $account),
            'author_uid' => FALSE,
          );
        }

        // add the active subscriptions
        foreach ($subscriptions_vid[$term->tid] as $author_uid => $subscription) {

          // TODO: support multi-parent hierarchies (currently, the child is not repeated)
          subscriptions_form_helper($form[$vocab->vid][0], $defaults, $author_uid, $term->tid, $title, $subscription);
          if (isset($forum_containers) && in_array($term->tid, $forum_containers)) {
            $tids = array_keys($form[$vocab->vid][0]['checkboxes']);
            $tid = end($tids);
            $form[$vocab->vid][0]['checkboxes'][$tid][-1]['#disabled'] = TRUE;
          }
        }
      }
    }
    else {
      $form[$vocab->vid]['info']['#value'] = '<p>' . t('This is a restricted category group; only subscribed categories show up in this list.<br />To subscribe to a category in this group, go to a post in that category and subscribe from there.') . '</p>';
    }
    $form[$vocab->vid][0]['defaults'] = array(
      '#type' => 'value',
      '#value' => $defaults,
    );
    subscriptions_form_column_filter($form[$vocab->vid][0], $uid);
  }
  if (empty($form)) {
    $form = array(
      '#value' => t('There are no active categories.'),
    );
  }
  else {
    $form['#tree'] = TRUE;
    $form['uid'] = array(
      '#type' => 'value',
      '#value' => $bulk_op ? $_SESSION['subscriptions']['uids'] : $uid,
    );
    $form['access_key'] = array(
      '#type' => 'value',
      '#value' => 'taxa',
    );
    $form['module'] = array(
      '#type' => 'value',
      '#value' => 'node',
    );
    $form['field'] = array(
      '#type' => 'value',
      '#value' => 'tid',
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 10,
    );
    $form['#submit'][] = 'subscriptions_page_form_submit';
  }
  return $form;
}