You are here

og_notifications.module in Organic groups 5.3

Provide notifications and messaging support for organic groups.

@author Karthik Kumar / Zen [ http://drupal.org/user/21209 ].

File

og_notifications/og_notifications.module
View source
<?php

/**
 * @file
 * Provide notifications and messaging support for organic groups.
 *
 * @author Karthik Kumar / Zen [ http://drupal.org/user/21209 ].
 */

/**
 * Implementation of hook_help().
 */
function og_notifications_help($section) {
  global $user;
  switch ($section) {
    case 'user/' . $user->uid . '/notifications/group':
      return t('Customize notifications for each of your groups and each of their content types along with their frequency and delivery method.');
  }
}

/**
 * Implementation of hook_menu().
 */
function og_notifications_menu($may_cache) {
  global $user;
  $items = array();
  if (!$may_cache) {
    if ($user->uid && arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == 'notifications' && ($user->uid == arg(1) || user_access('administer notifications'))) {
      $account = $user->uid == arg(1) ? $user : user_load(array(
        'uid' => arg(1),
      ));
      $items[] = array(
        'path' => 'user/' . $account->uid . '/notifications/group',
        'access' => user_access('subscribe to content in groups'),
        'title' => t('Groups'),
        'callback' => 'og_notifications_user_page',
        'callback arguments' => array(
          $account,
        ),
        'type' => MENU_LOCAL_TASK,
        'weight' => 10,
      );

      // grouptype redirects to group.
      $items[] = array(
        'path' => 'user/' . $account->uid . '/notifications/grouptype',
        'access' => user_access('subscribe to content in groups'),
        'callback' => 'og_notifications_grouptype_user_page',
        'callback arguments' => array(
          $account,
        ),
        'type' => MENU_CALLBACK,
      );
    }
  }
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function og_notifications_perm() {
  return array(
    'subscribe to content in groups',
  );
}

/**
 * Implementation of hook_user().
 * Handle uid entry in the og_notifications table.
 */
function og_notifications_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'insert':
      db_query("INSERT INTO {og_notifications} (uid) VALUES (%d)", $user->uid);
      break;
    case 'update':
      if (isset($edit['og_notifications_autosubscribe'])) {
        og_notifications_user_autosubscribe_set($user->uid, $edit['og_notifications_autosubscribe']);
        unset($edit['og_notifications_autosubscribe']);
      }
      break;
    case 'delete':
      db_query("DELETE FROM {og_notifications} WHERE uid = %d", $user->uid);
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function og_notifications_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'notifications_content_settings_form':
      $form['group'] = array(
        '#type' => 'fieldset',
        '#title' => t('Group subscriptions'),
        '#collapsible' => TRUE,
        '#weight' => 0,
      );

      // General content settings
      $select = array();
      $nodetypes = node_get_types();
      $ogtypes = og_get_types('group_post');
      foreach ($ogtypes as $ntype) {
        $select[$ntype] = $nodetypes[$ntype]->name;
      }
      $form['group']['og_notifications_content_types'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Allowed content types'),
        '#default_value' => variable_get('og_notifications_content_types', array()),
        '#options' => $select,
        '#description' => t('Select specific content types which should be <em>allowed</em> for subscriptions to <em>group + content type</em>.'),
        '#multiple' => TRUE,
      );
      break;
    case 'user_edit':

      // Insert autosubscribe option into the messaging section of the user edit
      // form.
      // user_edit is, oddly enough, also the form_id for forms in other
      // sub-tabs such as those added by the profile module.
      if (!arg(3)) {
        $account = $form['_account']['#value'];
        $form['messaging']['og_notifications_autosubscribe'] = array(
          '#type' => 'checkbox',
          '#title' => t('Automatically enable notifications for any groups that I join.'),
          '#description' => t('Group notifications can also be <a href="!manage-url">customised</a> in greater detail if required.', array(
            '!manage-url' => url('user/' . $account->uid . '/notifications/group'),
          )),
          '#default_value' => og_notifications_user_autosubscribe_get($account->uid),
        );
      }
      break;
    case 'og_admin_settings':
      unset($form['og_new_node_subject'], $form['og_new_node_body']);
      $form['og_settings']['notifications']['#description'] = t('Node event notifications can be configured via the <a href="!url">messaging templates</a> interface.', array(
        '!url' => url('admin/messaging/template'),
      ));

      // Default autosubscription setting.
      $form['og_settings']['notifications']['og_notifications_autosubscribe'] = array(
        '#type' => 'checkbox',
        '#title' => t('Autosubscribe users to any groups that they join.'),
        '#description' => t('Automatically enable notifications by default. Users can override this via their account page. Changing this setting will only affect new users and those who have not overridden the system default.'),
        '#default_value' => variable_get('og_notifications_autosubscribe', 1),
        '#weight' => -5,
      );
      break;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function og_notifications_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
    case 'delete':

      // Remove all group subscriptions for this node.
      notifications_delete_subscriptions(array(
        'type' => 'group',
      ), array(
        'group' => $node->nid,
      ));
      notifications_delete_subscriptions(array(
        'type' => 'grouptype',
      ), array(
        'group' => $node->nid,
      ));
      break;
  }
}

