You are here

notifications_tags.module in Notifications 5

Subscriptions to taxonomy terms

File

notifications_tags/notifications_tags.module
View source
<?php

/**
 * @file
 * Subscriptions to taxonomy terms
 */

/**
 * Implementation of hook_menu_()
 */
function notifications_tags_menu($may_cache) {
  global $user;

  // we need the user to to build some urls
  $items = array();
  if (!$may_cache) {
    if ($user->uid && arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == 'notifications' && ($user->uid == arg(1) || user_access('administer notifications'))) {
      $account = $user->uid == arg(1) ? $user : user_load(array(
        'uid' => arg(1),
      ));
      $items[] = array(
        'path' => 'user/' . $account->uid . '/notifications/taxonomy',
        'type' => MENU_LOCAL_TASK,
        'access' => user_access('subscribe to taxonomy terms'),
        'title' => t('Tags'),
        'callback' => 'notifications_tags_user_page',
        'callback arguments' => array(
          $account,
        ),
        'weight' => 10,
      );
    }
  }
  return $items;
}

/**
 * Implementation of hook_perm()
 */
function notifications_tags_perm() {
  return array(
    'subscribe to taxonomy terms',
  );
}

/**
 * Implementation of hook_notifications().
 */
function notifications_tags_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'names':
      $subs =& $arg0;
      if ($subs->event_type == 'node') {
        if (!empty($subs->fields['tid'])) {
          $term = taxonomy_get_term($subs->fields['tid']);
          $subs->names['term'] = t('Term: %name', array(
            '%name' => $term->name,
          ));
        }
      }
      break;
    case 'subscription types':
      $types['taxonomy'] = array(
        'event_type' => 'node',
        'title' => t('Tags'),
        'access' => 'subscribe to taxonomy terms',
        'fields' => array(
          'tid',
        ),
      );
      return $types;
    case 'subscription fields':

      // Information about available fields for subscriptions
      $fields['tid'] = array(
        'name' => t('Taxonomy term'),
        'field' => 'tid',
        'type' => 'int',
      );
      return $fields;
    case 'query':
      if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) || $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
        if ($tids = notifications_tags_node_get_terms($node->nid)) {
          $query[]['fields']['tid'] = $tids;
          return $query;
        }
      }
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 * 
 * Admin settings form. Omitted taxonomy vocabularies.
 */
function notifications_tags_form_alter($form_id, &$form) {
  if ($form_id == 'notifications_content_settings_form') {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $vocabulary) {
      $select[$vocabulary->vid] = $vocabulary->name;
    }
    $form['tags'] = array(
      '#type' => 'fieldset',
      '#title' => t('Tag subscriptions'),
      '#collapsible' => TRUE,
      '#weight' => 0,
    );
    $form['tags']['notifications_tags_vocabularies'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Allowed vocabularies'),
      '#default_value' => notifications_tags_vocabularies('vid'),
      '#options' => $select,
      '#description' => t('Select vocabularies to which subscriptions should be <em>allowed</em>.'),
      '#multiple' => TRUE,
    );
  }
}

/**
 * Returns a list of taxonomy subscriptions
 */
function notifications_tags_user_page($account = NULL) {
  global $user;
  if (is_null($account)) {
    $account = $user;
  }
  return drupal_get_form('notifications_tags_user_form', $account);
}

/**
 * Returns the taxonomy subscription form
 */
