$uid)); } $language = user_preferred_language($account); $message = notifications_lite_build($subject, $body, $action, $language, $params); $accounts = array($account); // Send using the first available option if (module_exists('notifications')) { // So we have the full Notifications system enabled return notifications_lite_notifications_post($accounts, $message, $params); } elseif (module_exists('messaging')) { // We just have the Messaging system, that's something return notifications_lite_messaging_post($accounts, $message, $params); } else { // We don't have anything else so we use Drupal's mail return notifications_lite_drupal_post($accounts, $message, $params); } } /** * Post message using notifications * * @param unknown_type $accounts * @param unknown_type $message * @param unknown_type $params * @return int * Number of messages sent */ function notifications_lite_notifications_post($accounts, $message, $params) { $event = notifications_lite_event($message, $params, count($accounts)); foreach ($accounts as $account) { $result = notifications_lite_queue($account, $event); } // Notify other modules, try inmediate sending notifications_module_invoke('event queued', $event); notifications_send_immediate($event->eid); return count($result); } /** * Post message using Messaging * * @param unknown_type $accounts * @param unknown_type $message * @param unknown_type $params * @return int * Number of messages sent */ function notifications_lite_messaging_post($accounts, $data, $params) { $message = array( 'type' => 'notifications-lite', 'subject' => $data['subject'], 'body' => $data['body'], 'language' => $data['language']->language, 'params' => $data['params'], ); foreach ($accounts as $account) { $result[] = messaging_message_send_user($account, $message); } return count($result); } /** * Post message using Drupal mail * * @param unknown_type $accounts * @param unknown_type $message * @param unknown_type $params * @return int * Number of messages sent */ function notifications_lite_drupal_post($accounts, $message, $params) { foreach ($accounts as $account) { $language = user_preferred_language($account); $result[] = drupal_mail('notifications_lite', $message['mailkey'], $account->mail, $language, $message); } return count($result); } /** * Build message array from parameters * * Special params are * - language = language object * - action = notifications lite action */ function notifications_lite_build($subject, $body = '', $action = 'default', $language = NULL, $params = array()) { return array( 'type' => 'notifications-lite', 'mailkey' => 'notifications-lite-' . $action, 'subject' => $subject, 'body' => empty($body) ? $subject : $body, 'language' => $language ? $language : language_default() , 'action' => $action, 'params' => $params, ); } /** * Build notifications event */ function notifications_lite_event($message, $params, $counter = 1) { // Build and store simple event $event = array( 'module' => 'notifications', 'uid' => 0, 'oid' => 0, 'type' => 'lite', 'action' => $message['action'], 'params' => $params + array('subject' => $message['subject'], 'body' => $message['body']), 'queue' => FALSE, // Do not send to notifications queue, save some queries 'counter' => $counter, // As many as destinations 'language' => $message['language']->language, ); return notifications_event($event); } /** * Queue notifications event for user/s * * 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 * * @param $accounts * User account or array of user accounts * @param $event * Event to be queued */ function notifications_lite_queue($account, $event) { $queue = array( 'uid' => $account->uid, '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(), // No subscription, no conditions 'sid' => 0, 'conditions' => 0, ); return drupal_write_record('notifications_queue', $queue); } /** * 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; } } /** * Implementation of hook_messaging_template() */ function notifications_lite_messaging_template($op, $type = NULL, $langcode = NULL) { switch ($op) { case 'templates': // Generic notifications lite event (message) $info['notifications-event-lite'] = array( 'module' => 'notifications_lite', 'type' => 'notifications', 'title' => t('Simple notifications', array(), $langcode), 'help' => t('The subject and main body will be provided by the event itself', array(), $langcode), 'description' => t('Simple notifications triggered by other modules using the Notifications Lite API module.', array(), $langcode), 'fallback' => 'notifications-event', ); $info['notifications-digest-lite'] = array( 'module' => 'notifications_lite', 'type' => 'notifications', 'title' => t('Group of simple notifications', array(), $langcode), 'description' => t('Simple notifications digested with short format.', array(), $langcode), 'help' => t('Every line of the digest will be a separate message.', array(), $langcode), 'fallback' => 'notifications-digest', ); return $info; case 'keys': switch ($type) { case 'notifications-event-lite': // The other parts for these messages will be given by the event itself return array( 'header' => t('Header', array(), $langcode), 'footer' => t('Footer', array(), $langcode), ); break; } break; case 'defaults': // Event notifications switch ($type) { case 'notifications-event-lite': return array( 'header' => t("Greetings, [user].", array(), $langcode), 'footer' => array( t('This is an automatic message from [site-name]', array(), $langcode), t('To manage your subscriptions, browse to [subscriptions-manage]', array(), $langcode), ), ); case 'notifications-digest-lite': return array( 'title' => t('Generic messages', array(), $langcode), 'closing' => '...', ); } break; } }