Mercurial > defr > drupal > ad
view notify/ad_notify.module @ 2:e5584a19768b ad
maj module ad
author | sly |
---|---|
date | Wed, 15 Apr 2009 07:58:32 +0000 |
parents | 948362c2a207 |
children |
line wrap: on
line source
<?php // $Id: ad_notify.module,v 1.1.2.2.2.13.2.6.2.7 2009/04/02 14:11:50 jeremy Exp $ /** * @file * Automatically generate email notifications. * * Copyright (c) 2005-2009. * Jeremy Andrews <jeremy@tag1consulting.com>. */ define('AD_NOTIFY_DISABLED', 0); define('AD_NOTIFY_ENABLED', 1); /** * Implementation of hook_help(). */ function ad_notify_help($path, $arg) { $output = ''; switch ($path) { case 'admin/help#ad_notify': $output = '<p>'. t('The ad_notify modules provides email notifications for the ad module.') .'</p>'; break; } return $output; } /** * Implementation of hook_menu(). */ function ad_notify_menu() { $items = array(); $items['admin/content/ad/notifications'] = array( 'title' => 'Global notifications', 'page callback' => 'drupal_get_form', 'page arguments' => array('ad_notify_overview_form', '0', '0'), 'access arguments' => array('administer advertisements'), 'type' => MENU_LOCAL_TASK, 'weight' => 6, ); $items['admin/content/ad/notifications/%ad_notification/delete'] = array( 'title' => 'Delete notification', 'page callback' => 'drupal_get_form', 'page arguments' => array('ad_notify_confirm_delete', '0', '0', 4), 'access arguments' => array('administer advertisements'), 'weight' => 2, ); $items['node/%node/notifications'] = array( 'title' => 'My notifications', 'page callback' => 'drupal_get_form', 'page arguments' => array('ad_notify_overview_form', 1), 'access callback' => 'ad_notify_tab_access', 'access arguments' => array(1), 'type' => MENU_LOCAL_TASK, 'weight' => 4, ); $items['node/%node/adowners/%user/notifications'] = array( 'title callback' => 'owner_notifications_title', 'title arguments' => array(3), 'page callback' => 'drupal_get_form', 'page arguments' => array('ad_notify_overview_form', 1, 3), 'access callback' => 'ad_permission', 'access arguments' => array(1, 'manage owners'), 'type' => MENU_LOCAL_TASK, 'weight' => 4, ); $items['node/%node/adowners/%user/notifications/%ad_notification/delete'] = array( 'title' => 'Delete notification', 'page callback' => 'drupal_get_form', 'page arguments' => array('ad_notify_confirm_delete', 1, 3, 5), 'access callback' => 'ad_permission', 'access arguments' => array(1, array('manage owners', 'manage own notifications')), 'type' => MENU_CALLBACK, ); return $items; } /** * Menu item title callback - use the user name. */ function owner_notifications_title($account) { if (isset($account) && is_object($account)) { return t('!owner\'s notifications', array('!owner' => $account->name)); } } function ad_notify_tab_access($node) { if (isset($node->adtype)) { return (ad_permission($node->nid, 'manage owners') || ad_permission($node->nid, 'manage own notifications')); } } /** * Implementation of hook_cron(). * Send time based notifications and those, who has negative delay. */ function ad_notify_cron() { // Walk through all configured notifications and determine if any need to be // emailed. $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); while ($notification = db_fetch_object($result)) { $send = FALSE; // Handle special case 'regular' notification that is simply a time-based // status email. if ($notification->event == 'regular') { if ((time() - $notification->delay) >= $notification->sent) { $send = TRUE; $count = 1; } } // Handle event based notifications based on information stored in the // ad_statistics table. else { if (($event = trim($notification->event, '-')) != $notification->event) { // Event was prefixed by a -, so time is negative. We can't pull a // future event out of the statistics table, so we let the module that // defined this event tell us whether or not it's happened. $event_count = module_invoke_all('adnotifyapi', $notification->event, $notification); if (isset($event_count[$notification->event])) { $send = TRUE; } } else { $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)); if ($count) { // See if the notification has been queued long enough to be sent. if (!$notification->delay || ($notification->queued && (time() > ($notification->queued + $notification->delay)))) { $send = TRUE; } else if (!$notification->queued) { // Queue up the notification to send it at a later time. db_query('UPDATE {ad_notify} SET queued = %d WHERE notid = %d', time(), $notification->notid); } } } } if ($send) { ad_notify_send_mail($notification, $count); if ($notification->expire) { // Update the sent timestamp and counter, and auto-expire the // notification so it is not sent again. db_query('UPDATE {ad_notify} SET queued = 0, sent = %d, counter = counter + 1, status = %d WHERE notid = %d', time(), AD_NOTIFY_DISABLED, $notification->notid); } else { // Simply update the sent timestamp and counter. db_query('UPDATE {ad_notify} SET queued = 0, sent = %d, counter = counter + 1 WHERE notid = %d', time(), $notification->notid); } } } } function ad_notify_mail($key, &$message, $params) { $language = $message['language']; $message['subject'] = $params['notification']->subject; $message['body'] = $params['notification']->body; } /** * Send email notifications. */ function ad_notify_send_mail($notification, $count = 0, $params = array(), $language = NULL) { $owner = user_load($notification->uid); $send = TRUE; // Send notification only for user who has a permission if (isset($notification->roles) && !empty($notification->roles)) { $send = FALSE; $notification->roles = unserialize($notification->roles); foreach ($notification->roles as $rid) { if (isset($owner->roles[$rid])) { $send = TRUE; break; } } } if ($send) { $node = node_load($notification->aid); $node->notification = $notification; $replacements = array( 'global' => NULL, 'ad' => $node, 'ad_owner' => $owner, ); $notification->body = token_replace_multiple($notification->body, $replacements, '%', ''); $notification->subject = token_replace_multiple($notification->subject, $replacements, '%', ''); $params['address'] = $notification->address ? $notification->address : $owner->mail; $params['notification'] = $notification; return drupal_mail('ad_notify', 'notification', $params['address'], $language, $params); } } /** * Implementation of hook_adapi(). */ function ad_notify_adapi($op, &$node) { $output = NULL; switch ($op) { case 'statistics_increment': break; case 'permissions': return array('manage own notifications' => TRUE, 'edit notification email' => TRUE); break; } } /** * Implementation of hook_adnotifyapi(). */ function ad_notify_adnotifyapi($op, $arg1 = NULL, $arg2 = NULL) { switch ($op) { case 'register': return array( 'regular' => t('Email every @when as long as the ad is active.'), ); break; } } /** * Implementation of hook_adowners(). */ function ad_notify_adowners($op, $arg1 = NULL, $arg2 = NULL) { switch ($op) { case 'overview': return l(t('notifications'), 'node/'. $arg1 .'/adowners/'. $arg2 .'/notifications'); case 'add': if ($arg2['aid'] && $arg2['oid']) { $owner = user_load($arg2['uid']); // Clone all template notification for new ad owner. $result = db_query('SELECT * FROM {ad_notify} WHERE aid = 0 AND oid = 0'); while ($template = db_fetch_object($result)) { 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); } } break; case 'remove': if ($arg1) { $result = db_query('DELETE FROM {ad_notify} WHERE oid = %d', $arg1); } break; } } /** * Notification overview form. */ function ad_notify_overview_form($form_state, $node = NULL, $owner = NULL, $notid = 0) { global $user; if (isset($owner) && !is_object($owner) && $owner == 0) { $owner = new stdClass(); $owner->uid = 0; } else if (empty($owner)) { $owner = $user; } if (!isset($node) || !is_object($node)) { $node = new stdClass(); $node->nid = 0; } $oid = (int)db_result(db_query('SELECT oid FROM {ad_owners} WHERE aid = %d AND uid = %d', $node->nid, $owner->uid)); $notifications = module_invoke_all('adnotifyapi', 'register'); $header = array( array('data' => t('Last sent'), 'field' => 'sent', 'sort' => 'desc'), array('data' => t('Notification'), 'field' => 'event'), array('data' => t('Status'), 'field' => 'status'), array('data' => t('Action')) ); $sql = "SELECT notid, event, delay, sent, address, status, roles FROM {ad_notify} WHERE oid = %d"; $sql .= tablesort_sql($header, 'oid DESC, '); $result = pager_query($sql, 25, 0, NULL, $oid); $rows = array(); while ($notify = db_fetch_object($result)) { $list_notification = TRUE; // Check if user has permission to see the notification. if ($owner->uid && $notify->roles) { $list_notification = FALSE; $notify->roles = unserialize($notify->roles); if (isset($notify->roles) && is_array($notify->roles)) { foreach ($notify->roles as $rid => $require) { if ($require && isset($owner->roles[$rid])) { $list_notification = TRUE; break; } } } } if ($list_notification) { $row = array(); $row[] = $notify->sent ? t('!time ago', array('!time' => format_interval(time() - $notify->sent))) : t('Never'); $row[] = t($notifications[$notify->event], array('@when' => format_interval($notify->delay))); $row[] = $notify->status == AD_NOTIFY_ENABLED ? t('enabled') : t('disabled'); if ($node->nid) { $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'); } else if (user_access('administer advertisements')) { $row[] = l(t('edit'), 'admin/content/ad/notifications/' .$notify->notid. '/edit') .' '. l(t('delete'), 'admin/content/ad/notifications/'. $notify->notid .'/delete'); } else { $row[] = 'N/A'; } $rows[] = $row; } } $output = theme('table', $header, $rows); $output .= theme('pager', NULL, 25, 0); $form = array(); if ($notid) { $notification = ad_notification_load($notid); } if (!isset($owner)) { $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.'); } else { $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>'; $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>'; } $form['create'] = array( '#type' => 'fieldset', '#description' => $help, '#title' => $notid ? t('Edit notification') : t('Create new notification'), '#collapsible' => TRUE, '#collapsed' => ($rows == array() || $notid) ? FALSE : TRUE, ); $form['create']['event'] = array( '#type' => 'select', '#title' => t('Event'), '#options' => $notifications, '#description' => t('Select an event for which you would like to receive a notification.'), '#default_value' => $notid ? $notification->event : 1, ); $form['create']['delay'] = array( '#type' => 'select', '#title' => t('When'), '#options' => drupal_map_assoc(array(0,3600,10800,21600,43200,86400,259200,432000,604800,1209600,1814400,2419200,4838400,9676800,31536000), 'format_interval'), '#description' => t('Select a value to replace @when in the event notification you selected above.'), '#default_value' => $notid ? $notification->delay : 0, ); $form['create']['expire'] = array( '#type' => 'checkbox', '#title' => t('One-time'), '#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.'), '#default_value' => $notid ? $notification->expire : 0, ); if (ad_permission($node->nid, 'manage owners') && arg(2) == 'adowners' && $user->uid != arg(3)) { $form['create']['locked'] = array( '#type' => 'checkbox', '#title' => t('Locked'), '#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.'), '#default_value' => $notid ? $notification->locked : 0, ); } else { $form['create']['locked'] = array( '#type' => 'hidden', '#value' => $notid ? $notification->locked : 0, ); } if ($notid) { $form['create']['mail'] = array( '#type' => 'fieldset', '#title' => t('Message'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); } if (isset($owner->name)) { // TODO: Make it possible for admins to modify email address, and even to // enter multiple email addresses. Wrap this in a special permission, as // it involves trust to configure notifications to unverified addresses. $form['create']['mail']['address-display'] = array( '#type' => 'markup', '#value' => '<b>'. t('Notify address') .':</b><br />'. t('The email will be sent to %address.', array('%address' => is_object($owner) ? $owner->mail : 0)), '#prefix' => '<div class="container-inline">', '#suffix' => '</div>', ); $form['create']['mail']['address'] = array( '#type' => 'hidden', '#value' => is_object($owner) ? $owner->mail : '', ); } else { $roles = user_roles(TRUE); $form['create']['roles'] = array( '#type' => 'checkboxes', '#title' => t('Notify roles'), '#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.'), '#options' => $roles, ); } if ($notid) { $form['create']['mail']['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#required' => TRUE, '#default_value' => $notification->subject, ); $form['create']['mail']['body'] = array( '#type' => 'textarea', '#title' => t('Body'), '#required' => TRUE, '#default_value' => $notification->body, ); $form['create']['mail']['tokens'] = array( '#type' => 'fieldset', '#title' => t('Replacement patterns'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['create']['mail']['tokens']['list'] = array( '#value' => theme('token_help', 'ad', '%', ''), ); if ($notification->roles) { $form['create']['roles']['#default_value'] = $notification->roles; } } $form['create']['oid'] = array( '#type' => 'hidden', '#value' => $oid, ); $form['create']['aid'] = array( '#type' => 'hidden', '#value' => $node->nid, ); $form['create']['uid'] = array( '#type' => 'hidden', '#value' => $owner->uid, ); if ($notid) { $form['create']['notid'] = array( '#type' => 'hidden', '#value' => $notid, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); } else { $form['create']['submit'] = array( '#type' => 'submit', '#value' => t('Create notification'), ); } if ($rows != array()) { if (!$notid) { $form['notifications'] = array( '#type' => 'fieldset', '#title' => t('Notifications'), '#collapsible' => TRUE, ); $form['notifications']['current'] = array( '#type' => 'markup', '#value' => $output, ); } } else { $form['notifications'] = array( '#type' => 'markup', '#value' => '<p>'. t('There are no notifications configured yet.') .'</p>', ); } if ($node->nid && isset($owner) && is_object($owner) && isset($owner->name) && $user->name == $owner->name) { drupal_set_title('My notifications'); } else { drupal_set_title('Notifications'); } return $form; } /** * Validate ad notifications before saving to database. */ function ad_notify_overview_form_validate($form, &$form_state) { if ($form_state['values']['event'] == 'regular' && $form_state['values']['delay'] < 3600) { form_set_error('form', t('You are not allowed to schedule a regular notification more frequently than once an hour.')); } else if (!isset($form_state['values']['notid'])) { 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']))) { form_set_error('form', t('You have already scheduled that notification.')); } } else if ($form_state['values']['locked'] && !ad_permission($form_state['values']['aid'], 'manage owners')) { form_set_error('form', t('This notification is locked, you will need to contact the site administrator to edit this notification for you.')); } } /** * Save notifications to database. */ function ad_notify_overview_form_submit($form, &$form_state) { global $user; // Clean up roles value before saving to DB if (isset($form_state['values']['roles'])) { foreach ($form_state['values']['roles'] as $rid => $require) { if (!$require) { unset($form_state['values']['roles'][$rid]); } } if (empty($form_state['values']['roles'])) { $form_state['values']['roles'] = NULL; } } if (isset($form_state['values']['notid'])) { 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']); drupal_set_message('Notification updated.'); } else { // Retrieve the default mail subject and body. $mail = module_invoke_all('adnotifyapi', 'mail_text', $form_state['values']['event']); if ($mail == array()) { // Default message text. $mail = array( 'subject' => t('[%site-name ad] %event notification'), '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_impressions times and clicked %today_clicks times today. It was viewed %yesterday_impressions times and clicked %yesterday_clicks times yesterday. It has been viewed %global_impressions 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"), ); } 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']) : ''); // Clone new template notification for all ad owners. if (!$form_state['values']['oid']) { $template_id = db_last_insert_id('ad_notify', 'notid'); $result = db_query('SELECT ao.aid, ao.oid, u.mail FROM {ad_owners} ao LEFT JOIN {users} u ON ao.uid = u.uid'); while ($owner = db_fetch_object($result)) { // This INSERT can throw "duplicate key" errors in some situations, so just running it silently @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); } } drupal_set_message('Notification created.'); } if ($form_state['values']['aid'] && $form_state['values']['uid'] && $user->uid != $form_state['values']['uid']) { $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications'; } else if ($form_state['values']['aid']) { $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/notifications'; } else { $form_state['redirect'] = 'admin/content/ad/notifications'; } } /** * Load a specified notification from the database, return as an object. */ function ad_notification_load($notid) { $notification = db_fetch_object(db_query('SELECT * FROM {ad_notify} WHERE notid = %d', $notid)); $notification->roles = unserialize($notification->roles); return $notification; } /** * Confirm deletion of a specified notification from the database. */ function ad_notify_confirm_delete(&$form_state, $node, $owner, $notification) { global $user; $form = array(); $form['oid'] = array( '#type' => 'hidden', '#value' => $notification->oid, ); $form['aid'] = array( '#type' => 'hidden', '#value' => isset($node->nid) ? $node->nid : 0, ); $form['uid'] = array( '#type' => 'hidden', '#value' => is_object($owner) ? $owner->uid : 0, ); $form['notid'] = array( '#type' => 'hidden', '#value' => $notification->notid, ); $form['locked'] = array( '#type' => 'hidden', '#value' => $notification->locked, ); $form['event'] = array( '#type' => 'fieldset', '#collapsible' => FALSE, ); $notifications = module_invoke_all('adnotifyapi', 'register'); $form['event']['type'] = array( '#type' => 'markup', '#value' => t($notifications[$notification->event], array('@when' => format_interval($notification->delay))), '#prefix' => '<div class="container-inline">', '#suffix' => '</div>', ); if (!$notification->oid) { $linked = db_result(db_query('SELECT COUNT(*) FROM {ad_notify} WHERE template = %d', $notification->notid)); if ($linked) { $form['remove_linked'] = array( '#type' => 'radios', '#options' => array( 1 => t('Completely remove this notification.'), 0 => t('Remove from further usage, but leave existing notifications.'), ), '#default_value' => 1, ); } } if (isset($node->nid) && $owner->uid && $user->uid != $owner->uid) { $path = 'node/'. $node->nid .'/adowners/'. $owner->uid .'/notifications'; } else if (isset($node->nid)) { $path = 'node/'. $node->nid .'/notifications'; } else { $path = 'admin/content/ad/notifications'; } $form = confirm_form( $form, 'Are you sure you want to delete this notification?', $path, 'This action cannot be undone.', 'Delete', 'Cancel' ); return $form; } /** * Validate that the selected notification can be deleted. */ function ad_notify_confirm_delete_validate($form, &$form_state) { if ($form_state['values']['locked'] && !ad_permission($form_state['values']['aid'], 'manage owners')) { form_set_error('form', t('This notification is locked, you will need to contact the site administrator to delete this notification for you.')); } } /** * Delete a specified notification from the database. */ function ad_notify_confirm_delete_submit($form, &$form_state) { global $user; db_query('DELETE FROM {ad_notify} WHERE notid = %d', $form_state['values']['notid']); if (isset($form_state['values']['remove_linked']) && $form_state['values']['remove_linked']) { db_query('DELETE FROM {ad_notify} WHERE template = %d', $form_state['values']['notid']); drupal_set_message('Template and all derived notifications were deleted.'); } else { drupal_set_message('Notification deleted.'); } if ($form_state['values']['aid'] && $form_state['values']['uid'] && $user->uid != $form_state['values']['uid']) { $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/adowners/'. $form_state['values']['uid'] .'/notifications'; } else if ($form_state['values']['aid']) { $form_state['redirect'] = 'node/'. $form_state['values']['aid'] .'/notifications'; } else { $form_state['redirect'] = 'admin/content/ad/notifications'; } }