You are here

function notifications_content_notifications in Notifications 5

Same name and namespace in other branches
  1. 6.4 notifications_content/notifications_content.module \notifications_content_notifications()
  2. 6 notifications_content/notifications_content.module \notifications_content_notifications()
  3. 6.2 notifications_content/notifications_content.module \notifications_content_notifications()
  4. 6.3 notifications_content/notifications_content.module \notifications_content_notifications()
  5. 7 notifications_content/notifications_content.module \notifications_content_notifications()

Implementation of hook_notifications()

File

notifications_content/notifications_content.module, line 116
Subscriptions to content events

Code

function notifications_content_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'names':
      $subs =& $arg0;
      if ($subs->event_type == 'node') {
        $subs->type_name = t('Content');
        if (!empty($subs->fields['type'])) {
          $subs->names['type'] = t('Content type: %type', array(
            '%type' => node_get_types('name', $subs->fields['type']),
          ));
        }
        if (!empty($subs->fields['author']) && ($author = user_load(array(
          'uid' => $subs->fields['author'],
        )))) {
          $subs->names['author'] = t('Author: %name', array(
            '%name' => $author->name,
          ));
        }
        if (!empty($subs->fields['nid']) && ($node = node_load($subs->fields['nid']))) {
          $subs->names['thread'] = t('Thread: %title', array(
            '%title' => $node->title,
          ));
        }
      }
      break;
    case 'subscription types':
      $types['thread'] = array(
        'event_type' => 'node',
        'title' => t('Thread'),
        'access' => 'subscribe to content',
        'page' => 'notifications_content_page_thread',
        'fields' => array(
          'nid',
        ),
      );
      $types['nodetype'] = array(
        'event_type' => 'node',
        'title' => t('Content type'),
        'access' => 'subscribe to content type',
        'page' => 'notifications_content_page_nodetype',
        'fields' => array(
          'type',
        ),
      );
      $types['author'] = array(
        'event_type' => 'node',
        'title' => t('Author'),
        'access' => 'subscribe to author',
        'page' => 'notifications_content_page_author',
        'fields' => array(
          'author',
        ),
      );
      return $types;
    case 'subscription fields':

      // Information about available fields for subscriptions
      $fields['nid'] = array(
        'name' => t('Node'),
        'field' => 'nid',
        'type' => 'int',
      );
      $fields['author'] = array(
        'name' => t('Author'),
        'field' => 'author',
        'type' => 'int',
      );
      $fields['type'] = array(
        'name' => t('Node type'),
        'field' => 'author',
        'type' => 'string',
        'options callback' => 'node_get_types',
        'options arguments' => array(
          'names',
        ),
      );
      return $fields;
    case 'query':

      // This returns query elements for queue or user queries:
      // - If $arg0 is 'event', $arg1 is event type and $arg2 is $event object.
      // - If $arg0 is 'user', $arg1 is object type ('node') and $arg2 is object ($node)
      if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) || $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
        $query[]['fields'] = array(
          'nid' => $node->nid,
          'type' => $node->type,
          'author' => $node->uid,
        );
        return $query;
      }
      break;
    case 'node options':
      return _notifications_content_node_options($arg0, $arg1);
    case 'event load':

      // $arg0 is event
      $event =& $arg0;
      $load = array();
      if ($event->type == 'node') {
        if (!empty($event->params['nid'])) {
          $event->objects['node'] = node_load($event->params['nid']);
        }
        if (!empty($event->params['cid'])) {
          $event->objects['comment'] = notifications_content_comment_load($event->params['cid']);
        }
      }
      break;
    case 'event types':

      // Node inserts are not grouped by node but all together. The digest will look like:
      //   New content has been submitted
      //   - Story Title1 by Author1
      //   - Event Title2 by Author2
      $types[] = array(
        'type' => 'node',
        'action' => 'insert',
        'name' => t('New content of type [type-name] has been submitted'),
        'line' => t('[type-name] [title] by [author-name]'),
        'digest' => array(
          'node',
          'type',
        ),
        'description' => t('Node creation'),
      );

      // These other events are grouped for each node. The digest will look like:
      //   Story: Title of the story
      //   - The story has been updated
      //   - New comment by User: Comment title
      $types[] = array(
        'type' => 'node',
        'action' => 'update',
        'name' => t('[type-name]: [title]'),
        'line' => t('The [type-name] has been updated'),
        'digest' => array(
          'node',
          'nid',
        ),
        'description' => t('Node update'),
      );
      $types[] = array(
        'type' => 'node',
        'action' => 'comment',
        'name' => t('[type-name]: [title]'),
        'line' => t('New comment by [comment-author-name]: [comment-title]'),
        'digest' => array(
          'node',
          'nid',
        ),
        'description' => t('Node comment'),
      );
      return $types;
    case 'access':
      $type = $arg0;
      $account =& $arg1;
      $object =& $arg2;
      $access = TRUE;

      // For events we check that node and comment are allowed
      if ($type == 'event' && $object->type == 'node') {
        if (!empty($object->objects['node'])) {
          $access = notifications_content_node_allow($account, $object->objects['node']);
        }

        // If no access to node, we don't check more
        if ($access && !empty($object->objects['comment'])) {
          $access = $access && notifications_content_comment_allow($account, $object->objects['comment']);
        }

        // For node subscriptions we check that user can view the node
      }
      elseif ($type == 'subscription') {
        $access = TRUE;
        if (!empty($object->fields['nid'])) {
          if ($node = node_load($object->fields['nid'])) {
            $access = notifications_content_node_allow($account, $node);
          }
          else {
            $access = FALSE;
          }
        }
        if (!empty($object->fields['type'])) {
          $access = $access && array_key_exists($object->fields['type'], notifications_content_types());
        }
      }
      return array(
        $access,
      );
      break;
  }
}