class EmailMerge in Forena Reports 8
Provides MS Excel Exports
Plugin annotation
@FrxDocument(
id= "email",
name="Email Merge",
ext="email"
)
Hierarchy
- class \Drupal\forena\FrxPlugin\Document\DocumentBase implements DocumentInterface uses FrxAPI
- class \Drupal\forena\FrxPlugin\Document\EmailMerge
Expanded class hierarchy of EmailMerge
1 file declares its use of EmailMerge
- EmailMergeForm.php in src/
Form/ EmailMergeForm.php
File
- src/
FrxPlugin/ Document/ EmailMerge.php, line 19
Namespace
Drupal\forena\FrxPlugin\DocumentView source
class EmailMerge extends DocumentBase {
public $emails = [];
public $count;
public $prompt_subject;
public $prompt_body;
public function header() {
$this->write_buffer = '';
}
public function flush() {
$content = [];
$body = $this->write_buffer;
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->strictErrorChecking = FALSE;
libxml_use_internal_errors(true);
$doc
->loadHTML($body);
libxml_clear_errors();
$xml = simplexml_import_dom($doc);
if (!$xml) {
return $content;
}
$docs = $xml
->xpath('.//*[@class="email-document"]');
$this->prompt_subject = TRUE;
$this->prompt_body = TRUE;
/** @var \SimpleXMLElement $doc */
if ($docs) {
foreach ($docs as $doc) {
// From
$from = $doc
->xpath('.//*[@class="email-header-from"]');
$from = $from ? html_entity_decode(strip_tags($from[0])) : '';
// Subject
$subject = $doc
->xpath('.//*[@class="email-header-subject"]');
if ($subject) {
$this->prompt_subject = FALSE;
}
$subject = $subject ? (string) $subject[0] : '';
// To
$to = $doc
->xpath('.//*[@class="email-header-to"]');
$to = $to ? html_entity_decode(strip_tags($to[0]
->asXML())) : '';
if ($to) {
$this->prompt_to = FALSE;
}
$body = $doc
->xpath('.//*[@class="email-body"]');
if ($body) {
$this->prompt_body = FALSE;
}
$body = $body ? $body[0]
->asXML() : $body;
// Assemble email
$email = array(
'to' => $to,
'from' => $from,
'parms' => array(
'subject' => $subject,
'body' => $body,
),
);
// Check for cc
$cc = $doc
->xpath('.//*[@class="email-header-cc"]');
if ($cc) {
$email['parms']['headers']['Cc'] = html_entity_decode(strip_tags($cc[0]
->asXML()));
}
// Check for bcc
$bcc = $doc
->xpath('.//*[@class="email-header-bcc"]');
if ($bcc) {
$email['parms']['headers']['Bcc'] = html_entity_decode(strip_tags($bcc[0]
->asXML()));
}
$this->emails[] = $email;
}
}
$count = count($docs);
$this->count = $count;
if ($count) {
$content['email_form'] = \Drupal::formBuilder()
->getForm(EmailMergeForm::class);
}
else {
$this
->app()
->error(t('No mail merge information in report. Displaying report instead.'));
$output = $body;
$content = array(
'content' => array(
'#markup' => $output,
),
);
}
return $content;
}
public function sendMail($email, $max, $subject = '', $body = '') {
$i = 0;
if (!$max) {
$max = count($this->emails);
}
foreach ($this->emails as $doc) {
$i++;
$to = !empty($email) ? $email : $doc['to'];
$from = $doc['from'];
// Replace body
if (!empty($body)) {
$doc['parms']['body'] = $body;
}
// Replace subject
if (!empty($subject)) {
$doc['parms']['subject'] = $subject;
}
// If we're in test mode foce unset of header.
if ($email) {
// Remove bcc and cc
unset($doc['parms']['headers']);
}
/** @var MailManagerInterface $mailManager */
$mailManager = \Drupal::service('plugin.manager.mail');
if ($i <= $max) {
$mailManager
->mail('forena', 'mailmerge', $to, \Drupal::languageManager()
->getLanguages(), $doc['parms'], $from, TRUE);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DocumentBase:: |
public | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
protected | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
protected | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
protected | property | ||
DocumentBase:: |
protected | property | ||
DocumentBase:: |
public | property | ||
DocumentBase:: |
protected | property | ||
DocumentBase:: |
public | function | ||
DocumentBase:: |
public | function | Wrapper function for check output to default the right type. | |
DocumentBase:: |
public | function |
Clear the buffer Overrides DocumentInterface:: |
|
DocumentBase:: |
public | function | Perform character set conversion | |
DocumentBase:: |
public | function |
No default footer. Overrides DocumentInterface:: |
1 |
DocumentBase:: |
public | function | ||
DocumentBase:: |
public | function |
Overrides DocumentInterface:: |
|
DocumentBase:: |
public | function |
Overrides DocumentInterface:: |
|
DocumentBase:: |
public | function |
Write Overrides DocumentInterface:: |
|
EmailMerge:: |
public | property | ||
EmailMerge:: |
public | property | ||
EmailMerge:: |
public | property | ||
EmailMerge:: |
public | property | ||
EmailMerge:: |
public | function |
Write the output to disk. Overrides DocumentBase:: |
|
EmailMerge:: |
public | function |
Default implementation to put in content type based headers. Overrides DocumentBase:: |
|
EmailMerge:: |
public | function | ||
FrxAPI:: |
public | function | Returns containing application service | |
FrxAPI:: |
public | function | Get the current data context. | |
FrxAPI:: |
public | function | ||
FrxAPI:: |
public | function | Returns the data manager service | |
FrxAPI:: |
public | function | Return Data Service | |
FrxAPI:: |
public | function | Returns the fornea document manager | |
FrxAPI:: |
public | function | Report an error | |
FrxAPI:: |
public | function | Get the context of a specific id. | |
FrxAPI:: |
public | function | Get the current document | |
FrxAPI:: |
public | function | Load the contents of a file in the report file system. | |
FrxAPI:: |
function | Enter description here... | 1 | |
FrxAPI:: |
public | function | Pop data off of the stack. | |
FrxAPI:: |
public | function | Push data onto the Stack | |
FrxAPI:: |
public | function | Run a report with a particular format. | 1 |
FrxAPI:: |
public | function | Get the current report file system. | |
FrxAPI:: |
public | function | Set Data context by id. | |
FrxAPI:: |
public | function | Change to a specific document type. | |
FrxAPI:: |
public | function | Get list of skins. |