View source
<?php
require_once 'webform_confirm_email.inc';
function webform_confirm_email_webform_component_delete($component) {
$nid = (int) $component['nid'];
$cid = (int) $component['cid'];
if (!$nid || !$cid) {
return;
}
$results = db_query('SELECT eid ' . ' FROM {webform_emails} ' . ' WHERE nid = :nid ' . ' AND email = :email ', array(
':nid' => $nid,
':email' => $cid,
));
foreach ($results as $wfemail) {
if (empty($wfemail->eid)) {
continue;
}
$eid = (int) $wfemail->eid;
db_delete('webform_confirm_email')
->condition('nid', $nid)
->condition('eid', $eid)
->execute();
db_delete('webform_emails')
->condition('nid', $nid)
->condition('eid', $eid)
->execute();
}
}
function webform_confirm_email_confirmation_request_email_edit($node, $email = array()) {
$form_id = 'webform_email_edit_form';
$form_state = array(
'build_info' => array(
'args' => array(
$node,
$email,
),
),
'submit_handlers' => array(
'webform_email_edit_form_submit',
'_webform_confirm_email_edit_confirmation_request_email_submit',
),
);
$form_state += form_state_defaults();
if (!isset($form_state['input'])) {
$form_state['input'] = $form_state['method'] == 'get' ? $_GET : $_POST;
}
$form = drupal_retrieve_form($form_id, $form_state);
$redirect_url = NULL;
if (!empty($email['eid'])) {
$redirect_url = db_query('SELECT redirect_url ' . ' FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' AND eid = :eid ', array(
':nid' => $node->nid,
':eid' => $email['eid'],
))
->fetchField();
}
$form['redirect_url_option'] = array(
'#type' => 'radios',
'#title' => t('Set the redirect URL'),
'#description' => t('Choose between default and custom redirect URL'),
'#default_value' => $redirect_url ? 'custom' : 'default',
);
$form['redirect_url_option']['#options']['default'] = t('Default redirect URL from the webform: %value', array(
'%value' => $node->webform['redirect_url'],
));
$form['redirect_url_option']['#options']['custom'] = t('Custom redirect URL');
$form['redirect_url_custom'] = array(
'#type' => 'textfield',
'#default_value' => $redirect_url ? $redirect_url : NULL,
);
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
$form['redirect_url_custom']['#attributes']['class'] = array(
'webform-set-active',
);
$form['redirect_url_option']['custom']['#theme_wrappers'] = array(
'webform_inline_radio',
);
$form['redirect_url_option']['custom']['#inline_element'] = drupal_render($form['redirect_url_custom']);
$form['redirect_url_option']['default']['#theme_wrappers'] = array(
'webform_inline_radio',
);
$instructions = $form['template']['tokens']['#markup'];
$search = '<p>' . t('You may use special tokens in this field that will be replaced with dynamic values.') . '</p>';
$confirmation_tokens = array(
t('@confirm_url - URL that is emailed to a submitee that will confirm his/her email address.', array(
'@confirm_url' => '%confirm_url',
)),
);
$replace = $search . theme('item_list', array(
'items' => $confirmation_tokens,
'title' => t('Email confirmation variables'),
));
$form['template']['tokens']['#markup'] = str_replace($search, $replace, $instructions);
return $form;
}
function _webform_confirm_email_edit_confirmation_request_email_submit($form, &$form_state) {
if (isset($form_state['values']['eid']) == TRUE && isset($form['node']['#value']->nid) == TRUE) {
$obj['nid'] = $form['node']['#value']->nid;
$obj['email_type'] = WEBFORM_CONFIRM_EMAIL_CONFIRMATION_REQUEST;
$obj['redirect_url'] = $form_state['values']['redirect_url_option'] === 'custom' ? $form_state['values']['redirect_url_custom'] : NULL;
if (empty($form['eid']['#value']) == TRUE) {
$obj['eid'] = $form_state['values']['eid'];
drupal_write_record('webform_confirm_email', $obj);
}
else {
$obj['eid'] = $form['eid']['#value'];
drupal_write_record('webform_confirm_email', $obj, array(
'nid',
'eid',
));
}
}
}
function webform_confirm_email_email_delete($form, &$form_state) {
$eid = $form_state['triggering_element']['#eid'];
if ($form_state['triggering_element']['#email_type_form'] == 'confirmation_request') {
if (_webform_confirm_email_verify_email_setup($form['#node']->nid, 'delete_confirmation_request_email', $eid) == FALSE) {
drupal_set_message(t('Deleting the only confirmation request email but still having some confirmation email(s) leads to unwanted behavior. Please delete all confirmation email(s) first.'), 'error');
form_error($form['confirmation_request'][$eid]);
return FALSE;
}
}
$form_state['redirect'] = array(
'node/' . $form['#node']->nid . '/webform/' . $form_state['triggering_element']['#email_type_form'] . '/' . $eid . '/delete',
);
}
function webform_confirm_email_delete($node, $email = array()) {
$form_state = array(
'build_info' => array(
'args' => array(
$node,
$email,
),
),
'submit_handlers' => array(
'webform_email_delete_form_submit',
'_webform_confirm_email_delete_submit',
),
);
$form = drupal_build_form('webform_email_delete_form', $form_state);
return $form;
}
function _webform_confirm_email_delete_submit($form, &$form_state) {
$node = $form_state['values']['node'];
$email = $form_state['values']['email'];
if (isset($email['eid']) == TRUE && isset($node->nid) == TRUE) {
db_query('DELETE FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' AND eid = :eid ', array(
':nid' => $node->nid,
':eid' => $email['eid'],
));
}
}
function webform_confirm_email_confirmation_email_edit($node, $email = array()) {
$form_state = array(
'build_info' => array(
'args' => array(
$node,
$email,
),
),
'submit_handlers' => array(
'webform_email_edit_form_submit',
'_webform_confirm_email_edit_confirmation_email_submit',
),
);
$form = drupal_build_form('webform_email_edit_form', $form_state);
$form['#theme'] = array(
'webform_email_edit_form',
);
return $form;
}
function _webform_confirm_email_edit_confirmation_email_submit($form, &$form_state) {
if (isset($form_state['values']['eid']) == TRUE && isset($form['node']['#value']->nid) == TRUE) {
$obj['eid'] = $form_state['values']['eid'];
$obj['nid'] = $form['node']['#value']->nid;
$obj['email_type'] = WEBFORM_CONFIRM_EMAIL_CONFIRMATION;
if (empty($form['eid']['#value']) == TRUE) {
$obj['eid'] = $form_state['values']['eid'];
drupal_write_record('webform_confirm_email', $obj);
}
else {
$obj['eid'] = $form['eid']['#value'];
drupal_write_record('webform_confirm_email', $obj, array(
'nid',
'eid',
));
}
}
}
function webform_confirm_email_webform_emails_form_validate(&$form, &$form_state) {
foreach (array(
'email_option',
'email_component',
) as $index) {
if (isset($form_state['values'][$index]) == FALSE) {
$form_state['values'][$index] = NULL;
}
}
}
function webform_confirm_email_form_webform_emails_form_alter(&$form, &$form_state) {
$eids = db_query('SELECT we.eid ' . ' FROM {webform_emails} we ' . ' LEFT JOIN {webform_confirm_email} wce ' . ' ON we.nid = wce.nid ' . ' AND we.eid = wce.eid ' . ' WHERE we.nid = :nid ' . ' AND wce.eid IS NULL ', array(
':nid' => (int) $form['#node']->nid,
))
->fetchCol();
foreach ($eids as $eid) {
$form['emails'][$eid]['delete_button'] = array(
'#type' => 'submit',
'#name' => 'delete_emails_' . $eid,
'#value' => t('Delete'),
'#email_type_form' => 'emails',
'#eid' => $eid,
'#submit' => array(
'webform_confirm_email_email_delete',
),
);
}
unset($form['add']);
$form['emails']['add_button'] = $form['add_button'];
unset($form['add_button']);
$form['emails']['add_button']['#value'] = t('Add standard email');
$form['emails']['add_button']['#submit'] = array(
'webform_emails_form_submit',
);
array_unshift($form['#validate'], 'webform_confirm_email_webform_emails_form_validate');
$query = db_query('SELECT eid, redirect_url ' . ' FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' AND email_type = :type', array(
':nid' => (int) $form['#node']->nid,
':type' => WEBFORM_CONFIRM_EMAIL_CONFIRMATION_REQUEST,
))
->fetchAllKeyed();
$form['confirmation_request'] = array();
foreach ($query as $eid => $redirect_url) {
$form['confirmation_request'][$eid]['email'] = $form['emails'][$eid]['email'];
$form['confirmation_request'][$eid]['subject'] = $form['emails'][$eid]['subject'];
$form['confirmation_request'][$eid]['from'] = $form['emails'][$eid]['from'];
$form['confirmation_request'][$eid]['redirect_url'] = array(
'#markup' => $redirect_url == NULL ? 'default' : $redirect_url,
);
$form['confirmation_request'][$eid]['delete_button'] = array(
'#type' => 'submit',
'#name' => 'delete_confirmation_request_' . $eid,
'#value' => t('Delete'),
'#email_type_form' => 'confirmation_request',
'#eid' => $eid,
'#submit' => array(
'webform_confirm_email_email_delete',
),
);
unset($form['emails'][$eid]);
}
$form['confirmation_request']['add_button'] = array(
'#type' => 'submit',
'#value' => t('Add confirmation request mail'),
'#submit' => array(
'webform_confirm_email_confirmation_request_email_add',
),
'#weight' => 45,
);
$eids = db_query('SELECT eid ' . ' FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' AND email_type = :type ', array(
':nid' => (int) $form['#node']->nid,
':type' => WEBFORM_CONFIRM_EMAIL_CONFIRMATION,
))
->fetchCol();
$form['confirmation'] = array();
foreach ($eids as $eid) {
$form['confirmation'][$eid]['email'] = $form['emails'][$eid]['email'];
$form['confirmation'][$eid]['subject'] = $form['emails'][$eid]['subject'];
$form['confirmation'][$eid]['from'] = $form['emails'][$eid]['from'];
$form['confirmation'][$eid]['delete_button'] = array(
'#type' => 'submit',
'#name' => 'delete_confirmation_' . $eid,
'#value' => t('Delete'),
'#email_type_form' => 'confirmation',
'#eid' => $eid,
'#submit' => array(
'webform_confirm_email_email_delete',
),
);
unset($form['emails'][$eid]);
}
$form['confirmation']['add_button'] = array(
'#type' => 'submit',
'#value' => t('Add confirmation mail'),
'#submit' => array(
'webform_confirm_email_confirmation_email_add',
),
'#weight' => 45,
);
}
function webform_confirm_email_confirmation_request_email_add($form, &$form_state) {
$form_state['redirect'] = array(
'node/' . $form['#node']->nid . '/webform/confirmation_request/new',
);
}
function _webform_confirm_email_verify_email_setup($nid, $operation = 'add_confirmation_email', $eid = NULL) {
$confirmation_request_emails = db_query('SELECT eid ' . ' FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' AND email_type = :type ', array(
':nid' => (int) $nid,
':type' => WEBFORM_CONFIRM_EMAIL_CONFIRMATION_REQUEST,
))
->fetchCol();
switch ($operation) {
case 'add_confirmation_email':
return $confirmation_request_emails == FALSE ? FALSE : TRUE;
break;
case 'delete_confirmation_request_email':
if (count($confirmation_request_emails) == 1) {
$confirmation_emails_exist = db_query('SELECT eid ' . ' FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' AND email_type = :type ', array(
':nid' => (int) $nid,
':type' => WEBFORM_CONFIRM_EMAIL_CONFIRMATION,
))
->fetchCol();
return $confirmation_emails_exist ? FALSE : TRUE;
}
break;
}
return TRUE;
}
function webform_confirm_email_confirmation_email_add($form, &$form_state) {
if (_webform_confirm_email_verify_email_setup($form['#node']->nid) == FALSE) {
drupal_set_message(t('Adding a confirmation email without having at least one confirmation request email leads to unwanted behavior. Please create a confirmation request email first.'), 'error');
return FALSE;
}
$form_state['redirect'] = array(
'node/' . $form['#node']->nid . '/webform/confirmation/new',
);
}
function webform_confirm_email_theme_registry_alter(&$theme_registry) {
$theme_registry['webform_emails_form']['function'] = 'theme_webform_confirm_email_emails_form';
$theme_registry['webform_email_add_form']['function'] = 'theme_webform_confirm_email_email_add_form';
}
function theme_webform_confirm_email_emails_form($variables) {
$form =& $variables['form'];
$node =& $form['#node'];
$header = array(
t('E-mail to'),
t('Subject'),
t('From'),
array(
'data' => t('Operations'),
'colspan' => 2,
),
);
$header_confirmation_request = array(
t('E-mail to'),
t('Subject'),
t('From'),
t('Redirect URL'),
array(
'data' => t('Operations'),
'colspan' => 2,
),
);
$output = '';
foreach (array(
'emails' => 'Standard emails (always send)',
'confirmation_request' => 'Confirmation request emails (always send)',
'confirmation' => 'Confirmation emails (only send when the confirmation URL is used)',
) as $email_type => $title) {
$rows = array();
$eids = element_children($form[$email_type]);
if (array_search('add_button', $eids) !== FALSE) {
unset($eids[array_search('add_button', $eids)]);
}
if (array_search('add', $eids) !== FALSE) {
unset($eids[array_search('add', $eids)]);
}
if (count($eids) > 0) {
foreach ($eids as $eid) {
$new_row = array(
drupal_render($form[$email_type][$eid]['email']),
drupal_render($form[$email_type][$eid]['subject']),
drupal_render($form[$email_type][$eid]['from']),
);
if ($email_type == 'confirmation_request') {
$new_row[] = drupal_render($form[$email_type][$eid]['redirect_url']);
}
$new_row[] = l(t('Edit'), 'node/' . $node->nid . '/webform/' . $email_type . '/' . $eid);
$new_row[] = drupal_render($form[$email_type][$eid]['delete_button']);
$rows[] = $new_row;
}
}
else {
switch ($email_type) {
case 'emails':
$no_email_comment = t('Currently not sending standard e-mails, add a standard email by clicking the button below.');
break;
case 'confirmation_request':
$no_email_comment = t('Currently not sending confirmation request e-mails, add a confirmation request email by clicking the button below.');
break;
case 'confirmation':
$no_email_comment = t('Currently not sending confirmation e-mails, add a confirmation email by clicking the button below.');
break;
}
$rows[] = array(
array(
'data' => $no_email_comment,
'colspan' => 5,
),
);
}
$row_add_email = array(
array(
'colspan' => $email_type == 'confirmation_request' ? 4 : 3,
'data' => drupal_render($form[$email_type]['add']),
),
array(
'colspan' => 2,
'data' => drupal_render($form[$email_type]['add_button']),
),
);
$rows[] = array(
'data' => $row_add_email,
'class' => array(
'webform-add-form',
),
);
$output .= '<h2>' . $title . '</h2>';
$output .= theme('table', array(
'header' => $email_type == 'confirmation_request' ? $header_confirmation_request : $header,
'rows' => $rows,
'attributes' => array(
'id' => 'webform-' . $email_type,
),
));
}
$output .= drupal_render_children($form);
return $output;
}
function theme_webform_confirm_email_email_add_form($form) {
$email_type = $form['#array_parents'][0];
$form[$email_type . '_custom']['#attributes']['rel'] = t('email@example.com');
$form[$email_type . '_custom']['#attributes']['class'] = 'webform-set-active webform-default-value';
$form[$email_type . '_option']['custom']['#title'] = $form[$email_type . '_option']['custom']['#title'] . ': ' . drupal_render($form[$email_type . '_custom']);
$form[$email_type . '_component']['#attributes']['class'] = 'webform-set-active';
$form[$email_type . '_option']['component']['#title'] = $form[$email_type . '_option']['component']['#title'] . ': ' . drupal_render($form[$email_type . '_component']);
foreach (element_children($form[$email_type . '_option']) as $option) {
$form[$email_type . '_option'][$option]['#prefix'] = '<div class="webform-container-inline">';
$form[$email_type . '_option'][$option]['#suffix'] = '</div>';
}
return $form;
}
function webform_confirm_email_settings($form, &$form_state, $node) {
$lifetime = array(
'days' => NULL,
'hours' => NULL,
'minutes' => NULL,
);
$delete_submissions = FALSE;
if (!empty($node)) {
$form['#node'] = $node;
$result = db_query('SELECT request_lifetime, delete_submissions ' . ' FROM {webform_confirm_email} ' . ' WHERE nid = :nid ' . ' LIMIT 1 ', array(
':nid' => $node->nid,
))
->fetchObject();
if (isset($result->request_lifetime) && isset($result->delete_submissions)) {
$delete_submissions = $result->delete_submissions;
$lifetime['days'] = (int) ($result->request_lifetime / 86400);
$lifetime['hours'] = (int) ($result->request_lifetime % 86400 / 3600);
$lifetime['minutes'] = (int) ($result->request_lifetime % 86400 % 3600) / 60;
foreach (array(
'days',
'hours',
'minutes',
) as $key) {
if ($lifetime[$key] == 0) {
$lifetime[$key] = NULL;
}
}
}
}
$form['request_lifetime'] = array(
'#type' => 'fieldset',
'#title' => t('Confirmation Request Lifetime'),
'#description' => t('Specify the lifetime of confirmation requests before they get deleted automatically. That means, webform_confirm_email deletes it\'s internal data about the expired confirmation requests. To not delete any requests leave fields blank.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 0,
);
$form['request_lifetime']['lifetime-container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'clearfix',
),
),
);
$form['request_lifetime']['lifetime-container']['days'] = array(
'#type' => 'textfield',
'#title' => t('Days'),
'#maxlength' => 3,
'#size' => 3,
'#default_value' => $lifetime['days'],
'#attributes' => array(
'class' => array(
'webform-confirm-email-request-lifetime-days',
),
),
);
$form['request_lifetime']['lifetime-container']['hours'] = array(
'#type' => 'textfield',
'#title' => t('Hours'),
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $lifetime['hours'],
'#attributes' => array(
'class' => array(
'webform-confirm-email-request-lifetime-hours',
),
),
);
$form['request_lifetime']['lifetime-container']['minutes'] = array(
'#type' => 'textfield',
'#title' => t('Minutes'),
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $lifetime['minutes'],
'#attributes' => array(
'class' => array(
'webform-confirm-email-request-lifetime-minutes',
),
),
);
$form['request_lifetime']['lifetime-container']['delete_submissions'] = array(
'#type' => 'checkbox',
'#title' => t('Delete submissions'),
'#description' => t('If checked then not only webform_confirm_email\'s internal data but also the submissions themselfs will be deleted. If unchecked, only webform_confirm_email\'s internal data will be deleted.'),
'#default_value' => $delete_submissions,
);
$form['actions'] = array(
'#type' => 'actions',
'#weight' => 3,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
function webform_confirm_email_settings_validate($form, &$form_state) {
foreach (array(
'days',
'hours',
'minutes',
) as $field_name) {
if (!empty($form_state['values'][$field_name]) && preg_match('/^\\d*$/', $form_state['values'][$field_name]) != 1) {
form_error($form['request_lifetime'][$field_name], t('The entered value %value is not a number.', array(
'%value' => $form_state['values'][$field_name],
)));
}
}
if (!empty($form_state['values']['hours']) && (int) $form_state['values']['hours'] > 23) {
form_error($form['request_lifetime']['hours'], t('Hours must be smaller than 24.'));
}
if (!empty($form_state['values']['minutes']) && (int) $form_state['values']['minutes'] > 59) {
form_error($form['request_lifetime']['minutes'], t('Minutes must be smaller than 60.'));
}
}
function webform_confirm_email_settings_submit($form, &$form_state) {
if (!empty($form['#node'])) {
$lifetime = 0;
if (!empty($form_state['values']['days'])) {
$lifetime += (int) $form_state['values']['days'] * 86400;
}
if (!empty($form_state['values']['hours'])) {
$lifetime += (int) $form_state['values']['hours'] * 3600;
}
if (!empty($form_state['values']['minutes'])) {
$lifetime += (int) $form_state['values']['minutes'] * 60;
}
db_query('UPDATE {webform_confirm_email} ' . ' SET request_lifetime = :lifetime , ' . ' delete_submissions = :delete_submissions ' . ' WHERE nid = :nid ', array(
':lifetime' => $lifetime,
':nid' => $form['#node']->nid,
':delete_submissions' => $form_state['values']['delete_submissions'],
));
drupal_set_message(t('The confirmation mail settings have been updated.'));
}
}