/**
 * Implementation of hook_og().
 */
function og_notifications_og($op, $gid, $uid, $args) {
  switch ($op) {
    case 'user insert':
      $account = user_load(array(
        'uid' => $uid,
      ));
      og_notifications_user_autosubscribe($account, $gid);
      break;
    case 'user delete':
      $account = user_load(array(
        'uid' => $uid,
      ));
      og_notifications_user_unsubscribe($account, $gid);
      break;
    case 'user request':

    // This and other notifications related ops drop down to the same case.
    // These different ops have been provided for consistency and flexibility
    // during use by other modules.
    case 'user approve':
    case 'admin create':
    case 'admin new':
    case 'user broadcast':
      $destinations = is_array($uid) ? $uid : array(
        $uid,
      );
      foreach ($destinations as $uid) {
        notifications_lite_send($uid, $args['subject'], $args['body']);
      }
      break;
  }
}

/**
 * Implementation of hook_messaging().
 */
function og_notifications_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  switch ($op) {
    case 'message groups':

      // Generic notifications event
      $info['og-notifications'] = array(
        'module' => 'og_notifications',
        'name' => t('OG notifications (default)'),
        'help' => t('Most fields will be provided during the event.'),
        'description' => t('Notifications for organic groups node events. Other group notification strings can be customised via the <a href="!url">OG config</a> page.', array(
          '!url' => url('admin/og/og'),
        )),
      );
      $info['og-notifications-insert'] = array(
        'module' => 'og_notifications',
        'name' => t('OG notifications for new content'),
        'help' => t('Most fields will be provided during the event.'),
        'description' => t('Notifications for organic groups node creation events.'),
      );
      $info['og-notifications-update'] = array(
        'module' => 'og_notifications',
        'name' => t('OG notifications for updated content'),
        'help' => t('Most fields will be provided during the event.'),
        'description' => t('Notifications for organic groups node update events.'),
      );
      $info['og-notifications-comment'] = array(
        'module' => 'og_notifications',
        'name' => t('OG notifications for comments'),
        'help' => t('Most fields will be provided during the event.'),
        'description' => t('Notifications for organic groups comment events.'),
      );
      return $info;
    case 'message keys':
      switch ($arg1) {
        case 'og-notifications':
        case 'og-notifications-insert':
        case 'og-notifications-update':
        case 'og-notifications-comment':
          return array(
            'subject' => t('Subject'),
            'header' => t('Body header'),
            'main' => t('Body'),
            'footer' => t('Body footer'),
          );
          break;
      }
      break;
    case 'messages':
      switch ($arg1) {
        case 'og-notifications':
        case 'og-notifications-update':
          return array(
            'subject' => t('[site-name] [ogname]: [title]'),
            'header' => t("Greetings, [user],"),
            'main' => array(
              t('A [type-name] has been updated in group [ogname]: [title]'),
              t('[node-teaser]'),
              t('Read more at [node-url].'),
            ),
            'footer' => array(
              t('This is an automatic message from [site-name]'),
              t('To manage your subscriptions, browse to [subscriptions-manage]'),
            ),
          );
        case 'og-notifications-insert':
          return array(
            'subject' => t('[site-name] [ogname]: [title]'),
            'header' => t("Greetings, [user],"),
            'main' => array(
              t('A [type-name] has been created in group [ogname]: [title]'),
              t('[node-teaser]'),
              t('Read more at [node-url].'),
            ),
            'footer' => array(
              t('This is an automatic message from [site-name]'),
              t('To manage your subscriptions, browse to [subscriptions-manage]'),
            ),
          );
        case 'og-notifications-comment':
          return array(
            'subject' => t('[site-name] [ogname]: [title]'),
            'header' => t("Greetings, [user],"),
            'main' => array(
              t('A new comment has been added by [comment-author-name] to this thread in group [ogname]: [comment-title]'),
              t('[comment-body]'),
              t('Read more at [comment-url] or reply via [comment-reply-url].'),
            ),
            'footer' => array(
              t('This is an automatic message from [site-name]'),
              t('To manage your subscriptions, browse to [subscriptions-manage]'),
            ),
          );
      }
      break;
    case 'tokens':
      $tokens = array();
      if (strpos($arg1, 'og-notifications') === 0) {
        $tokens = array(
          'global',
          'subscription',
          'user',
          'node',
          'comment',
        );
      }
      return $tokens;
  }
}

