Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev_1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Feb 15, 2017
2 parents 22067ff + 9c698a2 commit 82bd029
Show file tree
Hide file tree
Showing 17 changed files with 733 additions and 24 deletions.
23 changes: 23 additions & 0 deletions CRM/Admin/Form/Setting/DonrecSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ function buildQuickForm( ) {
ts('Packet size', array('domain' => 'de.systopia.donrec')),
CRM_Donrec_Logic_Settings::get('donrec_packet_size'));

$this->addElement('text',
'donrec_bcc_email',
ts('BCC Email', array('domain' => 'de.systopia.donrec')),
CRM_Donrec_Logic_Settings::get('donrec_bcc_email'));
$this->addRule('donrec_bcc_email', ts('Has to be a valid email address', array('domain' => 'de.systopia.donrec')), 'email');

$this->addElement('text',
'donrec_return_path_email',
ts('Email Return Path', array('domain' => 'de.systopia.donrec')),
CRM_Donrec_Logic_Settings::get('donrec_return_path_email'));
$this->addRule('donrec_return_path_email', ts('Has to be a valid email address', array('domain' => 'de.systopia.donrec')), 'email');

$this->addElement('select',
'donrec_email_template',
ts('Email Template', array('domain' => 'de.systopia.donrec')),
CRM_Donrec_Logic_Settings::getAllTemplates());


$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save', array('domain' => 'de.systopia.donrec')), 'isDefault' => TRUE),
Expand All @@ -80,6 +97,9 @@ public function setDefaultValues() {
$defaults['selected_profile'] = 'Default';
$defaults['pdfinfo_path'] = CRM_Donrec_Logic_Settings::get('donrec_pdfinfo_path');
$defaults['packet_size'] = CRM_Donrec_Logic_Settings::get('donrec_packet_size');
$defaults['donrec_email_template'] = CRM_Donrec_Logic_Settings::get('donrec_email_template');
$defaults['donrec_return_path_email'] = CRM_Donrec_Logic_Settings::get('donrec_return_path_email');
$defaults['donrec_bcc_email'] = CRM_Donrec_Logic_Settings::get('donrec_bcc_email');

return $defaults;
}
Expand All @@ -91,6 +111,9 @@ function postProcess() {

// save generic settings
CRM_Donrec_Logic_Settings::set('donrec_packet_size', $values['packet_size']);
CRM_Donrec_Logic_Settings::set('donrec_email_template', $values['donrec_email_template']);
CRM_Donrec_Logic_Settings::set('donrec_return_path_email', $values['donrec_return_path_email']);
CRM_Donrec_Logic_Settings::set('donrec_bcc_email', $values['donrec_bcc_email']);
if ($values['pdfinfo_path']) {
CRM_Donrec_Logic_Settings::set('donrec_pdfinfo_path', $values['pdfinfo_path']);
}
Expand Down
7 changes: 4 additions & 3 deletions CRM/Donrec/Exporters/BasePDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function exportSingle($snapshot_receipt, $is_test) {
return FALSE;
} else {
// save file names for wrapup()
$this->postprocessPDF($result, $snapshot_receipt->getID());
return TRUE;
return $this->postprocessPDF($result, $snapshot_receipt->getID(), $is_test);
}
}

Expand All @@ -74,6 +73,8 @@ public function exportBulk($snapshot_receipt, $is_test) {
/**
* allows the subclasses to process the newly created PDF file
*/
protected function postprocessPDF($file, $snapshot_line_id) {}
protected function postprocessPDF($file, $snapshot_line_id, $is_test) {
return TRUE;
}

}
309 changes: 309 additions & 0 deletions CRM/Donrec/Exporters/EmailPDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
<?php
/*-------------------------------------------------------+
| SYSTOPIA Donation Receipts Extension |
| Copyright (C) 2013-2016 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| License: AGPLv3, see LICENSE file |
+--------------------------------------------------------*/

