annotate notify/ad_notify.module @ 1:948362c2a207 ad

update advertisement
author pierre
date Thu, 02 Apr 2009 15:28:21 +0000
parents d8a3998dac8e
children e5584a19768b
rev   line source
pierre@0 1 <?php
pierre@1 2 // $Id: ad_notify.module,v 1.1.2.2.2.13.2.6.2.6 2009/03/29 20:17:21 jeremy Exp $
pierre@0 3
pierre@0 4 /**
pierre@0 5 * @file
pierre@1 6 * Automatically generate email notifications.
pierre@0 7 *
pierre@1 8 * Copyright (c) 2005-2009.
pierre@0 9 * Jeremy Andrews <jeremy@tag1consulting.com>.
pierre@0 10 */
pierre@0 11
pierre@0 12 define('AD_NOTIFY_DISABLED', 0);
pierre@0 13 define('AD_NOTIFY_ENABLED', 1);
pierre@0 14
pierre@0 15 /**
pierre@0 16 * Implementation of hook_help().
pierre@0 17 */
pierre@0 18 function ad_notify_help($path, $arg) {
pierre@0 19 $output = '';
pierre@0 20 switch ($path) {
pierre@0 21 case 'admin/help#ad_notify':
pierre@0 22 $output = '<p>'. t('The ad_notify modules provides email notifications for the ad module.') .'</p>';
pierre@0 23 break;
pierre@0 24 }
pierre@0 25 return $output;
pierre@0 26 }
pierre@0 27
pierre@0 28 /**
pierre@0 29 * Implementation of hook_menu().
pierre@0 30 */
pierre@0 31 function ad_notify_menu() {
pierre@0 32 $items = array();
pierre@1 33
pierre@1 34 $items['admin/content/ad/notifications'] = array(
pierre@1 35 'title' => 'Global notifications',
pierre@0 36 'page callback' => 'drupal_get_form',
pierre@1 37 'page arguments' => array('ad_notify_overview_form', '0', '0'),
pierre@1 38 'access arguments' => array('administer advertisements'),
pierre@0 39 'type' => MENU_LOCAL_TASK,
pierre@1 40 'weight' => 6,
pierre@0 41 );
pierre@1 42 $items['admin/content/ad/notifications/%ad_notification/delete'] = array(
pierre@1 43 'title' => 'Delete notification',
pierre@1 44 'page callback' => 'drupal_get_form',
pierre@1 45 'page arguments' => array('ad_notify_confirm_delete', '0', '0', 4),
pierre@1 46 'access arguments' => array('administer advertisements'),
pierre@1 47 'weight' => 2,
pierre@1 48 );
pierre@1 49
pierre@0 50 $items['node/%node/notifications'] = array(
pierre@0 51 'title' => 'My notifications',
pierre@0 52 'page callback' => 'drupal_get_form',
pierre@0 53 'page arguments' => array('ad_notify_overview_form', 1),
pierre@1 54 'access callback' => 'ad_notify_tab_access',
pierre@1 55 'access arguments' => array(1),
pierre@1 56 'type' => MENU_LOCAL_TASK,
pierre@1 57 'weight' => 4,
pierre@1 58 );
pierre@1 59 $items['node/%node/adowners/%user/notifications'] = array(
pierre@1 60 'title callback' => 'owner_notifications_title',
pierre@1 61 'title arguments' => array(3),
pierre@1 62 'page callback' => 'drupal_get_form',
pierre@1 63 'page arguments' => array('ad_notify_overview_form', 1, 3),
pierre@1 64 'access callback' => 'ad_permission',
pierre@1 65 'access arguments' => array(1, 'manage owners'),
pierre@0 66 'type' => MENU_LOCAL_TASK,
pierre@0 67 'weight' => 4,
pierre@0 68 );
pierre@0 69 $items['node/%node/adowners/%user/notifications/%ad_notification/delete'] = array(
pierre@0 70 'title' => 'Delete notification',
pierre@1 71 'page callback' => 'drupal_get_form',
pierre@1 72 'page arguments' => array('ad_notify_confirm_delete', 1, 3, 5),
pierre@1 73 'access callback' => 'ad_permission',
pierre@0 74 'access arguments' => array(1, array('manage owners', 'manage own notifications')),
pierre@0 75 'type' => MENU_CALLBACK,
pierre@0 76 );
pierre@0 77
pierre@0 78 return $items;
pierre@0 79 }
pierre@0 80
pierre@0 81 /**
pierre@1 82 * Menu item title callback - use the user name.
pierre@0 83 */
pierre@0 84 function owner_notifications_title($account) {
pierre@1 85 if (isset($account) && is_object($account)) {
pierre@1 86 return t('!owner\'s notifications', array('!owner' => $account->name));
pierre@1 87 }
pierre@1 88 }
pierre@1 89
pierre@1 90 function ad_notify_tab_access($node) {
pierre@1 91 if (isset($node->adtype)) {
pierre@1 92 return (ad_permission($node->nid, 'manage owners') || ad_permission($node->nid, 'manage own notifications'));
pierre@1 93 }
pierre@0 94 }
pierre@0 95
pierre@0 96 /**
pierre@0 97 * Implementation of hook_cron().
pierre@1 98 * Send time based notifications and those, who has negative delay.
pierre@0 99 */
pierre@0 100 function ad_notify_cron() {
pierre@0 101 // Walk through all configured notifications and determine if any need to be
pierre@1 102 // emailed.
pierre@1 103 $result = db_query('SELECT n.notid, o.aid, o.uid, n.oid, n.event, n.queued, n.delay, n.sent, n.expire, n.address, n.subject, n.body, n.roles FROM {ad_notify} n INNER JOIN {ad_owners} o ON n.oid = o.oid WHERE n.status = %d', AD_NOTIFY_ENABLED);
pierre@0 104 while ($notification = db_fetch_object($result)) {
pierre@0 105 $send = FALSE;
pierre@0 106 // Handle special case 'regular' notification that is simply a time-based
pierre@0 107 // status email.
pierre@0 108 if ($notification->event == 'regular') {
pierre@0 109 if ((time() - $notification->delay) >= $notification->sent) {
pierre@0 110 $send = TRUE;
pierre@0 111 $count = 1;
pierre@0 112 }
pierre@0 113 }
pierre@1 114 // Handle event based notifications based on information stored in the
pierre@0 115 // ad_statistics table.
pierre@0 116 else {
pierre@0 117 if (($event = trim($notification->event, '-')) != $notification->event) {
pierre@0 118 // Event was prefixed by a -, so time is negative. We can't pull a
pierre@0 119 // future event out of the statistics table, so we let the module that
pierre@0 120 // defined this event tell us whether or not it's happened.
pierre@0 121 $event_count = module_invoke_all('adnotifyapi', $notification->event, $notification);
pierre@0 122 if (isset($event_count[$notification->event])) {
pierre@0 123 $send = TRUE;
pierre@0 124 }
pierre@0 125 }
pierre@0 126
pierre@0 127 else {
pierre@0 128 $count = db_result(db_query("SELECT COUNT(aid) AS count FROM {ad_statistics} WHERE aid = %d AND date > %d AND action = '%s'", $notification->aid, date('YmdH', $notification->sent), $notification->event));
pierre@0 129 if ($count) {
pierre@0 130 // See if the notification has been queued long enough to be sent.
pierre@0 131 if (!$notification->delay || ($notification->queued &&
pierre@0 132 (time() > ($notification->queued + $notification->delay)))) {
pierre@0 133 $send = TRUE;
pierre@0 134 }
pierre@0 135 else if (!$notification->queued) {
pierre@0 136 // Queue up the notification to send it at a later time.
pierre@0 137 db_query('UPDATE {ad_notify} SET queued = %d WHERE notid = %d', time(), $notification->notid);
pierre@0 138 }
pierre@0 139 }
pierre@0 140 }
pierre@0 141 }
pierre@0 142
pierre@0 143 if ($send) {
pierre@0 144 ad_notify_send_mail($notification, $count);
pierre@0 145 if ($notification->expire) {
pierre@1 146 // Update the sent timestamp and counter, and auto-expire the
pierre@0 147 // notification so it is not sent again.
pierre@0 148 db_query('UPDATE {ad_notify} SET queued = 0, sent = %d, counter = counter + 1, status = %d WHERE notid = %d', time(), AD_NOTIFY_DISABLED, $notification->notid);
pierre@0 149 }
pierre@0 150 else {
pierre@0 151 // Simply update the sent timestamp and counter.
pierre@0 152 db_query('UPDATE {ad_notify} SET queued = 0, sent = %d, counter = counter + 1 WHERE notid = %d', time(), $notification->notid);
pierre@0 153 }
pierre@0 154 }
pierre@0 155 }
pierre@0 156 }
pierre@0 157
pierre@1 158 function ad_notify_mail($key, &$message, $params) {
pierre@1 159 $language = $message['language'];
pierre@1 160 $message['subject'] = $params['notification']->subject;
pierre@1 161 $message['body'] = $params['notification']->body;
pierre@1 162 }
pierre@1 163
pierre@0 164 /**
pierre@1 165 * Send email notifications.
pierre@0 166 */
pierre@1 167 function ad_notify_send_mail($notification, $count = 0, $params = array(), $language = NULL) {
pierre@1 168 $owner = user_load($notification->uid);
pierre@0 169
pierre@1 170 $send = TRUE;
pierre@1 171 // Send notification only for user who has a permission
pierre@1 172 if (isset($notification->roles) && !empty($notification->roles)) {
pierre@1 173 $send = FALSE;
pierre@1 174 $notification->roles = unserialize($notification->roles);
pierre@1 175 foreach ($notification->roles as $rid) {
pierre@1 176 if (isset($owner->roles[$rid])) {
pierre@1 177 $send = TRUE;
pierre@1 178 break;
pierre@1 179 }
pierre@1 180 }
pierre@1 181 }
pierre@1 182
pierre@1 183 if ($send) {
pierre@1 184 $node = node_load($notification->aid);
pierre@1 185 $node->notification = $notification;
pierre@1 186 $replacements = array(
pierre@1 187 'global' => NULL,
pierre@1 188 'ad' => $node,
pierre@1 189 'ad_owner' => $owner,
pierre@1 190 );
pierre@1 191 $notification->body = token_replace_multiple($notification->body, $replacements, '%', '');
pierre@1 192 $notification->subject = token_replace_multiple($notification->subject, $replacements, '%', '');
pierre@1 193 $params['address'] = $notification->address ? $notification->address : $owner->mail;
pierre@1 194 $params['notification'] = $notification;
pierre@1 195 return drupal_mail('ad_notify', 'notification', $params['address'], $language, $params);
pierre@1 196 }
pierre@0 197 }
pierre@0 198
pierre@0 199 /**
pierre@0 200 * Implementation of hook_adapi().
pierre@0 201 */
pierre@0 202 function ad_notify_adapi($op, &$node) {
pierre@0 203 $output = NULL;
pierre@0 204 switch ($op) {
pierre@0 205 case 'statistics_increment':
pierre@0 206 break;
pierre@0 207
pierre@0 208 case 'permissions':
pierre@1 209 return array('manage own notifications' => TRUE, 'edit notification email' => TRUE);
pierre@0 210 break;
pierre@0 211 }
pierre@0 212 }
pierre@0 213
pierre@0 214 /**
pierre@0 215 * Implementation of hook_adnotifyapi().
pierre@0 216 */
pierre@0 217 function ad_notify_adnotifyapi($op, $arg1 = NULL, $arg2 = NULL) {
pierre@0 218 switch ($op) {
pierre@0 219 case 'register':
pierre@0 220 return array(
pierre@0 221 'regular' => t('Email every @when as long as the ad is active.'),
pierre@0 222 );
pierre@0 223 break;
pierre@0 224 }
pierre@0 225 }
pierre@0 226
pierre@0 227 /**
pierre@1 228 * Implementation of hook_adowners().
pierre@1 229 */
pierre@1 230 function ad_notify_adowners($op, $arg1 = NULL, $arg2 = NULL) {
pierre@1 231 switch ($op) {
pierre@1 232 case 'overview':
pierre@1 233 return l(t('notifications'), 'node/'. $arg1 .'/adowners/'. $arg2 .'/notifications');
pierre@1 234
pierre@1 235 case 'add':
pierre@1 236 if ($arg2['aid'] && $arg2['oid']) {
pierre@1 237 $owner = user_load($arg2['uid']);
pierre@1 238
pierre@1 239 // Clone all template notification for new ad owner.
pierre@1 240 $result = db_query('SELECT * FROM {ad_notify} WHERE aid = 0 AND oid = 0');
pierre@1 241 while ($template = db_fetch_object($result)) {
pierre@1 242 db_query("INSERT INTO {ad_notify} (aid, oid, event, delay, expire, locked, status, address, subject, body, roles, template) VALUES(%d, %d, '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s', %d)", $arg2['aid'], $arg2['oid'], $template->event, $template->delay, $template->expire, $template->locked, AD_NOTIFY_ENABLED, $owner->mail, $template->subject, $template->body, $template->roles, $template->notid);
pierre@1 243 }
pierre@1 244 }
pierre@1 245 break;
pierre@1 246
pierre@1 247 case 'remove':
pierre@1 248 if ($arg1) {
pierre@1 249 $result = db_query('DELETE FROM {ad_notify} WHERE oid = %d', $arg1);
pierre@1 250 }
pierre@1 251 break;
pierre@1 252 }
pierre@1 253 }
pierre@1 254
pierre@1 255 /**
pierre@0 256 * Notification overview form.
pierre@0 257 */
pierre@1 258 function ad_notify_overview_form($form_state, $node = NULL, $owner = NULL, $notid = 0) {
pierre@0 259 global $user;
pierre@1 260
pierre@1 261 if (isset($owner) && !is_object($owner) && $owner == 0) {
pierre@1 262 $owner = new stdClass();
pierre@1 263 $owner->uid = 0;
pierre@0 264 }
pierre@1 265 else if (empty($owner)) {
pierre@0 266 $owner = $user;
pierre@0 267 }
pierre@1 268 if (!isset($node) || !is_object($node)) {
pierre@1 269 $node = new stdClass();
pierre@1 270 $node->nid = 0;
pierre@1 271 }
pierre@0 272
pierre@1 273 $oid = (int)db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $node->nid, $owner->uid));
pierre@1 274 $notifications = module_invoke_all('adnotifyapi', 'register');
pierre@0 275
pierre@1 276 $header = array(
pierre@1 277 array('data' => t('Last sent'), 'field' => 'sent', 'sort' => 'desc'),
pierre@1 278 array('data' => t('Notification'), 'field' => 'event'),
pierre@1 279 array('data' => t('Status'), 'field' => 'status'),
pierre@1 280 array('data' => t('Action'))
pierre@1 281 );
pierre@0 282
pierre@1 283 $sql = "SELECT notid, event, delay, sent, address, status, roles FROM {ad_notify} WHERE oid = %d";
pierre@1 284 $sql .= tablesort_sql($header, 'oid DESC, ');
pierre@1 285 $result = pager_query($sql, 25, 0, NULL, $oid);
pierre@0 286
pierre@1 287 $rows = array();
pierre@1 288 while ($notify = db_fetch_object($result)) {
pierre@1 289 $list_notification = TRUE;
pierre@1 290 // Check if user has permission to see the notification.
pierre@1 291 if ($owner->uid && $notify->roles) {
pierre@1 292 $list_notification = FALSE;
pierre@1 293 $notify->roles = unserialize($notify->roles);
pierre@1 294 if (isset($notify->roles) && is_array($notify->roles)) {
pierre@1 295 foreach ($notify->roles as $rid => $require) {
pierre@1 296 if ($require && isset($owner->roles[$rid])) {
pierre@1 297 $list_notification = TRUE;
pierre@1 298 break;
pierre@1 299 }
pierre@1 300 }
pierre@1 301 }
pierre@1 302 }
pierre@1 303
pierre@1 304 if ($list_notification) {
pierre@0 305 $row = array();
pierre@0 306 $row[] = $notify->sent ? t('!time ago', array('!time' => format_interval(time() - $notify->sent))) : t('Never');
pierre@0 307 $row[] = t($notifications[$notify->event], array('@when' => format_interval($notify->delay)));
pierre@0 308 $row[] = $notify->status == AD_NOTIFY_ENABLED ? t('enabled') : t('disabled');
pierre@1 309 if ($node->nid) {
pierre@1 310 $row[] = l(t('edit'), 'node/'. $node->nid .'/adowners/'. $owner->uid .'/notifications/' .$notify->notid. '/edit') .' '. l(t('delete'), 'node/'. $node->nid .'/adowners/'. $owner->uid .'/notifications/'. $notify->notid .'/delete');
pierre@1 311 }
pierre@1 312 else if (user_access('administer advertisements')) {
pierre@1 313 $row[] = l(t('edit'), 'admin/content/ad/notifications/' .$notify->notid. '/edit') .' '. l(t('delete'), 'admin/content/ad/notifications/'. $notify->notid .'/delete');
pierre@1 314 }
pierre@1 315 else {
pierre@1 316 $row[] = 'N/A';
pierre@1 317 }
pierre@0 318 $rows[] = $row;
pierre@0 319 }
pierre@0 320 }
pierre@1 321 $output = theme('table', $header, $rows);
pierre@1 322 $output .= theme('pager', NULL, 25, 0);
pierre@0 323
pierre@0 324 $form = array();
pierre@0 325
pierre@0 326 if ($notid) {
pierre@0 327 $notification = ad_notification_load($notid);
pierre@0 328 }
pierre@0 329
pierre@1 330 if (!isset($owner)) {
pierre@1 331 $help = '<p>'. t('You can configure one ore more notifications for all advertisements using the drop down menus below. Ad owners have the ability to manually disable individual global notifications.');
pierre@1 332 }
pierre@1 333 else {
pierre@1 334 $help = '<p>'. t('You can configure one or more notifications for your advertisement using the drop down menus below. For example, to receive a weekly notification with information about how often your advertisement was viewed and clicked, select the <em>email every @when as long as the ad is active</em> event, and <em>1 week</em> for when. Or, to receive a reminder that your advertisement will expire in 24 hours select the <em>email @when before the advertisement will expire</em>, and <em>1 day</em> for when.') .'</p>';
pierre@1 335 $help .= '<p>'. t('If you schedule a delay between an event and when you are notified and the event happens multiple times, only one notification will be sent. For example, if you create a notification for <em>email 1 day after the advertisement is clicked</em> and the ad is clicked 42 more times in the next 24 hours, you will only receive one email 24 hours after your ad was first clicked that notes that your ad was clicked a total of 43 times in the past 24 hours.') .'</p>';
pierre@1 336 }
pierre@0 337 $form['create'] = array(
pierre@0 338 '#type' => 'fieldset',
pierre@0 339 '#description' => $help,
pierre@0 340 '#title' => $notid ? t('Edit notification') : t('Create new notification'),
pierre@0 341 '#collapsible' => TRUE,
pierre@0 342 '#collapsed' => ($rows == array() || $notid) ? FALSE : TRUE,
pierre@0 343 );
pierre@0 344
pierre@0 345 $form['create']['event'] = array(
pierre@0 346 '#type' => 'select',
pierre@0 347 '#title' => t('Event'),
pierre@0 348 '#options' => $notifications,
pierre@0 349 '#description' => t('Select an event for which you would like to receive a notification.'),
pierre@0 350 '#default_value' => $notid ? $notification->event : 1,
pierre@0 351 );
pierre@0 352
pierre@0 353 $form['create']['delay'] = array(
pierre@0 354 '#type' => 'select',
pierre@0 355 '#title' => t('When'),
pierre@0 356 '#options' => drupal_map_assoc(array(0,3600,10800,21600,43200,86400,259200,432000,604800,1209600,1814400,2419200,4838400,9676800,31536000), 'format_interval'),
pierre@0 357 '#description' => t('Select a value to replace @when in the event notification you selected above.'),
pierre@0 358 '#default_value' => $notid ? $notification->delay : 0,
pierre@0 359 );
pierre@0 360
pierre@0 361 $form['create']['expire'] = array(
pierre@0 362 '#type' => 'checkbox',
pierre@0 363 '#title' => t('One-time'),
pierre@0 364 '#description' => t('Check this box if this notification email should only be sent one time. If not checked, an email will be sent each time the event happens. If checked, an email will only be sent the first time the event happens, then the notification will be automatically disabled.'),
pierre@0 365 '#default_value' => $notid ? $notification->expire : 0,
pierre@0 366 );
pierre@0 367
pierre@1 368 if (ad_permission($node->nid, 'manage owners') && arg(2) == 'adowners' && $user->uid != arg(3)) {
pierre@0 369 $form['create']['locked'] = array(
pierre@0 370 '#type' => 'checkbox',
pierre@0 371 '#title' => t('Locked'),
pierre@0 372 '#description' => t('Check this box if you are setting up a notification for someone else, and you don\'t want them to be able to disable the notification. Only users with the <em>manage owners</em> permission for this ad can edit or delete a locked notification.'),
pierre@0 373 '#default_value' => $notid ? $notification->locked : 0,
pierre@0 374 );
pierre@0 375 }
pierre@0 376 else {
pierre@0 377 $form['create']['locked'] = array(
pierre@0 378 '#type' => 'hidden',
pierre@0 379 '#value' => $notid ? $notification->locked : 0,
pierre@0 380 );
pierre@0 381 }
pierre@0 382
pierre@0 383 if ($notid) {
pierre@0 384 $form['create']['mail'] = array(
pierre@0 385 '#type' => 'fieldset',
pierre@0 386 '#title' => t('Message'),
pierre@0 387 '#collapsible' => TRUE,
pierre@0 388 '#collapsed' => TRUE,
pierre@0 389 );
pierre@0 390 }
pierre@0 391
pierre@1 392 if (isset($owner->name)) {
pierre@1 393 // TODO: Make it possible for admins to modify email address, and even to
pierre@1 394 // enter multiple email addresses. Wrap this in a special permission, as
pierre@1 395 // it involves trust to configure notifications to unverified addresses.
pierre@1 396 $form['create']['mail']['address-display'] = array(
pierre@1 397 '#type' => 'markup',
pierre@1 398 '#value' => '<b>'. t('Notify address') .':</b><br />'. t('The email will be sent to %address.', array('%address' => is_object($owner) ? $owner->mail : 0)),
pierre@1 399 '#prefix' => '<div class="container-inline">',
pierre@1 400 '#suffix' => '</div>',
pierre@1 401 );
pierre@0 402
pierre@1 403 $form['create']['mail']['address'] = array(
pierre@1 404 '#type' => 'hidden',
pierre@1 405 '#value' => is_object($owner) ? $owner->mail : '',
pierre@1 406 );
pierre@1 407 }
pierre@1 408 else {
pierre@1 409 $roles = user_roles(TRUE);
pierre@1 410 $form['create']['roles'] = array(
pierre@1 411 '#type' => 'checkboxes',
pierre@1 412 '#title' => t('Notify roles'),
pierre@1 413 '#description' => t('Select one or more roles to only send this notification to specific roles. Do not select a role to send notification to all roles.'),
pierre@1 414 '#options' => $roles,
pierre@1 415 );
pierre@1 416 }
pierre@0 417
pierre@0 418 if ($notid) {
pierre@0 419 $form['create']['mail']['subject'] = array(
pierre@0 420 '#type' => 'textfield',
pierre@0 421 '#title' => t('Subject'),
pierre@0 422 '#required' => TRUE,
pierre@0 423 '#default_value' => $notification->subject,
pierre@0 424 );
pierre@0 425
pierre@0 426 $form['create']['mail']['body'] = array(
pierre@0 427 '#type' => 'textarea',
pierre@0 428 '#title' => t('Body'),
pierre@0 429 '#required' => TRUE,
pierre@0 430 '#default_value' => $notification->body,
pierre@0 431 );
pierre@1 432 $form['create']['mail']['tokens'] = array(
pierre@1 433 '#type' => 'fieldset',
pierre@1 434 '#title' => t('Replacement patterns'),
pierre@1 435 '#collapsible' => TRUE,
pierre@1 436 '#collapsed' => TRUE,
pierre@1 437 );
pierre@1 438 $form['create']['mail']['tokens']['list'] = array(
pierre@1 439 '#value' => theme('token_help', 'ad', '%', ''),
pierre@1 440 );
pierre@1 441 if ($notification->roles) {
pierre@1 442 $form['create']['roles']['#default_value'] = $notification->roles;
pierre@1 443 }
pierre@0 444 }
pierre@0 445
pierre@0 446 $form['create']['oid'] = array(
pierre@0 447 '#type' => 'hidden',
pierre@0 448 '#value' => $oid,
pierre@0 449 );
pierre@0 450
pierre@0 451 $form['create']['aid'] = array(
pierre@0 452 '#type' => 'hidden',
pierre@0 453 '#value' => $node->nid,
pierre@0 454 );
pierre@0 455
pierre@0 456 $form['create']['uid'] = array(
pierre@0 457 '#type' => 'hidden',
pierre@0 458 '#value' => $owner->uid,
pierre@0 459 );
pierre@0 460
pierre@0 461 if ($notid) {
pierre@0 462 $form['create']['notid'] = array(
pierre@0 463 '#type' => 'hidden',
pierre@0 464 '#value' => $notid,
pierre@0 465 );
pierre@0 466 $form['submit'] = array(
pierre@0 467 '#type' => 'submit',
pierre@0 468 '#value' => t('Save'),
pierre@0 469 );
pierre@0 470 }
pierre@0 471 else {
pierre@0 472 $form['create']['submit'] = array(
pierre@0 473 '#type' => 'submit',
pierre@0 474 '#value' => t('Create notification'),
pierre@0 475 );
pierre@0 476 }
pierre@0 477
pierre@0 478 if ($rows != array()) {
pierre@0 479 if (!$notid) {
pierre@0 480 $form['notifications'] = array(
pierre@0 481 '#type' => 'fieldset',
pierre@0 482 '#title' => t('Notifications'),
pierre@0 483 '#collapsible' => TRUE,
pierre@0 484 );
pierre@0 485 $form['notifications']['current'] = array(
pierre@0 486 '#type' => 'markup',
pierre@0 487 '#value' => $output,
pierre@0 488 );
pierre@0 489 }
pierre@0 490 }
pierre@0 491 else {
pierre@0 492 $form['notifications'] = array(
pierre@0 493 '#type' => 'markup',
pierre@1 494 '#value' => '<p>'. t('There are no notifications configured yet.') .'</p>',
pierre@0 495 );
pierre@0 496 }
pierre@0 497
pierre@1 498 if ($node->nid && isset($owner) && is_object($owner) && isset($owner->name) && $user->name == $owner->name) {
pierre@1 499 drupal_set_title('My notifications');
pierre@1 500 }
pierre@1 501 else {
pierre@1 502 drupal_set_title('Notifications');
pierre@1 503 }
pierre@1 504
pierre@0 505 return $form;
pierre@0 506 }
pierre@0 507
pierre@0 508 /**
pierre@0 509 * Validate ad notifications before saving to database.
pierre@0 510 */
pierre@0 511 function ad_notify_overview_form_validate($form, &$form_state) {
pierre@0 512 if ($form_state['values']['event'] == 'regular' && $form_state['values']['delay'] < 3600) {
pierre@1 513 form_set_error('form', t('You are not allowed to schedule a regular notification more frequently than once an hour.'));
pierre@0 514 }
pierre@0 515 else if (!isset($form_state['values']['notid'])) {
pierre@0 516 if (db_result(db_query("SELECT notid FROM {ad_notify} WHERE oid = %d AND event = '%s' AND delay = %d", $form_state['values']['oid'], $form_state['values']['event'], $form_state['values']['delay']))) {
pierre@1 517 form_set_error('form', t('You have already scheduled that notification.'));
pierre@0 518 }
pierre@0 519 }
pierre@1 520 else if ($form_state['values']['locked'] && !ad_permission($form_state['values']['aid'], 'manage owners')) {
pierre@1 521 form_set_error('form', t('This notification is locked, you will need to contact the site administrator to edit this notification for you.'));
pierre@0 522 }
pierre@0 523 }
pierre@0 524
pierre@0 525 /**
pierre@0 526 * Save notifications to database.
pierre@0 527 */
pierre@0 528 function ad_notify_overview_form_submit($form, &$form_state) {
pierre@1 529 global $user;
pierre@1 530
pierre@1 531 // Clean up roles value before saving to DB
pierre@1 532 if (isset($form_state['values']['roles'])) {
pierre@1 533 foreach ($form_state['values']['roles'] as $rid => $require) {
pierre@1 534 if (!$require) {
pierre@1 535 unset($form_state['values']['roles'][$rid]);
pierre@1 536 }
pierre@1 537 }
pierre@1 538 if (empty($form_state['values']['roles'])) {
pierre@1 539 $form_state['values']['roles'] = NULL;
pierre@1 540 }
pierre@1 541 }
pierre@1 542
pierre@0 543 if (isset($form_state['values']['notid'])) {
pierre@1 544 db_query("UPDATE {ad_notify} SET aid = %d, oid = %d, event = '%s', delay = %d, expire = %d, locked = %d, status = %d, address = '%s', subject = '%s', body = '%s', roles = '%s' WHERE notid = %d", $form_state['values']['aid'], $form_state['values']['oid'], $form_state['values']['event'], $form_state['values']['delay'], $form_state['values']['expire'], $form_state['values']['locked'], AD_NOTIFY_ENABLED, isset($form_state['values']['address']) ? $form_state['values']['address'] : '', $form_state['values']['subject'], $form_state['values']['body'], isset($form_state['values']['roles']) ? serialize($form_state['values']['roles']) : '', $form_state['values']['notid']);
pierre@0 545 drupal_set_message('Notification updated.');
pierre@0 546 }
pierre@0 547 else {
pierre@0 548 // Retrieve the default mail subject and body.
pierre@0 549 $mail = module_invoke_all('adnotifyapi', 'mail_text', $form_state['values']['event']);
pierre@0 550 if ($mail == array()) {
pierre@0 551 // Default message text.
pierre@0 552 $mail = array(
pierre@1 553 'subject' => t('[%site-name ad] %event notification'),
pierre@1 554 'body' => t("Hello %owner_name,\n\n This is an automatically generated notification about your advertisement \"%title\" that is being displayed on the %site-name website.\n\n Your advertisement has been viewed %today_views times and clicked %today_clicks times today. It was viewed %yesterday_views times and clicked %yesterday_clicks times yesterday. It has been viewed %global_views times and clicked %global_clicks times since it was activated on %activated_large.\n\n You will receive this %frequency You can view additional statistics about this advertisement or update this notification at the following url:\n %url\n\nRegards,\n The %site-name Team\n\n-\n%site-url"),
pierre@0 555 );
pierre@0 556 }
pierre@1 557 db_query("INSERT INTO {ad_notify} (aid, oid, event, delay, expire, locked, status, address, subject, body, roles) VALUES(%d, %d, '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s')", $form_state['values']['aid'], $form_state['values']['oid'], $form_state['values']['event'], $form_state['values']['delay'], $form_state['values']['expire'], $form_state['values']['locked'], AD_NOTIFY_ENABLED, isset($form_state['values']['address']) ? $form_state['values']['address'] : '', $mail['subject'], $mail['body'], isset($form_state['values']['roles']) ? serialize($form_state['values']['roles']) : '');
pierre@1 558
pierre@1 559 // Clone new template notification for all ad owners.
pierre@1 560 if (!$form_state['values']['oid']) {
pierre@1 561 $template_id = db_last_insert_id('ad_notify', 'notid');
pierre@1 562
pierre@1 563 $result = db_query('SELECT ao.aid, ao.oid, u.mail FROM {ad_owners} ao LEFT JOIN {users} u ON ao.uid = u.uid');
pierre@1 564 while ($owner = db_fetch_object($result)) {
pierre@1 565 // This INSERT can throw "duplicate key" errors in some situations, so just running it silently
pierre@1 566 @db_query("INSERT INTO {ad_notify} (aid, oid, event, delay, expire, locked, status, address, subject, body, roles, template) VALUES(%d, %d, '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s', %d)", $owner->aid, $owner->oid, $form_state['values']['event'], $form_state['values']['delay'], $form_state['values']['expire'], $form_state['values']['locked'], AD_NOTIFY_ENABLED, $owner->mail, $mail['subject'], $mail['body'], isset($form_state['values']['roles']) ? serialize($form_state['values']['roles']) : '', $template_id);
pierre@1 567 }
pierre@1 568 }
pierre@0 569 drupal_set_message('Notification created.');
pierre@0 570 }
pierre@1 571
pierre@1 572 if ($form_state['values']['aid'] && $form_state['values']['uid'] && $user->uid != $form_state['values']['uid']) {
pierre@1 573 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications';
pierre@1 574 }
pierre@1 575 else if ($form_state['values']['aid']) {
pierre@1 576 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/notifications';
pierre@1 577 }
pierre@1 578 else {
pierre@1 579 $form_state['redirect'] = 'admin/content/ad/notifications';
pierre@1 580 }
pierre@0 581 }
pierre@0 582
pierre@0 583 /**
pierre@0 584 * Load a specified notification from the database, return as an object.
pierre@0 585 */
pierre@0 586 function ad_notification_load($notid) {
pierre@1 587 $notification = db_fetch_object(db_query('SELECT * FROM {ad_notify} WHERE notid = %d', $notid));
pierre@1 588 $notification->roles = unserialize($notification->roles);
pierre@1 589 return $notification;
pierre@0 590 }
pierre@0 591
pierre@0 592 /**
pierre@0 593 * Confirm deletion of a specified notification from the database.
pierre@0 594 */
pierre@0 595 function ad_notify_confirm_delete(&$form_state, $node, $owner, $notification) {
pierre@1 596 global $user;
pierre@0 597 $form = array();
pierre@0 598
pierre@0 599 $form['oid'] = array(
pierre@0 600 '#type' => 'hidden',
pierre@0 601 '#value' => $notification->oid,
pierre@0 602 );
pierre@0 603
pierre@0 604 $form['aid'] = array(
pierre@0 605 '#type' => 'hidden',
pierre@1 606 '#value' => isset($node->nid) ? $node->nid : 0,
pierre@0 607 );
pierre@0 608
pierre@0 609 $form['uid'] = array(
pierre@0 610 '#type' => 'hidden',
pierre@1 611 '#value' => is_object($owner) ? $owner->uid : 0,
pierre@0 612 );
pierre@0 613
pierre@0 614 $form['notid'] = array(
pierre@0 615 '#type' => 'hidden',
pierre@0 616 '#value' => $notification->notid,
pierre@0 617 );
pierre@0 618
pierre@0 619 $form['locked'] = array(
pierre@0 620 '#type' => 'hidden',
pierre@0 621 '#value' => $notification->locked,
pierre@0 622 );
pierre@0 623
pierre@0 624 $form['event'] = array(
pierre@0 625 '#type' => 'fieldset',
pierre@0 626 '#collapsible' => FALSE,
pierre@0 627 );
pierre@0 628 $notifications = module_invoke_all('adnotifyapi', 'register');
pierre@0 629 $form['event']['type'] = array(
pierre@0 630 '#type' => 'markup',
pierre@0 631 '#value' => t($notifications[$notification->event], array('@when' => format_interval($notification->delay))),
pierre@0 632 '#prefix' => '<div class="container-inline">',
pierre@0 633 '#suffix' => '</div>',
pierre@0 634 );
pierre@0 635
pierre@1 636 if (!$notification->oid) {
pierre@1 637 $linked = db_result(db_query('SELECT COUNT(*) FROM {ad_notify} WHERE template = %d', $notification->notid));
pierre@1 638 if ($linked) {
pierre@1 639 $form['remove_linked'] = array(
pierre@1 640 '#type' => 'radios',
pierre@1 641 '#options' => array(
pierre@1 642 1 => t('Completely remove this notification.'),
pierre@1 643 0 => t('Remove from further usage, but leave existing notifications.'),
pierre@1 644 ),
pierre@1 645 '#default_value' => 1,
pierre@1 646 );
pierre@1 647 }
pierre@1 648 }
pierre@1 649
pierre@1 650 if (isset($node->nid) && $owner->uid && $user->uid != $owner->uid) {
pierre@1 651 $path = 'node/'. $node->nid .'/adowners/'. $owner->uid .'/notifications';
pierre@1 652 }
pierre@1 653 else if (isset($node->nid)) {
pierre@1 654 $path = 'node/'. $node->nid .'/notifications';
pierre@1 655 }
pierre@1 656 else {
pierre@1 657 $path = 'admin/content/ad/notifications';
pierre@1 658 }
pierre@1 659
pierre@0 660 $form = confirm_form(
pierre@0 661 $form,
pierre@0 662 'Are you sure you want to delete this notification?',
pierre@1 663 $path,
pierre@0 664 'This action cannot be undone.',
pierre@0 665 'Delete',
pierre@0 666 'Cancel'
pierre@0 667 );
pierre@0 668 return $form;
pierre@0 669 }
pierre@0 670
pierre@0 671
pierre@0 672 /**
pierre@0 673 * Validate that the selected notification can be deleted.
pierre@0 674 */
pierre@0 675 function ad_notify_confirm_delete_validate($form, &$form_state) {
pierre@1 676 if ($form_state['values']['locked'] && !ad_permission($form_state['values']['aid'], 'manage owners')) {
pierre@1 677 form_set_error('form', t('This notification is locked, you will need to contact the site administrator to delete this notification for you.'));
pierre@0 678 }
pierre@0 679 }
pierre@0 680
pierre@0 681 /**
pierre@0 682 * Delete a specified notification from the database.
pierre@0 683 */
pierre@0 684 function ad_notify_confirm_delete_submit($form, &$form_state) {
pierre@1 685 global $user;
pierre@0 686 db_query('DELETE FROM {ad_notify} WHERE notid = %d', $form_state['values']['notid']);
pierre@0 687
pierre@1 688 if (isset($form_state['values']['remove_linked']) && $form_state['values']['remove_linked']) {
pierre@1 689 db_query('DELETE FROM {ad_notify} WHERE template = %d', $form_state['values']['notid']);
pierre@1 690 drupal_set_message('Template and all derived notifications were deleted.');
pierre@1 691 }
pierre@1 692 else {
pierre@1 693 drupal_set_message('Notification deleted.');
pierre@1 694 }
pierre@0 695
pierre@1 696 if ($form_state['values']['aid'] && $form_state['values']['uid'] && $user->uid != $form_state['values']['uid']) {
pierre@1 697 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications';
pierre@1 698 }
pierre@1 699 else if ($form_state['values']['aid']) {
pierre@1 700 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/notifications';
pierre@1 701 }
pierre@1 702 else {
pierre@1 703 $form_state['redirect'] = 'admin/content/ad/notifications';
pierre@0 704 }
pierre@0 705 }