| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Subscriptions module. Queue processing.
|
| 4 |
*
|
| 5 |
* @ TO DO: Support different languages for message localization
|
| 6 |
*/
|
| 7 |
|
| 8 |
// Number of users to process for each step
|
| 9 |
define('NOTIFICATIONS_STEP_ROWS', 1000);
|
| 10 |
define('NOTIFICATIONS_STEP_USERS', 1000);
|
| 11 |
|
| 12 |
// Minimum amount of seconds the process will need for clean-up tasks
|
| 13 |
// Just to make sure that after exhausting cron assigned time we'll have a few spare seconds for some cleanup
|
| 14 |
define('NOTIFICATIONS_TIME_MARGIN', 5);
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Function to be called on cron by the main notifications_cron
|
| 18 |
*
|
| 19 |
* It will call each subscription_process for each interval a number of times
|
| 20 |
*
|
| 21 |
* This should send out messages starting with immediate delivery. We send first immediate delivery
|
| 22 |
* because the other ones can be added up for each period.
|
| 23 |
* Assumption: The bigger the interval, the longer delay it may admit (?) I.e. sending hourly email
|
| 24 |
* after 1 hour 15 mins may be ok if the system is overloaded.
|
| 25 |
*/
|
| 26 |
function notifications_process_run($cron = TRUE) {
|
| 27 |
notifications_log('Starting queue process');
|
| 28 |
notifications_process('start');
|
| 29 |
// There may be special time adjustments for cron
|
| 30 |
if ($cron) {
|
| 31 |
notifications_process('cron');
|
| 32 |
}
|
| 33 |
$stop = FALSE;
|
| 34 |
$send_intervals = _notifications_send_intervals();
|
| 35 |
unset($send_intervals[-1]);
|
| 36 |
$max_sqid = notifications_process_prepare();
|
| 37 |
foreach ($send_intervals as $interval => $name) {
|
| 38 |
notifications_log('Processing send interval '.$name);
|
| 39 |
while (notifications_process_queue($interval, $max_sqid)) {
|
| 40 |
$stop = !notifications_process('check');
|
| 41 |
}
|
| 42 |
if ($stop) {
|
| 43 |
notifications_log('Process stopped');
|
| 44 |
break;
|
| 45 |
}
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Support of pull messaging
|
| 51 |
*
|
| 52 |
* @see messaging_pull_pending()
|
| 53 |
*
|
| 54 |
* @return
|
| 55 |
* Array of pending messages
|
| 56 |
*/
|
| 57 |
function notifications_process_pull($method, $users, $limit = 0, $delete = FALSE) {
|
| 58 |
$messages = array();
|
| 59 |
$maxsqid = 0;
|
| 60 |
// Just fetches row in creation order de-duping same events on the step
|
| 61 |
$sql = "SELECT uid, eid, MIN(sid) AS sid, MAX(sqid) AS sqid FROM {notifications_queue} ";
|
| 62 |
$sql .= "WHERE send_method = '%s' AND uid IN (%s) GROUP BY uid, eid ORDER BY sqid";
|
| 63 |
$str_uids = implode(',', $users);
|
| 64 |
if ($limit) {
|
| 65 |
$result = db_query_range($sql, $method, $str_uids, 0, $limit);
|
| 66 |
} else {
|
| 67 |
$result = db_query($sql, $method, $str_uids);
|
| 68 |
}
|
| 69 |
|
| 70 |
// Fetch and prepare messages
|
| 71 |
while ($queue = db_fetch_object($result)) {
|
| 72 |
$maxsqid = $queue->squid;
|
| 73 |
$account = notifications_load_user($queue->uid);
|
| 74 |
$event = notifications_load_event($queue->eid);
|
| 75 |
// Access control for event objects
|
| 76 |
if (notifications_user_allowed('event', $account, $event)) {
|
| 77 |
$subscriptions = array($queue->sid);
|
| 78 |
$message = notifications_process_message($account, $event, $subscriptions, $method);
|
| 79 |
$message['uid'] = $queue->uid;
|
| 80 |
$message['from'] = $event->uid;
|
| 81 |
$messages[] = $message;
|
| 82 |
}
|
| 83 |
}
|
| 84 |
// Delete returned rows
|
| 85 |
if ($messages && $delete) {
|
| 86 |
db_query("DELETE FROM {notifications_queue} WHERE sqid < %d AND send_method = '%s' AND uid IN (%s)", $maxsqid, $method, $str_uids);
|
| 87 |
}
|
| 88 |
|
| 89 |
// Return collected messages;
|
| 90 |
return $messages;
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Prepare subscriptions queue
|
| 95 |
*
|
| 96 |
* This is intended to avoid race conditions where new rows are added while the process is running
|
| 97 |
*
|
| 98 |
* @return
|
| 99 |
* Max $sqid that will be processed this cron
|
| 100 |
*/
|
| 101 |
function notifications_process_prepare($module = 'notifications') {
|
| 102 |
// Another funny race condition may occur when the event is saved but the subs queue not yet populated
|
| 103 |
$expiretime = time() - 60;
|
| 104 |
// Clean up event table.
|
| 105 |
// @ TODO Consider a different timing for this cleanup
|
| 106 |
db_query("DELETE FROM {notifications_event} WHERE created < %d AND eid NOT IN (SELECT eid FROM {notifications_queue})", $expiretime);
|
| 107 |
$squid = db_result(db_query("SELECT max(sqid) FROM {notifications_queue}"));
|
| 108 |
if ($squid) {
|
| 109 |
// Delete non existent or blocked users
|
| 110 |
db_query("DELETE FROM {notifications_queue} WHERE module = '%s' AND uid NOT IN (SELECT uid FROM {users} WHERE status = 1)", $module);
|
| 111 |
}
|
| 112 |
// @ TODO We may use this step to fill in some data or doing some other optimizations
|
| 113 |
return $squid;
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Controls and checks limits for queue processing
|
| 118 |
* It can be used by other modules to add their own limits here, like number of sms sent, etc...
|
| 119 |
* @param $op
|
| 120 |
* 'start' => Start the counters
|
| 121 |
* 'cron' => Special time adjustment for cron operations
|
| 122 |
* 'init' => Start a new counter with $value limit
|
| 123 |
* 'option' => Sets /gets options
|
| 124 |
* - debug
|
| 125 |
* - output Enables output for admin page
|
| 126 |
* @return
|
| 127 |
* TRUE if we are yet under the processing limits
|
| 128 |
*/
|
| 129 |
function notifications_process($op = 'check', $name = NULL, $value = NULL) {
|
| 130 |
static $limit = array(), $options = array();
|
| 131 |
static $current = array('message' => 0, 'step' => 0);
|
| 132 |
|
| 133 |
switch($op) {
|
| 134 |
case 'start':
|
| 135 |
$defaults = variable_get('notifications_process_limit', array('time' => 0, 'message' => 0, 'row' => 0, 'percent' => 0));
|
| 136 |
foreach ($defaults as $name => $value) {
|
| 137 |
if ($value && !isset($limit[$name])) {
|
| 138 |
$limit[$name] = $value;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
break;
|
| 142 |
case 'cron':
|
| 143 |
// Calculate time limit. We get the smaller of all these times in seconds
|
| 144 |
$timelimit[] = variable_get('cron_semaphore', 0) + ini_get('max_execution_time') - NOTIFICATIONS_TIME_MARGIN;
|
| 145 |
if ($limit['time']) {
|
| 146 |
$timelimit[] = time() + $limit['time'];
|
| 147 |
}
|
| 148 |
if ($limit['percent']) {
|
| 149 |
$timelimit[] = time() + ini_get('max_execution_time') * $limit['percent'] / 100;
|
| 150 |
unset($limit['percent']);
|
| 151 |
}
|
| 152 |
$limit['time'] = min($timelimit);
|
| 153 |
break;
|
| 154 |
case 'init':
|
| 155 |
$current[$name] = 0;
|
| 156 |
$limit[$name] = $value;
|
| 157 |
break;
|
| 158 |
case 'count':
|
| 159 |
$current[$name] += $value ? $value : 1;
|
| 160 |
break;
|
| 161 |
case 'option':
|
| 162 |
if (isset($value)) {
|
| 163 |
$options[$name] = $value;
|
| 164 |
}
|
| 165 |
return $options[$name];
|
| 166 |
}
|
| 167 |
|
| 168 |
$current['time'] = time();
|
| 169 |
|
| 170 |
// Check all limits till we find a false one
|
| 171 |
foreach ($limit as $name => $value) {
|
| 172 |
if (($current[$name] >= $value)) {
|
| 173 |
watchdog('notifications', t('Reached processing limit on queue processing: %name = %value', array('%name' => $name, '%value' => $value)));
|
| 174 |
return FALSE;
|
| 175 |
}
|
| 176 |
}
|
| 177 |
return TRUE;
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Process rows given query conditions
|
| 182 |
*
|
| 183 |
* This is used by the immediate sending feature
|
| 184 |
* @see notifications_queue_query()
|
| 185 |
*
|
| 186 |
* @param $conditions
|
| 187 |
* Array of query conditions
|
| 188 |
*/
|
| 189 |
function notifications_process_rows($conditions) {
|
| 190 |
$account = NULL;
|
| 191 |
$subscriptions = $events = $processed = array();
|
| 192 |
$send_method = $send_interval = NULL;
|
| 193 |
|
| 194 |
// Build query and fetch rows from queue
|
| 195 |
$query = notifications_queue_query($conditions);
|
| 196 |
$sql = "SELECT * FROM {notifications_queue} ";
|
| 197 |
$sql .= " WHERE ".implode(' AND ', $query['where']);
|
| 198 |
$sql .= " ORDER BY uid, send_method, send_interval";
|
| 199 |
$result = db_query($sql, $query['args']);
|
| 200 |
|
| 201 |
// Group rows by user, send_method, send_interval before sending
|
| 202 |
// This loop has to run a final time after all rows have been fetched
|
| 203 |
while (($queue = db_fetch_object($result)) || $processed) {
|
| 204 |
if (!$account || !$queue || ($queue->uid != $account->uid) || $queue->send_method != $send_method || $queue->send_interval != $send_interval) {
|
| 205 |
// New user or sending method, send if not the first row and reset
|
| 206 |
if ($account && $events && $subscriptions) {
|
| 207 |
notifications_process_send($account, $events, $subscriptions, $send_method, $send_interval);
|
| 208 |
notifications_update_sent($account->uid, $send_method, $send_interval, time());
|
| 209 |
}
|
| 210 |
if ($processed) {
|
| 211 |
notifications_queue_delete(array('sqids' => $processed));
|
| 212 |
}
|
| 213 |
$subscriptions = $events = $processed = array();
|
| 214 |
if ($queue) {
|
| 215 |
$account = notifications_load_user($queue->uid);
|
| 216 |
$send_method = $queue->send_method;
|
| 217 |
$send_interval = $queue->send_interval;
|
| 218 |
}
|
| 219 |
}
|
| 220 |
if ($queue) {
|
| 221 |
$event = notifications_load_event($queue->eid);
|
| 222 |
if (notifications_user_allowed('event', $account, $event)) {
|
| 223 |
$events[$queue->eid] = $event;
|
| 224 |
$subscriptions[$queue->eid][] = $queue->sid;
|
| 225 |
}
|
| 226 |
$processed[] = $queue->sqid;
|
| 227 |
}
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Process subscriptions queue
|
| 233 |
*
|
| 234 |
* The subscriptions queue has the following fields
|
| 235 |
* sqid, uid, eid, sid, digest
|
| 236 |
*
|
| 237 |
* This function should be able of splitting the whole processing in several steps.
|
| 238 |
* It will be called multiple times for each send interval
|
| 239 |
*
|
| 240 |
* Messages will be processed for each send interval, send_method, user
|
| 241 |
*
|
| 242 |
* @param $send_interval
|
| 243 |
* Send interval to process
|
| 244 |
* @param $max_sqid
|
| 245 |
* Max queue id to process
|
| 246 |
* @param $module
|
| 247 |
* Module's rows to process. There may be other types of rows sitting on the table
|
| 248 |
* @return Number of rows processed
|
| 249 |
*
|
| 250 |
* @ TODO Review time conditions
|
| 251 |
*/
|
| 252 |
function notifications_process_queue($send_interval, $max_sqid, $module = 'notifications') {
|
| 253 |
$count = 0;
|
| 254 |
// This is the time from which stored rows will be sent
|
| 255 |
$timelimit = time() - $send_interval;
|
| 256 |
|
| 257 |
// Get users to process messages for, with this time interval and ordered by squid
|
| 258 |
// Order by last sent for this send interval
|
| 259 |
// Note: If we get the users with more messages pending first this may save some time
|
| 260 |
$sql = "SELECT q.uid, q.send_method, count(*) AS count FROM {notifications_queue} q ";
|
| 261 |
$sql .= " LEFT JOIN {notifications_sent} su ON q.uid = su.uid AND q.send_interval = su.send_interval AND q.send_method = su.send_method ";
|
| 262 |
$sql .= " WHERE q.cron = 1 AND q.send_interval = '%d' AND q.sqid <= %d ";
|
| 263 |
$sql .= " AND (su.uid IS NULL OR su.sent < %d) ";
|
| 264 |
$sql .= " GROUP BY q.uid, q.send_method ORDER BY su.sent";
|
| 265 |
$result = db_query_range($sql, $send_interval, $max_sqid, $timelimit, 0, NOTIFICATIONS_STEP_USERS);
|
| 266 |
|
| 267 |
$sqid = 0;
|
| 268 |
// @ TODO Add time conditions
|
| 269 |
while (($user = db_fetch_object($result)) && notifications_process('check')) {
|
| 270 |
notifications_log("Processing user $user->uid, rows $user->count, send_method $user->send_method");
|
| 271 |
$events = $subscriptions = $processed = array();
|
| 272 |
$send_method = $user->send_method;
|
| 273 |
$account = notifications_load_user($user->uid);
|
| 274 |
|
| 275 |
// Process all rows for this user. With some hard limit to prevent process lock ups.
|
| 276 |
$result_subs = db_query_range("SELECT * FROM {notifications_queue} WHERE cron = 1 AND send_interval = '%d' AND uid = %d AND sqid <= %d ORDER BY send_method, sqid", $send_interval, $account->uid, $max_sqid, 0, NOTIFICATIONS_STEP_ROWS);
|
| 277 |
while (($queue = db_fetch_object($result_subs)) && notifications_process('count', 'row')) {
|
| 278 |
$count++;
|
| 279 |
$processed[] = $sqid = $queue->sqid;
|
| 280 |
// Load event and check access
|
| 281 |
$event = notifications_load_event($queue->eid, TRUE);
|
| 282 |
if (notifications_user_allowed('event', $account, $event)) {
|
| 283 |
// This will take care of duplicated events
|
| 284 |
$events[$queue->eid] = $event;
|
| 285 |
// We keep track also of subscriptions originating this event
|
| 286 |
$subscriptions[$queue->eid][] = $queue->sid;
|
| 287 |
}
|
| 288 |
notifications_log("Processing queued sid=$queue->sid event=$queue->eid ($event->type, $event->action) send_method=$send_method");
|
| 289 |
}
|
| 290 |
if ($events) {
|
| 291 |
notifications_process_send($account, $events, $subscriptions, $send_method, $send_interval);
|
| 292 |
notifications_update_sent($user->uid, $send_method, $send_interval, time());
|
| 293 |
}
|
| 294 |
if ($processed && !notifications_process('option', 'debug')) {
|
| 295 |
notifications_queue_delete(array('uid' => $user->uid, 'send_interval' => $send_interval, 'send_method' => $send_method, 'max_sqid' => $sqid));
|
| 296 |
}
|
| 297 |
}
|
| 298 |
|
| 299 |
return $count;
|
| 300 |
}
|
| 301 |
|
| 302 |
/**
|
| 303 |
* Update user last time sent
|
| 304 |
*/
|
| 305 |
function notifications_update_sent($uid, $method, $interval, $time) {
|
| 306 |
db_query("UPDATE {notifications_sent} SET sent = %d WHERE uid = %d AND send_interval = '%d' AND send_method = '%s'", $time, $uid, $interval, $method);
|
| 307 |
if (!db_affected_rows()) {
|
| 308 |
db_query("INSERT INTO {notifications_sent}(uid, send_interval, send_method, sent) VALUES(%d, '%d', '%s', %d)", $uid, $interval, $method, $time);
|
| 309 |
}
|
| 310 |
}
|
| 311 |
/**
|
| 312 |
* Message delivery.
|
| 313 |
*
|
| 314 |
* Processes everything, included digestion and sends message/s.
|
| 315 |
*/
|
| 316 |
function notifications_process_send($account, $events, $subscriptions, $send_method, $send_interval) {
|
| 317 |
|
| 318 |
notifications_log("Sending out, method=$send_method, interval=$send_interval, events=". count($events));
|
| 319 |
// Digest if send_interval > 0 (not immediate sending)
|
| 320 |
if ($send_interval) {
|
| 321 |
$messages[] = notifications_process_digest($account, $events, $send_interval, $send_method);
|
| 322 |
} else {
|
| 323 |
foreach ($events as $event) {
|
| 324 |
$messages[] = notifications_process_message($account, $event, $subscriptions[$event->eid], $send_method);
|
| 325 |
}
|
| 326 |
}
|
| 327 |
// Now send messages
|
| 328 |
foreach ($messages as $message) {
|
| 329 |
notifications_process('count', 'send');
|
| 330 |
// notifications_log($message, 'message');
|
| 331 |
notifications_message_send($account, $message, $send_method);
|
| 332 |
}
|
| 333 |
}
|
| 334 |
|
| 335 |
/**
|
| 336 |
* Digest multiple events in a single message
|
| 337 |
*
|
| 338 |
* @return array with message 'subject' and 'body' elements
|
| 339 |
*/
|
| 340 |
function notifications_process_digest($account, $events, $send_interval, $send_method) {
|
| 341 |
// Compile list of events for each object
|
| 342 |
$list = array();
|
| 343 |
|
| 344 |
// Build up the digested list with text replacement
|
| 345 |
// We need text replacement for each line because it depends on different objects
|
| 346 |
foreach ($events as $event) {
|
| 347 |
notifications_log($event, 'digesting');
|
| 348 |
$info = notifications_event_types($event->type, $event->action);
|
| 349 |
$digest_type = $info['digest'][0];
|
| 350 |
$digest_field = $info['digest'][1];
|
| 351 |
$digest_object = $event->objects[$digest_type];
|
| 352 |
$digest_value = $digest_object->$digest_field;
|
| 353 |
if (!isset($list[$digest_type][$digest_value]['name'])) {
|
| 354 |
$name = $list[$digest_type][$digest_value]['name'] = notifications_text_replace($info['name'], array($digest_type => $digest_object));
|
| 355 |
notifications_log("Digesting object $name (type=$digest_type value=$digest_value)");
|
| 356 |
}
|
| 357 |
// Check duplicate notifications for the same event
|
| 358 |
if (!isset($list[$digest_type][$digest_value]['line'][$event->eid])) {
|
| 359 |
$line = $list[$digest_type][$digest_value]['line'][$event->eid] = notifications_text_replace($info['line'], $event->objects);
|
| 360 |
notifications_log("Digesting line $line");
|
| 361 |
}
|
| 362 |
}
|
| 363 |
// Create message. Do all this in one replacement, then strip out the subject
|
| 364 |
$text['subject'] = notifications_message_part('digest', 'subject', $send_method);
|
| 365 |
$text['header'] = notifications_message_part('digest', 'header', $send_method);
|
| 366 |
$text['footer'] = notifications_message_part('digest', 'footer', $send_method);
|
| 367 |
// We dont pass a subscription object here, won't be too much use anyway
|
| 368 |
$text = notifications_text_replace($text, array('user' => $account, 'subscription' => NULL));
|
| 369 |
|
| 370 |
// Compose body. All these lines have been text replaced
|
| 371 |
$body['header'] = $text['header'];
|
| 372 |
foreach ($list as $type => $objects) {
|
| 373 |
foreach ($objects as $oid => $data) {
|
| 374 |
$body[] = $data['name'];
|
| 375 |
foreach ($data['line'] as $line) {
|
| 376 |
$body[] = '- '. $line;
|
| 377 |
}
|
| 378 |
}
|
| 379 |
}
|
| 380 |
$body['footer'] = $text['footer'];
|
| 381 |
|
| 382 |
return array('subject' => $text['subject'], 'body' => $body);
|
| 383 |
}
|
| 384 |
|
| 385 |
/**
|
| 386 |
* Creates a single message for a single event
|
| 387 |
*
|
| 388 |
* @param $account
|
| 389 |
* Destination user account
|
| 390 |
* @param $event
|
| 391 |
* Event object which caused this notification
|
| 392 |
* @param $subscriptions
|
| 393 |
* Array of subscription ids
|
| 394 |
*
|
| 395 |
*/
|
| 396 |
function notifications_process_message($account, $event, $subscriptions, $send_method) {
|
| 397 |
$info = notifications_event_text($event);
|
| 398 |
|
| 399 |
// Create message. Do all this in one replacemente
|
| 400 |
$options = array($event->type, $event->action);
|
| 401 |
$text = array(
|
| 402 |
'subject' => notifications_message_part('event', 'subject', $send_method, $options),
|
| 403 |
'header' => notifications_message_part('event' ,'header', $send_method, $options),
|
| 404 |
'event' => notifications_message_part('event', 'main', $send_method, $options), // Was $info['detail'],
|
| 405 |
'footer' => notifications_message_part('event', 'footer', $send_method, $options),
|
| 406 |
);
|
| 407 |
|
| 408 |
// We pass only the first subscription, which is at least something
|
| 409 |
// @ TODO Handle nicely the case where there are more than one subscription
|
| 410 |
if ($sid = array_shift($subscriptions)) {
|
| 411 |
$subscription = notifications_load_subscription($sid);
|
| 412 |
} else {
|
| 413 |
$subscription = NULL;
|
| 414 |
}
|
| 415 |
$objects = array('user' => $account, 'event' => $event, 'subscription' => $subscription);
|
| 416 |
$objects = array_merge($objects, $event->objects);
|
| 417 |
$text = notifications_text_replace($text, $objects);
|
| 418 |
|
| 419 |
// Get subject out of text
|
| 420 |
$subject = $text['subject'];
|
| 421 |
unset($text['subject']);
|
| 422 |
return array('subject' => $subject, 'body' => $text);
|
| 423 |
}
|
| 424 |
|
| 425 |
/**** Retrieving and replacing text parts, interfacing with tokens and messaging module ****/
|
| 426 |
|
| 427 |
/**
|
| 428 |
* Get message part
|
| 429 |
*
|
| 430 |
* It searches for optional message group keys for options defaulting to $type
|
| 431 |
* thus if $options = array('part1', 'part2') it will search in this order
|
| 432 |
* 1. $type-part1-part2
|
| 433 |
* 2. $type-part1
|
| 434 |
* 3. $type
|
| 435 |
*
|
| 436 |
* @param $type
|
| 437 |
* Message type to send, either 'event' or 'digest'
|
| 438 |
* @param $key
|
| 439 |
* Id of message part, ie 'header'
|
| 440 |
* @param $method
|
| 441 |
* Method by which message will be sent. Normally 'mail'
|
| 442 |
* @param $options
|
| 443 |
* ?
|
| 444 |
*
|
| 445 |
* @return
|
| 446 |
* Part of the message.
|
| 447 |
*
|
| 448 |
*/
|
| 449 |
function notifications_message_part($type, $key, $method, $options = array()) {
|
| 450 |
$keyparts = array_merge(array('notifications-'.$type), $options);
|
| 451 |
// Output some debugging info in case we dont find a suitable message part
|
| 452 |
$output = "[UNDEFINED type = $type, method = $method, key = ".implode('-', $keyparts).']';
|
| 453 |
while ($keyparts) {
|
| 454 |
$groupkey = implode('-', $keyparts);
|
| 455 |
if ($text = messaging_message_part($groupkey, $key, $method)) {
|
| 456 |
$output = $text;
|
| 457 |
break;
|
| 458 |
}
|
| 459 |
// If no text trim out latest part of the key and retry
|
| 460 |
array_pop($keyparts);
|
| 461 |
}
|
| 462 |
return $output;
|
| 463 |
}
|
| 464 |
|
| 465 |
/**
|
| 466 |
* Text replacement with tokens
|
| 467 |
*/
|
| 468 |
function notifications_text_replace($text, $objects) {
|
| 469 |
// Add some token types
|
| 470 |
$objects['global'] = NULL;
|
| 471 |
return token_replace_multiple($text, $objects);
|
| 472 |
}
|
| 473 |
|
| 474 |
/**
|
| 475 |
* Message sending
|
| 476 |
*/
|
| 477 |
function notifications_message_send($account, $message, $send_method) {
|
| 478 |
$message['type'] = 'notifications';
|
| 479 |
notifications_process('count', 'message');
|
| 480 |
messaging_message_send_user($account, $message, $send_method);
|
| 481 |
}
|
| 482 |
|
| 483 |
/**
|
| 484 |
* Get texts for event
|
| 485 |
*
|
| 486 |
* @ TODO Support for configurable texts
|
| 487 |
*/
|
| 488 |
function notifications_event_text($event) {
|
| 489 |
$info = notifications_event_types($event->type, $event->action);
|
| 490 |
return $info;
|
| 491 |
}
|
| 492 |
|
| 493 |
/**
|
| 494 |
* Get users with static caching
|
| 495 |
*/
|
| 496 |
function notifications_load_user($uid) {
|
| 497 |
static $cache = array();
|
| 498 |
if (!array_key_exists($uid, $cache)) {
|
| 499 |
$cache[$uid] = user_load(array('uid' => $uid));
|
| 500 |
}
|
| 501 |
return $cache[$uid];
|
| 502 |
}
|
| 503 |
|
| 504 |
/**
|
| 505 |
* Get events with static caching
|
| 506 |
*/
|
| 507 |
function notifications_load_event($id) {
|
| 508 |
static $cache = array();
|
| 509 |
if (!array_key_exists($id, $cache)) {
|
| 510 |
$event = db_fetch_object(db_query("SELECT * FROM {notifications_event} WHERE eid = %d", $id));
|
| 511 |
$event->params = unserialize($event->params);
|
| 512 |
// Load aditional objects for the event
|
| 513 |
$event->objects = array();
|
| 514 |
notifications_module_invoke('event load', $event);
|
| 515 |
$cache[$id] = $event;
|
| 516 |
}
|
| 517 |
return $cache[$id];
|
| 518 |
}
|
| 519 |
|
| 520 |
/**
|
| 521 |
* Delete rows from subscriptions queue
|
| 522 |
*
|
| 523 |
* @see notifications_queue_query()
|
| 524 |
*
|
| 525 |
* Note: Handle with care if no params may delete all rows
|
| 526 |
*/
|
| 527 |
function notifications_queue_delete($params) {
|
| 528 |
$query = notifications_queue_query($params);
|
| 529 |
db_query("DELETE FROM {notifications_queue} WHERE ".implode(' AND ', $query['where']), $query['args']);
|
| 530 |
}
|
| 531 |
|
| 532 |
/**
|
| 533 |
* Build query conditions for queue queries
|
| 534 |
*
|
| 535 |
* @param $params
|
| 536 |
* Array of parameters, field => value form
|
| 537 |
* Special parameters
|
| 538 |
* 'max_squid' => max squid to delete
|
| 539 |
* 'rows' => array of squid values to delte
|
| 540 |
* @return
|
| 541 |
* Array with 'where' and 'args' elements. Each of them is an array
|
| 542 |
*/
|
| 543 |
function notifications_queue_query($params) {
|
| 544 |
$where = $args = array();
|
| 545 |
|
| 546 |
foreach($params as $field => $value) {
|
| 547 |
switch ($field) {
|
| 548 |
case 'max_sqid':
|
| 549 |
$where[] = "sqid <= %d";
|
| 550 |
$args[] = $value;
|
| 551 |
break;
|
| 552 |
case 'sqids':
|
| 553 |
$where[] = "sqid IN (%s)";
|
| 554 |
$args[] = implode(',', array_map('db_escape_string', $value));
|
| 555 |
break;
|
| 556 |
default:
|
| 557 |
$where[] = "$field = '%s'";
|
| 558 |
$args[] = $value;
|
| 559 |
break;
|
| 560 |
}
|
| 561 |
}
|
| 562 |
return array('where' => $where, 'args' => $args);
|
| 563 |
}
|
| 564 |
|
| 565 |
/**
|
| 566 |
* Log for debugging
|
| 567 |
*/
|
| 568 |
function notifications_log($info = NULL, $type = 'info') {
|
| 569 |
static $logs;
|
| 570 |
if ($info) {
|
| 571 |
$message = $type.': ';
|
| 572 |
$message .= is_string($info) ? $info : print_r($info, TRUE);
|
| 573 |
$logs[] = $message;
|
| 574 |
if ($type == 'watchdog') {
|
| 575 |
watchdog('notifications', $info);
|
| 576 |
}
|
| 577 |
} else {
|
| 578 |
return $logs;
|
| 579 |
}
|
| 580 |
}
|