Commit 139429d3 authored by metzlerd's avatar metzlerd
Browse files

#1148134 Implementing email merge support in forena.

parent ecbbe8ef
......@@ -668,14 +668,22 @@ function forena_layout_form_submit($form, &$form_state) {
*/
function forena_doc_formats_settings() {
$supported_doctypes = forena_supported_doctypes();
$form['forena_doc_formats'] = array(
'#type' => 'checkboxes',
'#title' => t('Document Formats'),
'#title' => t('Allowed Document Formats'),
'#default_value' => variable_get('forena_doc_formats', $supported_doctypes),
'#description' => t('check your desired document format'),
'#options' => $supported_doctypes,
);
$form['forena_doc_defaults'] = array(
'#type' => 'checkboxes',
'#title' => t('Default Document Formats'),
'#default_value' => variable_get('forena_doc_defaults', $supported_doctypes),
'#description' => t('check your desired document format'),
'#options' => $supported_doctypes,
);
return system_settings_form($form);
}
......
......@@ -752,7 +752,7 @@ function forena_get_doctypes($fkey) {
$provider = $r;
if ($provider && method_exists($provider, 'doc_types')) {
$f = $provider->doc_types();
if ($f[$fkey] && method_exists($provider, $f[$fkey])) {
if (isset($f[$fkey]) && method_exists($provider, $f[$fkey])) {
// We found an object with the advertised method return it
return $provider;
}
......@@ -858,6 +858,7 @@ function forena_generate_doc($format, $output, $options = array()) {
header('Cache-Control:');
header('Pragma:');
header("Cache-Control: must-revalidate");
print $ret;
break;
case 'pdf':
header('Content-Type: application/pdf');
......@@ -865,6 +866,7 @@ function forena_generate_doc($format, $output, $options = array()) {
header('Pragma:');
header("Cache-Control: must-revalidate");
header("Content-Disposition: attachment; filename=report.pdf");
print $ret;
break;
case 'xls':
......@@ -873,6 +875,7 @@ function forena_generate_doc($format, $output, $options = array()) {
header('Pragma:');
header("Cache-Control: must-revalidate");
header("Content-Disposition: attachment; filename=report.xls");
print $ret;
break;
case 'csv':
header('Content-Type: application/csv');
......@@ -880,14 +883,17 @@ function forena_generate_doc($format, $output, $options = array()) {
header('Pragma:');
header("Cache-Control: must-revalidate");
header("Content-Disposition: attachment; filename=report.csv");
print $ret;
break;
default:
return $ret;
}
// Print the output.
print $ret;
}
else {
print $output;
return $ret;
}
}
......@@ -939,3 +945,34 @@ function forena_my_reports_block() {
$output .= '</ul>';
return $output;
}
/**
* Email confirmation form. Confirms an email send based on mail merge
* @param array $docs An array of SimpleXML email documents to send
* @param integer $count Number of documents to send.
*/
function forena_confirm_email($form, &$form_state, $docs, $count) {
if ($docs) {
$form_state['storage']['docs'] = $docs;
$form_state['storage']['count'] = $count;
}
return confirm_form($form, t('Send mail to users'), 'forena', t('Send email to %count users?', array('%count' => $count)));
}
function forena_confirm_email_submit($form, &$form_state) {
foreach($form_state['storage']['docs'] as $doc) {
$from = $doc->xpath('.//*[@class="email-header-from"]');
$from = $from ? (string)$from[0] : '';
$subject = $doc->xpath('.//*[@class="email-header-subject"]');
$subject = $subject ? (string)$subject[0] : '';
$to = $doc->xpath('.//*[@class="email-header-to"]');
$to = $to ? (string)$to[0] : '';
$body = $doc->xpath('.//*[@class="email-body"]');
$body = $body ? $body[0]->asXML() : $body;
$parms = array(
'body' => $body,
'subject' => $subject,
);
drupal_mail('forena', 'mailmerge', $to, language_default(), $parms, $from, TRUE);
}
}
......@@ -42,6 +42,14 @@ function forena_menu() {
'file' => 'forena.common.inc',
);
$items['report_doc/%'] = array(
'page callback' => 'forena_report',
'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'forena.common.inc',
);
$items['reports/%/view'] = array(
'title' => 'View',
......@@ -301,6 +309,7 @@ function forena_permission() {
'create any report' => array('title' => t('Create a report')),
'design any report' => array('title' => t('Design reports')),
'delete report' => array('title' => t('Delete reports')),
'perform email merge' => array('title' => t('Peform email merge')),
);
//@TODO: Add the ability to create subrepositories with different permissions.
return $perms;
......@@ -389,6 +398,8 @@ function forena_report($name_in, $parms = array()) {
if ($format) { //a format was requested
$header = '<h1>' . $title . '</h1>';
$output = $header . $output;
$css_text = '';
$r_text = '';
if ($css_files) foreach ($css_files as $css_file) {
$css_text .= file_get_contents($css_file);
}
......@@ -398,7 +409,8 @@ function forena_report($name_in, $parms = array()) {
'title' => $title[0],
);
forena_generate_doc($format, $output, $options);
$output = forena_generate_doc($format, $output, $options);
return $output;
}
else {
// We've got parameters so display the parameters form
......@@ -421,12 +433,13 @@ function forena_report($name_in, $parms = array()) {
$rpt_xml = $r->rpt_xml;
$nodes = $rpt_xml->xpath('//frx:docgen/frx:doc');
$div = '<div class="doclinks">';
$default_doctypes = variable_get('forena_doc_formats', array());
$default_doctypes = variable_get('forena_doc_defaults', array());
$base = base_path();
if (!$missing_parms) {
if (!$nodes) {
//show the default. All supported links
foreach ($default_doctypes as $value) if (is_object(forena_get_doctypes($value))) {
$div .= '<a class="doclinks" href="' . $name_in . '.' . $value . '?' . $q . '">' . strtoupper($value) . '</a>';
$div .= '<a class="doclinks" href="' . $base . '/report_doc/' . $name_in . '.' . $value . '?' . $q . '">' . strtoupper($value) . '</a>';
}
}
else {
......@@ -437,7 +450,7 @@ function forena_report($name_in, $parms = array()) {
$type = (string)$arr['type'];
if ($doctypes[$type]) {
if (is_object(forena_get_doctypes($type))) {
$div .= '<a class="doclinks" href="' . $name_in . '.' . $type . '?' . $q . '">' . strtoupper($type) . '</a>';
$div .= '<a class="doclinks" href="' . $base . '/report_doc/' . $name_in . '.' . $type . '?' . $q . '">' . strtoupper($type) . '</a>';
}
}
}
......@@ -465,7 +478,6 @@ function forena_report($name_in, $parms = array()) {
}
}
/**
* Render report with some data
*
......@@ -481,12 +493,9 @@ function forena_render_report($report, $format='', $data='', $options= array())
//If a format was requested render a custom non-drupal document
if ($format) {
forena_generate_doc($format, $output, $options);
}
else {
return $output;
$output = forena_generate_doc($format, $output, $options);
}
return $output;
}
/**
......@@ -616,3 +625,18 @@ function _forena_filter_process($text='') {
// If we didn't find anything return the text.
return $text;
}
/**
* Impementation of hook_mail
* Builds an email to send when mailing large numbers of users.
*/
function forena_mail($key, &$message, $parms) {
switch ($key) {
case 'mailmerge':
$message['subject'] = $parms['subject'];
$body = $parms['body'];
$message['body'][] = $body;
$htmlmail = FALSE;
break;
}
}
\ No newline at end of file
......@@ -28,6 +28,7 @@ class FrxControls {
'html' => 'htmldocument',
'csv' => 'csvdocument',
'xls' => 'xlsdocument',
'email' => 'emaildocument',
);
return $doc_types;
}
......@@ -141,7 +142,6 @@ class FrxControls {
}
public function xlsdocument($body, $options) {
$output .= '<?xml version="1.0"?>' . "\n";
$output .= '<?mso-application progid="Excel.Sheet"?>' . "\n";
......@@ -207,6 +207,22 @@ class FrxControls {
return $output;
}
public function emaildocument($body, $options) {
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML($body);
$xml = simplexml_import_dom($doc);
$docs = $xml->xpath('.//*[@class="email-document"]');
$count = count($docs);
if ($count) {
$form = drupal_get_form('forena_confirm_email', $docs, $count);
$output = drupal_render($form);
} else {
$output = $body;
}
return $output;
}
//template methods
public function table($columns, $data_block, $clause='') {
if ($clause) $clause = 'frx:clause ="' . $clause . '"';
......
--ACCESS=access administration pages
SELECT name, mail, login, status FROM users
WHERE status=1 order by name
\ No newline at end of file
<?xml version="1.0"?>
<html xmlns:frx="urn:FrxReports">
<head>
<title>Active Users </title>
<frx:category>Drupal Administration</frx:category>
<frx:options hidden="0" form=""/>
<frx:parameters>
</frx:parameters>
<frx:docgen>
</frx:docgen>
<frx:fields>
<frx:field id="name" link="user/{uid}/edit"/>
</frx:fields>
</head>
<body>
<div frx:block="drupal/active_users" id="forena-1"><table id="forena-2">
<thead>
<tr>
<td>User</td>
<td>Email</td>
<td>Last Logged in</td>
</tr>
</thead>
<tbody>
<tr frx:foreach="*">
<td>{name}</td>
<td>{mail}</td>
<td>{login}</td>
</tr>
</tbody>
</table>
</div>
</body></html>
<?xml version="1.0"?>
<html xmlns:frx="urn:FrxReports"><head>
<html xmlns:frx="urn:FrxReports">
<head>
<title>Roles </title>
<frx:category>Drupal Administration</frx:category>
<frx:options hidden="0" form=""/>
<frx:parameters>
<frx:parm id="role" label="" require="0" value="" desc="" data_source="drupal/roles" data_field="" type="select"/></frx:parameters>
<frx:parm id="role" label="" require="0" value="" desc="" data_source="drupal/roles" data_field="" type="select"/>
</frx:parameters>
<frx:docgen>
</frx:docgen>
<frx:fields>
<frx:field id="name" link="user/{uid}/edit"/>
</frx:fields>
</head><body><body>
</head>
<body>
<div frx:block="drupal/roles" id="forena-1" frx:clause="where rid=:role"><table frx:foreach="*" id="forena-2"><tbody><tr><th>rid</th><td>{rid}</td></tr><tr><th>name</th><td>{name}</td></tr></tbody></table></div>
<div id="forena-3" frx:block="drupal/users_in_role"><table><thead><tr><th>Members</th></tr></thead><tbody><tr id="forena-4" frx:foreach="*"><td>{name}</td></tr></tbody></table></div><div id="forena-5" frx:block="drupal/role_permissions"><table><thead><tr><th>Permissions</th></tr></thead><tbody><tr id="forena-6" frx:foreach="*"><td>{permission}</td></tr></tbody></table></div>
</body></body></html>
</body></html>
<?xml version="1.0"?>
<html xmlns:frx="urn:FrxReports">
<head>
<title>Active Users Email</title>
<frx:category>Drupal Administration</frx:category>
<frx:options hidden="0" form=""/>
<frx:parameters>
</frx:parameters>
<frx:docgen>
<frx:doc type="email"/>
</frx:docgen>
<frx:fields>
</frx:fields>
</head>
<body>
<div frx:block="drupal/active_users" id="forena-1" frx:foreach="*">
<div class="email-document">
<div class="email-header">
<table>
<tbody>
<tr>
<th>From</th>
<td class="email-header-from">metzlerd@evergreen.edu</td>
</tr>
<tr>
<th>To</th>
<td class="email-header-to">{mail}</td>
</tr>
<tr>
<th>Subject</th>
<td class="email-header-subject">Sample Active User Email</td>
</tr>
</tbody>
</table>
</div>
<div class="email-body">
<p>Dear {name}</p>
<p>This is a sample letter that demonstrates the ability to send email to all active users of a drupal site.
Copy or save this report as a different name, prior to customizing it or the report will be overwritten
during future upgrades to forena.
</p>
</div>
</div>
</div>
</body></html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment