subscriptions_taxonomy.module

Tracking 7.x-1.x branch
  1. drupal
    1. 5 contributions/subscriptions/subscriptions_taxonomy.module
    2. 6 contributions/subscriptions/subscriptions_taxonomy.module
    3. 7 contributions/subscriptions/subscriptions_taxonomy.module

Subscriptions to taxonomy terms.

Subscriptions_taxonomy extends the subscription module to allow users to subscribe by taxonomy term. If a user subscribes to a term he will receive notifications each time a node is published to that taxonomy term. The user can also select to receive notifications when such a node is updated or commented.

Functions & methods

NameDescription
subscriptions_taxonomy_dataProvides the data for resolving tokens.
subscriptions_taxonomy_disableImplements hook_disable().
subscriptions_taxonomy_form_subscriptions_settings_form_alterImplements hook_form_alter().
subscriptions_taxonomy_page_taxaReturns a list of taxonomy subscriptions.
subscriptions_taxonomy_subscriptionsImplements hook_subscriptions().
subscriptions_taxonomy_taxonomyImplements hook_taxonomy().
_subscriptions_taxonomy_node_optionsImplements _hook_node_options(), subhook of hook_subscriptions().
_subscriptions_taxonomy_typesImplements _hook_types(), subhook of hook_subscriptions().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Subscriptions to taxonomy terms.
  5. *
  6. * Subscriptions_taxonomy extends the subscription module to allow users to
  7. * subscribe by taxonomy term. If a user subscribes to a term he will receive
  8. * notifications each time a node is published to that taxonomy term. The user
  9. * can also select to receive notifications when such a node is updated or
  10. * commented.
  11. */
  12. /**
  13. * Implements hook_subscriptions().
  14. *
  15. * @param $op
  16. * @param null $arg0
  17. * @param null $arg1
  18. * @param null $arg2
  19. *
  20. * @return array|null
  21. *
  22. * @ingroup hooks
  23. */
  24. function subscriptions_taxonomy_subscriptions($op, $arg0 = NULL, $arg1 = NULL, $arg2 = NULL) {
  25. static $stypes = array('taxa' => array('node', 'tid'));
  26. $function = '_subscriptions_taxonomy_' . $op;
  27. if (function_exists($function)) {
  28. return $function($arg0, $arg1, $arg2);
  29. }
  30. switch ($op) {
  31. case 'queue':
  32. // $arg0 is $event array.
  33. if ($arg0['module'] == 'node') {
  34. $node = $arg0['node'];
  35. $params['node']['tid'] = array(
  36. 'join' => array('table' => 'taxonomy_index', 'alias' => 'tn', 'on' => (db_driver() != 'pgsql' ? 's.value = tn.tid' : 's.value = CAST(tn.tid AS VARCHAR)')),
  37. 'where' => array(array('tn.nid', $node->nid, '=')),
  38. 'groupby' => 'tn.nid',
  39. );
  40. if ($arg0['type'] == 'comment') {
  41. $params['node']['tid']['where'][] = array('s.send_comments', 1, '=');
  42. }
  43. elseif ($arg0['type'] == 'node' && $arg0['action'] == 'update') {
  44. $params['node']['tid']['where'][] = array('s.send_updates', 1, '=');
  45. }
  46. return $params;
  47. }
  48. break;
  49. case 'fields': // $arg0 is module.
  50. if ($arg0 == 'node' || $arg0 == 'comment') {
  51. $tr = 't';
  52. return array(
  53. 'tid' => array(
  54. 'data_function' => 'subscriptions_taxonomy_data',
  55. 'subs_mod' => 'subscriptions_taxonomy',
  56. 'subs_type' => $tr('category'),
  57. 'mailkey' => 'node-type-',
  58. ),
  59. );
  60. }
  61. break;
  62. case 'stypes':
  63. return $stypes;
  64. case 'stype':
  65. return (isset($stypes[$arg0]) ? array_merge( $stypes[$arg0], array($arg1, $arg2)) : NULL);
  66. }
  67. return NULL;
  68. }
  69. /**
  70. * Implements _hook_node_options(), subhook of hook_subscriptions().
  71. *
  72. * This is called by subscriptions_ui_node_form() in subscriptions_ui.module.
  73. *
  74. * @param $account
  75. * @param $node
  76. *
  77. * @return array|null
  78. *
  79. * @ingroup form
  80. * @ingroup hooks
  81. *
  82. * @see subscriptions_ui_node_form()
  83. */
  84. function _subscriptions_taxonomy_node_options($account, $node) {
  85. if (!user_access('subscribe to taxonomy terms')) {
  86. return NULL;
  87. }
  88. $options = array();
  89. /** @var $vids_to_omit array */
  90. $vids_to_omit = variable_get('subscriptions_omitted_taxa', array());
  91. foreach ((array) $node as $field_key => $field_value) {
  92. if (is_array($field_value)) {
  93. foreach ($field_value as $lang_key => $field_lang_value) {
  94. if (is_array($field_lang_value)) {
  95. foreach ($field_lang_value as $item) {
  96. if (is_array($item)) {
  97. if (isset($item['taxonomy_term'])) {
  98. $term = $item['taxonomy_term'];
  99. if (!in_array($term->vid, $vids_to_omit)) {
  100. $tid = $term->tid;
  101. $options['tid'][] = array(
  102. 'name' => t('To content in %term', array('%term' => $term->name)),
  103. 'params' => array('module' => 'node', 'field' => 'tid', 'value' => $tid),
  104. 'link' => 'taxa/' . $tid,
  105. );
  106. $options['tid'][] = array(
  107. 'name' => t('To content in %term by %name', array('%term' => $term->name, '%name' => format_username(user_load($node->uid)))),
  108. 'params' => array('module' => 'node', 'field' => 'tid', 'value' => $tid, 'author_uid' => $node->uid),
  109. 'link' => 'taxa/' . $tid . '/' . $node->uid,
  110. );
  111. if ($field_key == 'taxonomy_forums') {
  112. // Move forum items to the top.
  113. array_unshift($options['tid'], array_pop($options['tid']));
  114. array_unshift($options['tid'], array_pop($options['tid']));
  115. }
  116. $options['tid']['weight'] = -1;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return $options;
  126. }
  127. /**
  128. * Implements _hook_types(), subhook of hook_subscriptions().
  129. *
  130. * This is called by subscriptions_types() in subscriptions.module.
  131. *
  132. * @return array
  133. * Returns information about types for Subscriptions module interface.
  134. *
  135. * @ingroup form
  136. * @ingroup hooks
  137. *
  138. * @see subscriptions_types()
  139. */
  140. function _subscriptions_taxonomy_types() {
  141. $types['taxa'] = array(
  142. 'title' => 'Categories',
  143. 'page' => 'subscriptions_taxonomy_page_taxa',
  144. 'fields' => array('node', 'tid'),
  145. 'weight' => -20,
  146. 'access' => 'subscribe to taxonomy terms',
  147. 'permission' => array(
  148. 'title' => t('Subscribe to taxonomy terms'),
  149. 'description' => t('Subscribe to the available taxonomy terms.')
  150. ),
  151. );
  152. return $types;
  153. }
  154. /**
  155. * Implements hook_form_alter().
  156. *
  157. * Adds the Taxonomy Settings part to SUBSCRIPTIONS_CONFIG_PATH.
  158. *
  159. * @param array $form
  160. * @param array $form_state
  161. *
  162. * @ingroup hooks
  163. * @ingroup form
  164. */
  165. function subscriptions_taxonomy_form_subscriptions_settings_form_alter(array &$form, array &$form_state) {
  166. _subscriptions_module_load_include('subscriptions_taxonomy', 'admin.inc');
  167. _subscriptions_taxonomy_form_subscriptions_settings_form_alter($form, $form_state);
  168. }
  169. /**
  170. * Returns a list of taxonomy subscriptions.
  171. *
  172. * @param array $form
  173. * @param int $uid
  174. * ID of a user if >0 or of a role if <0.
  175. *
  176. * @return array
  177. *
  178. * @ingroup form
  179. */
  180. function subscriptions_taxonomy_page_taxa(array $form, $uid) {
  181. // traverse the taxonomy tree
  182. $vocabularies = function_exists('taxonomy_help') ? taxonomy_get_vocabularies() : array();
  183. // omit undesired vocabularies from listing
  184. $omits = variable_get('subscriptions_omitted_taxa', array());
  185. foreach ($omits as $omit) {
  186. unset($vocabularies[$omit]);
  187. }
  188. if ($vocabularies) {
  189. _subscriptions_module_load_include('subscriptions_taxonomy', 'admin.inc');
  190. return _subscriptions_taxonomy_taxa_form($form, $uid, $vocabularies);
  191. }
  192. else {
  193. return array(array('#markup' => t('There are no available category groups.')));
  194. }
  195. }
  196. /**
  197. * Provides the data for resolving tokens.
  198. *
  199. * @param array $data
  200. * @param $node
  201. * @param array $subs
  202. */
  203. function subscriptions_taxonomy_data(array &$data, $node, array $queue_item) {
  204. $data['subs']['category'] = taxonomy_term_load($queue_item['value']);
  205. subscriptions_content_data($data, $node, $queue_item);
  206. }
  207. /**
  208. * Implements hook_taxonomy().
  209. *
  210. * Remove taxonomy subscriptions when the underlying terms or vocabularies are removed.
  211. *
  212. * @param $op
  213. * @param $type
  214. * @param $array
  215. *
  216. * @return
  217. *
  218. * @ingroup hooks
  219. */
  220. function subscriptions_taxonomy_taxonomy($op, $type, $array) {
  221. if ($op != 'delete') {
  222. return;
  223. }
  224. switch ($type) {
  225. case 'term':
  226. $tid = $array['tid'];
  227. db_delete('subscriptions_queue')
  228. ->condition('module', 'node')
  229. ->condition('field', 'tid')
  230. ->condition('value', $tid)
  231. ->execute();
  232. subscriptions_delete_for_all_users('node', 'tid', $tid);
  233. break;
  234. case 'taxonomy_vocabulary':
  235. $vid = $array['vid'];
  236. foreach (array('omitted', 'restricted') as $key) {
  237. $array = variable_get('subscriptions_' . $key . '_taxa', array());
  238. unset($array[$vid]);
  239. variable_set('subscriptions_' . $key . '_taxa', $array);
  240. }
  241. break;
  242. }
  243. }
  244. /**
  245. * Implements hook_disable().
  246. *
  247. * Remove our queue items.
  248. *
  249. * @ingroup hooks
  250. */
  251. function subscriptions_taxonomy_disable() {
  252. db_delete('subscriptions_queue')
  253. ->condition('module', 'node')
  254. ->condition('field', 'tid')
  255. ->execute();
  256. }