View source
<?php
function webform_digest_periods() {
return array(
'day' => t('Daily'),
'week' => t('Weekly'),
'month' => t('Monthly'),
);
}
function webform_digest_form(&$form_state, $node) {
drupal_add_js(drupal_get_path('module', 'webform_digest') . '/webform_digest.js');
drupal_set_message(t('Cron should be configured to run every hour for correct work of this module.'));
drupal_set_message(t('Digest is sent to email addresses specified in E-mails configuration tab.'));
$form = array();
$digest_conf = db_fetch_array(db_query('SELECT * FROM {webform_digest} WHERE nid=%d', array(
$node->nid,
)));
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable digest delivery.'),
'#default_value' => isset($digest_conf['enabled']) ? $digest_conf['enabled'] : 0,
'#description' => t('If checkbox is checked, digest will be sent to email addresses specified in the E-mails settings page of Wefborm.'),
);
$form['new_data'] = array(
'#type' => 'checkbox',
'#title' => t('Send only new data.'),
'#default_value' => isset($digest_conf['new_data']) ? $digest_conf['new_data'] : 0,
'#description' => t('If checkbox is checked, digest will include only new data since last email.'),
);
$periods = webform_digest_periods();
$default_period = array_shift(array_keys($periods));
$form['period'] = array(
'#type' => 'select',
'#title' => t('Frequency'),
'#default_value' => isset($digest_conf['period']) ? $digest_conf['period'] : 0,
'#options' => $periods,
'#description' => t('Choose how often digest should be sent.'),
);
$form['daily_granularity_month'] = array(
'#type' => 'select',
'#title' => t('Day of month'),
'#default_value' => isset($digest_conf['daily_granularity']) ? $digest_conf['daily_granularity'] : 1,
'#options' => array_combine(range(1, 31), range(1, 31)),
'#description' => t('Choose day of month when digest should be sent.'),
);
$form['daily_granularity_week'] = array(
'#type' => 'select',
'#title' => t('Day of week'),
'#default_value' => isset($digest_conf['daily_granularity']) ? $digest_conf['daily_granularity'] : 0,
'#options' => array(
'0' => t('Sunday'),
'1' => t('Monday'),
'2' => t('Tuesday'),
'3' => t('Wednesday'),
'4' => t('Thursday'),
'5' => t('Friday'),
'6' => t('Saturday'),
),
'#description' => t('Choose day of week when digest should be sent.'),
);
$form['hourly_granularity'] = array(
'#type' => 'select',
'#title' => t('Hour (24 hour clock)'),
'#default_value' => isset($digest_conf['hourly_granularity']) ? $digest_conf['hourly_granularity'] : 0,
'#options' => range(0, 23),
'#description' => t('Choose time when digest should be sent (24 hour clock).'),
);
$form['sent'] = array(
'#type' => 'value',
'#value' => $digest_conf['sent'],
);
$download_form_state = unserialize($digest_conf['settings']);
module_load_include('inc', 'webform', 'includes/webform.report');
$download_form = call_user_func_array('drupal_retrieve_form', array(
'webform_results_download_form',
&$download_form_state,
$node,
));
array_walk($download_form, '_webform_digest_set_defualt_value', $download_form_state);
$download_form_state['values']['node'] = $node;
$form = array_merge($form, $download_form);
$form['submit']['#value'] = t('Save configuration');
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
);
$form['test'] = array(
'#type' => 'submit',
'#value' => t('Test delivery'),
);
return $form;
}
function webform_digest_form_submit($form, &$form_state) {
if ($form_state['values']['op'] == t('Save configuration')) {
$digest_conf = new stdClass();
$digest_conf->nid = $form_state['values']['node']->nid;
if (isset($form_state['values']['daily_granularity_' . $form_state['values']['period']])) {
$digest_conf->daily_granularity = $form_state['values']['daily_granularity_' . $form_state['values']['period']];
}
$fields = array(
'enabled',
'new_data',
'period',
'sent',
'hourly_granularity',
);
foreach ($fields as $field) {
$digest_conf->{$field} = $form_state['values'][$field];
}
$fields = array(
'enabled',
'new_data',
'period',
'sent',
'hourly_granularity',
'daily_granularity_week',
'daily_granularity_month',
'node',
'reset',
'test',
);
foreach ($fields as $field) {
unset($form_state['values'][$field]);
unset($form_state['clicked_button']['#post'][$field]);
}
foreach ($form_state['values']['components'] as $key => $value) {
if (empty($value)) {
unset($form_state['values']['components'][$key]);
}
}
$digest_conf->settings = serialize($form_state);
db_query('DELETE FROM {webform_digest} WHERE nid=%d', array(
$digest_conf->nid,
));
drupal_write_record('webform_digest', $digest_conf);
}
elseif ($form_state['values']['op'] == t('Reset')) {
db_query('DELETE FROM {webform_digest} WHERE nid=%d', array(
$form_state['values']['node']->nid,
));
}
elseif ($form_state['values']['op'] == t('Test delivery')) {
_webform_digest_send_digest($form_state['values']['node']->nid);
}
}
function _webform_digest_set_defualt_value(&$item, $key, &$form_state) {
if (is_array($item) && isset($item['#type']) && isset($form_state['values'][$key])) {
$default_value = $form_state['values'][$key];
$item['#default_value'] = $default_value;
}
elseif (is_array($item)) {
array_walk($item, '_webform_digest_set_defualt_value', $form_state);
}
}
function _webform_digest_send_digest($nid) {
module_load_include('inc', 'webform', 'includes/webform.report');
$GLOBALS['webform_digest_send'] = true;
$node = node_load($nid, NULL, TRUE);
$digest_conf = db_fetch_array(db_query('SELECT * FROM {webform_digest} WHERE nid=%d', array(
$node->nid,
)));
$download_form_state = unserialize($digest_conf['settings']);
$download_form_state['values']['node'] = $node;
$exporters = webform_export_fetch_definition();
$format = $download_form_state['values']['format'];
$exporter = $exporters[$format]['handler'];
$extender = "\n class {$exporter}_digest extends {$exporter} {\n function __construct(\$options) {\n parent::__construct(\$options);\n // Save global variables, which acts as arguments.\n \$this->options = \$options;\n \$this->nid = \$GLOBALS['webform_digest_nid'];\n \$this->map = \$GLOBALS['webform_digest_map'];\n \$this->sent = \$GLOBALS['webform_digest_sent'];\n // Get all the submissions for the node.\n \$this->submissions = webform_get_submissions(\$this->nid);\n // Save index of submissions id\n if (\$sid = array_search('sid', \$this->options['components'])) {\n \$this->sid_index = \$sid-1;\n }\n }\n function add_row(&\$file_handle, \$data) {\n // Check if sid is assigned.\n if (isset(\$this->sid_index)) {\n // Check if row contains data.\n // In case if it is header, following condition returns false.\n if (isset(\$this->submissions[\$data[\$this->sid_index]])) {\n // Include only new data\n if (\$this->sent && \$this->submissions[\$data[\$this->sid_index]]->submitted < \$this->sent) {\n return;\n }\n // Filter with mapped fields\n if (!empty(\$this->map)) {\n // Set return flag to true\n \$return = true;\n // Find matching between mapping and mapped components values.\n // In case if it matches set return flag to false.\n foreach (\$this->map as \$cid => \$values) {\n if (is_array(\$this->submissions[\$data[\$this->sid_index]]->data[\$cid]['value'])) {\n //dpm(\$this->submissions[\$data[\$this->sid_index]]->data[\$cid]['value']);\n \$match = array_intersect(\n \$this->submissions[\$data[\$this->sid_index]]->data[\$cid]['value'],\n \$values\n );\n if (!empty(\$match)) {\n \$return = false;\n break;\n }\n }\n }\n if (\$return) return;\n }\n // Update row counter.\n \$GLOBALS['webform_digest_count']++;\n }\n } else {\n // Update row counter.\n \$GLOBALS['webform_digest_count']++;\n }\n // Call parent function.\n parent::add_row(&\$file_handle, \$data);\n }\n }\n ";
if (!class_exists($exporter . '_digest')) {
eval($extender);
}
$download_form_state['values']['format'] = $format . '_digest';
$emails = array();
$maps = array();
$emails_conf = $node->webform['emails'];
foreach ($emails_conf as $key => $email) {
if (valid_email_address($email['email'])) {
$emails[$key] = $email['email'];
}
elseif (is_numeric($email['email']) && $node->webform['components'][$email['email']]['type'] == 'mapping') {
$items = explode("\n", $node->webform['components'][$email['email']]['extra']['items']);
foreach ($items as $item) {
$item_arr = explode('|', $item);
$address = trim($item_arr[1]);
if (!valid_email_address($address)) {
continue;
}
$cid = $node->webform['components'][$email['email']]['extra']['mapped_component'];
$mapped_value = trim($item_arr[0]);
$maps[$key][$address][$cid][] = $mapped_value;
}
}
elseif (is_numeric($email['email']) && $node->webform['components'][$email['email']]['type'] == 'select') {
$items = explode("\n", $node->webform['components'][$email['email']]['extra']['items']);
foreach ($items as $item) {
$item_arr = explode('|', $item);
$address = trim($item_arr[0]);
if (!valid_email_address($address)) {
continue;
}
$cid = $node->webform['components'][$email['email']]['cid'];
$maps[$key][$address][$cid][] = $address;
}
}
}
$GLOBALS['webform_digest_nid'] = $node->nid;
if ($digest_conf['new_data'] && $digest_conf['sent']) {
$GLOBALS['webform_digest_sent'] = $digest_conf['sent'];
}
foreach ($emails as $key => $email) {
_webform_digest_send_digest_email(array(
$email,
), $emails_conf, $key, $download_form_state, $node);
}
foreach ($maps as $key => $data) {
foreach ($data as $email => $map) {
$GLOBALS['webform_digest_map'] = $map;
$GLOBALS['webform_digest_count'] = 0;
_webform_digest_send_digest_email(array(
$email,
), $emails_conf, $key, $download_form_state, $node);
}
}
unset($GLOBALS['webform_digest_count']);
unset($GLOBALS['webform_digest_map']);
unset($GLOBALS['webform_digest_sent']);
unset($GLOBALS['webform_digest_nid']);
unset($GLOBALS['webform_digest_send']);
node_load($nid, NULL, TRUE);
}
function _webform_digest_send_digest_email($emails, &$emails_conf, $key, $download_form_state, $node) {
$file_content = '';
if (ob_start()) {
drupal_execute('webform_results_download_form', $download_form_state, $node);
$file_content = ob_get_contents();
ob_end_clean();
}
if (empty($GLOBALS['webform_digest_count'])) {
return;
}
$headers = array();
$headers_arr_str = split("[\n;]", drupal_get_headers());
foreach ($headers_arr_str as $header_str) {
$header = split("[=:]", $header_str);
$headers[trim($header[0])] = trim($header[1]);
}
$sender = array();
if (!($emails_conf[$key]['from_address'] == 'default' || is_numeric($emails_conf[$key]['from_address']))) {
$sender['mail'] = $emails_conf[$key]['from_address'];
}
if (!($emails_conf[$key]['from_name'] == 'default' || is_numeric($emails_conf[$key]['from_name']))) {
$sender['name'] = $emails_conf[$key]['from_name'];
}
else {
$sender['name'] = _webform_filter_values(webform_variable_get('webform_default_from_name'), $node);
}
if ($emails_conf[$key]['subject'] == 'default' || is_numeric($emails_conf[$key]['subject'])) {
$subject = 'Webform digest: ' . $node->title;
}
else {
$subject = $emails_conf[$key]['subject'];
}
$message = 'Webform digest is in attachment.';
$file = array(
'filename' => $headers['filename'],
'filemime' => $headers['content-type'],
'filecontent' => $file_content,
);
mimemail($sender, $emails, $subject, $message, true, array(), NULL, array(
$file,
), 'webform_digest');
}