Skip to content

Instantly share code, notes, and snippets.

@jessehs
Created January 31, 2014 23:38
Show Gist options
  • Save jessehs/8745522 to your computer and use it in GitHub Desktop.
Save jessehs/8745522 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_default_message_type().
*/
function mymodule_default_message_type() {
$items = array();
$items['message_comment_on_group_post'] = entity_import('message_type', '{
"name" : "message_comment_on_group_post",
"description" : "Message: Comment on Group Post",
"argument_keys" : [],
"argument" : [],
"category" : "message_type",
"data" : {
"token options" : { "clear" : 0 },
"purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
},
"language" : "",
"arguments" : null,
"message_text" : { "und" : [
{
"value" : "A new Message has been posted to [message:field-group-reference:title]",
"format" : "plain_text",
"safe_value" : "\\u003Cp\\u003EA new Message has been posted to [message:field-group-reference:title]\\u003C\\/p\\u003E\\n"
},
{
"value" : "Hello [message:field-comment-ref:node:author],\\r\\n\\r\\n[message:field-comment-ref:author:name] has commented on your post [message:field-comment-ref:node:title] in [message:field-group-reference:title].\\r\\n\\r\\nView comment author: [message:field-comment-ref:author:url] \\r\\nView your post: [message:field-comment-ref:node:url]\\r\\n\\r\\n-- Mymodule Groups Automessenger\\r\\n",
"format" : "plain_text",
"safe_value" : "\\u003Cp\\u003EHello [message:field-comment-ref:node:author],\\u003C\\/p\\u003E\\n\\u003Cp\\u003E[message:field-comment-ref:author:name] has commented on your post [message:field-comment-ref:node:title] in [message:field-group-reference:title].\\u003C\\/p\\u003E\\n\\u003Cp\\u003EView comment author: [message:field-comment-ref:author:url]\\u003Cbr \\/\\u003E\\nView your post: [message:field-comment-ref:node:url]\\u003C\\/p\\u003E\\n\\u003Cp\\u003E-- Mymodule Automessenger\\u003C\\/p\\u003E\\n"
}
]
},
"rdf_mapping" : []
}');
$items['message_new_group_post'] = entity_import('message_type', '{
"name" : "message_new_group_post",
"description" : "Message: New Group Post",
"argument_keys" : [],
"argument" : [],
"category" : "message_type",
"data" : {
"token options" : { "clear" : 0 },
"purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
},
"language" : "",
"arguments" : null,
"message_text" : { "und" : [
{
"value" : "A new Message has been posted to [message:field-group-reference:title]",
"format" : "plain_text",
"safe_value" : "\\u003Cp\\u003EA new Message has been posted to [message:field-group-reference:title]\\u003C\\/p\\u003E\\n"
},
{
"value" : "A new message entitled [message:field-node-reference:title] has been submitted to [message:field-group-reference:title].\\r\\n\\r\\nView new post: [message:field-node-reference:url]\\r\\nView group: [message:field-group-reference:url]\\r\\n\\r\\n-- Mymodule Groups Automessenger\\r\\n",
"format" : "plain_text",
"safe_value" : "\\u003Cp\\u003EA new message entitled [message:field-node-reference:title] has been submitted to [message:field-group-reference:title].\\u003C\\/p\\u003E\\n\\u003Cp\\u003EView new post: [message:field-node-reference:url]\\u003Cbr \\/\\u003E\\nView group: [message:field-group-reference:url]\\u003C\\/p\\u003E\\n\\u003Cp\\u003E-- Mymodule Groups Automessenger\\u003C\\/p\\u003E\\n"
}
]
},
"rdf_mapping" : []
}');
return $items;
}
<?php
/**
* Implements hook_node_insert().
*
* Send a message to group members when a new group post is inserted. Doing
* this as a hook instead of a rule due to rules integration lacking
* completeness.
*/
function mymodule_node_insert($node) {
// Make sure this is a group post/node.
if (og_is_group_content_type('node', $node->type)) {
// Get the group id.
$grp_ref = field_get_items('node', $node, 'og_group_ref', LANGUAGE_NONE);
$gid = $grp_ref[0]['target_id'];
// Save the args needed to send a message later.
$message_args = array(
'message_type' => 'message_new_group_post',
'nid' => $node->nid,
'gid' => $gid,
);
// Queue up the sending of group-wide notifications.
// We can't directly call message_notify_send_message here since some
// groups are known to have 1100+ members. Even though sending
// is queued with queue_mail, the process of queueing in the context of
// node_insert is unreasonable and causes max_execution_time timeouts.
// Note that this does NOT mean we need two cron runs to get the messages
// sent. Rather, these get queued to be scheduled, then the scheduled
// messages go out in a single cron run.
$queue = DrupalQueue::get('notify_group_members');
$queue->createItem($message_args);
}
}
/**
* Implements hook_cron_queue_info().
*/
function mymodule_cron_queue_info() {
$queues['notify_group_members'] = array(
'worker callback' => '_mymodule_notify_group_members',
'time' => 120,
);
return $queues;
}
/**
* Helper function that runs on cron to notify all members of a group.
*
* @param (array) $message_args
* an array containing the following:
* - 'nid' => nid of the node to notify about
* - 'gid' => gid of the group to notify members of
* - 'message_type' => the type of the message to send
*/
function _mymodule_notify_group_members($message_args) {
// Load the node.
$node = node_load($message_args['nid']);
// Load the group.
$group = node_load($message_args['gid']);
// Set the rendered fields.
$options = array(
'rendered fields' => array(
'message_notify_email_subject' => 'field_message_rendered_subject',
'message_notify_email_body' => 'field_message_rendered_body',
),
);
// Get the group members via our custom view.
$group_members = views_get_view_result(
'og_members', 'block_member_info', $message_args['gid']
);
// Iterate the group members and send a message to each.
foreach ($group_members as $idx => $member) {
// Create a new message, assigned to the node author.
$message = message_create($message_args['message_type'], array(
'uid' => $member->uid,
));
// Populate the comment and group references so we can use tokens
// when the message is generated.
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_node_reference->set($node);
$wrapper->field_group_reference->set($group);
// Messages are queued to go out on cron with queue_mail.
message_notify_send_message($message, $options);
}
}
/**
* Implements hook_comment_insert().
*
* Send a message to the node author when a new comment is created on a group
* post. Doing this as a hook instead of a rule due to rules integration
* lacking completeness.
*/
function mymodule_comment_insert($comment) {
$node = node_load($comment->nid);
// Only send a message if the comment author and the node author are
// different users.
if ($comment->uid != $node->uid) {
// Make sure a comment was posted to group content.
if (og_is_group_content_type('node', $node->type)) {
// Load up the group.
$grp_ref = field_get_items('node', $node, 'og_group_ref', LANGUAGE_NONE);
$group = node_load($grp_ref[0]['target_id']);
// Create a new message, assigned to the node author.
$message = message_create('message_comment_on_group_post', array(
'uid' => $node->uid,
));
// Populate the comment and group references so we can use tokens
// when the message is generated.
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_comment_ref->set($comment);
$wrapper->field_group_reference->set($group);
// Let message-notify deliver the email, and send the message for us.
// We pass in the options the field names, that will be used to capture
// the rendered message, and provide an email log.
$options = array(
'rendered fields' => array(
'message_notify_email_subject' => 'field_message_rendered_subject',
'message_notify_email_body' => 'field_message_rendered_body',
),
);
// Messages are queued to go out on cron with queue_mail.
message_notify_send_message($message, $options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment