'notifications-lite', 'subject' => $subject, 'body' => empty($body) ? $subject : $body, 'language' => user_preferred_language($account), ); return messaging_message_send_user($account, $message); } else { // We don't have anything else so we use Drupal's mail $account = user_load(array('uid' => $uid)); $params = array( 'type' => 'notifications-lite', 'subject' => $subject, 'body' => empty($body) ? $subject : $body, ); return drupal_mail('notifications_lite', 'notifications-lite-'.$action, $account->mail, user_preferred_language($account), $params); } } /** * Put simple notification into queue * * @param $uid * User id for destination * @param $subject * Notification subject * @param $body * Optional notification body * @param $action * Optional action name, so other modules can define specific message parts for their actions * If so, they must define event types too */ function notifications_lite_add_to_queue($uid, $subject, $body = '', $action = 'default', $params = array()) { // Build and store simple event $event = array( 'module' => 'notifications', 'uid' => 0, 'oid' => $uid, 'type' => 'lite', 'action' => $action, 'params' => array('for-uid' => $uid, 'subject' => $subject, 'body' => $body), 'queue' => FALSE, // Do not send to notifications queue, save some queries 'counter' => 1, // There will be just one queued row for this event ); $event['params'] = array_merge($event['params'], $params); $event = notifications_event($event); // Store row in notifications queue and for immediate sending notifications_lite_queue_event($event); notifications_module_invoke('event queued', $event); notifications_send_immediate($event->eid); } /** * Implementation of hook_mail() */ function notifications_lite_mail($key, &$message, $params) { $message['subject'] = $params['subject']; $message['body'] = $params['body']; } /** * Implementation of hook_notifications() * * It handles event texts to rebuild the message from stored event parameters */ function notifications_lite_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) { switch ($op) { case 'event load': // $arg0 is event $event = &$arg0; if ($event->type == 'lite') { if (!empty($event->params['subject'])) { $event->text['subject'] = $event->params['subject']; $event->text['digest'] = $event->params['subject']; } if (!empty($event->params['body'])) { $event->text['main'] = $event->params['body']; $event->text['digest'] .= ': ' . $event->params['body']; } } break; case 'event types': // We just define event type 'lite', action 'default' $types[] = array( 'type' => 'lite', 'action' => 'default', 'name' => t('Message for [user]'), 'digest' => NULL, // This means grouping by: event type, event action 'description' => t('Notifications lite message'), ); return $types; } } /** * Insert lite notification into queue. * * This just insert a row into notifications_queue table for this event, so it will be processed on cron * The message information (subject, body) will be in the event itself, as serialized parameters */ function notifications_lite_queue_event($event) { $uid = $event->params['for-uid']; $account = user_load(array('uid' => $uid)); $queue = array( 'uid' => $uid, 'sid' => 0, 'module' => 'notifications', 'eid' => $event->eid, 'send_interval' => notifications_user_setting('send_interval', $account), 'send_method' => notifications_user_setting('send_method', $account), 'cron' => 1, 'created' => time(), 'conditions' => 0, ); return drupal_write_record('notifications_queue', $queue); } /** * Implementation of hook_messaging() */ function notifications_lite_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) { switch ($op) { case 'message groups': // Generic notifications lite event (message) $info['notifications-event-lite'] = array( 'module' => 'notifications_lite', 'name' => t('Simple notifications'), 'help' => t('The subject and main body will be provided by the event itself'), 'description' => t('Simple notifications triggered by other modules using the Notifications Lite API module.'), 'fallback' => 'notifications-event', ); $info['notifications-digest-lite'] = array( 'module' => 'notifications_lite', 'name' => t('Group of simple notifications'), 'description' => t('Simple notifications digested with short format.'), 'help' => t('Every line of the digest will be a separate message.'), 'fallback' => 'notifications-digest', ); return $info; case 'message keys': $type = $arg1; switch ($type) { case 'notifications-event-lite': // The other parts for these messages will be given by the event itself return array( 'header' => t('Header'), 'footer' => t('Footer'), ); break; case 'notifications-digest-lite': $parts['title'] = t('Group title'); $parts['closing'] = t('Group footer'); return $parts; } break; case 'messages': $type = $arg1; // Event notifications switch ($type) { case 'notifications-event-lite': return array( 'header' => t("Greetings, [user]."), 'footer' => array( t('This is an automatic message from [site-name]'), t('To manage your subscriptions, browse to [subscriptions-manage]'), ), ); case 'notifications-digest-lite': return array( 'title' => t('Generic messages'), 'closing' => '...', ); } break; } }