/**
 * A workaround to ensure that OG can provide custom message templates for
 * notifications.
 *
 * @param Object $message
 *   The message object.
 * @param Object $info
 *   Sending method information.
 */
function og_notifications_message_alter(&$message, $info) {
  if ($sid = _og_notification_check_message($message)) {
    $event = $message->notifications['events'][0];

    // Cater for different message groups (actions).
    $group = 'og-notifications-' . $event->action;
    $send_method = $message->method;
    $subscription = notifications_load_subscription($sid);
    $text = array(
      'subject' => messaging_message_part($group, 'subject', $send_method, $event),
      'header' => messaging_message_part($group, 'header', $send_method, $event),
      'main' => messaging_message_part($group, 'main', $send_method, $event),
      'footer' => messaging_message_part($group, 'footer', $send_method, $event),
    );
    $objects = array(
      'user' => $message->account,
      'node' => $event->objects['node'],
      'subscription' => $subscription,
    );
    if ($event->action == 'comment') {
      $objects['comment'] = $event->objects['comment'];
    }
    $objects = array_merge($objects, $event->objects);
    $text = messaging_text_replace($text, $objects);
    $message->subject = $text['subject'];
    unset($text['subject']);
    $message->body = $text;
  }
}

/**
 * Implementation of hook_notifications().
 */
function og_notifications_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'names':
      $subs =& $arg0;
      if ($subs->event_type == 'node') {
        if (!empty($subs->fields['group']) && ($group = node_load($subs->fields['group']))) {
          $subs->names['group'] = t('Group: %name', array(
            '%name' => $group->title,
          ));
        }
      }
      break;
    case 'subscription types':
      $types['group'] = array(
        'event_type' => 'node',
        'title' => t('Groups'),
        'access' => 'subscribe to content in groups',
        'page' => 'og_notifications_user_page',
        'fields' => array(
          'group',
        ),
      );
      $types['grouptype'] = array(
        'event_type' => 'node',
        'title' => t('Content type in group'),
        'access' => 'subscribe to content in groups',
        'page' => 'og_notifications_user_page',
        'fields' => array(
          'group',
          'type',
        ),
      );
      return $types;
    case 'subscription fields':

      // Information about available fields for subscriptions
      $fields['group'] = array(
        'name' => t('Group'),
        'field' => 'group',
        'type' => 'int',
      );
      return $fields;
    case 'query':
      if ($arg0 == 'event' && $arg1 == 'node' && ($node = $arg2->node) || $arg0 == 'user' && $arg1 == 'node' && ($node = $arg2)) {
        $query = array();
        if ($node->og_groups) {
          $query[]['fields']['group'] = $node->og_groups;
        }
        if ($arg0 == 'user' && og_is_group_type($node->type)) {
          $query[]['fields']['group'] = $node->nid;
        }
        return $query;
      }
      break;
    case 'node options':
      return _og_notifications_node_options($arg0, $arg1);
    case 'event load':

      // Piggy-backing on notifications_content.
      break;
    case 'event types':

      // Piggy-backing on notifications_content.
      break;
    case 'access':
      $type = $arg0;
      $account = $arg1;
      $object = $arg2;
      if ($type == 'subscription' && !empty($object->fields['group'])) {
        if (($group = node_load($object->fields['group'])) && og_is_group_type($group->type) && notifications_content_node_allow($account, $group)) {
          return array(
            TRUE,
          );
        }
        else {
          return array(
            FALSE,
          );
        }
      }
      break;
  }
}

