comparison notify/ad_notify.module @ 0:d8a3998dac8e ad

ajout module ad
author pierre
date Fri, 20 Feb 2009 14:04:09 +0000
parents
children 948362c2a207
comparison
equal deleted inserted replaced
-1:000000000000 0:d8a3998dac8e
1 <?php
2 // $Id: ad_notify.module,v 1.1.2.2.2.13.2.6 2009/02/16 23:12:29 jeremy Exp $
3
4 /**
5 * @file
6 * Receive email notifications regarding ads.
7 *
8 * Copyright (c) 2005-2008.
9 * Jeremy Andrews <jeremy@tag1consulting.com>.
10 */
11
12 define('AD_NOTIFY_DISABLED', 0);
13 define('AD_NOTIFY_ENABLED', 1);
14
15 /**
16 * Implementation of hook_help().
17 */
18 function ad_notify_help($path, $arg) {
19 $output = '';
20 switch ($path) {
21 case 'admin/help#ad_notify':
22 $output = '<p>'. t('The ad_notify modules provides email notifications for the ad module.') .'</p>';
23 break;
24 }
25 return $output;
26 }
27
28 /**
29 * Implementation of hook_menu().
30 */
31 function ad_notify_menu() {
32 $items = array();
33
34 $items['node/%node/adowners/%user/notifications'] = array(
35 'title callback' => 'owner_notifications_title',
36 'title arguments' => array('!owner' => 3),
37 'page callback' => 'drupal_get_form',
38 'page arguments' => array('ad_notify_overview_form', 1, 3),
39 'access callback' => 'ad_adaccess',
40 'access arguments' => array(1, 'manage owners'),
41 'type' => MENU_LOCAL_TASK,
42 'weight' => 4,
43 );
44 $items['node/%node/notifications'] = array(
45 'title' => 'My notifications',
46 'page callback' => 'drupal_get_form',
47 'page arguments' => array('ad_notify_overview_form', 1),
48 'access callback' => 'ad_adaccess',
49 'access arguments' => array(1, array('manage owners', 'manage own notifications')),
50 'type' => MENU_LOCAL_TASK,
51 'weight' => 4,
52 );
53 $items['node/%node/adowners/%user/notifications/%ad_notification/delete'] = array(
54 'title' => 'Delete notification',
55 'page callback' => 'ad_notify_confirm_delete_page',
56 'page arguments' => array(1, 3, 5),
57 'access callback' => 'ad_adaccess',
58 'access arguments' => array(1, array('manage owners', 'manage own notifications')),
59 'type' => MENU_CALLBACK,
60 );
61
62 return $items;
63 }
64
65 /**
66 * Menu item title callback - use the user name
67 */
68 function owner_notifications_title($account) {
69 return t('!owner\'s notifications', array('!owner' => $account->name));
70 }
71
72 /**
73 * Implementation of hook_cron().
74 * For performance reasons, all notifications are actually sent via this cron
75 * hook.
76 */
77 function ad_notify_cron() {
78 // Walk through all configured notifications and determine if any need to be
79 // emailed.
80 $result = db_query('SELECT n.notid, o.aid, n.oid, n.event, n.queued, n.delay, n.sent, n.expire, n.address, n.subject, n.body FROM {ad_notify} n INNER JOIN {ad_owners} o ON n.oid = o.oid WHERE n.status = %d', AD_NOTIFY_ENABLED);
81 while ($notification = db_fetch_object($result)) {
82 $send = FALSE;
83 // Handle special case 'regular' notification that is simply a time-based
84 // status email.
85 if ($notification->event == 'regular') {
86 if ((time() - $notification->delay) >= $notification->sent) {
87 $send = TRUE;
88 $count = 1;
89 }
90 }
91 // Handle event based notifications based on information stored in the
92 // ad_statistics table.
93 else {
94 if (($event = trim($notification->event, '-')) != $notification->event) {
95 // Event was prefixed by a -, so time is negative. We can't pull a
96 // future event out of the statistics table, so we let the module that
97 // defined this event tell us whether or not it's happened.
98 $event_count = module_invoke_all('adnotifyapi', $notification->event, $notification);
99 if (isset($event_count[$notification->event])) {
100 $send = TRUE;
101 }
102 }
103
104 else {
105 $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));
106 if ($count) {
107 // See if the notification has been queued long enough to be sent.
108 if (!$notification->delay || ($notification->queued &&
109 (time() > ($notification->queued + $notification->delay)))) {
110 $send = TRUE;
111 }
112 else if (!$notification->queued) {
113 // Queue up the notification to send it at a later time.
114 db_query('UPDATE {ad_notify} SET queued = %d WHERE notid = %d', time(), $notification->notid);
115 }
116 }
117 }
118 }
119
120 if ($send) {
121 ad_notify_send_mail($notification, $count);
122 if ($notification->expire) {
123 // Update the sent timestamp and counter, and auto-expire the
124 // notification so it is not sent again.
125 db_query('UPDATE {ad_notify} SET queued = 0, sent = %d, counter = counter + 1, status = %d WHERE notid = %d', time(), AD_NOTIFY_DISABLED, $notification->notid);
126 }
127 else {
128 // Simply update the sent timestamp and counter.
129 db_query('UPDATE {ad_notify} SET queued = 0, sent = %d, counter = counter + 1 WHERE notid = %d', time(), $notification->notid);
130 }
131 }
132 }
133 }
134
135 /**
136 * Send email notifications using PHP mail() function.
137 */
138 function ad_notify_send_mail($notification, $count = 0) {
139 $uid = db_result(db_query('SELECT uid FROM {ad_owners} WHERE oid = %d', $notification->oid));
140 $node = node_load(array('nid' => $notification->aid));
141 $owner = user_load(array('uid' => $uid));
142 $statistics = ad_statistics($notification->aid);
143 $notifications = module_invoke_all('adnotifyapi', 'register');
144 $variables = array(
145 '%owner_name' => $owner->name,
146 '%owner_mail' => $owner->mail,
147 '%owner_uid' => $owner->uid,
148 '%sitename' => variable_get('site_name', 'drupal'),
149 '%status' => $node->adstatus,
150 '%type' => $node->adtype,
151 '%event' => $notification->event,
152 '%frequency' => t(strtolower($notifications[$notification->event]), array('@when' => format_interval($notification->delay))),
153 '%redirect' => url($node->redirect, array('absolute' => TRUE)),
154 '%aid' => $notification->aid,
155 '%title' => $node->title,
156 '%url' => url("node/$node->nid", array('absolute' => TRUE)),
157 '%siteurl' => url('', array('absolute' => TRUE)),
158 '%comments' => $node->comment_count,
159 '%count' => $count,
160 '%created_small' => format_date($node->created, 'small'),
161 '%created_medium' => format_date($node->created, 'medium'),
162 '%created_large' => format_date($node->created, 'large'),
163 '%activated_small' => $node->activated ? format_date($node->activated, 'small') : t('never'),
164 '%activated_medium' => $node->activated ? format_date($node->activated, 'medium') : t('never'),
165 '%activated_large' => $node->activated ? format_date($node->activated, 'large') : t('never'),
166 '%expired_small' => $node->expired ? format_date($node->expired, 'small') : t('never'),
167 '%expired_medium' => $node->expired ? format_date($node->expired, 'medium') : t('never'),
168 '%expired_large' => $node->expired ? format_date($node->expired, 'large') : t('never'),
169 '%autoactivate_small' => $node->autoactivate ? format_date($node->autoactivate, 'small') : t('never'),
170 '%autoactivate_medium' => $node->autoactivate ? format_date($node->autoactivate, 'medium') : t('never'),
171 '%autoactivate_large' => $node->autoactivate ? format_date($node->autoactivate, 'large') : t('never'),
172 '%autoexpire_small' => $node->autoexpire ? format_date($node->autoexpire, 'small') : t('never'),
173 '%autoexpire_medium' => $node->autoexpire ? format_date($node->autoexpire, 'medium') : t('never'),
174 '%autoexpire_large' => $node->autoexpire ? format_date($node->autoexpire, 'large') : t('never'),
175 '%maxviews' => $node->maxviews,
176 '%maxclicks' => $node->maxclicks,
177 '%global_views' => $statistics['global']['views'],
178 '%global_clicks' => $statistics['global']['clicks'],
179 '%last_year_views' => $statistics['last_year']['views'],
180 '%last_year_clicks' => $statistics['last_year']['clicks'],
181 '%this_year_views' => $statistics['this_year']['views'],
182 '%this_year_clicks' => $statistics['this_year']['clicks'],
183 '%last_month_views' => $statistics['last_month']['views'],
184 '%last_month_clicks' => $statistics['last_month']['clicks'],
185 '%this_month_views' => $statistics['this_month']['views'],
186 '%this_month_clicks' => $statistics['this_month']['clicks'],
187 '%this_week_views' => $statistics['this_week']['views'],
188 '%this_week_clicks' => $statistics['this_week']['clicks'],
189 '%yesterday_views' => $statistics['yesterday']['views'],
190 '%yesterday_clicks' => $statistics['yesterday']['clicks'],
191 '%today_views' => $statistics['today']['views'],
192 '%today_clicks' => $statistics['today']['clicks'],
193 '%last_hour_views' => $statistics['last_hour']['views'],
194 '%last_hour_clicks' => $statistics['last_hour']['clicks'],
195 '%this_hour_views' => $statistics['this_hour']['views'],
196 '%this_hour_clicks' => $statistics['this_hour']['clicks'],
197 );
198 // TODO: Add hook to allow other modules to define variables.
199
200 // TODO: Should the from_address be configurable?
201 drupal_mail(
202 'ad_notify_mail', // mail key
203 $notification->address, // to address
204 strtr($notification->subject, $variables), // subject
205 wordwrap(strtr($notification->body, $variables), 72), // message
206 variable_get('site_mail', ini_get('sendmail_from')) // from address
207 );
208 }
209
210 /**
211 * Implementation of hook_adapi().
212 */
213 function ad_notify_adapi($op, &$node) {
214 $output = NULL;
215 switch ($op) {
216 case 'statistics_increment':
217 break;
218
219 case 'permissions':
220 return array('manage own notifications', 'edit notification email');
221 break;
222 }
223 }
224
225 /**
226 * Implementation of hook_adnotifyapi().
227 */
228 function ad_notify_adnotifyapi($op, $arg1 = NULL, $arg2 = NULL) {
229 switch ($op) {
230 case 'register':
231 return array(
232 'regular' => t('Email every @when as long as the ad is active.'),
233 );
234 break;
235 }
236 }
237
238 /**
239 * Notification overview form.
240 */
241 function ad_notify_overview_form($form_state, $node, $owner = NULL, $notid = 0) {
242 global $user;
243 if (arg(2) == 'notifications') {
244 drupal_set_title('My notifications');
245 }
246 else {
247 drupal_set_title('Notifications');
248 }
249 if (!isset($owner)) {
250 $owner = $user;
251 }
252
253 $oid = db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $node->nid, $owner->uid));
254 if (isset($oid)) {
255 $notifications = module_invoke_all('adnotifyapi', 'register');
256
257 $header = array(
258 array('data' => t('Last sent'), 'field' => 'sent', 'sort' => 'desc'),
259 array('data' => t('Notification'), 'field' => 'event'),
260 array('data' => t('Status'), 'field' => 'status'),
261 array('data' => t('Action'))
262 );
263
264 $sql = "SELECT notid, event, delay, sent, address, status FROM {ad_notify} WHERE oid = %d";
265 $sql .= tablesort_sql($header);
266 $result = pager_query($sql, 25, 0, NULL, $oid);
267
268 $rows = array();
269 while ($notify = db_fetch_object($result)) {
270 $row = array();
271 $row[] = $notify->sent ? t('!time ago', array('!time' => format_interval(time() - $notify->sent))) : t('Never');
272 $row[] = t($notifications[$notify->event], array('@when' => format_interval($notify->delay)));
273 $row[] = $notify->status == AD_NOTIFY_ENABLED ? t('enabled') : t('disabled');
274 $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');
275 $rows[] = $row;
276 }
277 $output = theme('table', $header, $rows);
278 $output .= theme('pager', NULL, 25, 0);
279 }
280
281 $form = array();
282
283 if ($notid) {
284 $notification = ad_notification_load($notid);
285 }
286
287 $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>';
288 $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>';
289 $form['create'] = array(
290 '#type' => 'fieldset',
291 '#description' => $help,
292 '#title' => $notid ? t('Edit notification') : t('Create new notification'),
293 '#collapsible' => TRUE,
294 '#collapsed' => ($rows == array() || $notid) ? FALSE : TRUE,
295 );
296
297 $form['create']['event'] = array(
298 '#type' => 'select',
299 '#title' => t('Event'),
300 '#options' => $notifications,
301 '#description' => t('Select an event for which you would like to receive a notification.'),
302 '#default_value' => $notid ? $notification->event : 1,
303 );
304
305 $form['create']['delay'] = array(
306 '#type' => 'select',
307 '#title' => t('When'),
308 '#options' => drupal_map_assoc(array(0,3600,10800,21600,43200,86400,259200,432000,604800,1209600,1814400,2419200,4838400,9676800,31536000), 'format_interval'),
309 '#description' => t('Select a value to replace @when in the event notification you selected above.'),
310 '#default_value' => $notid ? $notification->delay : 0,
311 );
312
313 $form['create']['expire'] = array(
314 '#type' => 'checkbox',
315 '#title' => t('One-time'),
316 '#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.'),
317 '#default_value' => $notid ? $notification->expire : 0,
318 );
319
320 if (ad_adaccess($node->nid, 'manage owners') && arg(2) == 'adowners' &&
321 $user->uid != arg(3)) {
322 $form['create']['locked'] = array(
323 '#type' => 'checkbox',
324 '#title' => t('Locked'),
325 '#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.'),
326 '#default_value' => $notid ? $notification->locked : 0,
327 );
328 }
329 else {
330 $form['create']['locked'] = array(
331 '#type' => 'hidden',
332 '#value' => $notid ? $notification->locked : 0,
333 );
334 }
335
336 if ($notid) {
337 $form['create']['mail'] = array(
338 '#type' => 'fieldset',
339 '#title' => t('Message'),
340 '#collapsible' => TRUE,
341 '#collapsed' => TRUE,
342 );
343 }
344
345 // TODO: Make it possible for admins to modify email address, and even to
346 // enter multiple email addresses. Wrap this in a special permission, as
347 // it involves trust to configure notifications to unverified addresses.
348 $form['create']['mail']['address-display'] = array(
349 '#type' => 'markup',
350 '#value' => '<b>'. t('Notify address') .':</b><br />'. t('The email will be sent to %address.', array('%address' => $owner->mail)),
351 '#prefix' => '<div class="container-inline">',
352 '#suffix' => '</div>',
353 );
354
355 $form['create']['mail']['address'] = array(
356 '#type' => 'hidden',
357 '#value' => $owner->mail,
358 );
359
360 if ($notid) {
361 $form['create']['mail']['subject'] = array(
362 '#type' => 'textfield',
363 '#title' => t('Subject'),
364 '#required' => TRUE,
365 '#default_value' => $notification->subject,
366 );
367
368 $form['create']['mail']['body'] = array(
369 '#type' => 'textarea',
370 '#title' => t('Body'),
371 '#required' => TRUE,
372 '#default_value' => $notification->body,
373 '#description' => t('Enter the body of your notification email. The following variables can be used in your message and will be automatically replaced before the email is sent:') .'<ul>'
374 .'<li>'. t('%sitename: the name of this website.')
375 .'<li>'. t('%owner_name: the username of the ad owner.')
376 .'<li>'. t('%owner_mail: the email address of the ad owner.')
377 .'<li>'. t('%owner_uid: the user ID of the ad owner.')
378 .'<li>'. t('%event: the type of event that has triggered this notification.')
379 .'<li>'. t('%count: the number of times the event happened.')
380 .'<li>'. t('%frequency: a complete sentence describing the frequency this notification will be sent.')
381 .'<li>'. t('%type: the type of ad.')
382 .'<li>'. t('%status: the status of the ad.')
383 .'<li>'. t('%url: the url of the advertisement.')
384 .'<li>'. t('%siteurl: the url of the website.')
385 .'<li>'. t('%redirect: the redirection url of the advertisement.')
386 .'<li>'. t('%title: the title of the advertisement.')
387 .'<li>'. t('%aid: the ID of the advertisement.')
388 .'<li>'. t('%comments: the number of comments attached to the advertisement.')
389 .'<li>'. t('%created_small, %created_medium, %created_large: various formats of when the advertisement was created.')
390 .'<li>'. t('%activated_small, %activated_medium, %activated_large: various formats of when the advertisement was activated.')
391 .'<li>'. t('%expired_small, %expired_medium, %expired_large: various formats of when the advertisement was expired.')
392 .'<li>'. t('%autoactivate_small, %autoactivate_medium, %autoactivate_large: various formats of when the advertisement was automatically activated.')
393 .'<li>'. t('%autoexpire_small, %autoexpire_medium, %autoexpire_large: various formats of when the advertisement was automatically expired.')
394 .'<li>'. t('%maxviews: the maximum number of times this advertisement is allowed to be viewed.')
395 .'<li>'. t('%maxclicks: the maximum number of times this advertisement is allowed to be clicked.')
396 .'<li>'. t('%global_views, %global_clicks, %last_year_views, %last_year_clicks, %this_year_views, %this_year_clicks, %last_month_views, %last_month_clicks, %this_month_views, %this_month_clicks, %this_week_views, %this_week_clicks, %yesterday_views, %yesterday_clicks, %today_views, %today_clicks, %last_hour_views, %last_hour_clicks, %this_hour_views, %this_hour_clicks: various advertisement statistics')
397 .'</ul>',
398 );
399 }
400
401 $form['create']['oid'] = array(
402 '#type' => 'hidden',
403 '#value' => $oid,
404 );
405
406 $form['create']['aid'] = array(
407 '#type' => 'hidden',
408 '#value' => $node->nid,
409 );
410
411 $form['create']['uid'] = array(
412 '#type' => 'hidden',
413 '#value' => $owner->uid,
414 );
415
416 if ($notid) {
417 $form['create']['notid'] = array(
418 '#type' => 'hidden',
419 '#value' => $notid,
420 );
421 $form['submit'] = array(
422 '#type' => 'submit',
423 '#value' => t('Save'),
424 );
425 }
426 else {
427 $form['create']['submit'] = array(
428 '#type' => 'submit',
429 '#value' => t('Create notification'),
430 );
431 }
432
433 if ($rows != array()) {
434 if (!$notid) {
435 $form['notifications'] = array(
436 '#type' => 'fieldset',
437 '#title' => t('Notifications'),
438 '#collapsible' => TRUE,
439 );
440 $form['notifications']['current'] = array(
441 '#type' => 'markup',
442 '#value' => $output,
443 );
444 }
445 }
446 else {
447 $form['notifications'] = array(
448 '#type' => 'markup',
449 '#value' => '<p>'. t('There are no notifications configured for %owner.', array('%owner' => $owner->name)) .'</p>',
450 );
451 }
452
453 return $form;
454 }
455
456 /**
457 * Validate ad notifications before saving to database.
458 */
459 function ad_notify_overview_form_validate($form, &$form_state) {
460 $redirect = FALSE;
461 if ($form_state['values']['event'] == 'regular' && $form_state['values']['delay'] < 3600) {
462 drupal_set_message(t('You are not allowed to schedule a regular notification more frequently than once an hour.'), 'error');
463 $redirect = TRUE;
464 }
465 else if (!isset($form_state['values']['notid'])) {
466 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']))) {
467 drupal_set_message(t('You have already scheduled that notification.'), 'error');
468 $redirect = TRUE;
469 }
470 }
471 else if ($form_state['values']['locked'] && !ad_adaccess($form_state['values']['aid'], 'manage owners')) {
472 $redirect = TRUE;
473 drupal_set_message(t('This notification is locked, you will need to contact the site administrator to edit this notification for you.'), 'error');
474 }
475
476 if ($redirect) {
477 if (arg(2) == 'adowners' && arg(4) == 'notifications') {
478 drupal_goto('node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications');
479 }
480 else {
481 drupal_goto('node/'. $form_state['values']['aid'] .'/notifications');
482 }
483 }
484 }
485
486 /**
487 * Save notifications to database.
488 */
489 function ad_notify_overview_form_submit($form, &$form_state) {
490 if (isset($form_state['values']['notid'])) {
491 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' 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, $form_state['values']['address'], $form_state['values']['subject'], $form_state['values']['body'], $form_state['values']['notid']);
492 drupal_set_message('Notification updated.');
493 }
494 else {
495 // Retrieve the default mail subject and body.
496 $mail = module_invoke_all('adnotifyapi', 'mail_text', $form_state['values']['event']);
497 if ($mail == array()) {
498 // Default message text.
499 $mail = array(
500 'subject' => t('[%sitename ad] %event notification'),
501 'body' => t("Hello %owner_name,\n\n This is an automatically generated notification about your advertisement \"%title\" that is being displayed on the %sitename 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 %sitename Team\n\n-\n%siteurl"),
502 );
503 }
504 db_query("INSERT INTO {ad_notify} (aid, oid, event, delay, expire, locked, status, address, subject, body) VALUES(%d, %d, '%s', %d, %d, %d, %d, '%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, $form_state['values']['address'], $mail['subject'], $mail['body']);
505 drupal_set_message('Notification created.');
506 }
507 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications';
508 }
509
510 /**
511 * Load a specified notification from the database, return as an object.
512 */
513 function ad_notification_load($notid) {
514 return db_fetch_object(db_query('SELECT * FROM {ad_notify} WHERE notid = %d', $notid));
515 }
516
517 /**
518 * Display confirm form.
519 */
520 function ad_notify_confirm_delete_page($node, $owner, $notification) {
521 return drupal_get_form('ad_notify_confirm_delete', $node, $owner, $notification);
522 }
523
524 /**
525 * Confirm deletion of a specified notification from the database.
526 */
527 function ad_notify_confirm_delete(&$form_state, $node, $owner, $notification) {
528 $form = array();
529
530 $form['oid'] = array(
531 '#type' => 'hidden',
532 '#value' => $notification->oid,
533 );
534
535 $form['aid'] = array(
536 '#type' => 'hidden',
537 '#value' => $node->nid,
538 );
539
540 $form['uid'] = array(
541 '#type' => 'hidden',
542 '#value' => $owner->uid,
543 );
544
545 $form['notid'] = array(
546 '#type' => 'hidden',
547 '#value' => $notification->notid,
548 );
549
550 $form['locked'] = array(
551 '#type' => 'hidden',
552 '#value' => $notification->locked,
553 );
554
555 $form['event'] = array(
556 '#type' => 'fieldset',
557 '#collapsible' => FALSE,
558 );
559 $notifications = module_invoke_all('adnotifyapi', 'register');
560 $form['event']['type'] = array(
561 '#type' => 'markup',
562 '#value' => t($notifications[$notification->event], array('@when' => format_interval($notification->delay))),
563 '#prefix' => '<div class="container-inline">',
564 '#suffix' => '</div>',
565 );
566
567 $form = confirm_form(
568 $form,
569 'Are you sure you want to delete this notification?',
570 'node/'. $node->nid .'/adowners/'. $owner->uid .'/notifications',
571 'This action cannot be undone.',
572 'Delete',
573 'Cancel'
574 );
575 return $form;
576 }
577
578
579 /**
580 * Validate that the selected notification can be deleted.
581 */
582 function ad_notify_confirm_delete_validate($form, &$form_state) {
583 if ($form_state['values']['locked'] && !ad_adaccess($form_state['values']['aid'], 'manage owners')) {
584 drupal_set_message(t('This notification is locked, you will need to contact the site administrator to delete this notification for you.'), 'error');
585 if (arg(2) == 'adowners' && arg(4) == 'notifications') {
586 drupal_goto('node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications');
587 }
588 else {
589 drupal_goto('node/'. $form_state['values']['aid'] .'/notifications');
590 }
591 }
592 }
593
594 /**
595 * Delete a specified notification from the database.
596 */
597 function ad_notify_confirm_delete_submit($form, &$form_state) {
598 db_query('DELETE FROM {ad_notify} WHERE notid = %d', $form_state['values']['notid']);
599 drupal_set_message('Notification deleted.');
600 $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications';
601 }
602
603 /**
604 * Implementation of hook_adowners().
605 */
606 function ad_notify_adowners($op, $arg1 = NULL, $arg2 = NULL) {
607 switch ($op) {
608 case 'overview':
609 return l(t('notifications'), 'node/'. $arg1 .'/adowners/'. $arg2 .'/notifications');
610
611 case 'delete':
612 if ($arg1) {
613 db_query('DELETE FROM {ad_notify} WHERE oid = %d', $arg1);
614 }
615 break;
616 }
617 }