t('Views mail configuration'), 'description' => t('Configure Views Mail for sending of e-mails using Views. Also review and clear logs.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('views_mail_settings'), 'access callback' => 'user_access', 'access arguments' => array('administer views mail'), ); $items['admin/settings/views_mail/settings'] = array( 'title' => t('Settings'), 'description' => t('Configure Views Mail settings.'), 'weight' => 10, 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/settings/views_mail/logs'] = array( 'title' => t('Views Mail log views'), 'page callback' => 'drupal_get_form', 'page arguments' => array('views_mail_logs'), 'access callback' => 'user_access', 'access arguments' => array('administer views mail'), 'weight' => 20, 'type' => MENU_LOCAL_TASK, ); $items['admin/settings/views_mail/logs/view/%'] = array( 'title' => t('Log viewer'), 'page callback' => 'views_mail_logs_view', 'access callback' => 'user_access', 'access arguments' => array('administer views mail'), 'weight' => 10, 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/settings/views_mail/clear'] = array( 'title' => t('Clear Views Mail sent log'), 'page callback' => 'drupal_get_form', 'page arguments' => array('views_mail_clear_logs_confirm'), 'access callback' => 'user_access', 'access arguments' => array('administer views mail'), 'weight' => 30, 'type' => MENU_LOCAL_TASK, ); $items['views_mail'] = array( 'title' => t('Views Mail processing'), 'page callback' => 'drupal_get_form', 'page arguments' => array('views_mail_select_view'), 'access callback' => 'user_access', 'access arguments' => array('send views mail'), ); $items['views_mail/user'] = array( 'title' => t('Confirm Views Mail removal'), 'page callback' => 'views_mail_remove', 'access callback' => 'user_access', 'access arguments' => array('unsubscribe from views mail'), 'type' => MENU_CALLBACK, ); $items['views_mail/view/%'] = array( 'title' => t('Select recipients'), 'page callback' => 'views_mail_view_view', 'access callback' => 'user_access', 'access arguments' => array('send views mail'), 'weight' => 11, 'type' => MENU_LOCAL_TASK, ); $items['views_mail/config/%'] = array( 'title' => t('Configure mailing'), 'page callback' => 'drupal_get_form', 'page arguments' => array('views_mail_view_configure'), 'access callback' => 'user_access', 'access arguments' => array('send views mail'), 'weight' => 12, 'type' => MENU_LOCAL_TASK, ); $items['views_mail/confirm/%'] = array( 'title' => t('Confirm mailing'), 'page callback' => 'drupal_get_form', 'page arguments' => array('views_mail_view_confirm'), 'access callback' => 'user_access', 'access arguments' => array('send views mail'), 'weight' => 20, 'type' => MENU_LOCAL_TASK, ); return $items; } /** * Prepare a list of newsletters for default selection. */ function views_mail_default_newsletters() { $options = array(); $res = db_query("SELECT {term_data}.tid,{term_data}.name FROM {term_data} INNER JOIN {vocabulary} ON {term_data}.vid = {vocabulary}.vid WHERE {vocabulary}.module = 'simplenews' ORDER BY {term_data}.name"); while ($view = db_fetch_object($res)) { $options[$view->tid] = $view->name; } return $options; } /** * Prepare a list of views that contain the views_mail tag for default selection. */ function views_mail_view_list() { $options = array(); // $res = db_query("SELECT name,description FROM {views_view} WHERE tag LIKE '%views_mail%' ORDER BY description"); $res = db_query("SELECT name,description FROM {views_view} WHERE tag = 'views_mail' ORDER BY description"); while ($view = db_fetch_object($res)) { $options[$view->name] = $view->description; } return $options; } /** * Select the view to use. */ function views_mail_select_view() { if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('Views Mail is running in debug mode.'); // DEBUG } $form['views_mail_view'] = array( '#type' => 'select', '#title' => t('Select view'), '#options' => views_mail_view_list(), '#description' => t('Select the view to use in order to generate the list of users to send e-mail to.'), '#required' => TRUE, ); $form['submit'] = array('#type' => 'submit', '#value' => t('Continue')); return $form; } /** * Process Views Mail select form submission. */ function views_mail_select_view_submit($form, &$form_state) { $view = $form_state['values']['views_mail_view']; $query = ""; $_SESSION['views_mail'][$view]['gid'] = 0; // This is designed to set the group context if it is present. if (isset($_REQUEST['gids'])) { $gids = $_GET['gids']; $gid = intval(current($_REQUEST['gids'])); $_SESSION['views_mail'][$view]['gid'] = $gid; $query = 'gids[]='.$gid; } drupal_goto('views_mail/view/'.$view, $query); } /** * Display view and use exposed filters to select the e-mail list you wish * wish to send to. */ function views_mail_view_view() { if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('Views Mail is running in debug mode.'); // DEBUG } $viewname = arg(2); // Create the embedded view $view = views_mail_embed_view($viewname, 'default', 'views_mail/view/'.$viewname); $args = $_SESSION['views_mail'][$viewname]['args']; drupal_set_message('Use the exposed filters below to select the list of e-mail addresses that you wish to send to. Once you see the desired list of recipients on this screen, then you may proceed by clicking on the '.l(t('Configure mailing'), "views_mail/config/$viewname", array('query' => "$args")).' tab. Do NOT continue on unless the list you see on this page contains the e-mail addresses to which you wish to send your message.'); // Set group context if (module_exists('og')) { // If gids[] are present, user is attempting to set the group, so // do that here. Note that the group session is also saved at the top // when the user selects the view. if (isset($_REQUEST['gids'])) { $gids = $_GET['gids']; $gid = intval(current($_REQUEST['gids'])); $_SESSION['views_mail'][$viewname]['gid'] = $gid; } // If group is set, then apply group context if ($_SESSION['views_mail'][$viewname]['gid'] > 0) { $gids = $_GET['gids']; $gid = $_SESSION['views_mail'][$viewname]['gid']; $group_node = node_load($gid); og_set_group_context($group_node); } } return $view; } /** * Configure how e-mail will be sent */ function views_mail_view_configure() { global $user; if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('Views Mail is running in debug mode.'); // DEBUG } drupal_set_message('Configure the mailing: Provide the sender, recipient, newsletter, subject, message and schedule information.'); $viewname = arg(2); $args = $_SESSION['views_mail'][$viewname]['args']; // Set group context if (module_exists('og')) { if ($_SESSION['views_mail'][$viewname]['gid'] > 0) { $gids = $_GET['gids']; $gid = $_SESSION['views_mail'][$viewname]['gid']; $group_node = node_load($gid); og_set_group_context($group_node); } } // If the subject and message variables have been set, then use them. $subject = ''; $message = ''; if (isset($_SESSION['views_mail'][$viewname]['subject'])) $subject = $_SESSION['views_mail'][$viewname]['subject']; if (isset($_SESSION['views_mail'][$viewname]['message'])) $message = $_SESSION['views_mail'][$viewname]['message']; // If the from_name and from_mail variables have been set, then use them. $from_name = $user->name; $from_mail = $user->mail; if (isset($_SESSION['views_mail'][$viewname]['from_name'])) $from_name = $_SESSION['views_mail'][$viewname]['from_name']; if (isset($_SESSION['views_mail'][$viewname]['from_mail'])) $from_mail = $_SESSION['views_mail'][$viewname]['from_mail']; // If the name_field and email_field variables have been set, then use them. $name_field = ''; $email_field = ''; $nid_field = 0; if (isset($_SESSION['views_mail'][$viewname]['name_field'])) $name_field = $_SESSION['views_mail'][$viewname]['name_field']; if (isset($_SESSION['views_mail'][$viewname]['email_field'])) $email_field = $_SESSION['views_mail'][$viewname]['email_field']; if (isset($_SESSION['views_mail'][$viewname]['nid_field'])) $nid_field = $_SESSION['views_mail'][$viewname]['nid_field']; // If the additional_mails variable has been set, then use it. $additional_mails = array(); if (isset($_SESSION['views_mail'][$viewname]['additional_mails'])) $additional_mails = $_SESSION['views_mail'][$viewname]['additional_mails']; // If the newsletter variable has been set, then use it. $newsletter = ''; if (isset($_SESSION['views_mail'][$viewname]['newsletter'])) $name_field = $_SESSION['views_mail'][$viewname]['newsletter']; // If the number and unit variables have been set, then use them. $number = 0; $unit = 86400; if (isset($_SESSION['views_mail'][$viewname]['number'])) $number = $_SESSION['views_mail'][$viewname]['number']; if (isset($_SESSION['views_mail'][$viewname]['unit'])) $unit = $_SESSION['views_mail'][$viewname]['unit']; $count = 0; $view = views_get_view($viewname); // $view->set_arguments($args); $view->set_items_per_page(0); $view->set_display('default'); $view->render(); foreach ($view->result as $result) { $count++; } if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('
'. print_r($view->display['default']->display_options, TRUE) .''); // DEBUG } $GLOBALS['current_view'] = &$view; $form['viewname'] = array( '#type' => 'hidden', '#value' => $viewname, ); $form['args'] = array( '#type' => 'hidden', '#value' => $args, ); $form['uid'] = array( '#type' => 'hidden', '#value' => $user->uid, ); $view_list = views_mail_view_list(); $form['viewname'] = array( '#type' => 'textfield', '#title' => 'View title', '#description' => t('The title of the view used for this mailing.'), '#value' => $view_list[$viewname], '#disabled' => TRUE ); $form['count'] = array( '#type' => 'textfield', '#title' => 'View count', '#description' => t('The number of records in this view. Please note that the number of e-mails in your final recipient list may be smaller than the number of view records indicated here. If there are multiple records with the same e-mail address, these records will not be duplicated in the final recipient list. Also, records listed here which contain invalid e-mail addresses will be ignored, as well as the e-mail addresses of users who have opted-out of Views Mail mailings.'), '#value' => $count, '#size' => 10, '#disabled' => TRUE ); $form['from_name'] = array( '#type' => 'textfield', '#title' => t('Sender name'), '#default_value' => $from_name, '#maxlength' => '50', '#required' => TRUE, ); $form['from_mail'] = array( '#type' => 'textfield', '#title' => t('Sender e-mail address'), '#default_value' => $from_mail, '#maxlength' => '60', '#required' => TRUE, ); // This gets the list of fields from the view that has been selected. // I think that the column key is created by combining the // table name and field name. $view_fields = array(); $view_email_fields = array(); foreach ($view->display['default']->display_options['fields'] as $name=>$value) { $field = $value['table'].'_'.$value['field']; $label = $value['label']; $view_fields[$field] = $label; $view_email_fields[$field] = $label; } $none_option[0] = 'None'; $view_fields_nid = array_merge($view_fields, $none_option); if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('view_fields
'. print_r($view_fields, TRUE) .''); // DEBUG } $form['name_field'] = array( '#type' => 'select', '#title' => t('Select recipient name field'), '#options' => $view_fields, '#default_value' => $name_field, '#description' => t('Select the field which contains the name of the person to whom the message will be sent.'), '#required' => TRUE, ); $form['email_field'] = array( '#type' => 'select', '#title' => t('Select recipient e-mail field'), '#options' => $view_fields, '#default_value' => $email_field, '#description' => t('Select the field which contains the e-mail address of the person to whom the message will be sent.'), '#required' => TRUE, ); $form['nid_field'] = array( '#type' => 'select', '#title' => t('Select recipient node id field'), '#options' => $view_fields_nid, '#default_value' => $nid_field, '#description' => t('Select the field which contains the node id (\'nid\') of the node record which contains the e-mail address of the person to whom the message will be sent. This is optional.'), '#required' => FALSE, ); $form['newsletter'] = array( '#type' => 'select', '#title' => t('Newsletter to send to this group'), '#default_value' => $newsletter, '#options' => views_mail_newsletters(), '#description' => t('The newsletter that contains the message to be sent. If you are going to enter a custom message below, then select the newsletter that will serve as your newsletter template'), '#required' => TRUE, ); $form['custom'] = array( '#type' => 'fieldset', '#title' => t('Custom message'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('You can enter your own subject line for this e-mail. You can also enter a custom message for this e-mail. If you enter anything into either the subject line or message body below, then these values will be used instead of those from the newsletter issue you have selected above. If you get to the Confirm screen and realize you need to modify text entered here, you must use your browser BACK key to return to this page. If you click on the \'Configure mailing\' tab, everything you entered will be lost.'), ); $form['custom']['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#default_value' => $subject, '#maxlength' => '130', '#description' => t('The subject to use for this e-mail. Leave blank to use the title of the newsletter.'), '#required' => FALSE, ); $form['custom']['views_mail_message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => $message, '#rows' => '5', '#required' => FALSE, '#description' => t('The message to use for this e-mail. Leave blank to use the body of the newsletter.'), ); $form['schedule'] = array( '#type' => 'fieldset', '#title' => t('Schedule'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('Set time period to wait before sending this message. If you wish this message to be sent immediately, do not enter anything here -- leave at 0.'), ); $form['schedule']['number'] = array( '#type' => 'textfield', '#title' => t('Number'), '#required' => true, '#size' => 3, '#default_value' => $number, ); $form['schedule']['unit'] = array( '#type' => 'select', '#title' => t('Unit'), '#options' => array( '1' => 'seconds', '60' => 'minutes', '3600' => 'hours', '86400' => 'days', ), '#default_value' => $unit, ); $form['additional'] = array( '#type' => 'fieldset', '#title' => t('Additional e-mail addresses'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('If your view contains additional e-mail address fields to which you would like to send this views mail, you can select them here.'), ); $form['additional']['additional_mails'] = array( '#type' => 'checkboxes', '#title' => t('Additional e-mail addresses'), '#options' => $view_email_fields, '#default_value' => $additional_mails, '#description' => t('Select the fields here which contain the additional e-mail addresses you would like to send this mailing to. Note that if you select the primary e-mail field here (selected above) it will not be duplicated in the mailing. Also note that the primary name field you selected above will be used for the e-mail addresses of all fields selected here. Do not select fields here that do not contain valid e-mail addresses.'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Continue')); return $form; } /** * Validate the configuration. */ function views_mail_view_configure_validate($form, &$form_state) { $view = $GLOBALS['current_view']; if ($form_state['values']['count'] == 0) { form_set_error('count', t('The view you have selected does not contain any recipients.')); } if (!valid_email_address($form_state['values']['from_mail'])) { form_set_error('from_mail', t('The sender e-mail address you have entered is not valid.')); } $name_field = $form_state['values']['name_field']; $email_field = $form_state['values']['email_field']; $nid_field = $form_state['values']['nid_field']; $additional_mails = $form_state['values']['additional_mails']; $count = views_mail_get_recipients($name_field, $email_field, $nid_field, $additional_mails, $view, TRUE); if ($count == 0) { form_set_error('email_field', t('Either the recipient or one of the additional e-mail fields you have selected does not contain a valid e-mail address.')); } } /** * Process the configuration. */ function views_mail_view_configure_submit($form, &$form_state) { $view = $GLOBALS['current_view']; $args = $_SESSION['views_mail'][$view->name]['args']; $name_field = $form_state['values']['name_field']; $email_field = $form_state['values']['email_field']; $nid_field = $form_state['values']['nid_field']; $additional_mails = $form_state['values']['additional_mails']; // Build the recipient list and store in session. $count = views_mail_get_recipients($name_field, $email_field, $nid_field, $additional_mails, $view, TRUE); $_SESSION['views_mail'][$view->name]['count'] = $count; $_SESSION['views_mail'][$view->name]['from_name'] = $form_state['values']['from_name']; $_SESSION['views_mail'][$view->name]['from_mail'] = $form_state['values']['from_mail']; $_SESSION['views_mail'][$view->name]['name_field'] = $form_state['values']['name_field']; $_SESSION['views_mail'][$view->name]['email_field'] = $form_state['values']['email_field']; $_SESSION['views_mail'][$view->name]['nid_field'] = $form_state['values']['nid_field']; $_SESSION['views_mail'][$view->name]['additional_mails'] = $form_state['values']['additional_mails']; $_SESSION['views_mail'][$view->name]['newsletter'] = $form_state['values']['newsletter']; $nid = $form_state['values']['newsletter']; if (empty($form_state['values']['subject'])) { $_SESSION['views_mail'][$view->name]['subject'] = views_mail_get_node_field($nid, 'title'); } else { $_SESSION['views_mail'][$view->name]['subject'] = $form_state['values']['subject']; } if (empty($form_state['values']['views_mail_message'])) { $_SESSION['views_mail'][$view->name]['message'] = views_mail_get_node_field($nid, 'body'); } else { $_SESSION['views_mail'][$view->name]['message'] = $form_state['values']['views_mail_message']; } $_SESSION['views_mail'][$view->name]['number'] = $form_state['values']['number']; $_SESSION['views_mail'][$view->name]['unit'] = $form_state['values']['unit']; drupal_goto('views_mail/confirm/'.$view->name, $args); } /** * Confirm the configuration. */ function views_mail_view_confirm() { if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('Views Mail is running in debug mode.'); // DEBUG } $viewname = arg(2); // Set group context if (module_exists('og')) { if ($_SESSION['views_mail'][$viewname]['gid'] > 0) { $gids = $_GET['gids']; $gid = $_SESSION['views_mail'][$viewname]['gid']; $group_node = node_load($gid); og_set_group_context($group_node); } } $recipients = $_SESSION['views_mail'][$viewname]['recipients']; $count = $_SESSION['views_mail'][$viewname]['count']; $from_name = $_SESSION['views_mail'][$viewname]['from_name']; $from_mail = $_SESSION['views_mail'][$viewname]['from_mail']; $subject = $_SESSION['views_mail'][$viewname]['subject']; $message = $_SESSION['views_mail'][$viewname]['message']; $number = $_SESSION['views_mail'][$viewname]['number']; $unit = $_SESSION['views_mail'][$viewname]['unit']; $form['confirm'] = array( '#value' => t('Are you sure you want to send this message to these recipients?
'. print_r($form, TRUE) .''); // DEBUG drupal_set_message('
'. print_r($form_state, TRUE) .''); // DEBUG drupal_set_message('Clicked button : '. $form_state['clicked_button']['#value']); } $viewname = $form_state['clicked_button']['#value']; drupal_goto('admin/settings/views_mail/logs/view/'.$viewname); } /** * Display views_mail log tables */ function views_mail_logs_view() { $viewname = arg(5); if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('Views Mail is running in debug mode.'); // DEBUG drupal_set_message('viewname : '.$viewname); // DEBUG } // Create the embedded view $view = views_mail_embed_view($viewname, 'default', 'admin/settings/views_mail/logs/view/'.$viewname); return $view; } /** * Submit callback; clear views_mail logs. * * @ingroup forms */ function views_mail_clear_logs_submit(&$form_state, $form) { drupal_goto('admin/settings/views_mail/clear'); } function views_mail_clear_logs_confirm() { $form = array(); $form['#redirect'] = 'admin/settings/views_mail'; return confirm_form($form, t('Confirm clear Views Mail logs?'), 'admin/settings/views_mail', t('This action will delete all Views Mail logs for mailings that have been sent. br>This action an not be undone.'), t('Clear the logs'), t('Cancel') ); } function views_mail_clear_logs_confirm_submit($form, $form_state) { db_query("DELETE FROM {views_mail} WHERE status = 1"); db_query("DELETE FROM {views_mail_schedule} WHERE status = 1"); watchdog('views_mail', 'Cleared sent logs.'); drupal_set_message(t('Views Mail logs cleared.')); } /** * Prepare a list of newsletter issues for selection. */ function views_mail_newsletters() { global $user; $options = array(); $newsletter = variable_get('views_mail_email_default_newsletter', ''); // get the newsletter term id if (user_access('send own views mail')) { $res = db_query("SELECT node.nid, node.title FROM {node} INNER JOIN {simplenews_newsletters} ON node.nid = simplenews_newsletters.nid WHERE tid = %d AND uid = %d ORDER BY node.title", $newsletter, $user->uid); } if (user_access('send views mail')) { $res = db_query("SELECT node.nid, node.title FROM {node} INNER JOIN {simplenews_newsletters} ON node.nid = simplenews_newsletters.nid WHERE tid = %d ORDER BY node.title", $newsletter); } while ($view = db_fetch_object($res)) { $options[$view->nid] = $view->title; } return $options; } /** * Get the body of the newsletter for this nid. */ function views_mail_get_node_field($nid, $field) { $output = ""; if (!empty($nid)) { $node = node_load($nid); $output = $node->$field; } return $output; } /** * Create a recipient list from the filtered view. * Store it in session if $build = TRUE. * Return the count of actual emails which will be sent. This number could be less * than the number of view records because we check for valid e-mail addresses * and we do not duplicate e-mail addresses. * * @param $name_field * The key for the name field. * @param $email_field * The key for the email field. * @param $nid_field * The key for the node id field. * @param $view * The view. * @param boolean $build * TRUE or FALSE * If TRUE, store recipient list in $_SESSION['views_mail'][$view->name]['recipients']. * @return * Return count of valid e-mails. */ function views_mail_get_recipients($name_field, $email_field, $nid_field, $additional_mails = array(), $view, $build = TRUE) { $count = 0; $recipients = array(); $_SESSION['views_mail'][$view->name]['recipients'] = array(); if (empty($name_field) || empty($email_field)) { return $count; } if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('name_field : '.$name_field); // DEBUG drupal_set_message('email_field : '.$email_field); // DEBUG drupal_set_message('nid_field : '.$nid_field); // DEBUG } foreach ($view->result as $result) { $email = $result->$email_field; $name = $result->$name_field; if ($nid_field) { $nid = $result->$nid_field; // Looks like this might just be nid instead of node_nid if (!$nid) $nid = $result->nid; } else { $nid = 0; } if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('
'. print_r($result, TRUE) .''); // DEBUG } if (valid_email_address($email)) { // See if this user has opted out of receiving emails from us. if (views_mail_accept_mail($email) === TRUE) { $recipients[$email]['name'] = $name; $recipients[$email]['nid'] = $nid; } } // Add additional email addresses if they exist // They will use the name field selected in this record if ($additional_mails) { foreach ($additional_mails as $additional_email_field => $checked) { if ($checked) { $additional_email = $result->$additional_email_field; if (valid_email_address($additional_email)) { // See if this user has opted out of receiving emails from us. if (views_mail_accept_mail($additional_email) === TRUE) { $recipients[$additional_email]['name'] = $name; $recipients[$additional_email]['nid'] = $nid; } } } } } } $count = count($recipients); if ($build == TRUE) { $_SESSION['views_mail'][$view->name]['recipients'] = $recipients; } return $count; } /** * Implementation of hook_user(). * * Provide form element to opt in to content mailouts. */ function views_mail_user($type, $edit, &$user, $category = NULL) { if ($type == 'register' || ($type == 'form' && $category == 'account')) { $form['views_mail'] = array( '#type' => 'fieldset', '#title' => t('Group e-mail settings'), '#weight' => 5, '#collapsible' => TRUE, ); // Force checkbox to default on if this is first time registration; if ($type == 'register') { // If force group email setting, then hide group e-mail settings checkbox on registration // because it will default to "on". $type = 'checkbox'; $opt_in = variable_get("views_mail_force_mail_accept", 0); if ($opt_in == 1) $type = 'hidden'; $form['views_mail']['views_mail_accept'] = array( '#type' => $type, '#title' => t('Accept email'), '#default_value' => 1, '#description' => t('Allow yourself to be included in group e-mails from privileged users. Note that your e-mail address is not made public and that site administrators are able to email you even if you choose not to enable this feature.'), ); } else { $form['views_mail']['views_mail_accept'] = array('#type' => 'checkbox', '#title' => t('Accept email'), '#default_value' => $edit['views_mail_accept'], '#description' => t('Allow yourself to be included in group e-mails from privileged users. Note that your e-mail address is not made public and that site administrators are able to email you even if you choose not to enable this feature.'), ); } return $form; } // end register if if ($type == 'validate') { return array('views_mail_accept' => $edit['views_mail_accept']); } // end validate if if ($type == "insert") { // Subscribe this user to the newsletter if it exists $str = variable_get("views_mail_subscribe_newsletter", ''); if (!empty($str)) { $array = preg_split('/\|/', $str, -1, PREG_SPLIT_NO_EMPTY); // split at the | = newsletter | comment if (!empty($array)) { $newsletter = $array[0]; simplenews_subscribe_user($user->mail, $newsletter, $confirm = FALSE); } // end if } // end if } // end insert if } /** * Accept Mail function * * Pass recipient e-mail address through this function to see if the user has opted * out of receiving e-mails from you. * * @param string $email - e-mail address to check * @return boolean $accept - true or false * */ function views_mail_accept_mail($email) { $accept_mail = TRUE; // First check the user's views_mail_accept status (if this is a user email account) $ooresult = db_query("SELECT uid FROM {users} where mail = '" . $email . "'"); while ($oouid = db_fetch_object($ooresult)) { $account = user_load(array('uid' => $oouid->uid, 'status' => 1)); if (!$account->views_mail_accept) { $accept_mail = FALSE; } if (variable_get("views_mail_force_mail_accept", 0) == 1) $accept_mail = TRUE; } // Run through opt-out email list $optout = views_mail_check_optout($email); if ($optout) $accept_mail = FALSE; return $accept_mail; } /** * Make a list of recipients. * * TODO: Sort is really funky. Need to do something about it. * * @param $recipients * The recipients array. * @param $title * (optional) Title to pass on to theme_item_list(). * * @ingroup themeable */ function views_mail_recipients_list($recipients, $title = NULL) { if (!empty($recipients)) { foreach ($recipients as $email=>$name) { $items[] = $name; } } return theme('item_list', $items, $title); } /** * Theme the recipient list as a table. */ function views_mail_recipients_table($recipients, $title = NULL) { $headers = array( array( 'data'=> t('Name'), 'field'=> 'name', 'sort'=> 'asc', ), array( 'data'=> t('Mail'), 'field'=> 'email', ), array( 'data'=> t('Node id'), 'field'=> 'nid', ), ); asort($recipients); if (!empty($recipients)) { foreach ($recipients as $email=>$value) { $items[] = array($value['name'], $email, $value['nid']); } } return '
'. t('Are you sure you want to remove %user from the Views Mail subscription list?', array('%user' => $mail)) ."
\n"); $form['mail'] = array('#type' => 'value', '#value' => $mail); $form['nid'] = array('#type' => 'value', '#value' => $nid); $form['#redirect'] = ''; return confirm_form($form, t('Confirm unsubscription'), '', t('This action will only remove you from the Views Mail subscription list. If you are registered at our site, your account information will remain unchanged.'), t('Unsubscribe'), t('Cancel') ); } /** * Forms API callback; handles form submission for a user confirming unsubscribe * request. */ function views_mail_confirm_removal_form_submit($form_id, &$form_state) { views_mail_unsubscribe_user($form_state['values']['mail'], $form_state['values']['nid'], FALSE); drupal_set_message(t('%user was successfully removed from the Views Mail subscription list.', array('%user' => $form_state['values']['mail']))); return ''; } /** * API function; unsubscribes a user from Views Mail. * * @param $mail * The e-mail address to unsubscribe. * @param $nid * The nodeID of the newsletter where the user clicked on unsubscribe. * @param $confirm * Whether we should send a confirmation e-mail and hold off removing this * user from the newsletter until he clicks the confirm link in the e-mail. */ function views_mail_unsubscribe_user($mail, $nid, $confirm = TRUE) { //Prevent mismatches from accidental capitals in mail address $mail = strtolower($mail); db_query("INSERT INTO {views_mail_optout} (email, nid) VALUES ('%s', %d)", $mail, $nid); return TRUE; } /** * API function; checks to see if user is in opt out table. * * @param $mail * The e-mail address to unsubscribe. * * @return * Return TRUE if email found. * */ function views_mail_check_optout($mail_in) { // Get email address only. $pattern = '/.+\<(.+)\>/i'; $replacement = '${1}'; $mail = preg_replace($pattern, $replacement, $mail_in); // Prevent mismatches from accidental capitals in mail address $mail = strtolower($mail); $count = db_result(db_query("SELECT COUNT(email) FROM {views_mail_optout} WHERE email = '%s'", $mail)); if ($count == 0) { return FALSE; } else { return TRUE; } } /** * Implementation of hook_theme. */ function views_mail_theme() { return array( 'views_mail_newsletter_footer' => array( 'arguments' => array('format' => NULL, 'hash' => NULL, 'language' => NULL), ), ); } /** * Implementation of hook_link(). */ function views_mail_link($type, $node = NULL, $teaser = FALSE) { $links = array(); // If this is a newsletter node in the Views Mail newsletter group // and views_mail_link is clicked on if ($type == 'node' && $node->type == 'simplenews') { $count = db_result(db_query('SELECT COUNT(nid) FROM {simplenews_newsletters} WHERE nid = %d AND tid = %d', $node->nid, variable_get('views_mail_email_default_newsletter', 0))); if ($count > 0 && variable_get('views_mail_link', 0) == 1) { if (user_access('send views mail') || user_access('send own views mail')) { // If we are in a group context, then use that group context in the link if (module_exists('og')) { // Get the group context $group_node = og_get_group_context(); if (isset($group_node->nid) && variable_get('views_mail_link_og', 0) == 1) { if (variable_get('views_mail_link_url_query', '')) { $query = 'gids[]='.$group_node->nid.'&'.variable_get('views_mail_link_url_query', ''); } else { $query = 'gids[]='.$group_node->nid; } } else { $query = variable_get('views_mail_link_url_query', ''); } $links['views_mail_newsletter'] = array( 'title' => t(variable_get('views_mail_link_title', 'Views Mail')), 'href' => variable_get('views_mail_link_url', 'views_mail'), 'query' => $query, ); } else { $links['views_mail_newsletter'] = array( 'title' => t(variable_get('views_mail_link_title', 'Views Mail')), 'href' => variable_get('views_mail_link_url', 'views_mail'), 'query' => variable_get('views_mail_link_url_query', ''), ); } // end og if } // end user_access if } // end views_mail_link if } // end simplenews node if return $links; } /** * Get gid from referrer */ function views_mail_gid_from_referrer() { $gid = 0; // Need to use the referrer to get OG group in some cases; $ref = $_SERVER["HTTP_REFERER"]; $ref_url = parse_url($ref); $ref_path = $ref_url[path]; $ref_query = $ref_url[query]; $ref_arg = explode('/', $ref_path); if ($ref_arg[1] == 'views_mail' && $ref_arg[2] == 'view') { parse_str($ref_query); $gid = $gids[0]; } drupal_set_message('referrer gid '.$gid); return $gid; } // --------- Table views_mail function views_mail_views_data() { $data['views_mail']['table']['group'] = t('Views Mail'); $data['views_mail']['table']['base'] = array( 'field' => 'vmid', 'title' => t('Views Mail log'), 'help' => t('Views Mail mailings.'), ); $data['views_mail']['table']['join'] = array( 'node' => array( 'left_table' => 'views_mail_schedule', 'left_field' => 'vmid', 'field' => 'vmid', ), 'users' => array( 'left_field' => 'uid', 'field' => 'uid', ), ); $data['views_mail']['vmid'] = array( 'title' => t('Mailing id'), 'help' => t('Id of the Views Mail mailing.'), 'field' => array( 'handler' => 'views_handler_field', ), ); $data['views_mail']['nid'] = array( 'title' => t('Newsletter node id'), 'help' => t('Node id of the newsletter used for this mailing.'), 'relationship' => array( 'title' => t('Views Mail: Newsletter node'), 'help' => t("Bring in information about the Views Mail newsletter node."), 'base' => 'node', 'field' => 'nid', 'handler' => 'views_handler_relationship', 'label' => t('Newsletter node'), ), 'field' => array( 'handler' => 'views_handler_field_node', ), ); $data['views_mail']['uid'] = array( 'title' => t('User'), 'help' => t('Id of user who creates Views Mail mailing.'), 'field' => array( 'handler' => 'views_handler_field_user_name', 'click sortable' => TRUE, ), 'argument' => array( 'handler' => 'views_handler_argument_user_uid', 'name field' => 'name', // display this field in the summary ), 'filter' => array( 'title' => t('Name'), 'handler' => 'views_handler_filter_user_name', ), 'sort' => array( 'handler' => 'views_handler_sort', ), ); // created field $data['views_mail']['created'] = array( 'title' => t('Post date'), // The item it appears as on the UI, 'help' => t('The date the Views Mail mailing was posted.'), // The help that appears on the UI, 'field' => array( 'handler' => 'views_handler_field_date', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort_date', ), 'filter' => array( 'handler' => 'views_handler_filter_date', ), ); // created field $data['views_mail']['sent'] = array( 'title' => t('Send date'), 'help' => t('The date the Views Mail mailing was sent (if the mailing has been sent). Otherwise, it is the date the mailing is scheduled to be sent.'), 'field' => array( 'handler' => 'views_handler_field_date', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort_date', ), 'filter' => array( 'handler' => 'views_handler_filter_date', ), ); $data['views_mail']['view'] = array( 'title' => t('View name'), 'help' => t('The name of the view used in this Views Mail mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail']['filter'] = array( 'title' => t('Filter'), 'help' => t('The exposed view filter used for this mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail']['from_name'] = array( 'title' => t('Sender name'), 'help' => t('The name of the sender used in this mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail']['from_mail'] = array( 'title' => t('Sender e-mail'), 'help' => t('The sender e-mail address used in this mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail']['subject'] = array( 'title' => t('Subject'), 'help' => t('The subject line of the e-mails sent in this mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail']['status'] = array( 'title' => t('Sent'), 'help' => t('Whether or not the mailing has been sent.'), 'field' => array( 'handler' => 'views_handler_field_boolean', 'click sortable' => TRUE, ), 'filter' => array( 'handler' => 'views_handler_filter_boolean_operator', 'label' => t('Sent'), 'type' => 'yes-no', ), 'sort' => array( 'handler' => 'views_handler_sort', ), ); $data['views_mail']['approved'] = array( 'title' => t('Approved'), 'help' => t('Whether or not the mailing has been approved. Currently not used.'), 'field' => array( 'handler' => 'views_handler_field_boolean', 'click sortable' => TRUE, ), 'filter' => array( 'handler' => 'views_handler_filter_boolean_operator', 'label' => t('Sent'), 'type' => 'yes-no', ), 'sort' => array( 'handler' => 'views_handler_sort', ), ); $data['views_mail']['recipients_scheduled'] = array( 'title' => t('Recipients scheduled.'), 'help' => t('The number of recipients scheduled to receive this mailing.'), 'field' => array( 'handler' => 'views_handler_field_numeric', 'click sortable' => TRUE, ), 'filter' => array( 'handler' => 'views_handler_filter_numeric', ), 'sort' => array( 'handler' => 'views_handler_sort', ), ); $data['views_mail']['recipients_sent'] = array( 'title' => t('Recipients sent.'), 'help' => t('The number of recipients who were actually sent e-mails in this mailing.'), 'field' => array( 'handler' => 'views_handler_field_numeric', 'click sortable' => TRUE, ), 'filter' => array( 'handler' => 'views_handler_filter_numeric', ), 'sort' => array( 'handler' => 'views_handler_sort', ), ); // Join our custom table to the node table; $table = variable_get('views_mail_table', 'node'); $left_field = variable_get('views_mail_left_field', 'mail'); $data['views_mail_schedule']['table']['group'] = t('Views Mail scheduled recipients'); $data['views_mail_schedule']['table']['join'] = array( 'views_mail' => array( 'left_field' => 'vmid', 'field' => 'vmid', ), 'users' => array( 'left_field' => 'mail', 'field' => 'recipient_mail', ), 'node' => array( 'left_field' => 'nid', 'field' => 'recipient_nid', ), ); $data['views_mail_schedule']['vmid'] = array( 'title' => t('Mailing id'), 'help' => t('Id of the Views Mail mailing.'), 'field' => array( 'handler' => 'views_handler_field', ), ); $data['views_mail_schedule']['recipient_name'] = array( 'title' => t('Recipient name'), 'help' => t('The name of the recipient for this mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail_schedule']['recipient_mail'] = array( 'title' => t('Recipient e-mail'), 'help' => t('The e-mail address of recipient for this mailing.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); $data['views_mail_schedule']['status'] = array( 'title' => t('Sent'), 'help' => t('Whether or not the mailing has been sent to this recipient.'), 'field' => array( 'handler' => 'views_handler_field_boolean', 'click sortable' => TRUE, ), 'filter' => array( 'handler' => 'views_handler_filter_boolean_operator', 'label' => t('Sent'), 'type' => 'yes-no', ), 'sort' => array( 'handler' => 'views_handler_sort', ), ); $data['views_mail_schedule']['schedule'] = array( 'title' => t('Scheduled send date'), 'help' => t('The date the Views Mail mailing is scheduled to be sent. Or, if the mailing has been sent, the date it was sent.'), 'field' => array( 'handler' => 'views_handler_field_date', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort_date', ), 'filter' => array( 'handler' => 'views_handler_filter_date', ), ); $data['views_mail_optout']['table']['group'] = t('Views Mail opt-out'); $data['views_mail_optout']['table']['join'] = array( 'views_mail' => array( 'left_field' => 'nid', 'field' => 'nid', ), 'users' => array( 'left_field' => 'mail', 'field' => 'email', ), ); $data['views_mail_optout']['nid'] = array( 'title' => t('Newsletter node id'), 'help' => t('Node id of the newsletter from which this user opted out.'), 'relationship' => array( 'title' => t('Opt-out Newsletter node'), 'help' => t("Bring in information about the Views Mail opted-out newsletter node."), 'base' => 'node', 'field' => 'nid', 'handler' => 'views_handler_relationship', 'label' => t('Opt-out Newsletter node'), ), 'field' => array( 'handler' => 'views_handler_field_node', ), ); $data['views_mail_optout']['email'] = array( 'title' => t('E-mail address'), 'help' => t('E-mail address of individual who has opted out of Views Mail mailings.'), // Information for displaying a title as a field 'field' => array( 'handler' => 'views_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), // Information for accepting a title as a filter 'filter' => array( 'handler' => 'views_handler_filter_string', ), 'argument' => array( 'handler' => 'views_handler_argument_string', ), ); return $data; } /** * Implementation of hook_views_handlers() to register all of the basic handlers * views uses. */ function views_mail_views_handlers() { return array( 'info' => array( 'path' => drupal_get_path('module', 'views_mail') . '/views/handlers', ), 'handlers' => array( 'views_mail_handler_field_numeric' => array( 'parent' => 'views_handler_field', ), ), ); } /** * Implementation of hook_views_pre_render() */ function views_mail_views_pre_render(&$view) { if (variable_get("views_mail_debug", 0) == 1) { drupal_set_message('pre_render tag : '. $view->tag); // DEBUG drupal_set_message('
'. print_r($view, TRUE) .''); // DEBUG } // If set, display link on view if (variable_get('views_mail_view_link', 0) == 1) { if ($view->tag == 'views_mail') { $view->display_handler->set_option('footer', 'name; $link = views_mail_create_link($viewname); drupal_set_message($link); ?>'); $view->display_handler->set_option('footer_format', '3'); $view->display_handler->set_option('footer_empty', 0); } } } /** * Build link to views_mail configure view. * * @param string: $viewname Name of the view * * @return link to views_mail configure view */ function views_mail_create_link($viewname) { // We got the view name. Now, get the $query (exposed filters). // We only need to return a $query if there is a '&' present. $query = $_SERVER['QUERY_STRING']; if (preg_match('/\&/', $query)) { //Remove the 'q=' and only return what's after the & $args = preg_replace('/^q=([^&]*)&(.*)$/', '\2', $query); } else { $args = ""; } // If og is installed and this has a group context if (module_exists('og')) { if ($group = og_get_group_context()) { if ($args) { $args = $args.'&gids[]='.$group->gid; } else { $args = $args.'?gids[]='.$group->gid; } } } $link = l(t('Configure Views Mail mailing'), "views_mail/config/$viewname", array('query' => "$args")); return $link; }