function notifications_tags_user_form($account) {

  // query string for category subscriptions
  $vocabularies = notifications_tags_vocabularies();
  $subscriptions = notifications_get_subscriptions(array(
    'type' => 'taxonomy',
    'uid' => $account->uid,
  ), array(
    'tid' => NULL,
  ), TRUE, 'value');

  // Complete defaults
  $defaults = array(
    'sid' => 0,
    'send_interval' => notifications_user_setting('send_interval', $account),
    'send_method' => notifications_user_setting('send_method', $account),
    'type' => 'taxonomy',
    'event_type' => 'node',
  );
  $form['defaults'] = array(
    '#type' => 'value',
    '#value' => $defaults,
  );
  $form['account'] = array(
    '#type' => 'value',
    '#value' => $account,
  );
  $form['current'] = array(
    '#type' => 'value',
    '#value' => $subscriptions,
  );
  $form['subscription_fields'] = array(
    '#type' => 'value',
    '#value' => array(),
  );

  //$subsrows['subform'][] = array('#value' => t('Current subscriptions:'));
  $form['subscriptions'] = array(
    '#tree' => TRUE,
  );
  foreach ($vocabularies as $vid => $vocab) {

    // display vocabulary name and group terms together
    $form['subscriptions'][$vid] = array(
      '#type' => 'fieldset',
      '#title' => $vocab->name,
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#theme' => 'notifications_form_table',
      '#header' => array(
        theme('table_select_header_cell'),
        t('Term'),
        t('Send interval'),
        t('Send method'),
      ),
      '#parents' => array(
        'subscriptions',
      ),
    );

    // @ TODO create mechanism to allow users to
    //        subscribe to all terms under this vocabulary
    $tree = taxonomy_get_tree($vocab->vid);
    $field = 'tid';
    foreach ($tree as $term) {
      $key = $term->tid;
      $rowdefaults = isset($subscriptions[$key]) ? (array) $subscriptions[$key] : array();
      $rowdefaults += $defaults;
      $form['subscriptions'][$vid]['checkbox'][$key] = array(
        '#type' => 'checkbox',
        '#default_value' => $rowdefaults['sid'],
      );
      $form['subscriptions'][$vid]['title'][$key] = array(
        '#value' => $term->name,
      );
      $form['subscriptions'][$vid]['send_interval'][$key] = array(
        '#type' => 'select',
        '#options' => _notifications_send_intervals(),
        '#default_value' => $rowdefaults['send_interval'],
      );
      $form['subscriptions'][$vid]['send_method'][$key] = array(
        '#type' => 'select',
        '#options' => _notifications_send_methods(),
        '#default_value' => $rowdefaults['send_method'],
      );

      // Pass on the fields for processing
      $form['subscription_fields']['#value'][$key] = array(
        $field => $key,
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['#submit']['notifications_content_form_submit'] = array();
  return $form;
}

/**
 * Get list of allowed vocabularies
 * 
 * @param $field
 *   Optional field to retrieve as array value.
 *   If empty the whole vocalubary object will be returned.
 */
function notifications_tags_vocabularies($field = NULL) {
  $allvocab = taxonomy_get_vocabularies();
  if ($allowed = variable_get('notifications_tags_vocabularies', array())) {
    $vocabularies = array();
    foreach (array_filter($allowed) as $vid) {
      $vocabularies[$vid] = $allvocab[$vid];
    }
  }
  else {
    $vocabularies = $allvocab;
  }
  if ($field) {
    $list = array();
    foreach ($vocabularies as $vid => $vocab) {
      $list[$vid] = $vocab->{$field};
    }
    return $list;
  }
  else {
    return $vocabularies;
  }
}

/**
 * Helper function to get latest node terms.
 * 
 * We cannot use the one from taxonomy module because it has static caching and we'll be sending
 * notifications right after the node has been updated
 */
function notifications_tags_node_get_terms($nid) {
  static $terms;
  if (!isset($terms[$nid])) {
    $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $nid);
    $terms[$nid] = array();
    while ($term = db_fetch_object($result)) {
      $terms[$nid][] = $term->tid;
    }
  }
  return $terms[$nid];
}

Functions

Namesort descending Description
notifications_tags_form_alter Implementation of hook_form_alter().
notifications_tags_menu Implementation of hook_menu_()
notifications_tags_node_get_terms Helper function to get latest node terms.
notifications_tags_notifications Implementation of hook_notifications().
notifications_tags_perm Implementation of hook_perm()
notifications_tags_user_form Returns the taxonomy subscription form
notifications_tags_user_page Returns a list of taxonomy subscriptions
notifications_tags_vocabularies Get list of allowed vocabularies