annotate notify/ad_notify.module @ 0:d8a3998dac8e ad

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