Files
booking/booking.emails_admin.inc
Nathan Coad c5179b059e test
2018-05-22 10:36:38 +10:00

963 lines
44 KiB
PHP

<?php
/**
* Function to allow admin user to define contents of workflow emails that can be sent by Bookings module
*
* @param
* @return form render array
*/
function booking_emails_workflow_admin() {
global $event;
$form = array();
//some initial workflow email definitions
$default_email_text = "Dear [booking:fname],\n" .
"Congratulations, you are officially booked into [booking:eventname]. " .
"Your registration reference number is [booking:booking-id].\n" .
"Please don't hesitate to contact us if you have any queries by replying to this email. " .
"We look forward to seeing you (God Willing) at [booking:eventname]!\n" .
"Love in Jesus,\n[booking:eventname] Registrations Team\n" .
"________________________________________________________\n" .
"The following information shows the details you entered when you registered. " .
"If any of this information is incorrect, please reply to this email with the corrections as soon as possible.\n" .
"________________________________________________________";
$booking_email_regn_complete_text = "Dear [booking:fname],\n" .
"Thank-you for completing your payment for [booking:eventname].\n" .
"Please don't hesitate to contact us if you have any queries by replying to this email. " .
"We look forward to seeing you (God Willing) at [booking:eventname]!\n" .
"Love in Jesus,\n[booking:eventname] Registrations Team\n" .
"________________________________________________________\n" .
"The following information shows the details you entered when you registered. " .
"If any of this information is incorrect, please reply to this email with the corrections as soon as possible.\n" .
"________________________________________________________";
$booking_email_waitinglist_text = "Dear [booking:fname],\n" .
"Thank-you for registering to attend [booking:eventname]. Unfortunately the venue for this event is full, and you have been placed on the waiting list to attend.\n" .
"We will contact you if a position becomes available for you. Please don't hesitate to contact us if you have any queries by replying to this email.\n" .
"Love in Jesus,\n[booking:eventname] Registrations Team\n" .
"________________________________________________________\n" .
"The following information shows the details you entered when you registered. " .
"If any of this information is incorrect, please reply to this email with the corrections as soon as possible.\n" .
"________________________________________________________";
$booking_email_paymentoutstanding_text = "Dear [booking:fname],\n" .
"Thank-you for registering to attend [booking:eventname]. Our records indicate that you currently have $[booking:payment-required] outstanding to finalise your registration.\n" .
"Please visit [booking:balance-payment-link] in order to make your final payment.\n" .
"Love in Jesus,\n[booking:eventname] Registrations Team\n" .
"________________________________________________________\n";
$booking_email_waitinglistpromotion = "Dear [booking:fname],\n" .
"We have some great news for you. A place at [booking:eventname] for you has just become available. " .
"If you wish to secure your place at [booking:eventname], please visit [booking:balance-payment-link] to make your final payment. " .
"Our records indicate that you currently have $[booking:paypal-total-amount] outstanding (including Paypal transaction fees).\n" .
"Once we have received your payment, you will be sent an automatic confirmation email thanking you for paying your outstanding fees. " .
"If you are paying via Paypal's eCheque feature, please be aware that payments take 3-5 working days to clear, " .
"and you will not receive the confirmation email until that has occurred.\n" .
"Please don't hesitate to contact us if you have any queries by replying to this email. We look forward to seeing you, God Willing, at [booking:eventname]\n" .
"Love in Jesus,\n[booking:eventname] Registrations Team.";
$booking_email_waitinglistpromotion_fullypaid = "Dear [booking:fname],\n" .
"We have some great news for you. A place at [booking:eventname] for you has just become available. " .
"Our records indicate that you have already fully paid, so there is nothing further needed to secure your spot.\n" .
"Please don't hesitate to contact us if you have any queries by replying to this email. We look forward to seeing you, God Willing, at [booking:eventname]\n" .
"Love in Jesus,\n[booking:eventname] Registrations Team.";
$booking_email_notcoming_demotion = "";
$booking_email_paymentoutstanding_married_text = "";
if(variable_get('booking_enable_html_mail', 0) == 1) {
$form_type = 'text_format';
$form_format = 'full_html';
}
else {
$form_type = 'textarea';
$form_format = NULL;
}
//include the token definitions
$form['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => array('booking'),
);
/*Text for emails*/
$form['emails'] = array(
'#type' => 'fieldset',
'#title' => 'Built-In Workflow Email Definitions',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
//if HTML mail is enabled, allow user to specify footer
if(variable_get('booking_enable_html_mail', 0) == 1) {
$form['emails']['booking_html_mail_footer'] = array(
'#title' => t('Footer text for HTML email'),
'#description' => t('Enter HTML to go in the footer of the email template (above the social media icons).<br /><strong>NOTE:</strong>&nbsp;No token subsitutions will be made here.'),
'#default_value' => isset(variable_get('booking_html_mail_footer')['value']) ? variable_get('booking_html_mail_footer')['value'] : variable_get('booking_html_mail_footer', '<p>&nbsp;</p>'),
'#type' => 'text_format',
'#format' => 'full_html',
);
}
$form['emails']['booking_email_notification_text'] = array(
'#title' => t('Notification Email'),
'#description' => t('Email to send to the notification email address (defined in general configuration) when a person has registered'),
'#default_value' => isset(variable_get('booking_email_notification_text')['value']) ? variable_get('booking_email_notification_text')['value'] : variable_get('booking_email_notification_text', '[booking:regn-summary]'),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_bookedin_text'] = array(
'#title' => t('Registration Successful Email'),
'#description' => t('Text to use in an email indicating the person has booked in, paid their deposit and is not on the waiting list'),
'#default_value' => isset(variable_get('booking_email_bookedin_text')['value']) ? variable_get('booking_email_bookedin_text')['value'] : variable_get('booking_email_bookedin_text', $default_email_text),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_regn_complete_text'] = array(
'#title' => t('Payment Complete Email'),
'#description' => t('Email text to indicate a person has booked in, fully paid and is not on the waiting list. ' .
'This will be sent either at initial registration if the full amount is paid, or when the balance is paid.'),
'#default_value' => isset(variable_get('booking_email_regn_complete_text')['value']) ? variable_get('booking_email_regn_complete_text')['value'] : variable_get('booking_email_regn_complete_text', $booking_email_regn_complete_text),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_waitinglist_text'] = array(
'#title' => t('Registration on Waiting List Email'),
'#description' => t('Email text to indicate a person has registered but is on the waiting list. ' .
'This will be sent instead of the Registration Successful or Registration Complete emails if the person is on the waiting list.'),
'#default_value' => isset(variable_get('booking_email_waitinglist_text')['value']) ? variable_get('booking_email_waitinglist_text')['value'] : variable_get('booking_email_waitinglist_text', $booking_email_waitinglist_text),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_partialpayment_received_text'] = array(
'#title' => t('Email text to send a person thanking them for their partial payment'),
'#description' => t(''),
'#default_value' => isset(variable_get('booking_email_partialpayment_received_text')['value']) ? variable_get('booking_email_partialpayment_received_text')['value'] : variable_get('booking_email_partialpayment_received_text', ''),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_paymentoutstanding_subject'] = array (
'#type' => 'textfield',
'#title' => t('Balance Outstanding Email Subject'),
'#description' => t('Subject line for email advising attendee they missed the payment deadline'),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get('booking_email_paymentoutstanding_subject','[booking:eventname] payment required'),
);
$form['emails']['booking_email_paymentoutstanding_text'] = array(
'#description' => t('Email text to send a person reminding them of how much they owe'),
'#title' => t('Balance Outstanding Email Text'),
'#default_value' => isset(variable_get('booking_email_paymentoutstanding_text')['value']) ? variable_get('booking_email_paymentoutstanding_text')['value'] : variable_get('booking_email_paymentoutstanding_text', $booking_email_paymentoutstanding_text),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_paymentoutstanding_married_text'] = array(
'#title' => t('Email text to send a married couple reminding them of how much they both owe (only applies when combined pricing enabled)'),
'#description' => t(''),
'#default_value' => isset(variable_get('booking_email_paymentoutstanding_married_text')['value']) ? variable_get('booking_email_paymentoutstanding_married_text')['value'] : variable_get('booking_email_paymentoutstanding_married_text', $booking_email_paymentoutstanding_married_text),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_waitinglistpromotion'] = array(
'#title' => t('Email text to send a person on the waiting list when a spot opens up for them but they have an outstanding balance.'),
'#description' => t(''),
'#default_value' => isset(variable_get('booking_email_waitinglistpromotion')['value']) ? variable_get('booking_email_waitinglistpromotion')['value'] : variable_get('booking_email_waitinglistpromotion', $booking_email_waitinglistpromotion),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_waitinglistpromotion_fullypaid'] = array(
'#title' => t('Email text to send a person on the waiting list when a spot opens up for them and they have already fully paid.'),
'#description' => t(''),
'#default_value' => isset(variable_get('booking_email_waitinglistpromotion_fullypaid')['value']) ?
variable_get('booking_email_waitinglistpromotion_fullypaid')['value'] :
variable_get('booking_email_waitinglistpromotion_fullypaid', $booking_email_waitinglistpromotion_fullypaid),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_notcoming_demotion_subject'] = array (
'#type' => 'textfield',
'#title' => t('Withdrawal Processed Email'),
'#description' => t('Subject line for email advising attendee their withdrawal has been processed'),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get('booking_email_notcoming_demotion_subject','[booking:eventname] withdrawal processed'),
);
$form['emails']['booking_email_notcoming_demotion'] = array(
'#title' => t('Email text to send a person who withdraws their registration'),
'#description' => t(''),
'#default_value' => isset(variable_get('booking_email_notcoming_demotion')['value']) ? variable_get('booking_email_notcoming_demotion')['value'] : variable_get('booking_email_notcoming_demotion', $booking_email_notcoming_demotion),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_missedpayment_subject'] = array (
'#type' => 'textfield',
'#title' => t('Missed Payment Email Subject'),
'#description' => t('Subject line for email advising attendee they missed the payment deadline'),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get('booking_email_missedpayment_subject','[booking:eventname] payment deadline missed'),
);
$form['emails']['booking_email_missedpayment'] = array(
'#title' => t('Missed Payment Email Text'),
'#description' => t('Email text to send a person who missed the payment deadline'),
'#default_value' => isset(variable_get('booking_email_missedpayment')['value']) ? variable_get('booking_email_missedpayment')['value'] : variable_get('booking_email_missedpayment', ''),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_travel_initial_email_subject'] = array (
'#type' => 'textfield',
'#title' => t('Initial Travel Form Required Subject'),
'#description' => t('Subject line for first email requesting attendee to complete the travel form'),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get('booking_email_travel_initial_email_subject','[booking:eventname] Travel Details Required'),
);
$form['emails']['booking_email_travel_initial_email_text'] = array(
'#title' => t('Initial Travel Form Required Text'),
'#description' => t('Text for first email requesting attendee to complete the travel form. This email will be sent from the !email email address', array('!email' => variable_get('booking_logistics_email'))),
'#default_value' => isset(variable_get('booking_email_travel_initial_email_text')['value']) ? variable_get('booking_email_travel_initial_email_text')['value'] : variable_get('booking_email_travel_initial_email_text', ''),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_travel_reminder_email_subject'] = array (
'#type' => 'textfield',
'#title' => t('Reminder Travel Form Required Subject'),
'#description' => t('Subject line for reminder email requesting attendee to complete the travel form'),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get('booking_email_travel_reminder_email_subject','[booking:eventname] Travel Details Required'),
);
$form['emails']['booking_email_travel_reminder_email_text'] = array(
'#title' => t('Reminder Travel Form Required Text'),
'#description' => t('Text for reminder email requesting attendee to complete the travel form. This email will be sent from the !email email address', array('!email' => variable_get('booking_logistics_email'))),
'#default_value' => isset(variable_get('booking_email_travel_reminder_email_text')['value']) ? variable_get('booking_email_travel_reminder_email_text')['value'] : variable_get('booking_email_travel_reminder_email_text', ''),
'#type' => $form_type,
'#format' => $form_format,
);
$form['emails']['booking_email_travel_complete_subject'] = array (
'#type' => 'textfield',
'#title' => t('Travel Form Complete Subject'),
'#description' => t('Subject line for email indicating a person has completed the travel form'),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get('booking_email_travel_complete_subject','[booking:eventname] Travel Details Received'),
);
$form['emails']['booking_email_travel_complete_text'] = array(
'#title' => t('Travel Form Complete Text'),
'#description' => t('Email text to indicate a person has completed the travel form. This email will be sent from the !email email address', array('!email' => variable_get('booking_logistics_email'))),
'#default_value' => isset(variable_get('booking_email_travel_complete_text')['value']) ? variable_get('booking_email_travel_complete_text')['value'] : variable_get('booking_email_travel_complete_text', ''),
'#type' => $form_type,
'#format' => $form_format,
);
//let the builtin hook do all the hard work
return system_settings_form($form, FALSE);
}
// TODO : Use ajax to select email definition instead of drawing so many WYSIWYG text editors which take ages to load
/**
* Hook form() to use ajax to allow admin user to define custom emails that can be sent by Bookings module
*
* @param
* @return form render array
*/
function booking_emails_custom_ajax_form($node, &$form_state) {
global $event;
$form = array();
$data = $node;
//$email_options_array = _booking_custom_email_types();
//add in the custom email types
for ($i = 1; $i <= variable_get('booking_custom_email_count','5'); $i++) {
$email_options_array['custom' . $i] = variable_get('booking_email_subject_custom' . $i, $event->booking_eventname . ' custom ' . $i);
}
//add in the custom email types from logistics
for ($i = 1; $i <= 5; $i++) {
$email_options_array['customlogistics' . $i] = variable_get('booking_email_subject_customlogistics' . $i,
$event->booking_eventname . ' logistics custom ' . $i) . " **Logistics**";
}
if(variable_get('booking_enable_html_mail', 0) == 1) {
$form_type = 'text_format';
$form_format = 'full_html';
}
else {
$form_type = 'textarea';
$form_format = NULL;
}
$form['email-type'] = array(
'#type' => 'select',
'#title' => t('Email Type'),
'#required' => TRUE,
'#default_value' => 'NULL',
'#options' => $email_options_array,
'#ajax' => array(
'event' => 'change',
'callback' => 'booking_emails_custom_ajax_form_callback',
'wrapper' => 'booking_emails_custom_fieldset_wrapper',
),
);
$form['email-definition'] = array(
'#type' => 'fieldset',
'#title' => 'Email Definition',
'#prefix' => '<div id="booking_emails_custom_fieldset_wrapper">',
'#suffix' => '</div>'
);
$form['email-definition']['booking_email_subjectline_custom'] = array (
'#type' => 'textfield',
'#title' => t('Subject line for Custom Email'),
'#size' => 150,
'#maxlength' => 300,
//'#value' => isset($form_state['values']['booking_email_subjectline_custom']) ? $form_state['values']['booking_email_subjectline_custom'] : '',
);
$form['email-definition']['booking_email_body_custom'] = array(
'#title' => t('Email text for custom email'),
'#description' => t(''),
//'#value' => isset($form_state['values']['booking_email_body_custom']) ? $form_state['values']['booking_email_body_custom'] : '',
'#type' => $form_type,
'#format' => $form_format,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return array (
'form' => $form,
);
}
/**
* Callback function to display the custom email definition for form booking_emails_custom_ajax_form
*/
function booking_emails_custom_ajax_form_callback($form, &$form_state) {
global $event;
$text = "";
$data = $form_state['input'];
//check the selected email type
//configure the input text field and textarea based on select field
$emailtype = $form_state['input']['email-type'];
if (strpos($emailtype, 'custom') !== false) {
$subject = variable_get('booking_email_subject_' . $emailtype, '');
$form['form']['email-definition']['booking_email_subjectline_custom']['#value'] = $subject;
$text = variable_get('booking_email_' . $emailtype, '');
$text = isset($text['format']) ? $text['value'] : $text;
watchdog('booking_debug', 'custom text: <pre>@info</pre>', array('@info' => print_r( $text, true)));
$form['form']['email-definition']['booking_email_body_custom']['#default_value'] = $text;
}
watchdog('booking_debug', 'booking_emails_custom_ajax_form_callback: <pre>@info</pre>', array('@info' => print_r( $form, true)));
// Rebuild the form
$form_state['rebuild'] = TRUE;
return $form['form']['email-definition'];
}
/**
* Function to allow admin user to define custom emails that can be sent by Bookings module
*
* @param
* @return form render array
*/
function booking_emails_custom_admin() {
global $event;
$form = array();
if(variable_get('booking_enable_html_mail', 0) == 1) {
$form_type = 'text_format';
$form_format = 'full_html';
}
else {
$form_type = 'textarea';
$form_format = NULL;
}
//include the token definitions
$form['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => array('booking'),
);
/*Text for emails*/
$form['custom-emails'] = array(
'#type' => 'fieldset',
'#title' => 'Custom Email Text Definitions',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
//add a bunch of custom emails
for ($i = 1; $i <= variable_get('booking_custom_email_count','5'); $i++) {
$subject_fieldname = 'booking_email_subject_custom' . $i;
$body_fieldname = 'booking_email_custom' . $i;
$form['custom-emails'][$subject_fieldname] = array (
'#type' => 'textfield',
'#title' => t('Subject line for Custom Email ' . $i),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get($subject_fieldname,'[booking:eventname]'),
);
$form['custom-emails'][$body_fieldname] = array(
'#title' => t('Email text for custom email ' . $i),
'#description' => t(''),
'#default_value' => isset(variable_get($body_fieldname)['value']) ? variable_get($body_fieldname)['value'] : variable_get($body_fieldname, ''),
'#type' => $form_type,
'#format' => $form_format,
);
}
//let the builtin hook do all the hard work
return system_settings_form($form, FALSE);
}
/**
* Function to allow admin user to define custom logistics emails that can be sent by Bookings module, from the logistics email address
*
* @param
* @return form render array
*/
function booking_emails_customlogistics_admin() {
global $event;
$form = array();
if(variable_get('booking_enable_html_mail', 0) == 1) {
$form_type = 'text_format';
$form_format = 'full_html';
}
else {
$form_type = 'textarea';
$form_format = NULL;
}
//include the token definitions
$form['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => array('booking'),
);
/*Text for logistics emails*/
$form['custom-logistics-emails'] = array(
'#type' => 'fieldset',
'#title' => 'Custom Logistics Email Text Definitions',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
//add a bunch of custom emails
//@todo add an admin variable for this
for ($i = 1; $i <= 5; $i++) {
$subject_fieldname = 'booking_email_subject_customlogistics' . $i;
$body_fieldname = 'booking_email_customlogistics' . $i;
$form['custom-logistics-emails'][$subject_fieldname] = array (
'#type' => 'textfield',
'#title' => t('Subject line for Logistics Custom Email ' . $i),
'#size' => 150,
'#maxlength' => 300,
'#default_value' => variable_get($subject_fieldname,'[booking:eventname] Logistics'),
);
$form['custom-logistics-emails'][$body_fieldname] = array(
'#title' => t('Email text for Logistics Custom Email ' . $i),
'#description' => t(''),
'#default_value' => isset(variable_get($body_fieldname)['value']) ? variable_get($body_fieldname)['value'] : variable_get($body_fieldname, ''),
'#type' => $form_type,
'#format' => $form_format,
);
}
//let the builtin hook do all the hard work
return system_settings_form($form, FALSE);
}
/**
* Function to allow admin user to manually generate and send a workflow or custom email
*
* @param
* @return form render array
*/
function booking_manual_email_form($form, &$form_state)
{
global $event;
//see http://www.jaypan.com/blog/themeing-drupal-7-forms-tables-checkboxes-or-radios
/*
create hidden form element that we will populate with JSON data via ajax
create select element defining the pre-selections available
create submit button that calls ajax to update hidden form element, then calls ajax to check the checkboxes based on the hidden form element
info on turning JSON data back to array
http://stackoverflow.com/questions/29076219/javascript-storing-array-of-objects-in-hidden-field
ajax references:
http://www.maged.me/blog/drupal-7-execute-javascript-code-after-ajax-call
https://www.drupal.org/node/1028410
https://www.drupal.org/node/2046693
jquery to check the checkboxes:
http://jsfiddle.net/nanoquantumtech/dnhwz/
*/
$form = array();
$options = array();
$preselection_options = array();
$values = array();
$group_text = "";
$prefix = t("<p>Send a manual email to people registered for this event.</p>");
$email_options_array = array();
$email_options_array['NULL'] = "---";
$email_options_array['registration'] = 'Registration Successful Email';
$email_options_array['balance'] = 'Balance Outstanding Email';
$email_options_array['complete'] = 'Payment Complete Email';
$email_options_array['withdrawal'] = 'Withdrawal Processed Email';
$email_options_array['waitinglistpromotion'] = 'Position Available (Balance outstanding)';
$email_options_array['waitinglistpromotionfullypaid'] = 'Position Available (Fully Paid)';
$email_options_array['missedpayment'] = 'Missed Payment Email';
$email_options_array['initialtravelrequired'] = 'Initial Travel Form Required Email';
$email_options_array['remindertravelrequired'] = 'Reminder Travel Form Required Email';
$email_options_array['travelcomplete'] = 'Travel Form Complete Email';
$preselection_options['---'] = "---";
$preselection_options['notpaid'] = 'People who have not yet paid';
$preselection_options['bookedin'] = 'People who are booked-in';
$preselection_options['waitinglist'] = 'People on waiting list';
$preselection_options['unpaid'] = 'People booked-in with outstanding balance';
$preselection_options['refundreqd'] = 'Refund Required';
$preselection_options['notravelform'] = "People booked-in no travel form received";
$preselection_options['notravelforminclwaiting'] = "People booked-in/waiting no travel form received";
$preselection_options['leaderhelper'] = "Leaders/Helpers";
$header = array(
'booking_nid' => array('data' => t('Id'), 'field' => 'nid', 'sort' => 'asc'),
'booking_name' => array('data' => t('Name'), 'field' => 'booking_lastname'),
'booking_gender' => array('data' => t('Gender'), 'field' => 'booking_gender'),
'booking_email' => array('data' => t('Email'), 'field' => 'booking_email'),
'booking_state' => array('data' => t('State'), 'field' => 'booking_state'),
'booking_status' => array('data' => t('Status'), 'field' => 'booking_status'),
'amount_paid' => array('data' => t('Payment To Date'), 'field' => 'booking_amount_paid'),
'amount_reqd' => array('data' => t('Total Payment Required'), 'field' => 'booking_total_pay_reqd'),
'booking_fully_paid' => array('data' => t('Fully paid?'), 'field' => 'booking_payment_complete'),
'welfare_required' => array('data' => t('Welfare Required?'), 'field' => 'booking_welfare_required'),
);
//add in the custom email types
for ($i = 1; $i <= variable_get('booking_custom_email_count','5'); $i++)
{
$email_options_array['custom' . $i] = variable_get('booking_email_subject_custom' . $i, $event->booking_eventname . ' custom ' . $i);
}
//add in the custom email types from logistics
for ($i = 1; $i <= 5; $i++)
{
$email_options_array['customlogistics' . $i] = variable_get('booking_email_subject_customlogistics' . $i,
$event->booking_eventname . ' logistics custom ' . $i) . " **Logistics**";
}
//attach css so we can customise colours for some people
$form['#attached']['css'] = array(
drupal_get_path('module', 'booking') . '/booking.css',
);
//attach js for dynamically setting checkboxes
$form['#attached']['js'][] = drupal_get_path("module", "booking")."/booking.js";
$form['pretext'] = array(
'#type' => 'container',
'#children' => $prefix,
);
//@todo add booking prefix
$form['email-type'] = array(
'#type' => 'select',
'#title' => t('Email Type'),
'#required' => TRUE,
'#default_value' => 'NULL',
'#options' => $email_options_array,
'#ajax' => array(
'event' => 'change',
'callback' => '_booking_email_get_preview_callback',
'wrapper' => 'booking_email_preview_wrapper',
),
);
$form['booking-email-default-ids'] = array (
'#type' => 'hidden',
'#value' => "[]",
'#prefix' => '<div id="booking_email_default_ids_wrapper">',
'#suffix' => '</div>',
'#attributes' => array('id' => 'booking_email_default_ids'),
);
$form['booking-email-preselection'] = array(
'#type' => 'select',
'#title' => t('Pre-select attendees from list'),
'#description' => t('Use this to select a large group of people based on pre-defined criteria.'),
'#options' => $preselection_options,
'#default_value' => '---',
'#suffix' => '<div id="booking_email_preselection_suffix_wrapper"></div>',
'#ajax' => array(
'event' => 'change',
'callback' => '_booking_email_get_default_selection_callback',
'wrapper' => 'booking_email_default_ids_wrapper',
),
);
$form['booking-email-preview'] = array(
'#type' => 'container',
'#children' => "<h3>Email Preview</h3><pre>No email type selected.</pre>",
'#prefix' => '<div id="booking_email_preview_wrapper">',
'#suffix' => '</div>',
);
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Send Email'),
);
if (variable_get('booking_enable_travelform', 0) == 1) {
$header['travel_form'] = array('data' => t('Travel Submitted?'), 'field' => 'tid');
}
//query the database for studygroup info if it is enabled
if (variable_get('booking_enable_studygroups', 0) == 1)
{
//select entries from the study groups mapping table
$query = db_select('booking_studygroup_mapping', 'm');
$query->join('booking_studygroup_list', 's', 's.sid = m.booking_studygroup_id');
$query->condition('m.booking_eventid', $event->eid, '=');
$query->fields('m')->fields('s', array('booking_studygroup_descrip'));
$group_mapping = $query->execute()->fetchAllAssoc('sid');
//add a column to the table header
$header['group_roles'] = array('data' => t('Group Role'));
}
//query for attendee data
$query = db_select('booking_person', 'p');
$query->join('booking_price', 'pr', 'pr.pid = p.booking_payment_id');
$query->leftJoin('booking_travel', 't', 'p.nid = t.booking_person_nid');
$query->fields('p')
->fields('pr', array('booking_price', 'booking_late_price'))
->fields('t')
->condition('p.booking_eventid', $event->eid, '=');
//make the result user-sortable
$table_sort = $query->extend('TableSort')->orderbyHeader($header);
$result = $table_sort->execute();
//generate the tableselect data
foreach($result as $data) {
$group_text = "";
$class = $data->booking_welfare_required == 'Y' ? "welfare-row" : "normal-row";
$options[$data->nid] = array (
'#attributes' => array('class' => array($class)),
'booking_nid' => l(t('!id', array('!id' => $data->nid)), t('node/!id', array('!id' => $data->nid))),
'booking_name' => $data->booking_firstname . " " . $data->booking_lastname,
'booking_gender' => $data->booking_gender == 'M' ? 'Male' : 'Female',
'booking_email' => $data->booking_email,
'booking_state' => $data->booking_state,
'booking_status' => _booking_status_generate($data->booking_status),
'amount_paid' => $data->booking_amount_paid,
'amount_reqd' => $data->booking_total_pay_reqd,
'booking_fully_paid' => $data->booking_payment_complete == 'Y' ? 'Yes' : 'No',
'welfare_required' => $data->booking_welfare_required == 'Y' ? 'Yes' : 'No',
);
//add optional columns if those features are enabled
if (variable_get('booking_enable_travelform', 0) == 1) {
$options[$data->nid]['travel_form'] = $data->tid > 0 ? 'Yes' : 'No';
}
if (variable_get('booking_enable_studygroups', 0) == 1) {
foreach ($group_mapping as $group) {
$role = $group->booking_studygroup_role;
if ($group->booking_node_id == $data->nid && $role > 0) {
$text = _booking_studygroup_role_lookup($role);
$group_text .= "<b>" . $text . "</b> for " . $group->booking_studygroup_descrip . " #" . $group->booking_session_id . "; ";
}
}
$options[$data->nid]['group_roles'] = $group_text;
}
}
$form['table'] = array (
'#type' => 'tableselect',
//'#multiple' => TRUE,
'#header' => $header,
'#options' => $options,
'#default_value' => $values,
'#empty' => t('No attendees found.'),
'#attributes' => array('id' => 'sort-table'),
);
//watchdog('booking_debug', "<pre>Manual email form default values\n@info</pre>", array('@info' => print_r( $values, true)));
//watchdog('booking_debug', "<pre>Manual email form table\n@info</pre>", array('@info' => print_r( $form['table'], true)));
$form['submit-bottom'] = array (
'#type' => 'submit',
'#value' => t('Send Email'),
);
return array (
/*
'first_para' => array (
'#type' => 'markup',
'#markup' => $prefix,
),
*/
'form' => $form,
);
}
/**
* Callback to update preview of email that will be sent
*
*/
function _booking_email_get_preview_callback($form, $form_state) {
global $event;
$text = "";
$emailtype = $form_state['values']['email-type'];
switch ($emailtype) {
case "registration":
$text = variable_get('booking_email_bookedin_text');
break;
case "balance":
$text = variable_get('booking_email_paymentoutstanding_text');
// perform the checking of HTML value now since we're pre-pending text to it
$text = isset($text['format']) ? $text['value'] : $text;
if (variable_get('booking_enable_combined_pricing', 0) == 1) {
// no <pre> tags necessary if its a HTML mail
if(variable_get('booking_enable_html_mail', 0) == 1) {
$text .= "\n<h3>If married</h3>\n";
}
else {
$text .= "\n</pre><h3>If married</h3>\n<pre>";
}
$married_text = variable_get('booking_email_paymentoutstanding_married_text');
// repeat HTML check for rest of preview
$text .= isset($married_text['format']) ? $married_text['value'] : $married_text;
}
break;
case "complete":
$text = variable_get('booking_email_regn_complete_text');
break;
case "withdrawal":
$text = variable_get('booking_email_notcoming_demotion');
break;
case "missedpayment":
$text = variable_get('booking_email_missedpayment');
break;
case "initialtravelrequired":
$text = variable_get('booking_email_travel_initial_email_text');
break;
case "remindertravelrequired":
$text = variable_get('booking_email_travel_reminder_email_text');
break;
case "travelcomplete":
$text = variable_get('booking_email_travel_complete_text');
break;
case "waitinglistpromotion":
$text = variable_get('booking_email_waitinglistpromotion');
break;
case "waitinglistpromotionfullypaid":
$text = variable_get('booking_email_waitinglistpromotion_fullypaid');
break;
}
//handle custom email types
if (strpos($emailtype, 'custom') !== false) {
$text = variable_get('booking_email_' . $form_state['values']['email-type']);
}
//use the html value if it is set, otherwise use the plaintext input which is just a string
$text = isset($text['format']) ? $text['value'] : $text;
//watchdog('booking_debug', "<pre>Email generation:\n@info</pre>", array('@info' => print_r( $text, true)));
if(variable_get('booking_enable_html_mail', 0) == 1) {
$markup_pre = "<div id=\"booking_email_preview_wrapper\"><h3>Email Preview</h3>";
$markup_post = "<br /><br /></div>";
}
else {
$markup_pre = "<div id=\"booking_email_preview_wrapper\"><h3>Email Preview</h3><pre>";
$markup_post = "</pre><br /><br /></div>";
}
//generate html that will be used to update div booking_email_preview_wrapper
return $markup_pre . $text . $markup_post;
}
/**
* Callback to update hidden form value with default checkbox details
*
* Callback element needs only select the portion of the form to be updated.
* Since #ajax['callback'] return can be HTML or a renderable array (or an
* array of commands), we can just return a piece of the form.
* Source: http://www.maged.me/blog/drupal-7-execute-javascript-code-after-ajax-call
*/
function _booking_email_get_default_selection_callback($form, $form_state) {
global $event;
$defaults = array();
$replacementform_data = array();
//get type of selection from $form_state['values']['booking-email-preselection']
$selection = $form_state['values']['booking-email-preselection'];
//load nodes
$people = booking_load_query(NULL, TRUE);
//populate $defaults based on type of selection
foreach ($people as $person) {
switch ($selection) {
case 'notpaid':
if ($person->booking_status == 0) {
$defaults[] = $person->nid;
}
break;
case 'bookedin':
if ($person->booking_status == 1) {
$defaults[] = $person->nid;
}
break;
case 'waitinglist':
if ($person->booking_status == 2) {
$defaults[] = $person->nid;
}
break;
case 'refundreqd':
if ($person->booking_status == 3 && $person->booking_refund_processed == 'N') {
$defaults[] = $person->nid;
}
break;
case 'unpaid':
if ($person->booking_payment_complete == 'N' && $person->booking_status == 1) {
$defaults[] = $person->nid;
}
break;
case 'notravelform':
if ($person->booking_status == 1 && $person->tid == 0) {
$defaults[] = $person->nid;
}
break;
case 'notravelforminclwaiting':
if (($person->booking_status == 1 || $person->booking_status == 2) && $person->tid == 0) {
$defaults[] = $person->nid;
}
break;
case 'leaderhelper':
foreach ($person as $key => $value) {
if (preg_match('/^session(\d+)_role/', $key, $matches) && $value > 0) {
//don't add the person multiple times if they're leading/helping multiple groups
if (! in_array($person->nid, $defaults)) {
$defaults[] = $person->nid;
}
}
}
break;
} //end switch
} //loop attendees
//create new hidden form element to return
$replacementform_data['booking-email-default-ids'] = array (
'#type' => 'hidden',
'#value' => drupal_json_encode($defaults), //encode the data as json so we can process it with jQuery
'#prefix' => '<div id="booking_email_default_ids_wrapper">',
'#suffix' => '</div>',
'#attributes' => array('id' => 'booking_email_default_ids'),
);
$output_html = render($replacementform_data['booking-email-default-ids']);
$feedback_html = t("<div id=\"booking_email_preselection_suffix_wrapper\">Number of people selected: <b>!num</b></div>",
array('!num' => count($defaults)));
//watchdog('booking_debug', "<pre>Manual Email ajax html\n@info</pre>", array('@info' => print_r( $output_html, true)));
//return a sequence of commands to run
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#booking_email_default_ids_wrapper", $output_html),
ajax_command_replace("#booking_email_preselection_suffix_wrapper", $feedback_html),
// This will call the command bookingEmailIDs in our custom JS file.
array('command' => 'bookingAjaxCheckboxes',
'formDataElement' => 'booking_email_default_ids', //name of the hidden form element that contains the json data
'formName' => 'booking-manual-email-form', //note the dashes instead of underscores!
'checkboxName' => 'table', //form element for tableselect
),
)
);
}
/**
* Function to handle sending the manual emails
*/
function booking_manual_email_form_submit($form, &$form_state) {
$counter = 0;
$update_messages = array();
$checkboxes = $form_state['values']['table']; //$values['booking_price_active'];
//watchdog('booking', 'Formstate when setting buttons: @info', array ('@info' => var_export($form_state['values'], TRUE)));
//watchdog('booking', 'Checkboxes when setting buttons: @info', array ('@info' => var_export($checkboxes, TRUE)));
foreach($checkboxes as $key => $value)
{
$message = "";
//do a sanity check on the key => value pair from the form submission
if (is_numeric($key) && $value != 0)
{
//check the person exists in the database
$person = db_query("SELECT person.nid " .
"FROM {booking_person} person " .
"WHERE nid = :nid",
array(':nid' => $key))
->fetchObject();
if ($person)
{
if ($form_state['values']['email-type'] == 'registration') {
$message = t('Processing a manual registration email to id @info', array ('@info' => $key));
_booking_registration_email($key, false, true);
}
elseif ($form_state['values']['email-type'] == 'initialtravelrequired') {
$message = t('Processing a manual initial travel form request email to id @info', array ('@info' => $key));
_booking_travelform_initial_request_email($key);
}
elseif ($form_state['values']['email-type'] == 'remindertravelrequired') {
$message = t('Processing a manual reminder travel form request email to id @info', array ('@info' => $key));
_booking_travelform_reminder_request_email($key);
}
elseif ($form_state['values']['email-type'] == 'balance') {
$message = t('Processing a manual outstanding balance email to id @info', array ('@info' => $key));
_booking_balance_payment_email($key);
}
elseif ($form_state['values']['email-type'] == 'complete') {
$message = t('Processing a manual registration complete email to id @info', array ('@info' => $key));
_booking_registration_email($key, true, true);
}
elseif ($form_state['values']['email-type'] == 'travelcomplete') {
$message = t('Processing a manual travelform complete email to id @info', array ('@info' => $key));
_booking_travelform_confirmation_email($key);
}
elseif ($form_state['values']['email-type'] == 'withdrawal') {
$message = t('Processing a manual withdrawal email to id @info', array ('@info' => $key));
_booking_demoted_to_notcoming_email($key);
}
elseif ($form_state['values']['email-type'] == 'missedpayment') {
$message = t('Processing a manual missedpayment email to id @info', array ('@info' => $key));
_booking_missedpayment_email($key);
}
elseif ($form_state['values']['email-type'] == 'waitinglistpromotion') {
$message = t('Processing a manual waitinglistpromotion email to id @info', array ('@info' => $key));
_booking_promoted_from_waitinglist_email($key);
}
elseif ($form_state['values']['email-type'] == 'waitinglistpromotionfullypaid') {
$message = t('Processing a manual waitinglistpromotionfullypaid email to id @info', array ('@info' => $key));
_booking_promoted_from_waitinglist_email($key);
}
elseif (strpos($form_state['values']['email-type'], 'customlogistics') !== false) {
$message = t('Processing a @custom type email from logistics to id @info', array ('@custom' => $form_state['values']['email-type'], '@info' => $key));
_booking_custom_email($key, $form_state['values']['email-type'], 'logistics');
}
elseif (strpos($form_state['values']['email-type'], 'custom') !== false) {
$message = t('Processing a @custom type email to id @info', array ('@custom' => $form_state['values']['email-type'], '@info' => $key));
_booking_custom_email($key, $form_state['values']['email-type'], 'contact');
}
//increase the counter of people we've emailed
$counter++;
//store info about the email
$update_messages[] = $message;
}
}
}
$final_message = "Sent manual email for $counter people of type " . $form_state['values']['email-type'];
drupal_set_message($final_message, 'status', FALSE);
watchdog('booking', "<pre>" . $final_message . "\n" . implode("\n", $update_messages) . "</pre>");
//watchdog('booking', "Sent manual email for $counter people.");
}