4 * @file classes/mail/MailTemplate.inc.php
6 * Copyright (c) 2003-2008 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
12 * @brief Subclass of PKPMailTemplate for mailing a template email.
15 // $Id: MailTemplate.inc.php,v 1.6 2009/06/09 23:37:13 tylerl Exp $
18 import('mail.PKPMailTemplate');
20 class MailTemplate
extends PKPMailTemplate
{
21 /** @var $press object The press this message relates to */
26 * @param $emailKey string unique identifier for the template
27 * @param $locale string locale of the template
28 * @param $enableAttachments boolean optional Whether or not to enable monograph attachments in the template
29 * @param $press object optional The press this message relates to
31 function MailTemplate($emailKey = null, $locale = null, $enableAttachments = null, $press = null) {
32 parent
::PKPMailTemplate($emailKey, $locale, $enableAttachments);
34 // If a press wasn't specified, use the current request.
35 if ($press === null) $press =& Request
::getPress();
37 if (isset($this->emailKey
)) {
38 $emailTemplateDao =& DAORegistry
::getDAO('EmailTemplateDAO');
39 $emailTemplate =& $emailTemplateDao->getEmailTemplate($this->emailKey
, $this->locale
, $press == null ?
0 : $press->getId());
43 $user =& Request
::getUser();
45 $userSig = $user->getLocalizedSignature();
46 if (!empty($userSig)) $userSig = "\n" . $userSig;
49 if (isset($emailTemplate) && Request
::getUserVar('subject')==null && Request
::getUserVar('body')==null) {
50 $this->setSubject($emailTemplate->getSubject());
51 $this->setBody($emailTemplate->getBody() . $userSig);
52 $this->enabled
= $emailTemplate->getEnabled();
54 if (Request
::getUserVar('usePostedAddresses')) {
55 $to = Request
::getUserVar('to');
57 $this->setRecipients($this->processAddresses ($this->getRecipients(), $to));
59 $cc = Request
::getUserVar('cc');
61 $this->setCcs($this->processAddresses ($this->getCcs(), $cc));
63 $bcc = Request
::getUserVar('bcc');
65 $this->setBccs($this->processAddresses ($this->getBccs(), $bcc));
69 $this->setSubject(Request
::getUserVar('subject'));
70 $body = Request
::getUserVar('body');
71 if (empty($body)) $this->setBody($userSig);
72 else $this->setBody($body);
73 $this->skip
= (($tmp = Request
::getUserVar('send')) && is_array($tmp) && isset($tmp['skip']));
74 $this->enabled
= true;
76 if (is_array($toEmails = Request
::getUserVar('to'))) {
77 $this->setRecipients($this->processAddresses ($this->getRecipients(), $toEmails));
79 if (is_array($ccEmails = Request
::getUserVar('cc'))) {
80 $this->setCcs($this->processAddresses ($this->getCcs(), $ccEmails));
82 if (is_array($bccEmails = Request
::getUserVar('bcc'))) {
83 $this->setBccs($this->processAddresses ($this->getBccs(), $bccEmails));
87 // Default "From" to user if available, otherwise site/press principal contact
88 $user =& Request
::getUser();
90 $this->setFrom($user->getEmail(), $user->getFullName());
91 } elseif ($press == null) {
92 $site =& Request
::getSite();
93 $this->setFrom($site->getLocalizedContactEmail(), $site->getLocalizedContactName());
96 $this->setFrom($press->getSetting('contactEmail'), $press->getSetting('contactName'));
99 if ($press && !Request
::getUserVar('continued')) {
100 $this->setSubject('[' . $press->getLocalizedSetting('initials') . '] ' . $this->getSubject());
103 $this->press
=& $press;
107 * Assigns values to e-mail parameters.
108 * @param $paramArray array
111 function assignParams($paramArray = array()) {
112 // Add commonly-used variables to the list
113 if (isset($this->press
)) {
114 // FIXME Include affiliation, title, etc. in signature?
115 $paramArray['pressName'] = $this->press
->getLocalizedName();
116 $paramArray['principalContactSignature'] = $this->press
->getSetting('contactName');
118 $site =& Request
::getSite();
119 $paramArray['principalContactSignature'] = $site->getLocalizedContactName();
121 if (!isset($paramArray['pressUrl'])) $paramArray['pressUrl'] = Request
::url(Request
::getRequestedPressPath());
123 return parent
::assignParams($paramArray);
127 * Displays an edit form to customize the email.
128 * @param $formActionUrl string
129 * @param $hiddenFormParams array
132 function displayEditForm($formActionUrl, $hiddenFormParams = null, $alternateTemplate = null, $additionalParameters = array()) {
133 $templateMgr =& TemplateManager
::getManager();
134 $templateMgr->assign('helpTopicId', 'press.managementPages.emails');
136 parent
::displayEditForm($formActionUrl, $hiddenFormParams, $alternateTemplate, $additionalParameters);
141 * Aside from calling the parent method, this actually attaches
142 * the persistent attachments if they are used.
143 * @param $clearAttachments boolean Whether to delete attachments after
145 function send($clearAttachments = true) {
146 if (isset($this->press
)) {
147 //If {$templateSignature} exists in the body of the
148 // message, replace it with the press signature;
149 // otherwise just append it. This is here to
150 // accomodate MIME-encoded messages or other cases
151 // where the signature cannot just be appended.
152 $searchString = '{$templateSignature}';
153 if (strstr($this->getBody(), $searchString) === false) {
154 $this->setBody($this->getBody() . "\n" . $this->press
->getSetting('emailSignature'));
156 $this->setBody(str_replace($searchString, $this->press
->getSetting('emailSignature'), $this->getBody()));
159 $envelopeSender = $this->press
->getSetting('envelopeSender');
160 if (!empty($envelopeSender) && Config
::getVar('email', 'allow_envelope_sender')) $this->setEnvelopeSender($envelopeSender);
163 return parent
::send($clearAttachments);