notifications_user.module

Tracking 7.x-1.x branch
  1. drupal
    1. 7 contributions/notifications/notifications_user/notifications_user.module

Notifications module - User subscriptions tabs

Functions & methods

NameDescription
notifications_user_account_accessCheck access to user account tab
notifications_user_menuImplementation of hook_menu()
notifications_user_notificationsImplementation of hook_notifications()
notifications_user_notifications_object_nodeImplementation of hook_notifications_object_node()
notifications_user_notifications_object_userImplementation of hook_notifications_object_user()
notifications_user_notifications_subscriptionImplementation of hook notifications_subscription()
notifications_user_permissionImplements hook_permission()
notifications_user_user_viewImplements hook_user_view().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Notifications module - User subscriptions tabs
  5. */
  6. /**
  7. * Implementation of hook_menu()
  8. */
  9. function notifications_user_menu() {
  10. $items = array();
  11. // User pages for each subscription type, will be disabled by default
  12. foreach (array('user_content') as $type) {
  13. if (notifications_subscription_type_enabled($type)) {
  14. $items['user/%user/notifications/' . $type] = array(
  15. 'type' => MENU_LOCAL_TASK,
  16. 'access callback' => 'notifications_user_account_access',
  17. 'access arguments' => array(1, $type),
  18. 'title' => notifications_subscription_type($type, 'title'),
  19. 'page callback' => 'notifications_account_subscription_list_page',
  20. 'page arguments' => array($type, 1),
  21. 'weight' => 10,
  22. );
  23. }
  24. }
  25. return $items;
  26. }
  27. /**
  28. * Check access to user account tab
  29. */
  30. function notifications_user_account_access($account, $type) {
  31. return module_exists('notifications_account') && notifications_account_tab_access($account, $type);
  32. }
  33. /**
  34. * Implements hook_permission()
  35. */
  36. function notifications_user_permission() {
  37. return array(
  38. 'subscribe to author' => array(
  39. 'title' => t('Subscribe to author'),
  40. 'description' => t('Subscribe to content posted by a given user.'),
  41. ),
  42. );
  43. }
  44. /**
  45. * Implementation of hook_notifications()
  46. */
  47. function notifications_user_notifications($op) {
  48. switch ($op) {
  49. case 'subscription types':
  50. $types['user_content'] = array(
  51. 'title' => t('Author'),
  52. 'class' => 'Notifications_User_Content_Subscription',
  53. 'field_types' => array('node:uid'),
  54. 'object_types' => array('node', 'user'),
  55. 'access' => array('subscribe to author'),
  56. 'description' => t('Subscribe to all content submitted by a user.'),
  57. 'display_options' => array('user_links', 'account_tabs'),
  58. );
  59. // This is a complex type, combining two fields
  60. $types['user_content_type'] = array(
  61. 'title' => t('Content type by author'),
  62. 'class' => 'Notifications_User_Content_Subscription',
  63. 'field_types' => array('node:type', 'node:uid'),
  64. 'object_types' => array('node', 'node_type', 'user'),
  65. 'access' => array('subscribe to content', 'subscribe to author'),
  66. 'description' => t('Subscribe to all content of a given type submitted by a user.'),
  67. );
  68. return $types;
  69. case 'field types':
  70. $fields['node:uid'] = array(
  71. 'title' => t('Author'),
  72. 'class' => 'Notifications_User_Field',
  73. );
  74. return $fields;
  75. case 'display options':
  76. // All types can be in block
  77. $types['user_links'] = array(
  78. '#title' => t('User links'),
  79. '#description' => t('Display subscription links on user account page.'),
  80. );
  81. return $types;
  82. }
  83. }
  84. /**
  85. * Implementation of hook notifications_subscription()
  86. */
  87. function notifications_user_notifications_subscription($op, $subscription = NULL) {
  88. switch ($op) {
  89. case 'page objects':
  90. $objects = array();
  91. // Return objects on current page to which we can subscribe
  92. if (arg(0) == 'user' && is_numeric(arg(1)) && ($user = menu_get_object('user'))) {
  93. $objects[] = notifications_object('user', $user);
  94. }
  95. return $objects;
  96. }
  97. }
  98. /**
  99. * Implementation of hook_notifications_object_user()
  100. */
  101. function notifications_user_notifications_object_user($op, $user, $account = NULL) {
  102. switch ($op) {
  103. case 'conditions':
  104. // Condition fields for subscriptions to this object type (user)
  105. return array(
  106. 'uid' => $user->uid,
  107. 'author' => $user->uid,
  108. );
  109. case 'subscriptions':
  110. // Option subscriptions to user account. Checking permissions here will save some processing.
  111. $options = array();
  112. // All posts by author
  113. if (!$account || user_access('subscribe to author', $account)) {
  114. $options[] = notifications_subscription('content_author')
  115. ->add_field('node:uid', $user->uid)
  116. ->set_name(t('All posts by @name', array('@name' => $user->name)));
  117. }
  118. // Content types with author subscriptions
  119. if (!$account || user_access('subscribe to content type', $account) && user_access('subscribe to author', $account)) {
  120. foreach (notifications_content_types('content_type_author') as $type => $type_name) {
  121. $options[] = notifications_subscription('content_type_author')
  122. ->add_field('node:uid', $user->uid)
  123. ->add_field('node:type', $type)
  124. ->set_name(t('@type posts by @name', array('@name' => $user->name, '@type' => node_type_get_name($type))));
  125. }
  126. }
  127. return $options;
  128. }
  129. }
  130. /**
  131. * Implementation of hook_notifications_object_node()
  132. */
  133. function notifications_user_notifications_object_node($op, $node, $account = NULL) {
  134. switch ($op) {
  135. case 'subscription types':
  136. return array('user_content', 'user_content_type');
  137. case 'subscriptions':
  138. // Return available subscription options for this node and this user account
  139. $options = array();
  140. $author = user_load($node->uid);
  141. $author_name = check_plain(format_username($author));
  142. // Node author subscriptions
  143. if (notifications_content_type_enabled($node->type, 'user_content')) {
  144. $options[] = notifications_subscription('user_content')
  145. ->set_author($node->uid)
  146. ->set_name(t('Posts by @name', array('@name' => $author_name)));
  147. }
  148. // Subscribe to content type by author
  149. if (notifications_content_type_enabled($node->type, 'user_content_type')) {
  150. $options[] = notifications_subscription('user_content_type')
  151. ->set_node($node)
  152. ->set_name(t('@type posts by @name', array('@name' => $author_name, '@type' => node_type_get_name($node))));
  153. }
  154. return $options;
  155. break;
  156. }
  157. }
  158. /**
  159. * Implements hook_user_view().
  160. */
  161. function notifications_user_user_view($account) {
  162. $list = new Notifications_Subscription_List();
  163. if (user_access('subscribe to author')) {
  164. if (notifications_subscription_type('user_content', 'user_links') && notifications_subscription_type_enabled('user_content')) {
  165. $list->add(notifications_subscription('user_content')
  166. ->set_author($account)
  167. );
  168. }
  169. // @todo Add here subscriptions to content type x author if enabled
  170. }
  171. if ($list->count()) {
  172. $list->set_user($GLOBALS['user']);
  173. $account->content['summary']['notifications'] = array(
  174. '#type' => 'user_profile_item',
  175. '#title' => t('Subscriptions'),
  176. '#markup' => theme('item_list', array('items' => $list->get_links())),
  177. '#attributes' => array('class' => array('notifications')),
  178. );
  179. }
  180. }