| 1 |
<?php
|
| 2 |
// $Id: notifications.module,v 1.39 2007/10/22 12:21:26 jareyero Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Notifications module
|
| 7 |
*
|
| 8 |
* This is the base module of the notifications framework. It handles event processing, queueing,
|
| 9 |
* message composition and sending.
|
| 10 |
*
|
| 11 |
* Different subscriptions types are provided by plug-in modules implementing hook_notifications()
|
| 12 |
* Most of the UI is implemented in notifications_ui module
|
| 13 |
* The messaging framework is used for message delivery
|
| 14 |
* Token module is used for token replacement in messages
|
| 15 |
*
|
| 16 |
* This is based on the previous subscriptions module
|
| 17 |
*
|
| 18 |
* Development Seed, http://www.developmentseed.org, 2007
|
| 19 |
*
|
| 20 |
*/
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_menu().
|
| 24 |
*/
|
| 25 |
function notifications_menu($may_cache) {
|
| 26 |
global $user; // we need the user to to build some urls
|
| 27 |
|
| 28 |
if ($may_cache) {
|
| 29 |
// Administration
|
| 30 |
$items[] = array('path' => 'admin/notifications',
|
| 31 |
'title' => t('Notifications'),
|
| 32 |
'description' => t('Configure and manage notifications modules.'),
|
| 33 |
'callback' => 'notifications_admin_overview_page',
|
| 34 |
'access' => user_access('administer notifications'),
|
| 35 |
);
|
| 36 |
$items[] = array('path' => 'admin/notifications/status',
|
| 37 |
'title' => t('Status'),
|
| 38 |
'description' => t('Manage users notifications.'),
|
| 39 |
'callback' => 'notifications_admin_status_page',
|
| 40 |
'access' => user_access('administer notifications'),
|
| 41 |
);
|
| 42 |
$items[] = array('path' => 'admin/notifications/status/overview',
|
| 43 |
'title' => t('Overview'),
|
| 44 |
'description' => t('Subscriptions overview.'),
|
| 45 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 46 |
'weight' => -10,
|
| 47 |
);
|
| 48 |
$items[] = array('path' => 'admin/notifications/status/queue',
|
| 49 |
'title' => t('Queue'),
|
| 50 |
'description' => t('Subscriptions queue.'),
|
| 51 |
'callback' => 'notifications_admin_queue',
|
| 52 |
'type' => MENU_LOCAL_TASK
|
| 53 |
);
|
| 54 |
|
| 55 |
// Site settings
|
| 56 |
$items[] = array(
|
| 57 |
'path' => 'admin/notifications/settings',
|
| 58 |
'title' => t('Settings'),
|
| 59 |
'description' => t('Site settings for user notifications.'),
|
| 60 |
'callback' => 'drupal_get_form',
|
| 61 |
'callback arguments' => 'notifications_settings_form',
|
| 62 |
'access' => user_access('administer site configuration'),
|
| 63 |
);
|
| 64 |
$items[] = array(
|
| 65 |
'path' => 'admin/notifications/settings/general',
|
| 66 |
'title' => t('General'),
|
| 67 |
'weight' => -10,
|
| 68 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 69 |
);
|
| 70 |
// Subscribe links. For this items access will be checked later in the page
|
| 71 |
$items[] = array(
|
| 72 |
'path' => 'notifications/subscribe',
|
| 73 |
'type' => MENU_CALLBACK,
|
| 74 |
'callback' => 'notifications_page_subscribe',
|
| 75 |
'access' => $user->uid,
|
| 76 |
);
|
| 77 |
// Unsubscribe links This page will need to work with anonymous users
|
| 78 |
$items[] = array(
|
| 79 |
'path' => 'notifications/unsubscribe',
|
| 80 |
'type' => MENU_CALLBACK,
|
| 81 |
'callback' => 'notifications_page_unsubscribe',
|
| 82 |
'access' => TRUE,
|
| 83 |
);
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
if (arg(0) == 'notifications' || arg(1) == 'notifications' || arg(2) == 'notifications') {
|
| 87 |
include_once drupal_get_path('module', 'notifications') .'/notifications.admin.inc';
|
| 88 |
}
|
| 89 |
|
| 90 |
if ($user->uid && arg(0) == 'user' && is_numeric(arg(1)) && ($user->uid == arg(1) && user_access('maintain own subscriptions') || user_access('administer notifications'))) {
|
| 91 |
$account = ($user->uid == arg(1)) ? $user : user_load(array('uid' => arg(1)));
|
| 92 |
$items[] = array(
|
| 93 |
'path' => 'user/'. $account->uid .'/notifications',
|
| 94 |
'type' => MENU_LOCAL_TASK,
|
| 95 |
'title' => t('Notifications'),
|
| 96 |
'callback' => 'notifications_page_user_overview',
|
| 97 |
'callback arguments' => array($account)
|
| 98 |
);
|
| 99 |
$items[] = array(
|
| 100 |
'path' => 'user/'. $account->uid .'/notifications/overview',
|
| 101 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 102 |
'title' => t('Overview'),
|
| 103 |
'weight' => -10,
|
| 104 |
);
|
| 105 |
}
|
| 106 |
}
|
| 107 |
return $items;
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Implementation of hook_perms()
|
| 112 |
*
|
| 113 |
*/
|
| 114 |
function notifications_perm() {
|
| 115 |
return array_merge(array('administer notifications', 'maintain own subscriptions'));
|
| 116 |
}
|
| 117 |
|
| 118 |
/**
|
| 119 |
* Implementation of hook_user().
|
| 120 |
*/
|
| 121 |
function notifications_user($type, $edit, &$user, $category = NULL) {
|
| 122 |
switch ($type) {
|
| 123 |
case 'form':
|
| 124 |
if ((user_access('maintain own subscriptions') || user_access("admin users subscriptions") )&& $category == 'account') {
|
| 125 |
$form['notifications'] = array(
|
| 126 |
'#type' => 'fieldset',
|
| 127 |
'#title' => t('Notifications settings'),
|
| 128 |
'#weight' => 5,
|
| 129 |
'#collapsible' => TRUE,
|
| 130 |
);
|
| 131 |
$form['notifications']['notifications_send_method'] = array(
|
| 132 |
'#type' => 'select',
|
| 133 |
'#title' => t('Default send method'),
|
| 134 |
'#options' => _notifications_send_methods($user),
|
| 135 |
'#default_value' => notifications_user_setting('send_method', $user),
|
| 136 |
);
|
| 137 |
$form['notifications']['notifications_send_interval'] = array(
|
| 138 |
'#type' => 'select',
|
| 139 |
'#title' => t('Default send interval'),
|
| 140 |
'#options' => _notifications_send_intervals(),
|
| 141 |
'#default_value' => notifications_user_setting('send_interval', $user),
|
| 142 |
);
|
| 143 |
/*
|
| 144 |
$form['notifications']['notifications_teaser'] = array(
|
| 145 |
'#type' => 'checkbox',
|
| 146 |
'#title' => t('Include teaser'),
|
| 147 |
'#default_value' => isset($edit['notifications_teaser']) ? $edit['notifications_teaser'] : variable_get('notifications_teaser', 0),
|
| 148 |
'#description' => t('Checking this box adds an excerpt of the post to the subscription email.'),
|
| 149 |
);
|
| 150 |
*/
|
| 151 |
return $form;
|
| 152 |
}
|
| 153 |
break;
|
| 154 |
}
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Gets a user setting, defaults to default system setting for each
|
| 159 |
*
|
| 160 |
* @param $name
|
| 161 |
* Setting name
|
| 162 |
* @param $account
|
| 163 |
* Optional user account, will default to current user
|
| 164 |
* @param $default
|
| 165 |
* Optional default to return if this is not set
|
| 166 |
*/
|
| 167 |
function notifications_user_setting($name, $account = NULL, $default = NULL) {
|
| 168 |
global $user;
|
| 169 |
$account = $account ? $account : $user;
|
| 170 |
$field = 'notifications_'.$name;
|
| 171 |
if (isset($account->$field)) {
|
| 172 |
return $account->$field;
|
| 173 |
} else {
|
| 174 |
return variable_get('notifications_default_'.$name, $default);
|
| 175 |
}
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Process subscriptions events
|
| 180 |
*/
|
| 181 |
function notifications_event($event){
|
| 182 |
global $user;
|
| 183 |
// Fill in event with default values
|
| 184 |
$event += array(
|
| 185 |
'uid' => $user->uid,
|
| 186 |
'load_args' => '',
|
| 187 |
'created' => time(),
|
| 188 |
'module' => 'notifications', // Module that triggered the event
|
| 189 |
'type' => '', // Object/event type
|
| 190 |
'action' => '', // Action that happened to the object
|
| 191 |
'params' => array(), // Object parameters
|
| 192 |
);
|
| 193 |
$event = (object)$event;
|
| 194 |
// Notify other modules we are about to trigger some subscriptions event
|
| 195 |
// Modules can do cleanup operations or modify event properties
|
| 196 |
notifications_module_invoke('event trigger', $event);
|
| 197 |
// Store event
|
| 198 |
notifications_event_save($event);
|
| 199 |
// Queue subscriptions
|
| 200 |
notifications_queue($event);
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Queue events for notifications
|
| 205 |
*
|
| 206 |
* @param $event
|
| 207 |
* Event array.
|
| 208 |
*/
|
| 209 |
function notifications_queue($event) {
|
| 210 |
|
| 211 |
// Build query fields for this event type
|
| 212 |
$query_args = notifications_module_info('query', 'event', $event->type, $event);
|
| 213 |
$query['args'] = array($event->eid, $event->created, $event->type);
|
| 214 |
foreach ($query_args as $query_params) {
|
| 215 |
$query = notifications_query_build($query_params, $query);
|
| 216 |
}
|
| 217 |
|
| 218 |
// We throw in all the conditions and check the number of matching conditions
|
| 219 |
// that must be equal to the subscription conditions number
|
| 220 |
$sql = 'INSERT INTO {notifications_queue} (uid, sid, module, eid, send_interval, send_method, cron, created, conditions) '.
|
| 221 |
'SELECT DISTINCT s.uid, s.sid, s.module, %d, s.send_interval, s.send_method, s.cron, %d, s.conditions '.
|
| 222 |
'FROM {notifications} s INNER JOIN {notifications_fields} f ON s.sid = f.sid '.
|
| 223 |
implode (' ', $query['join']).
|
| 224 |
" WHERE s.status = 1 AND s.event_type = '%s' AND ((".implode(') OR (', $query['where']).'))'.
|
| 225 |
' GROUP BY s.uid, s.sid, s.module, s.conditions '. // May need more grouping here
|
| 226 |
' HAVING s.conditions = count(f.sid)';
|
| 227 |
db_query($sql, $query['args']);
|
| 228 |
|
| 229 |
// Modules can do cleanup operations or modify the queue
|
| 230 |
notifications_module_invoke('event queued', $event);
|
| 231 |
|
| 232 |
// Optional immediate sending.
|
| 233 |
if (variable_get('notifications_send_immediate', 0)) {
|
| 234 |
require_once drupal_get_path('module', 'notifications') .'/notifications.cron.inc';
|
| 235 |
notifications_process_rows(array('cron' => 1, 'eid' => $event->eid, 'send_interval' => 0));
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
/**
|
| 240 |
* Query builder for subscriptions
|
| 241 |
*
|
| 242 |
* @param $params
|
| 243 |
* Array of query conditions
|
| 244 |
*/
|
| 245 |
function notifications_query_build($params, $base = array()) {
|
| 246 |
$query = $base + array('select' => array(), 'join' => array(), 'where' => array(), 'args' => array());
|
| 247 |
foreach ($params as $name => $elements) {
|
| 248 |
if ($name == 'fields') {
|
| 249 |
foreach ($elements as $field => $value ) {
|
| 250 |
$query['where'][] = "f.field = '%s' AND f.value = '%s'";
|
| 251 |
$query['args'][] = $field;
|
| 252 |
$query['args'][] = $value;
|
| 253 |
}
|
| 254 |
} elseif (is_array($elements)) {
|
| 255 |
$query[$name] = array_merge($query[$name], $elements);
|
| 256 |
} else {
|
| 257 |
$query[$name][] = $elements;
|
| 258 |
}
|
| 259 |
}
|
| 260 |
return $query;
|
| 261 |
}
|
| 262 |
|
| 263 |
/**
|
| 264 |
* Stores new events
|
| 265 |
*
|
| 266 |
* @param $event
|
| 267 |
* Event object
|
| 268 |
* @ TODO: Avoid to and from array conversion
|
| 269 |
*/
|
| 270 |
function notifications_event_save(&$event) {
|
| 271 |
$event = (array)$event;
|
| 272 |
$event['eid'] = db_next_id('{notifications_event}_eid');
|
| 273 |
db_query ("INSERT INTO {notifications_event} (eid, module, type, action, uid, created, params) VALUES (%d, '%s', '%s', '%s', %d, %d, '%s')",
|
| 274 |
$event['eid'], $event['module'], $event['type'], $event['action'], $event['uid'], $event['created'], serialize($event['params']));
|
| 275 |
$event = (object)$event;
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
* Get subscription for a given user
|
| 280 |
*
|
| 281 |
* @param $uid
|
| 282 |
* User id
|
| 283 |
* @param $event_type
|
| 284 |
* Event type
|
| 285 |
* @param $oid
|
| 286 |
* Object id for caching. I.e. for a node it will be nid
|
| 287 |
* @param $object
|
| 288 |
* Object to check subscriptions to. I.e. $node
|
| 289 |
*
|
| 290 |
* @return
|
| 291 |
* Array of subscriptions for this user and object indexed by sid
|
| 292 |
*/
|
| 293 |
function notifications_user_get_subscriptions($uid, $event_type, $oid, $object = NULL, $refresh = FALSE) {
|
| 294 |
static $subscriptions;
|
| 295 |
|
| 296 |
if ($refresh || !isset($subscriptions[$uid][$event_type][$oid])){
|
| 297 |
$subscriptions[$uid][$event_type][$oid] = array();
|
| 298 |
$query_args = notifications_module_info('query', 'user', $event_type, $object);
|
| 299 |
// Base query
|
| 300 |
$query = array(
|
| 301 |
'args' => array($uid, $event_type),
|
| 302 |
);
|
| 303 |
foreach ($query_args as $query_params) {
|
| 304 |
$query = notifications_query_build($query_params, $query);
|
| 305 |
}
|
| 306 |
|
| 307 |
$sql = 'SELECT s.*, f.* FROM {notifications} s INNER JOIN {notifications_fields} f ON s.sid = f.sid '.
|
| 308 |
implode(' ', $query['join']).
|
| 309 |
" WHERE s.uid = %d AND event_type = '%s' AND ((".implode(') OR (', $query['where']).'))';
|
| 310 |
$result = db_query($sql, $query['args']);
|
| 311 |
while ($sub = db_fetch_object($result)) {
|
| 312 |
if (!isset($subscriptions[$uid][$event_type][$oid][$sub->sid])) {
|
| 313 |
$subscriptions[$uid][$event_type][$oid][$sub->sid] = $sub;
|
| 314 |
}
|
| 315 |
$subscriptions[$uid][$event_type][$oid][$sub->sid]->fields[$sub->field] = $sub->value;
|
| 316 |
}
|
| 317 |
}
|
| 318 |
|
| 319 |
return $subscriptions[$uid][$event_type][$oid];
|
| 320 |
}
|
| 321 |
|
| 322 |
/**
|
| 323 |
* Update or create subscription
|
| 324 |
*
|
| 325 |
* This function checks for duplicated subscriptions before saving.
|
| 326 |
* If a similar subscription is found it will be updated.
|
| 327 |
* If no subscription is found and it is new, the sid will be added into the object.
|
| 328 |
*
|
| 329 |
* @param $subscription
|
| 330 |
* Subscription object or array
|
| 331 |
*/
|
| 332 |
function notifications_save_subscription(&$subscription) {
|
| 333 |
global $user;
|
| 334 |
$subscription = (object)$subscription;
|
| 335 |
$subscription->conditions = count($subscription->fields);
|
| 336 |
$account = $subscription->uid ? user_load(array('uid' => $subscription->uid)) : $user;
|
| 337 |
// Default values for fields: send_interval, send_method, etc...
|
| 338 |
foreach (_notifications_subscription_defaults($account) as $field => $value) {
|
| 339 |
if (!isset($subscription->$field)) {
|
| 340 |
$subscription->$field = $value;
|
| 341 |
}
|
| 342 |
}
|
| 343 |
// If the send method is a pull method, do not mark for cron processing
|
| 344 |
// We make the decission here to allow some flexibility in the future
|
| 345 |
// like allowing mixed pull/push for the same sending method in the future
|
| 346 |
if (messaging_method_info('type', $subscriptions->send_method, 0) & MESSAGING_TYPE_PULL) {
|
| 347 |
$subscription->cron = 0;
|
| 348 |
} else {
|
| 349 |
$subscription->cron = 1;
|
| 350 |
}
|
| 351 |
if ($subscription->sid) {
|
| 352 |
$op = 'update';
|
| 353 |
db_query("UPDATE {notifications} SET uid = %d, type = '%s', event_type = '%s', conditions = %d, send_interval = '%d', send_method = '%s', cron = %d, module = '%s', status = %d WHERE sid = %d", $subscription->uid, $subscription->type, $subscription->event_type, $subscription->conditions, $subscription->send_interval, $subscription->send_method, $subscription->cron, $subscription->module, $subscription->status, $subscription->sid);
|
| 354 |
db_query("DELETE FROM {notifications_fields} WHERE sid = %d", $subscription->sid);
|
| 355 |
} elseif ($duplicate = notifications_get_subscriptions(array('uid' => $subscription->uid, 'type' => $subscription->type, 'event_type' => $subscription->event_type, 'module' => $subscription->module), $subscription->fields)) {
|
| 356 |
// We've found duplicates, resolve conflict updating first, deleting the rest
|
| 357 |
$update = array_shift($duplicate);
|
| 358 |
$update->send_interval = $subscription->send_interval;
|
| 359 |
$uddate->send_method = $subscription->send_method;
|
| 360 |
$subscription = $update;
|
| 361 |
drupal_set_message(t('A duplicated subscription has been found and updated'));
|
| 362 |
// If there are more, delete, keep the table clean
|
| 363 |
while (array_shift($duplicate)) {
|
| 364 |
notifications_delete_subscription($duplicate->sid);
|
| 365 |
}
|
| 366 |
return notifications_save_subscription($subscription);
|
| 367 |
} else {
|
| 368 |
$op = 'insert';
|
| 369 |
$subscription->sid = db_next_id('{notifications}_sid');
|
| 370 |
db_query("INSERT INTO {notifications} (sid, uid, type, event_type, conditions, send_interval, send_method, cron, module, status) VALUES(%d, %d, '%s', '%s', %d, %d, '%s', %d, '%s', %d)", $subscription->sid, $subscription->uid, $subscription->type, $subscription->event_type, $subscription->conditions, $subscription->send_interval, $subscription->send_method, $subscription->cron, $subscription->module, $subscription->status);
|
| 371 |
}
|
| 372 |
foreach ($subscription->fields as $name => $value) {
|
| 373 |
db_query("INSERT INTO {notifications_fields} (sid, field, value) VALUES(%d, '%s', '%s')", $subscription->sid, $name, $value);
|
| 374 |
}
|
| 375 |
notifications_module_invoke($op, $subscription);
|
| 376 |
}
|
| 377 |
|
| 378 |
/**
|
| 379 |
* Get an individual subscription.
|
| 380 |
*
|
| 381 |
* @param $subs
|
| 382 |
* Either a subscription object or a subscription id (sid).
|
| 383 |
* @param $refresh
|
| 384 |
* Force cache refresh
|
| 385 |
* @return
|
| 386 |
* Subscriptions object.
|
| 387 |
*/
|
| 388 |
function notifications_load_subscription($subs, $refresh = FALSE) {
|
| 389 |
static $cache = array();
|
| 390 |
if (is_object($subs)) {
|
| 391 |
$sid = $subs->sid;
|
| 392 |
$subscription = $subs;
|
| 393 |
} else {
|
| 394 |
$sid = $subs;
|
| 395 |
}
|
| 396 |
if ($refresh || !array_key_exists($sid, $cache)) {
|
| 397 |
if (!isset($subscription)) {
|
| 398 |
$subscription = db_fetch_object(db_query("SELECT * FROM {notifications} WHERE sid = %d", $sid));
|
| 399 |
}
|
| 400 |
if ($subscription) {
|
| 401 |
$subscription->fields = array();
|
| 402 |
$result = db_query("SELECT * FROM {notifications_fields} WHERE sid = %d", $sid);
|
| 403 |
while ($condition = db_fetch_object($result)) {
|
| 404 |
$subscription->fields[$condition->field] = $condition->value;
|
| 405 |
}
|
| 406 |
}
|
| 407 |
$cache[$sid] = $subscription;
|
| 408 |
}
|
| 409 |
return $cache[$sid];
|
| 410 |
}
|
| 411 |
|
| 412 |
/**
|
| 413 |
* Delete subscription and clean up related data.
|
| 414 |
*
|
| 415 |
* It also removes pending notifications related to that subscription
|
| 416 |
*
|
| 417 |
* @param $sid
|
| 418 |
* Id of subscriptin to delete
|
| 419 |
*/
|
| 420 |
function notifications_delete_subscription($sid) {
|
| 421 |
foreach (array('notifications', 'notifications_fields', 'notifications_queue') as $table) {
|
| 422 |
db_query("DELETE FROM {$table} WHERE sid = %d", $sid);
|
| 423 |
}
|
| 424 |
}
|
| 425 |
|
| 426 |
/**
|
| 427 |
* Delete multiple subscriptions and clean up related data.
|
| 428 |
*
|
| 429 |
* It also removes pending notifications related to that subscription
|
| 430 |
*
|
| 431 |
* @param $conditions
|
| 432 |
* Array of multiple conditions to delete subscriptions
|
| 433 |
*/
|
| 434 |
function notifications_delete_subscriptions($conditions) {
|
| 435 |
foreach ($conditions as $field => $value) {
|
| 436 |
$where[] = "$field = '%s'";
|
| 437 |
}
|
| 438 |
foreach (array('notifications_fields', 'notifications_queue') as $table) {
|
| 439 |
db_query("DELETE FROM {$table} WHERE sid IN (SELECT sid FROM {notifications} WHERE ". implode(' AND ', $where) . ")", $conditions);
|
| 440 |
}
|
| 441 |
db_query("DELETE FROM {notifications} WHERE ". implode(' AND ', $where), $conditions);
|
| 442 |
}
|
| 443 |
|
| 444 |
/**
|
| 445 |
* Get subscriptions that fit a set of conditions.
|
| 446 |
*
|
| 447 |
* @param $params
|
| 448 |
* Array of parameters for the query
|
| 449 |
* @param $conditions
|
| 450 |
* Optional array of condition fields
|
| 451 |
* @param $limit
|
| 452 |
* Whether to limit the result to subscriptions with exatly that condition fields
|
| 453 |
* @param $key
|
| 454 |
* Optional key field to use as the array index. Will default to sid
|
| 455 |
*
|
| 456 |
* @return
|
| 457 |
* Array of subscriptions indexed by uid, module, field, value, author
|
| 458 |
*/
|
| 459 |
function notifications_get_subscriptions($params, $conditions = array(), $limit = TRUE, $key = 'sid') {
|
| 460 |
// Build query
|
| 461 |
$join = $where = array();
|
| 462 |
if ($conditions) {
|
| 463 |
if ($limit) {
|
| 464 |
$params += array('conditions' => count($conditions));
|
| 465 |
}
|
| 466 |
$index = 0;
|
| 467 |
foreach ($conditions as $name => $value) {
|
| 468 |
$alias = "f$index";
|
| 469 |
$join[] = "INNER JOIN {notifications_fields} $alias ON s.sid = $alias.sid ";
|
| 470 |
$params["$alias.field"] = $name;
|
| 471 |
if (!is_null($value)) {
|
| 472 |
$params["$alias.value"] = $value;
|
| 473 |
}
|
| 474 |
$index++;
|
| 475 |
}
|
| 476 |
}
|
| 477 |
foreach ($params as $field => $value) {
|
| 478 |
$name = strstr($field, '.') ? $field : 's.'.$field;
|
| 479 |
$where[] = is_numeric($value) ? $name.' = %d' : "$name = '%s'";
|
| 480 |
}
|
| 481 |
|
| 482 |
$sql = 'SELECT * FROM {notifications} s '. implode(' ', $join) .
|
| 483 |
$sql .= ' WHERE '.implode(' AND ', $where);
|
| 484 |
|
| 485 |
$result = db_query($sql, $params);
|
| 486 |
$subscriptions = array();
|
| 487 |
while ($s = db_fetch_object($result)) {
|
| 488 |
$subscriptions[$s->$key] = notifications_load_subscription($s);
|
| 489 |
}
|
| 490 |
return $subscriptions;
|
| 491 |
}
|
| 492 |
|
| 493 |
/**
|
| 494 |
* Get info about subscription types
|
| 495 |
*
|
| 496 |
* @param $type
|
| 497 |
* String, the subscriptions type OPTIONAL
|
| 498 |
* @param $field
|
| 499 |
* String, a specific field to retrieve info from OPTIONAL
|
| 500 |
*
|
| 501 |
* @return
|
| 502 |
* Information for a given field and type
|
| 503 |
* or information for a given field for all types
|
| 504 |
*/
|
| 505 |
function notifications_subscription_types($type = NULL, $field = NULL) {
|
| 506 |
static $types;
|
| 507 |
if (!isset($types)) {
|
| 508 |
$types = notifications_module_info('subscription types');
|
| 509 |
}
|
| 510 |
|
| 511 |
if ($field && $type) {
|
| 512 |
return isset($types[$type][$field]) ? $types[$type][$field] : NULL;
|
| 513 |
}
|
| 514 |
elseif ($field) {
|
| 515 |
$return = array();
|
| 516 |
foreach ($types as $id => $info) {
|
| 517 |
$return[$id] = $info[$field];
|
| 518 |
}
|
| 519 |
return $return;
|
| 520 |
}
|
| 521 |
elseif ($type) {
|
| 522 |
return isset($types[$type]) ? $types[$type] : array();
|
| 523 |
}
|
| 524 |
else {
|
| 525 |
return $types;
|
| 526 |
}
|
| 527 |
}
|
| 528 |
|
| 529 |
/**
|
| 530 |
* Invokes hook_notifications() with a single parameter or more but not needing
|
| 531 |
* an object to be passed as reference.
|
| 532 |
*/
|
| 533 |
function notifications_module_info($op, $arg0 = NULL, $arg1 = NULL, $arg2 = NULL) {
|
| 534 |
$object = NULL;
|
| 535 |
return notifications_module_invoke($op, $arg0, $arg1, $arg2);
|
| 536 |
}
|
| 537 |
|
| 538 |
/**
|
| 539 |
* Invokes hook_notifications() in every module.
|
| 540 |
*
|
| 541 |
* We cannot use module_invoke() for this, because the arguments need to
|
| 542 |
* be passed by reference.
|
| 543 |
*/
|
| 544 |
function notifications_module_invoke($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
|
| 545 |
$result = array();
|
| 546 |
foreach (module_implements('notifications') as $module) {
|
| 547 |
$function = $module .'_notifications';
|
| 548 |
if ($return = $function($op, $arg0, $arg1, $arg2)) {
|
| 549 |
$result = array_merge($result, $return);
|
| 550 |
}
|
| 551 |
}
|
| 552 |
return $result;
|
| 553 |
}
|
| 554 |
|
| 555 |
/**
|
| 556 |
* Implementation of hook_messaging()
|
| 557 |
*/
|
| 558 |
function notifications_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
|
| 559 |
switch ($op) {
|
| 560 |
case 'message groups':
|
| 561 |
// Generic notifications event
|
| 562 |
$info['notifications-event'] = array(
|
| 563 |
'module' => 'notifications',
|
| 564 |
'name' => t('Notifications event'),
|
| 565 |
);
|
| 566 |
$info['notifications-digest'] = array(
|
| 567 |
'module' => 'notifications',
|
| 568 |
'name' => t('Notifications digest'),
|
| 569 |
);
|
| 570 |
return $info;
|
| 571 |
case 'message keys':
|
| 572 |
$type = $arg1;
|
| 573 |
switch ($type) {
|
| 574 |
case 'notifications-event':
|
| 575 |
// Event notifications
|
| 576 |
return array(
|
| 577 |
'subject' => t('Subject for event notifications'),
|
| 578 |
'header' => t('Body header for event notifications'),
|
| 579 |
'main' => t('Body for event notifications'),
|
| 580 |
'footer' => t('Body footer for event notifications'),
|
| 581 |
);
|
| 582 |
case 'notifications-digest':
|
| 583 |
return array(
|
| 584 |
'subject' => t('Subject for digested notifications'),
|
| 585 |
'header' => t('Header for digested notifications'),
|
| 586 |
'main' => t('Line for digested events'),
|
| 587 |
'footer' => t('Footer for digested notifications'),
|
| 588 |
);
|
| 589 |
}
|
| 590 |
break;
|
| 591 |
case 'messages':
|
| 592 |
$type = $arg1;
|
| 593 |
// Event notifications
|
| 594 |
if ($type == 'notifications-event') {
|
| 595 |
return array(
|
| 596 |
'subject' => t('Event notification for [user] from [site-name]'),
|
| 597 |
'header' => t("Greetings [user],"),
|
| 598 |
'main' => t("A item to which you are subscribed has been updated"),
|
| 599 |
'footer' => array(
|
| 600 |
t('This is an automatic message from [site-name]'),
|
| 601 |
t('To manage your subscriptions, browse to [subscriptions-manage]'),
|
| 602 |
t('You can unsubscribe at [unsubscribe-url]'),
|
| 603 |
),
|
| 604 |
);
|
| 605 |
}
|
| 606 |
// Digested messages
|
| 607 |
if ($type == 'notifications-digest') {
|
| 608 |
return array(
|
| 609 |
'subject' => t('[site-name] subscription update for [user]'),
|
| 610 |
'header' => t("Greetings, [user].\n\nThese are your messages"),
|
| 611 |
'main' => t("A [type] has been updated: [title]\n\n[event_list]"),
|
| 612 |
'footer' => array(
|
| 613 |
t('This is an automatic message from [site-name])'),
|
| 614 |
t('To manage your subscriptions, browse to [subscriptions-manage]'),
|
| 615 |
),
|
| 616 |
);
|
| 617 |
}
|
| 618 |
break;
|
| 619 |
case 'tokens':
|
| 620 |
$type = explode('-', $arg1);
|
| 621 |
$tokens = array();
|
| 622 |
// These are the token groups that will be used for this module's messages
|
| 623 |
if ($type[0] == 'notifications' ) {
|
| 624 |
$tokens = array('global', 'subscription', 'user');
|
| 625 |
if($type[1] == 'event') {
|
| 626 |
$tokens[] = 'event';
|
| 627 |
}
|
| 628 |
}
|
| 629 |
return $tokens;
|
| 630 |
case 'pull':
|
| 631 |
require_once drupal_get_path('module', 'notifications') .'/notifications.cron.inc';
|
| 632 |
return notifications_process_pull($arg1, $arg2, $arg3, $arg4);
|
| 633 |
}
|
| 634 |
}
|
| 635 |
|
| 636 |
/**
|
| 637 |
* Implementation of hook_token_values()
|
| 638 |
*
|
| 639 |
* @ TODO: Work out event tokens
|
| 640 |
*/
|
| 641 |
function notifications_token_values($type, $object = NULL, $options = array()) {
|
| 642 |
switch ($type) {
|
| 643 |
case 'subscription':
|
| 644 |
$values = array();
|
| 645 |
if ($subscription = $object) {
|
| 646 |
$link = notifications_get_link('unsubscribe', array('sid' => $subscription->sid, 'signed' => TRUE, 'destination' => FALSE));
|
| 647 |
$values['unsubscribe-url'] = url($link['href'], $link['query'], NULL, TRUE);
|
| 648 |
}
|
| 649 |
return $values;
|
| 650 |
case 'user':
|
| 651 |
if ($account = $object) {
|
| 652 |
return array(
|
| 653 |
'subscriptions-manage' => $account->uid ? url("user/$account->uid/notifications", NULL, NULL, TRUE) : '',
|
| 654 |
);
|
| 655 |
}
|
| 656 |
}
|
| 657 |
}
|
| 658 |
|
| 659 |
/**
|
| 660 |
* Implementation of hook_token_list(). Documents the individual
|
| 661 |
* tokens handled by the module.
|
| 662 |
*/
|
| 663 |
function notifications_token_list($type = 'all') {
|
| 664 |
$tokens = array();
|
| 665 |
if ($type == 'user' || $type == 'all') {
|
| 666 |
$tokens['user']['subscriptions-manage'] = t('The url for the current user to manage subscriptions.');
|
| 667 |
}
|
| 668 |
if ($type == 'subscription' || $type == 'all') {
|
| 669 |
$tokens['subscription']['unsubscribe-url'] = t('The url for disabling a specific subscription.');
|
| 670 |
}
|
| 671 |
if ($type == 'event' || $type == 'all') {
|
| 672 |
$tokens['event']['event-list'] = t('List of events for message digests');
|
| 673 |
$tokens['event']['event-detail'] = t('Detailed information for event');
|
| 674 |
}
|
| 675 |
return $tokens;
|
| 676 |
}
|
| 677 |
|
| 678 |
/**
|
| 679 |
* Get event types
|
| 680 |
*/
|
| 681 |
function notifications_event_types($type = NULL, $action = NULL) {
|
| 682 |
static $info;
|
| 683 |
if (!$info) {
|
| 684 |
$types = notifications_module_info('event types');
|
| 685 |
foreach ($types as $type_info) {
|
| 686 |
$info[$type_info['type']][$type_info['action']] = $type_info;
|
| 687 |
}
|
| 688 |
|
| 689 |
}
|
| 690 |
if ($action) {
|
| 691 |
return isset($info[$type][$action]) ? $info[$type][$action] : array();
|
| 692 |
} elseif ($type) {
|
| 693 |
return isset($info[$type]) ? $info[$type] : array();
|
| 694 |
} else {
|
| 695 |
return $info;
|
| 696 |
}
|
| 697 |
}
|
| 698 |
|
| 699 |
/**
|
| 700 |
* Implementation of hook_cron()
|
| 701 |
*/
|
| 702 |
function notifications_cron() {
|
| 703 |
include_once drupal_get_path('module', 'notifications') .'/notifications.cron.inc';
|
| 704 |
notifications_process_run();
|
| 705 |
}
|
| 706 |
|
| 707 |
/**
|
| 708 |
* Return url for subscriptions
|
| 709 |
*/
|
| 710 |
function notifications_get_link($type, $params) {
|
| 711 |
global $user;
|
| 712 |
$params += array(
|
| 713 |
'uid' => $user->uid,
|
| 714 |
'confirm' => TRUE,
|
| 715 |
'signed' => FALSE,
|
| 716 |
'destination' => $_GET['q'],
|
| 717 |
);
|
| 718 |
switch ($type) {
|
| 719 |
case 'subscribe':
|
| 720 |
$elements = array(
|
| 721 |
'subscribe',
|
| 722 |
$params['uid'],
|
| 723 |
$params['type'],
|
| 724 |
implode(',', array_keys($params['fields'])),
|
| 725 |
implode(',', $params['fields'])
|
| 726 |
);
|
| 727 |
break;
|
| 728 |
case 'unsubscribe':
|
| 729 |
$elements = array(
|
| 730 |
'unsubscribe',
|
| 731 |
$params['sid']
|
| 732 |
);
|
| 733 |
break;
|
| 734 |
}
|
| 735 |
// Build query string
|
| 736 |
$query = array();
|
| 737 |
if ($params['destination']) {
|
| 738 |
$query[] = 'destination='.$params['destination'];
|
| 739 |
}
|
| 740 |
if ($params['confirm']) {
|
| 741 |
$query[] = 'confirm=1';
|
| 742 |
}
|
| 743 |
if ($params['signed']) {
|
| 744 |
$query[] = 'signature='._notifications_signature($elements);
|
| 745 |
}
|
| 746 |
return array(
|
| 747 |
'href' => 'notifications/'.implode('/', $elements),
|
| 748 |
'query' => implode('&', $query),
|
| 749 |
);
|
| 750 |
}
|
| 751 |
|
| 752 |
/**
|
| 753 |
* Check access to objects
|
| 754 |
*
|
| 755 |
* This will check permissions for subscriptions and events before subscribing
|
| 756 |
* and before getting updates.
|
| 757 |
*
|
| 758 |
* @param $type
|
| 759 |
* Type of object to check for access. Possible values:
|
| 760 |
* - 'event', will check access to event objects
|
| 761 |
* - 'subscription', will check access to subscribed objects
|
| 762 |
*/
|
| 763 |
function notifications_user_allowed($type, $account, $object = NULL) {
|
| 764 |
|
| 765 |
// Invoke notifications hook and check for a FALSE return value
|
| 766 |
|
| 767 |
$permissions = notifications_module_info('access', $type, $account, $object);
|
| 768 |
|
| 769 |
if ($permissions) {
|
| 770 |
return !in_array(FALSE, $permissions);
|
| 771 |
} else {
|
| 772 |
// If no module has anthing to say about access I guess it will be true
|
| 773 |
return TRUE;
|
| 774 |
}
|
| 775 |
}
|
| 776 |
|
| 777 |
/**
|
| 778 |
* Implementation of notifications_hook()
|
| 779 |
*
|
| 780 |
* Check access permissions to subscriptions
|
| 781 |
*/
|
| 782 |
function notifications_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
|
| 783 |
switch ($op) {
|
| 784 |
case 'access':
|
| 785 |
if ($arg0 == 'subscription') {
|
| 786 |
$account = $arg1;
|
| 787 |
$subscription = $arg2;
|
| 788 |
// First we check valid subscription type
|
| 789 |
$access = FALSE;
|
| 790 |
if ($subscription->type && ($info = notifications_subscription_types($subscription->type))) {
|
| 791 |
// To allow mixed subscription types to work we dont have a fixed field list
|
| 792 |
// Then check specific access to this type. Each type must have a permission
|
| 793 |
if (!empty($info['access callback'])) {
|
| 794 |
$access = call_user_func($info['access callback'], $account, $subscription);
|
| 795 |
} elseif (!empty($info['access']) && user_access($info['access'], $account)|| user_access('administer notifications', $account)) {
|
| 796 |
// Check matching fields
|
| 797 |
if (!array_diff($info['fields'], array_keys($subscription->fields))) {
|
| 798 |
$access = TRUE;
|
| 799 |
}
|
| 800 |
}
|
| 801 |
}
|
| 802 |
return array($access);
|
| 803 |
}
|
| 804 |
}
|
| 805 |
}
|
| 806 |
|
| 807 |
/**
|
| 808 |
* List of send intervals
|
| 809 |
*/
|
| 810 |
function _notifications_send_intervals() {
|
| 811 |
return variable_get('notifications_send_intervals',
|
| 812 |
array(
|
| 813 |
-1 => t('Never'),
|
| 814 |
0 => t('As soon as possible'),
|
| 815 |
900 => t('Every 15 minutes'),
|
| 816 |
3600 => t('Hourly'),
|
| 817 |
10800 => t('Every three hours'),
|
| 818 |
86400 => t('Daily'),
|
| 819 |
)
|
| 820 |
);
|
| 821 |
}
|
| 822 |
|
| 823 |
/**
|
| 824 |
* List of send methods
|
| 825 |
*/
|
| 826 |
function _notifications_send_methods($account = NULL) {
|
| 827 |
return variable_get('notifications_send_methods', messaging_method_list($account));
|
| 828 |
}
|
| 829 |
|
| 830 |
/**
|
| 831 |
* Signature for url parameters
|
| 832 |
*/
|
| 833 |
function _notifications_signature($params) {
|
| 834 |
return md5('notifications:'.drupal_get_private_key().':'.implode(':', $params));
|
| 835 |
}
|
| 836 |
|
| 837 |
/**
|
| 838 |
* Default values for subscription
|
| 839 |
*/
|
| 840 |
function _notifications_subscription_defaults($account = NULL) {
|
| 841 |
return array(
|
| 842 |
'send_interval' => notifications_user_setting('send_interval', $account, 0),
|
| 843 |
'send_method' => notifications_user_setting('send_method', $account, ''),
|
| 844 |
'module' => 'notifications',
|
| 845 |
'status' => 1,
|
| 846 |
);
|
| 847 |
}
|
| 848 |
|
| 849 |
/**
|
| 850 |
* Implementation of hook_simpletest().
|
| 851 |
*/
|
| 852 |
function notifications_simpletest() {
|
| 853 |
$dir = drupal_get_path('module', 'notifications'). DIRECTORY_SEPARATOR . 'tests';
|
| 854 |
$tests = file_scan_directory($dir, '\.test');
|
| 855 |
return array_keys($tests);
|
| 856 |
}
|