Index: notifications_tags.module
===================================================================
--- notifications_tags.module	(revision 577)
+++ notifications_tags.module	(working copy)
@@ -1,245 +1,270 @@
-<?php
-// $Id: notifications_tags.module,v 1.1.2.5.2.4 2008/08/09 19:40:23 jareyero Exp $
-/**
- * @file
- * Subscriptions to taxonomy terms
- */
-
-/**
- * Implementation of hook_menu_()
- */
-function notifications_tags_menu() {
-  $items['user/%user/notifications/taxonomy'] = array(
-    'type' => MENU_LOCAL_TASK,
-    'access callback' => 'notifications_content_access',
-    'access arguments' => array(1, 'subscribe to taxonomy terms'),
-    'title' => t('Tags'),
-    'page callback' => 'notifications_tags_user_page',
-    'page arguments' => array(1),
-    '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 'query':
-      if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) || $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
-        $params[] = array(
-          'join' => "LEFT JOIN {term_node} t ON f.field = 'tid' AND f.value = CAST(t.tid AS CHAR)",
-          'where' => 't.nid = %d',
-          'args' => array($node->nid),
-        );
-        return $params;
-      }
-      break;
-    /* We cannot add these options for every taxonomy term, will be too much
-    case 'node options':
-        $account = &$arg0;
-        $node = &$arg1;
-        $options = array();        
-        $vocabularies = notifications_tags_vocabularies();
-        foreach ($node->taxonomy as $tid => $term) {
-          if (isset($vocabularies[$term->vid])) {
-            // Taxonomy term
-            $options['taxonomy']['tid'][] = array(
-              'name' => t('Subscribe to posts in %term.', array('%term' => $term->name)),
-              'fields' => array('tid' => $tid),
-            );
-            $options['taxonomy']['tid_author'][] = array(
-              'name' => t('Subscribe to posts in %term by %name', array('%term' => $term->name, '%name' => $node->name)),
-              'fields' => array('tid' => $tid, 'author' => $node->uid),
-            );
-          }
-        }
-        return $options;
-    */
-  }
-}
-
-/**
- * Implementation of hook_form_alter().
- * 
- * Admin settings form. Omitted taxonomy vocabularies.
- */
-function notifications_tags_form_alter(&$form, $form_state, $form_id) {
-  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($form_state, $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,
-  );
-  // Hide send methods if only one
-  $send_methods = _notifications_send_methods();
-  $header = array(theme('table_select_header_cell'), t('Term'), t('Send interval'));
-  if (count($send_methods) > 1) {
-    $header[] = t('Send method');
-  }  
-  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' => $header,
-      '#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'],
-      );
-      
-      if (count($send_methods) > 1) {
-        $form['subscriptions'][$vid]['send_method'][$key] = array(
-          '#type' => 'select',
-          '#options' => _notifications_send_methods(),
-          '#default_value' => $rowdefaults['send_method'],
-        );
-      } else {
-        $form['subscriptions'][$vid]['send_method'][$key] =  array('#type' => 'value', '#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';
-  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;
-  }
-}
+<?php
+// $Id: notifications_tags.module,v 1.1.2.5.2.4 2008/08/09 19:40:23 jareyero Exp $
+/**
+ * @file
+ * Subscriptions to taxonomy terms
+ */
+
+/**
+ * Implementation of hook_menu_()
+ */
+function notifications_tags_menu() {
+  $items['admin/messaging/notifications/taxonomy'] = array(
+    'title' => 'Taxonomy',
+    'description' => 'Taxonomy subscription settings.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('notifications_tags_settings_form'),
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_LOCAL_TASK,
+    );
+  $items['user/%user/notifications/taxonomy'] = array(
+    'type' => MENU_LOCAL_TASK,
+    'access callback' => 'notifications_content_access',
+    'access arguments' => array(1, 'subscribe to taxonomy terms'),
+    'title' => t('Tags'),
+    'page callback' => 'notifications_tags_user_page',
+    'page arguments' => array(1),
+    'weight' => 10,
+  );
+  return $items;
+}
+
+function notifications_tags_settings_form(){
+    $form['notifications_tags_showsubscribed'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('Limit the displayed taxonomy terms in User Interface form to subscribed taxonomy terms only'),
+    '#default_value' => variable_get('notifications_tags_showsubscribed', 1),
+    '#description'   => t('When you have too many taxonomy terms causing the Tags tab to run out of memory under Notifications UI, enabling this setting can fix the problem by only displaying the subscribed taxonomy terms. Note that if you enable this setting, you will have to provide your users an alternative way to subscribe to taxonomy items, such as providing links like: notifications/subscribe/%user/taxonomy/tid/%tid . The link will bring up the standard subscription confirmation dialog.'),
+    );
+    return system_settings_form($form);
+}
+
+/**
+ * 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 'query':
+      if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) || $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
+        $params[] = array(
+          'join' => "LEFT JOIN {term_node} t ON f.field = 'tid' AND f.value = CAST(t.tid AS CHAR)",
+          'where' => 't.nid = %d',
+          'args' => array($node->nid),
+        );
+        return $params;
+      }
+      break;
+    /* We cannot add these options for every taxonomy term, will be too much
+    case 'node options':
+        $account = &$arg0;
+        $node = &$arg1;
+        $options = array();        
+        $vocabularies = notifications_tags_vocabularies();
+        foreach ($node->taxonomy as $tid => $term) {
+          if (isset($vocabularies[$term->vid])) {
+            // Taxonomy term
+            $options['taxonomy']['tid'][] = array(
+              'name' => t('Subscribe to posts in %term.', array('%term' => $term->name)),
+              'fields' => array('tid' => $tid),
+            );
+            $options['taxonomy']['tid_author'][] = array(
+              'name' => t('Subscribe to posts in %term by %name', array('%term' => $term->name, '%name' => $node->name)),
+              'fields' => array('tid' => $tid, 'author' => $node->uid),
+            );
+          }
+        }
+        return $options;
+    */
+  }
+}
+
+/**
+ * Implementation of hook_form_alter().
+ * 
+ * Admin settings form. Omitted taxonomy vocabularies.
+ */
+function notifications_tags_form_alter(&$form, $form_state, $form_id) {
+  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;
+  }
+  $subscriptions = notifications_get_subscriptions(array('type' => 'taxonomy', 'uid' =>           $account->uid), array('tid' => NULL), TRUE, 'value');
+  // If show only the subscribed items setting is enabled and there are no subscriptions, return a string saying so
+  if (!count($subscriptions) && variable_get('notifications_tags_showsubscribed', 1)) {
+    return t('You are not currently subscribed to any taxonomy items');
+  }
+  return drupal_get_form('notifications_tags_user_form',  $account, $subscriptions);
+}
+
+/**
+ * Returns the taxonomy subscription form
+ */
+function notifications_tags_user_form($form_state, $account, $subscriptions) {
+  // query string for category subscriptions
+  $vocabularies = notifications_tags_vocabularies();  
+  // 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,
+  );
+  // Hide send methods if only one
+  $send_methods = _notifications_send_methods();
+  $header = array(theme('table_select_header_cell'), t('Term'), t('Send interval'));
+  if (count($send_methods) > 1) {
+    $header[] = t('Send method');
+  }  
+  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' => $header,
+      '#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;
+      // If show only the subscribed items setting is enabled, omit the term if the user hasn't subscribed to it.
+      if (!isset($subscriptions[$key]) && variable_get('notifications_tags_showsubscribed', 1)){
+        continue;
+      }
+      $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'],
+      );
+      
+      if (count($send_methods) > 1) {
+        $form['subscriptions'][$vid]['send_method'][$key] = array(
+          '#type' => 'select',
+          '#options' => _notifications_send_methods(),
+          '#default_value' => $rowdefaults['send_method'],
+        );
+      } else {
+        $form['subscriptions'][$vid]['send_method'][$key] =  array('#type' => 'value', '#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';
+  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;
+  }
+}
