You are here

function notifications_ui_user_node in Notifications 5

Get list of possible and existing subscriptions for user/node

Return value

Array of subscription options The enabled ones will have a 'subscriptions' element loaded

2 calls to notifications_ui_user_node()
notifications_ui_link in notifications_ui/notifications_ui.module
Implementation of hook_link()
notifications_ui_node_form in notifications_ui/notifications_ui.module
Form for node subscriptions @ TODO: offer the same form in a block to be put in the contents region.

File

notifications_ui/notifications_ui.module, line 259
User Interface for subscriptions modules

Code

function notifications_ui_user_node($account, $node) {

  // Get allowed node options and current subscriptions
  $node_options = notifications_module_information('node options', $account, $node);
  $allowed_types = notifications_ui_allowed_types();
  $allowed_options = array();

  // We also keep track of event types for each subscription type
  $event_types = array(
    'node' => TRUE,
  );
  foreach ($node_options as $index => $option) {
    if (isset($allowed_types[$option['type']])) {
      $allowed_options[] = $option;

      // We add the event type to our list
      $event_types[$allowed_types[$option['type']]['event_type']] = TRUE;
    }
  }

  // Check if user is subscribed to show 'subscribe'/'unsubscribe' links
  $subscriptions = array();
  foreach (array_keys($event_types) as $type) {
    if ($more = notifications_user_get_subscriptions($account->uid, $type, $node->nid, $node)) {
      $subscriptions += $more;
    }
  }

  // Populate subscriptions when they exist
  foreach ($allowed_options as $index => $option) {
    foreach ($subscriptions as $sub) {
      if ($sub->type == $option['type'] && !array_diff_key($option['fields'], $sub->fields)) {
        $allowed_options[$index]['subscription'] = $sub;
      }
    }
  }

  // Finally check permissions for all of them
  foreach ($allowed_options as $index => $option) {
    if (!notifications_user_allowed('subscription', $account, (object) $option)) {
      unset($allowed_options[$index]);
    }
  }
  return $allowed_options;
}