/**
 * Menu callback: Display the user subscription management forms.
 *
 * @param Object $account
 *   User object of the user whose page is to be displayed.
 */
function og_notifications_user_page($account = NULL) {
  global $user;
  $account = $account ? $account : $user;
  if (!empty($account->og_groups)) {
    return drupal_get_form('og_notifications_add_form', $account) . drupal_get_form('og_notifications_manage_form', $account);
  }
  else {
    drupal_set_message(t('There are no active group subscriptions available.'));
  }
}

/**
 * Grouptype subscription creation form.
 *
 * @param Object $account
 *   User object of the user whose page is to be displayed.
 * @return Array $form
 *   Form array.
 */
function og_notifications_add_form($account) {
  $content_types = array_filter(variable_get('og_notifications_content_types', array()));
  $content_names = node_get_types('names');
  foreach ($content_types as $type) {
    $content_types[$type] = $content_names[$type];
  }
  $defaults = _notifications_subscription_defaults($account);
  $send_methods = _notifications_send_methods();
  $send_intervals = _notifications_send_intervals();
  foreach ($account->og_groups as $gid => $group) {
    $account->og_groups[$gid] = $group['title'];
  }
  $header = array(
    t('Group'),
    t('Type'),
    t('Send method'),
    t('Send Interval'),
  );

  // Reuse notifications theme function for the embedded table. This also
  // necessitates the use of a keyed array.
  $form['subscription'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add subscription'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#theme' => 'notifications_form_table',
    '#header' => &$header,
  );
  $form['subscription']['group'][0] = array(
    '#type' => 'select',
    '#options' => $account->og_groups,
  );
  $form['subscription']['node_type'][0] = array(
    '#type' => 'select',
    '#options' => array(
      'all' => t('All content types'),
    ) + $content_types,
  );

  // Hide send methods if only one available.
  if (count($send_methods) > 1) {
    $form['subscription']['send_method'][0] = array(
      '#type' => 'select',
      '#options' => $send_methods,
      '#default_value' => $defaults['send_method'],
    );
  }
  else {

    // Unset send method column if only one is available.
    unset($header[2]);

    // Pass default outside the subscriptions fieldset to avoid theming issues.
    $form['send_method'] = array(
      '#type' => 'value',
      '#value' => $defaults['send_method'],
    );
  }
  $form['subscription']['send_interval'][0] = array(
    '#type' => 'select',
    '#options' => $send_intervals,
    '#default_value' => $defaults['send_interval'],
  );
  $form['subscription']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
  );
  $form['account'] = array(
    '#type' => 'value',
    '#value' => $account,
  );
  return $form;
}

/**
 * Process og_notifications_add_form submission.
 */
function og_notifications_add_form_submit($form, $form_values) {
  $subscription = new stdClass();
  $subscription->type = 'grouptype';
  $subscription->uid = $form_values['account']->uid;
  $subscription->send_interval = $form_values['subscription']['send_interval'][0];
  $subscription->send_method = isset($form_values['send_method']) ? $form_values['send_method'] : $form_values['subscription']['send_method'][0];

  // String cast due to notifications requiring it as the value field is
  // a varchar.
  $subscription->fields = array(
    'group' => (string) $form_values['subscription']['group'][0],
    'type' => $form_values['subscription']['node_type'][0],
  );
  if ($form_values['subscription']['node_type'][0] == 'all') {
    $types = array_filter(variable_get('og_notifications_content_types', array()));
    foreach ($types as $type) {
      $subscription->fields['type'] = $type;
      notifications_save_subscription($subscription);
      unset($subscription->sid);
    }
  }
  else {
    notifications_save_subscription($subscription);
  }
  drupal_set_message(t('Subscription saved.'));
}

/**
 * Grouptype subscription management form.
 *
 * @param Object $account
 *   User object of the user whose page is to be displayed.
 * @return Array $form
 *   Form array.
 */
function og_notifications_manage_form($account) {
  $content_types = array_filter(variable_get('og_notifications_content_types', array()));
  $content_names = node_get_types('names');
  $send_methods = _notifications_send_methods();
  $send_intervals = _notifications_send_intervals();
  $header = array(
    theme('table_select_header_cell'),
    array(
      'data' => t('Group'),
      'field' => 'n.title',
      'sort' => 'asc',
    ),
    array(
      'data' => t('Type'),
      'field' => 'node_type',
    ),
    array(
      'data' => t('Send method'),
      'field' => 'no.send_method',
    ),
    array(
      'data' => t('Send Interval'),
      'field' => 'no.send_interval',
    ),
  );
  $sql = "SELECT n.nid AS group_nid, n.title, nof2.value AS node_type, no.* FROM {notifications} no\n    INNER JOIN {notifications_fields} nof1 ON no.sid = nof1.sid\n    INNER JOIN {notifications_fields} nof2 ON no.sid = nof2.sid\n    INNER JOIN {node} n ON nof1.value = n.nid\n    WHERE no.uid = %d AND (no.type = 'grouptype') AND no.conditions = 2 AND nof1.field = 'group' AND nof2.field = 'type'";
  $sql .= tablesort_sql($header);
  $count_sql = "SELECT COUNT(nof2.value) FROM {notifications} no\n    INNER JOIN {notifications_fields} nof1 ON no.sid = nof1.sid\n    INNER JOIN {notifications_fields} nof2 ON no.sid = nof2.sid\n    WHERE no.uid = %d AND (no.type = 'grouptype') AND no.conditions = 2 AND nof1.field = 'group' AND nof2.field = 'type'";
  $result = pager_query($sql, 50, 0, $count_sql, $account->uid);

  // Reuse notifications theme function for the embedded table.
  $form['subscriptions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Current subscriptions'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#theme' => 'notifications_form_table',
    '#header' => &$header,
  );
  $subscriptions_current = array();
  while ($subscription = db_fetch_object($result)) {
    $key = $subscription->sid;
    $subscriptions_current[$key] = $subscription;
    $form['subscriptions']['checkbox'][$key] = array(
      '#type' => 'checkbox',
      '#default_value' => $subscription->sid,
    );
    $form['subscriptions']['group'][$key] = array(
      '#value' => l($subscription->title, 'node/' . $subscription->group_nid),
    );
    $form['subscriptions']['node_type'][$key] = array(
      '#value' => $content_names[$subscription->node_type],
    );

    // Hide send methods if only one available.
    if (count($send_methods) > 1) {
      $form['subscriptions']['send_method'][$key] = array(
        '#type' => 'select',
        '#options' => $send_methods,
        '#default_value' => $subscription->send_method,
      );
    }
    else {

      // Unset send method column if only one is available.
      unset($header[3]);

      // Pass default outside the subscriptions fieldset to avoid theming issues.
      $form['send_method'] = array(
        '#type' => 'value',
        '#value' => $subscription->send_method,
      );
    }
    $form['subscriptions']['send_interval'][$key] = array(
      '#type' => 'select',
      '#options' => $send_intervals,
      '#default_value' => $subscription->send_interval,
    );
  }
  if (empty($subscriptions_current)) {
    $form = array();
  }
  else {
    $form['subscriptions']['current'] = array(
      '#type' => 'value',
      '#value' => $subscriptions_current,
    );
    $form['subscriptions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update'),
    );
  }
  return $form;
}

/**
 * Process og_notifications_manage_form form submission.
 */