//FIXME: implement own getID-method
/**
* Exporter for GROUPED, ZIPPED PDF files
*/
class CRM_Donrec_Exporters_EmailPDF extends CRM_Donrec_Exporters_BasePDF {

protected $_activity_type_id = NULL;

/**
* @return the display name
*/
static function name() {
return ts('Send PDFs via Email', array('domain' => 'de.systopia.donrec'));
}

/**
* @return a html snippet that defines the options as form elements
*/
static function htmlOptions() {
return '';
}

/**
* @return the ID of this importer class
*/
public function getID() {
return 'EMAIL';
}

/**
* check whether all requirements are met to run this exporter
*
* @return array:
* 'is_error': set if there is a fatal error
* 'message': error message
*/
public function checkRequirements() {
// Check if email template is set up
$template_id = CRM_Donrec_Logic_Settings::getEmailTemplateID();
if ($template_id) {
return array('is_error' => FALSE, 'message' => '');
} else {
return array(
'is_error' => TRUE,
'message' => ts("Please select email template in the Donrec settings.", array('domain' => 'de.systopia.donrec')),
);
}
}


/**
* allows the subclasses to process the newly created PDF file
*/
protected function postprocessPDF($file, $snapshot_line_id, $is_test) {
// find the receipt
$error = NULL;
$snapshot = CRM_Donrec_Logic_Snapshot::getSnapshotForLineID($snapshot_line_id);
if (!$snapshot) {
$error = 'snapshot error';
} else {
$receipt = $snapshot->getLine($snapshot_line_id);
if (!$receipt) {
$error = 'snapshot error';
}
}

// try to send the email
if (!$error) {
$error = $this->sendEmail($receipt, $file, $is_test, $snapshot_line_id);
}

if ($error) {
// create error activity
if (!$is_test) {
civicrm_api3('Activity', 'create', array(
'activity_type_id' => $this->getEmailErrorActivityID(),
'subject' => ts("Donation receipt not delivered", array('domain' => 'de.systopia.donrec')),
'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'),
'activity_date_time' => date('YmdHis'),
'source_contact_id' => $receipt['created_by'],
'target_id' => $receipt['contact_id'],
// 'assignee_contact_id'=> (int) $this->config->getAdminContactID(),
'details' => $this->getErrorMessage($error),
));
}

// store the error in the process information (to be processed in wrap-up)
$this->updateProcessInformation($snapshot_line_id, array('email_error' => $error));

} // END if $error
return $error == NULL;
}


/**
* Will try to send the PDF to the given email
*
* @return NULL if all good, an error message string if it FAILED
*/
protected function sendEmail($receipt, $pdf_file, $is_test, $snapshot_line_id) {
try {
// load contact data
$contact = civicrm_api3('Contact', 'getsingle', array('id' => $receipt['contact_id']));

// load email address
if (empty($contact['email'])) {
return 'no email';
}

// load the domain
list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();

// compile the attachment
$attachment = array('fullPath' => $pdf_file,
'mime_type' => 'application/pdf',
'cleanName' => ts("Donation Receipt.pdf", array('domain' => 'de.systopia.donrec')));

// register some variables
$smarty_variables = array(
'receipt' => $receipt,
'contact' => $contact);

if ($is_test) {
// in test mode: create message
$this->updateProcessInformation($snapshot_line_id, array('sent' => $contact['email']));
} else {
// in case this is required: make sure the bouce processing is temporarily changed
self::modifyBounceProcessing();

civicrm_api3('MessageTemplate', 'send', array(
'id' => CRM_Donrec_Logic_Settings::getEmailTemplateID(),
'contact_id' => $contact['id'],
'to_name' => $contact['display_name'],
'to_email' => $contact['email'],
'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
'template_params' => $smarty_variables,
'attachments' => array($attachment),
'bcc' => CRM_Donrec_Logic_Settings::get('donrec_bcc_email'),
));
}
} catch (Exception $e) {
return $e->getMessage();
}
return NULL;
}

/**
* generate the final result
*
* @return array:
* 'is_error': set if there is a fatal error
* 'log': array with keys: 'type', 'level', 'timestamp', 'message'
* 'download_url: URL to download the result
* 'download_name: suggested file name for the download
*/
public function wrapUp($snapshot_id, $is_test, $is_bulk) {
$reply = array();
$error_counters = array();

$snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id);
$ids = $snapshot->getIds();
foreach($ids as $id) {
$proc_info = $this->getProcessInformation($id);
if(!empty($proc_info['email_error'])) {
if (!isset($error_counters[$proc_info['email_error']])) {
$error_counters[$proc_info['email_error']] = 1;
} else {
$error_counters[$proc_info['email_error']] += 1;
}
}
if(!empty($proc_info['sent'])) {
$message = ts("Email <i>would</i> be sent to '%1' (test mode).", array(
1 => $proc_info['sent'],
'domain' => 'de.systopia.donrec'));
CRM_Donrec_Logic_Exporter::addLogEntry($reply, $message, CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO);
}
}

foreach ($error_counters as $error => $count) {
$error_msg = "{$count}x " . $this->getErrorMessage($error);
CRM_Donrec_Logic_Exporter::addLogEntry($reply, $error_msg, CRM_Donrec_Logic_Exporter::LOG_TYPE_ERROR);
}

// in case there was temporary changes: roll back
self::rollbackBounceProcessing();

return $reply;
}

