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