function og_notifications_manage_form_submit($form, $form_values) {
  $current = $form_values['subscriptions']['current'];
  foreach ($form_values['subscriptions']['checkbox'] as $sid => $check) {
    $subscription = $current[$sid];

    // Insert fields array as per notifications' requirements.
    $subscription->fields = array(
      'group' => (string) $subscription->group_nid,
      'type' => $subscription->node_type,
    );
    if ($check == 1) {
      if ($subscription->send_interval != $form_values['subscriptions']['send_interval'][$sid] || !isset($form_values['send_method']) && $subscription->send_method != $form_values['subscriptions']['send_method'][$sid]) {
        $subscription->send_interval = $form_values['subscriptions']['send_interval'][$sid];
        $subscription->send_method = isset($form_values['send_method']) ? $form_values['send_method'] : $form_values['subscriptions']['send_method'][$sid];

        // String cast due to notifications requiring it as the value field is
        // a varchar.
        notifications_save_subscription($subscription);
      }
    }
    else {

      // Unsubscribe if not checked ...
      notifications_delete_subscription($sid);
    }
  }
  drupal_set_message(t('Subscriptions updated.'));
}

/**
 * Menu callback: Since the default notifications UI lists group and grouptype
 * as separate entries, redirect the grouptype management link to the group
 * management page.
 *
 * @param Object $account
 *   User object of the user whose grouptypes are to be managed.
 */
function og_notifications_grouptype_user_page($account) {
  drupal_goto('user/' . $account->uid . '/notifications/group');
}

/**
 * Handle autosubscriptions for users when they join a group.
 *
 * @param Object $account
 *   The user account object.
 * @param Integer $gid
 *   The node ID of the group being subscribed to.
 */
function og_notifications_user_autosubscribe($account, $gid) {
  if (og_notifications_user_autosubscribe_get($account->uid)) {
    og_notifications_user_subscribe($account, $gid);
  }
}

/**
 * Retrieve autosubscription setting for a particular user. -1 in the
 * og_notifications table indicates that the site default is to be used.
 *
 * @param Integer $uid
 *   The uid of the user.
 * @return Integer
 *   1 or 0 as per the autosubscribe preference.
 */
function og_notifications_user_autosubscribe_get($uid) {
  $autosubscribe = db_result(db_query("SELECT autosubscribe FROM {og_notifications} WHERE uid = %d", $uid));
  return $autosubscribe == -1 ? variable_get('og_notifications_autosubscribe', 1) : $autosubscribe;
}

/**
 * Retrieve autosubscription setting for a particular user.
 *
 * @param Integer $uid
 *   The uid of the user.
 * @param Integer $autosubscribe
 *   Autosubscription option: 0 or 1.
 */
function og_notifications_user_autosubscribe_set($uid, $autosubscribe) {
  return db_query("UPDATE {og_notifications} SET autosubscribe = %d WHERE uid = %d", $autosubscribe, $uid);
}

/**
 * Subscribe a user to a group.
 *
 * @param Object $account
 *   The user account object.
 * @param Integer $gid
 *   The node ID of the group being subscribed to.
 */
function og_notifications_user_subscribe($account, $gid) {
  $subscription = _notifications_subscription_defaults($account);
  $subscription['uid'] = $account->uid;
  $subscription['type'] = 'group';
  $subscription['event_type'] = 'node';

  // String cast due to notifications requiring it as the value field is
  // a varchar.
  $subscription['fields'] = array(
    'group' => (string) $gid,
  );
  notifications_save_subscription($subscription);
}

/**
 * Unsubscribe a user from a group. This also unsubscribes the user from any
 * grouptype subscriptions within the group.
 *
 * @param Object $account
 *   The user account object.
 * @param Integer $gid
 *   The node ID of the group being subscribed to.
 */
function og_notifications_user_unsubscribe($account, $gid) {

  // @todo: Handle direct node subscriptions for private groups.
  // Niche cases include multi-group nodes.
  notifications_delete_subscriptions(array(
    'uid' => $account->uid,
    'type' => 'grouptype',
  ), array(
    'group' => $gid,
  ));
  notifications_delete_subscriptions(array(
    'uid' => $account->uid,
    'type' => 'group',
  ), array(
    'group' => $gid,
  ));
}

/**
 * Options to display for node subscriptions.
 */
