This guide will show you how to get started on a system that allows users enable or disable messaging for their account. This is an intermediate to advanced guide, so I'll go as far as outlining the steps involved and go into detail on querying the database for users that have enabled a flag on their profile.

The dependencies for this to work are:
Flag
Message
Message Notify

Read the documentation to learn how to set up each of these modules.

The basic idea is as follows:

  • Create User Flags (Messaging Enabled/Disabled, and Digest Mode Enabled/Disabled)
  • In your message processing module/feature, query the database for enabled flags
  • Act on the results of that query and send notifications with message notify

Link to the flags on the User Profile

I used hook_menu, with a custom page callback to put my notifications flags on the user edit page as a MENU_LOCAL_TASK. The page callback is just a function that returns rendered markup for the links to the flags that you created. See Placing a flag link on a page. You may also want to consider restricting access to the flag to the user provided as an argument in your menu item.

The flags themselves should not be global each flag is a 1 to 1 relationship to the user to reflect their messaging preferences.

Write a function to retrieve a list of users for each group with messaging enabled

The signature will look something like

<?php
function example_get_group_subscribers($gid, $digest = FALSE)
?>

$gid identifies the group and $digest determines whether or not you are asking for users with the messaging and digest flags enabled. The function will return an array of $uids.

Two queries are necessary, they are identical except for the condition that matches the name of the flag. The user's membership is stored in og_membership along with other group memberships. To get the group we want, there is a condition to match membership.gid to $gid. To make sure only users are returned we add a second condition for membership.entity_type = 'user'. Finally ensure the name column in the flags table matches the flag you made. The fields that are needed to determine the user id are flag_counts.content_id which must be joined to og_membership.etid.

The Query

<?php
  $query = db_select('og_membership', 'm');
  $query->join('flag_counts', 'fc', 'fc.content_id = m.etid');
  $query->join('flags', 'f', 'f.fid = fc.fid');
  $query->fields('m', array('etid'))
    ->fields('f', array('name'))
    ->condition('m.gid', $gid)
    ->condition('m.entity_type', 'user')
    ->condition('f.name', 'messaging');

  $message_uids = $query->execute()->fetchAllAssoc('etid', PDO::FETCH_ASSOC);
?>

Now you have an array with keys representing the uids of the users with messaging turned on. If you want to also allow them to enable digest mode, you'll have to massage your data a bit. First overwrite the array with array_keys(), so the field name is excluded. Sort both arrays from each query using sort() This helps the next two functions operate more efficiently. Use array_intersect($message_uids, $digest_uids) to determine which users enabled messaging and digests. If you want only those with messaging, and not digest use array_diff($message_uids, $digest_uids).

Message Notify

The best way to learn how to use this module is to look at the example module's code and see how the message is referenced to the comment that it was created for. This same use of entityreference fields can be used to reference a message to any content that you need.