/**
* Get the activity type id for email errors
*
* If no such activity exists, create a new one
*/
public function getEmailErrorActivityID() {
if ($this->activity_type_id === NULL) {
$this->activity_type_id = (int) CRM_Core_OptionGroup::getValue('activity_type', 'donrec_email_failed', 'name');
if (!$this->activity_type_id) {
// create new activity type
$option_group = civicrm_api3('OptionGroup', 'getsingle', array('name' => 'activity_type'));
$activity_type = civicrm_api3('OptionValue', 'create', array(
'option_group_id' => $option_group['id'],
'name' => 'donrec_email_failed',
'label' => ts("DonationReceipt Failure", array('domain' => 'de.systopia.donrec')),
));
$this->activity_type_id = $activity_type['value'];
}
}

return $this->activity_type_id;
}

/**
* Will produce a human-readable version of the given error
*/
protected function getErrorMessage($error) {
switch ($error) {

case 'snapshot error':
// no translation, as this shouldn't happen :)
return 'Internal error, problems with the snapshot. Please file a bug at https://github.com/systopia/de.systopia.donrec/issues';

case 'no email':
return ts("No valid email address found for this contact.", array('domain' => 'de.systopia.donrec'));

default:
$error_msg = ts("Error was: ", array('domain' => 'de.systopia.donrec'));
$error_msg .= $error;
return $error_msg;
}
}

/**
* If the setting donrec_return_path_email is set:
* - that email is set as the default bounce address, storing the old one
* - the task to send out newsletters will be disabled
* - the old values of the two settings above will be stored
*/
public static function modifyBounceProcessing() {
$stashed_settings = CRM_Donrec_Logic_Settings::get('donrec_email_stashed_settings');
if (!empty($stashed_settings)) return; // the settings are already manipulated

// check if somebody entered something
$custom_return_path = CRM_Donrec_Logic_Settings::get('donrec_return_path_email');
if (empty($custom_return_path)) return; // nothing to be done here

// disable running mail delivery jobs
$stashed_settings = array('disabled_jobs' => array());
$active_jobs = civicrm_api3('Job', 'get', array(
'api_entity' => 'job',
'api_action' => 'process_mailing',
'is_active' => 1));
foreach ($active_jobs['values'] as $job) {
$stashed_settings['disabled_jobs'][] = $job['id'];
civicrm_api3('Job', 'create', array(
'id' => $job['id'],
'is_active' => 0));
}

// adjust the return path
$stashed_settings['modified_return_paths'] = array();
$old_return_path = CRM_Core_DAO::executeQuery("SELECT id AS account_id, return_path FROM `civicrm_mail_settings` WHERE is_default=1;");
while ($old_return_path->fetch()) {
$stashed_settings['modified_return_paths'][$old_return_path->account_id] = $old_return_path->return_path;
CRM_Core_DAO::executeQuery("UPDATE `civicrm_mail_settings` SET return_path=%1 WHERE id=%2;", array(
1 => array($custom_return_path, 'String'),
2 => array($old_return_path->account_id, 'Integer')));
}

// stash the changes
CRM_Donrec_Logic_Settings::set('donrec_email_stashed_settings', json_encode($stashed_settings));
}

/**
* If the modifyBounceProcessing had modified the settings, this should roll it back
*/
public static function rollbackBounceProcessing() {
$stashed_settings = CRM_Donrec_Logic_Settings::get('donrec_email_stashed_settings');
if (empty($stashed_settings)) return; // no changes

// decode data
$stashed_settings = json_decode($stashed_settings, TRUE);

// re-enable the jobs
foreach ($stashed_settings['disabled_jobs'] as $job_id) {
civicrm_api3('Job', 'create', array('id' => $job_id, 'is_active' => 1));
}

// restore return paths
foreach ($stashed_settings['modified_return_paths'] as $account_id => $original_return_path) {
CRM_Core_DAO::executeQuery("UPDATE `civicrm_mail_settings` SET return_path=%1 WHERE id=%2;", array(
1 => array($original_return_path, 'String'),
2 => array($account_id, 'Integer')));
}

// clear stashed_settings
CRM_Donrec_Logic_Settings::set('donrec_email_stashed_settings', '');
}
}
4 changes: 3 additions & 1 deletion CRM/Donrec/Exporters/GroupedPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ public function checkRequirements() {
/**
* allows the subclasses to process the newly created PDF file
*/
protected function postprocessPDF($file, $snapshot_line_id) {
protected function postprocessPDF($file, $snapshot_line_id, $is_test) {
$pageCount = $this->getPDFPageCount($file);

$this->updateProcessInformation($snapshot_line_id,
array( 'pdf_file' => $file,
'pdf_pagecount' => $pageCount));

return TRUE;
}

/**
Expand Down
Loading

0 comments on commit 82bd029

Please sign in to comment.