View source
<?php
module_load_include('inc', 'answers_notifications', 'includes/answers_notifications.flag_default');
module_load_include('inc', 'answers_notifications', 'includes/answers_notifications.message_default');
function answers_notifications_node_insert($node) {
if ($node->type == 'answers_answer') {
answers_notifications_new_answer_notify($node);
}
}
function answers_notifications_new_answer_notify($answer) {
$question = answers_answer_question($answer);
$fields = array(
'answers_notifications_answer' => $answer->nid,
'answers_notifications_question' => $question->nid,
);
answers_notifications_create_message('answers_notifications_new_answer', $answer->uid, $question, $fields);
}
function answers_notifications_create_message($type, $uid, $node, array $fields = array(), array $uids = array()) {
$message = message_create($type, array(
'uid' => $uid,
));
$wrapper = entity_metadata_wrapper('message', $message);
foreach ($fields as $name => $value) {
$wrapper->{$name}
->set($value);
}
$message
->save();
$notify_options = array(
'internal' => array(
'save on fail' => FALSE,
'save on success' => FALSE,
'mid' => $message->mid,
),
);
$subscribe_options = array(
'save message' => FALSE,
'author' => $uid,
);
if (!empty($uids)) {
$subscribe_options['uids'] = $uids;
}
return message_subscribe_send_message('node', $node, $message, $notify_options, $subscribe_options);
}