function _og_notifications_node_options($account, $node) {
  $options = array();

  // If node is a group type and the user is subscribed to this group.
  if (og_is_group_type($node->type) && isset($account->og_groups[$node->nid])) {
    $options[] = array(
      'name' => t('All posts in %group', array(
        '%group' => $node->title,
      )),
      'type' => 'group',
      'fields' => array(
        'group' => $node->nid,
      ),
    );
    foreach (array_filter(variable_get('og_notifications_content_types', array())) as $type) {
      $options[] = array(
        'name' => t('%type posts in %group', array(
          '%group' => $node->title,
          '%type' => node_get_types('name', $type),
        )),
        'type' => 'grouptype',
        'fields' => array(
          'group' => $node->nid,
          'type' => $type,
        ),
      );
    }
  }

  // If node is part of a group user may be subscribed to the node through one
  // of the groups.
  if ($node->og_groups) {
    foreach ($node->og_groups as $index => $gid) {

      // Only members get to see subscription options.
      if (isset($account->og_groups[$gid])) {

        // Content type
        $options[] = array(
          'name' => t('Posts in group %name', array(
            '%name' => $node->og_groups_both[$gid],
          )),
          'type' => 'group',
          'fields' => array(
            'group' => $gid,
          ),
        );
        $options[] = array(
          'name' => t('%type posts in %group', array(
            '%group' => $node->og_groups_both[$gid],
            '%type' => node_get_types('name', $node->type),
          )),
          'type' => 'grouptype',
          'fields' => array(
            'group' => $gid,
            'type' => $node->type,
          ),
        );
      }
    }
  }
  return $options;
}

/**
 * Helper function for og_notifications_message_alter. This function checks to
 * see if the message object being passed is an OG notification.
 *
 * @param Object $message
 *   The message object.
 * @return Integer
 *   The subscription ID if this is an OG notification message. 0, 
 * otherwise ...
 */
function _og_notification_check_message($message) {
  $sid = 0;
  if (isset($message->notifications) && isset($message->notifications['subscriptions'])) {
    $params = current($message->notifications['subscriptions']);
    if (count($params) > 1) {

      // Check if "group" is one of the fields. This is a general presumption
      // that any subscription with at least two fields, one of them being a
      // group, is an OG subscription.
      $sid = db_result(db_query("SELECT sid FROM {notifications_fields} WHERE field = 'group' AND sid IN (" . db_placeholders($params) . ")", $params));
    }
  }
  return $sid > 0 ? $sid : 0;
}

Functions

Namesort descending Description
og_notifications_add_form Grouptype subscription creation form.
og_notifications_add_form_submit Process og_notifications_add_form submission.
og_notifications_form_alter Implementation of hook_form_alter().
og_notifications_grouptype_user_page Menu callback: Since the default notifications UI lists group and grouptype as separate entries, redirect the grouptype management link to the group management page.
og_notifications_help Implementation of hook_help().
og_notifications_manage_form Grouptype subscription management form.
og_notifications_manage_form_submit Process og_notifications_manage_form form submission.
og_notifications_menu Implementation of hook_menu().
og_notifications_message_alter A workaround to ensure that OG can provide custom message templates for notifications.
og_notifications_messaging Implementation of hook_messaging().
og_notifications_nodeapi Implementation of hook_nodeapi().
og_notifications_notifications Implementation of hook_notifications().
og_notifications_og Implementation of hook_og().
og_notifications_perm Implementation of hook_perm().
og_notifications_user Implementation of hook_user(). Handle uid entry in the og_notifications table.
og_notifications_user_autosubscribe Handle autosubscriptions for users when they join a group.
og_notifications_user_autosubscribe_get Retrieve autosubscription setting for a particular user. -1 in the og_notifications table indicates that the site default is to be used.
og_notifications_user_autosubscribe_set Retrieve autosubscription setting for a particular user.
og_notifications_user_page Menu callback: Display the user subscription management forms.
og_notifications_user_subscribe Subscribe a user to a group.
og_notifications_user_unsubscribe Unsubscribe a user from a group. This also unsubscribes the user from any grouptype subscriptions within the group.
_og_notifications_node_options Options to display for node subscriptions.
_og_notification_check_message Helper function for og_notifications_message_alter. This function checks to see if the message object being passed is an OG notification.