Open Journal Systems  3.3.0
MailTemplate.inc.php
1 <?php
2 
17 import('lib.pkp.classes.mail.Mail');
18 
19 define('MAIL_ERROR_INVALID_EMAIL', 0x000001);
20 
21 class MailTemplate extends Mail {
23  var $context;
24 
27 
29  var $emailKey;
30 
32  var $locale;
33 
35  var $enabled;
36 
39 
41  var $bccSender;
42 
45 
47  var $params;
48 
55  function __construct($emailKey = null, $locale = null, $context = null, $includeSignature = true) {
56  parent::__construct();
57  $this->emailKey = isset($emailKey) ? $emailKey : null;
58 
59  // If a context wasn't specified, use the current request.
60  $request = Application::get()->getRequest();
61  if ($context === null) $context = $request->getContext();
62 
63  $this->includeSignature = $includeSignature;
64  // Use current user's locale if none specified
65  $this->locale = isset($locale) ? $locale : AppLocale::getLocale();
66 
67  // Record whether or not to BCC the sender when sending message
68  $this->bccSender = $request->getUserVar('bccSender');
69 
70  $this->addressFieldsEnabled = true;
71 
72  if (isset($this->emailKey)) {
73  $emailTemplate = Services::get('emailTemplate')->getByKey($context ? $context->getId() : CONTEXT_SITE, $this->emailKey);
74  }
75 
76  $userSig = '';
77  $user = defined('SESSION_DISABLE_INIT')?null:$request->getUser();
78  if ($user && $this->includeSignature) {
79  $userSig = $user->getLocalizedSignature();
80  if (!empty($userSig)) $userSig = "<br/>" . $userSig;
81  }
82 
83  if (isset($emailTemplate)) {
84  $this->setSubject($emailTemplate->getData('subject', $this->locale));
85  $this->setBody($emailTemplate->getData('body', $this->locale) . $userSig);
86  $this->enabled = $emailTemplate->getData('enabled');
87  } else {
88  $this->setBody($userSig);
89  $this->enabled = true;
90  }
91 
92  // Default "From" to user if available, otherwise site/context principal contact
93  if ($user) {
94  $this->setFrom($user->getEmail(), $user->getFullName());
95  } elseif (is_null($context) || is_null($context->getData('contactEmail'))) {
96  $site = $request->getSite();
97  $this->setFrom($site->getLocalizedContactEmail(), $site->getLocalizedContactName());
98  } else {
99  $this->setFrom($context->getData('contactEmail'), $context->getData('contactName'));
100  }
101 
102  if ($context) {
103  $this->setSubject('[' . $context->getLocalizedAcronym() . '] ' . $this->getSubject());
104  }
105 
106  $this->context = $context;
107  $this->params = array();
108  }
109 
118  $this->addressFieldsEnabled = $addressFieldsEnabled;
119  }
120 
125  function getAddressFieldsEnabled() {
127  }
128 
133  function hasErrors() {
134  return ($this->errorMessages != null);
135  }
136 
141  function assignParams($params = array()) {
143  $request = $application->getRequest();
144  $site = $request->getSite();
145 
146  if ($this->context) {
147  // Add context-specific variables
148  $dispatcher = $application->getDispatcher();
149  $params = array_merge(array(
150  'principalContactSignature' => $this->context->getData('contactName'),
151  'contextName' => $this->context->getLocalizedName(),
152  'contextUrl' => $dispatcher->url($request, ROUTE_PAGE, $this->context->getPath()),
153  ), $params);
154  } else {
155  // No context available
156  $params = array_merge(array(
157  'principalContactSignature' => $site->getLocalizedContactName(),
158  ), $params);
159  }
160 
161  if (!defined('SESSION_DISABLE_INIT') && ($user = $request->getUser())) {
162  // Add user-specific variables
163  $params = array_merge(array(
164  'senderEmail' => $user->getEmail(),
165  'senderName' => $user->getFullName(),
166  ), $params);
167  }
168 
169  // Add some general variables
170  $params = array_merge(array(
171  'siteTitle' => $site->getLocalizedTitle(),
172  ), $params);
173 
174  $this->params = $params;
175  }
176 
181  function isEnabled() {
182  return $this->enabled;
183  }
184 
189  function send() {
190  if (isset($this->context)) {
191  $signature = $this->context->getData('emailSignature');
192  if (strstr($this->getBody(), '{$templateSignature}') === false) {
193  $this->setBody($this->getBody() . "<br/>" . $signature);
194  } else {
195  $this->setBody(str_replace('{$templateSignature}', $signature, $this->getBody()));
196  }
197 
198  $envelopeSender = $this->context->getData('envelopeSender');
199  if (!empty($envelopeSender) && Config::getVar('email', 'allow_envelope_sender')) $this->setEnvelopeSender($envelopeSender);
200  }
201 
202  $request = Application::get()->getRequest();
203  $user = defined('SESSION_DISABLE_INIT')?null:$request->getUser();
204 
205  if ($user && $this->bccSender) {
206  $this->addBcc($user->getEmail(), $user->getFullName());
207  }
208 
209  // Replace variables in message with values
210  $this->replaceParams();
211 
212  return parent::send();
213  }
214 
219  function replaceParams() {
220  $subject = $this->getSubject();
221  $body = $this->getBody();
222  foreach ($this->params as $key => $value) {
223  if (!is_object($value)) {
224  // $value is checked to identify URL pattern
225  if (filter_var($value, FILTER_VALIDATE_URL) != false) {
226  $body = $this->manageURLValues($body, $key, $value);
227  } else {
228  $body = str_replace('{$' . $key . '}', $value, $body);
229  }
230  }
231 
232  $subject = str_replace('{$' . $key . '}', $value, $subject);
233  }
234  $this->setSubject($subject);
235  $this->setBody($body);
236  }
237 
244  function sendWithParams($params) {
245  $savedHeaders = $this->getHeaders();
246  $savedSubject = $this->getSubject();
247  $savedBody = $this->getBody();
248 
249  $this->assignParams($params);
250  $ret = $this->send();
251 
252  $this->setHeaders($savedHeaders);
253  $this->setSubject($savedSubject);
254  $this->setBody($savedBody);
255 
256  return $ret;
257  }
258 
264  function clearRecipients($clearHeaders = true) {
265  $this->setData('recipients', null);
266  $this->setData('ccs', null);
267  $this->setData('bccs', null);
268  if ($clearHeaders) {
269  $this->setData('headers', null);
270  }
271  }
272 
280  function manageURLValues($targetString, $key, $value) {
281  // If the value is URL, we need to find if $key resides in a href={$...} pattern.
282  preg_match_all('/=[\\\'"]{\\$' . preg_quote($key) . '}/', $targetString, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
283 
284  // if we find some ={$...} occurences of the $key in the email body, then we need to replace them with
285  // the corresponding value
286  if ($matches) {
287  // We make the change backwords (last offset replaced first) so that smaller offsets correctly mark the string they supposed to.
288  for($i = count($matches)-1; $i >= 0; $i--) {
289  $match = $matches[$i][0];
290  $targetString = substr_replace($targetString, str_replace('{$' . $key . '}', $value, $match[0]), $match[1], strlen($match[0]));
291  }
292  }
293 
294  // all the ={$...} patterns have been replaced - now we can change the remaining URL $keys with the following pattern
295  $value = "<a href='$value' class='$key-style-class'>$value</a>";
296 
297  $targetString = str_replace('{$' . $key . '}', $value, $targetString);
298 
299  return $targetString;
300  }
301 }
302 
303 
Mail\getHeaders
getHeaders()
Definition: Mail.inc.php:242
Mail\__construct
__construct()
Definition: Mail.inc.php:33
$application
$application
Definition: index.php:65
Mail\setHeaders
setHeaders(&$headers)
Definition: Mail.inc.php:250
MailTemplate\replaceParams
replaceParams()
Definition: MailTemplate.inc.php:246
Mail\setSubject
setSubject($subject)
Definition: Mail.inc.php:366
MailTemplate\$bccSender
$bccSender
Definition: MailTemplate.inc.php:62
MailTemplate\setAddressFieldsEnabled
setAddressFieldsEnabled($addressFieldsEnabled)
Definition: MailTemplate.inc.php:144
MailTemplate\$context
$context
Definition: MailTemplate.inc.php:26
Mail\getSubject
getSubject()
Definition: Mail.inc.php:374
Mail\addBcc
addBcc($email, $name='')
Definition: Mail.inc.php:162
MailTemplate\isEnabled
isEnabled()
Definition: MailTemplate.inc.php:208
MailTemplate\assignParams
assignParams($params=array())
Definition: MailTemplate.inc.php:168
Mail\setBody
setBody($body)
Definition: Mail.inc.php:382
MailTemplate
Subclass of Mail for mailing a template email.
Definition: MailTemplate.inc.php:21
Mail
Class defining basic operations for handling and sending emails.
Definition: Mail.inc.php:23
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
MailTemplate\send
send()
Definition: MailTemplate.inc.php:216
MailTemplate\hasErrors
hasErrors()
Definition: MailTemplate.inc.php:160
MailTemplate\$errorMessages
$errorMessages
Definition: MailTemplate.inc.php:56
MailTemplate\clearRecipients
clearRecipients($clearHeaders=true)
Definition: MailTemplate.inc.php:291
MailTemplate\$enabled
$enabled
Definition: MailTemplate.inc.php:50
MailTemplate\$locale
$locale
Definition: MailTemplate.inc.php:44
MailTemplate\getAddressFieldsEnabled
getAddressFieldsEnabled()
Definition: MailTemplate.inc.php:152
MailTemplate\__construct
__construct($emailKey=null, $locale=null, $context=null, $includeSignature=true)
Definition: MailTemplate.inc.php:82
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
Mail\setEnvelopeSender
setEnvelopeSender($envelopeSender)
Definition: Mail.inc.php:77
MailTemplate\manageURLValues
manageURLValues($targetString, $key, $value)
Definition: MailTemplate.inc.php:307
MailTemplate\sendWithParams
sendWithParams($params)
Definition: MailTemplate.inc.php:271
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
MailTemplate\$includeSignature
$includeSignature
Definition: MailTemplate.inc.php:32
MailTemplate\$addressFieldsEnabled
$addressFieldsEnabled
Definition: MailTemplate.inc.php:68
MailTemplate\$params
$params
Definition: MailTemplate.inc.php:74
Mail\getBody
getBody()
Definition: Mail.inc.php:390
DataObject\setData
setData($key, $value, $locale=null)
Definition: DataObject.inc.php:132
MailTemplate\$emailKey
$emailKey
Definition: MailTemplate.inc.php:38
Mail\setFrom
setFrom($email, $name='')
Definition: Mail.inc.php:309
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49