00001 <?php
00002
00015
00016
00017
00018 import('mail.Mail');
00019
00020 define('MAIL_ERROR_INVALID_EMAIL', 0x000001);
00021 class MailTemplate extends Mail {
00022
00024 var $emailKey;
00025
00027 var $locale;
00028
00030 var $journal;
00031
00033 var $enabled;
00034
00036 var $errorMessages;
00037
00041 var $persistAttachments;
00042 var $attachmentsEnabled;
00043
00046 var $skip;
00047
00049 var $bccSender;
00050
00052 var $addressFieldsEnabled;
00053
00061 function MailTemplate($emailKey = null, $locale = null, $enableAttachments = null, $journal = null) {
00062 parent::Mail();
00063 $this->emailKey = isset($emailKey) ? $emailKey : null;
00064
00065
00066
00067 $this->locale = isset($locale) ? $locale : Locale::getLocale();
00068
00069
00070 if ($journal === null) $journal = &Request::getJournal();
00071
00072 if (isset($this->emailKey)) {
00073 $emailTemplateDao = &DAORegistry::getDAO('EmailTemplateDAO');
00074 $emailTemplate = &$emailTemplateDao->getEmailTemplate($this->emailKey, $this->locale, $journal == null ? 0 : $journal->getJournalId());
00075 }
00076
00077 $userSig = '';
00078 $user =& Request::getUser();
00079 if ($user) {
00080 $userSig = $user->getUserSignature();
00081 if (!empty($userSig)) $userSig = "\n" . $userSig;
00082 }
00083
00084 if (isset($emailTemplate) && Request::getUserVar('subject')==null && Request::getUserVar('body')==null) {
00085 $this->setSubject($emailTemplate->getSubject());
00086 $this->setBody($emailTemplate->getBody() . $userSig);
00087 $this->enabled = $emailTemplate->getEnabled();
00088
00089 if (Request::getUserVar('usePostedAddresses')) {
00090 $to = Request::getUserVar('to');
00091 if (is_array($to)) {
00092 $this->setRecipients($this->processAddresses ($this->getRecipients(), $to));
00093 }
00094 $cc = Request::getUserVar('cc');
00095 if (is_array($cc)) {
00096 $this->setCcs($this->processAddresses ($this->getCcs(), $cc));
00097 }
00098 $bcc = Request::getUserVar('bcc');
00099 if (is_array($bcc)) {
00100 $this->setBccs($this->processAddresses ($this->getBccs(), $bcc));
00101 }
00102 }
00103 } else {
00104 $this->setSubject(Request::getUserVar('subject'));
00105 $body = Request::getUserVar('body');
00106 if (empty($body)) $this->setBody($userSig);
00107 else $this->setBody($body);
00108 $this->skip = (($tmp = Request::getUserVar('send')) && is_array($tmp) && isset($tmp['skip']));
00109 $this->enabled = true;
00110
00111 if (is_array($toEmails = Request::getUserVar('to'))) {
00112 $this->setRecipients($this->processAddresses ($this->getRecipients(), $toEmails));
00113 }
00114 if (is_array($ccEmails = Request::getUserVar('cc'))) {
00115 $this->setCcs($this->processAddresses ($this->getCcs(), $ccEmails));
00116 }
00117 if (is_array($bccEmails = Request::getUserVar('bcc'))) {
00118 $this->setBccs($this->processAddresses ($this->getBccs(), $bccEmails));
00119 }
00120 }
00121
00122
00123 $this->bccSender = Request::getUserVar('bccSender');
00124
00125
00126 $user = &Request::getUser();
00127 if ($user) {
00128 $this->setFrom($user->getEmail(), $user->getFullName());
00129 } elseif ($journal == null) {
00130 $site = &Request::getSite();
00131 $this->setFrom($site->getSiteContactEmail(), $site->getSiteContactName());
00132
00133 } else {
00134 $this->setFrom($journal->getSetting('contactEmail'), $journal->getSetting('contactName'));
00135 }
00136
00137 if ($journal && !Request::getUserVar('continued')) {
00138 $this->setSubject('[' . $journal->getLocalizedSetting('initials') . '] ' . $this->getSubject());
00139 }
00140
00141
00142
00143 if ($enableAttachments === null) {
00144 $enableAttachments = Config::getVar('email', 'enable_attachments')?true:false;
00145 }
00146
00147 if ($enableAttachments && $user) {
00148 $this->_handleAttachments($user->getUserId());
00149 } else {
00150 $this->attachmentsEnabled = false;
00151 }
00152
00153 $this->addressFieldsEnabled = true;
00154 $this->journal =& $journal;
00155 }
00156
00164 function setAddressFieldsEnabled($addressFieldsEnabled) {
00165 $this->addressFieldsEnabled = $addressFieldsEnabled;
00166 }
00167
00172 function getAddressFieldsEnabled() {
00173 return $this->addressFieldsEnabled;
00174 }
00175
00180 function hasErrors() {
00181 return ($this->errorMessages != null);
00182 }
00183
00189 function assignParams($paramArray = array()) {
00190 $subject = $this->getSubject();
00191 $body = $this->getBody();
00192
00193
00194 if (isset($this->journal)) {
00195
00196 $paramArray['journalName'] = $this->journal->getJournalTitle();
00197 $paramArray['principalContactSignature'] = $this->journal->getSetting('contactName');
00198 } else {
00199 $site = &Request::getSite();
00200 $paramArray['principalContactSignature'] = $site->getSiteContactName();
00201 }
00202 if (!isset($paramArray['journalUrl'])) $paramArray['journalUrl'] = Request::url(Request::getRequestedJournalPath());
00203
00204
00205 foreach ($paramArray as $key => $value) {
00206 if (!is_object($value)) {
00207 $subject = str_replace('{$' . $key . '}', $value, $subject);
00208 $body = str_replace('{$' . $key . '}', $value, $body);
00209 }
00210 }
00211
00212 $this->setSubject($subject);
00213 $this->setBody($body);
00214 }
00215
00220 function isEnabled() {
00221 return $this->enabled;
00222 }
00223
00230 function &processAddresses($currentList, &$newAddresses) {
00231 foreach ($newAddresses as $newAddress) {
00232 $regs = array();
00233
00234 if (ereg('^([^<>' . "\n" . ']*[^<> ' . "\n" . '])[ ]*<([-A-Za-z0-9]+([-_\+\.][A-Za-z0-9]+)*@[A-Za-z0-9]+([-_\.][A-Za-z0-9]+)*\.[A-Za-z]{2,})>$', $newAddress, $regs)) {
00235 $currentList[] = array('name' => $regs[1], 'email' => $regs[2]);
00236 } elseif (ereg('^[A-Za-z0-9]+([-_\+\.][A-Za-z0-9]+)*@[A-Za-z0-9]+([-_\.][A-Za-z0-9]+)*\.[A-Za-z]{2,}$', $newAddress)) {
00237 $currentList[] = array('name' => '', 'email' => $newAddress);
00238 } else if ($newAddress != '') {
00239 $this->errorMessages[] = array('type' => MAIL_ERROR_INVALID_EMAIL, 'address' => $newAddress);
00240 }
00241 }
00242 return $currentList;
00243 }
00244
00251 function displayEditForm($formActionUrl, $hiddenFormParams = null, $alternateTemplate = null, $additionalParameters = array()) {
00252 import('form.Form');
00253 $form = &new Form($alternateTemplate!=null?$alternateTemplate:'email/email.tpl');
00254
00255 $form->setData('formActionUrl', $formActionUrl);
00256 $form->setData('subject', $this->getSubject());
00257 $form->setData('body', $this->getBody());
00258
00259 $form->setData('to', $this->getRecipients());
00260 $form->setData('cc', $this->getCcs());
00261 $form->setData('bcc', $this->getBccs());
00262 $form->setData('blankTo', Request::getUserVar('blankTo'));
00263 $form->setData('blankCc', Request::getUserVar('blankCc'));
00264 $form->setData('blankBcc', Request::getUserVar('blankBcc'));
00265 $form->setData('from', $this->getFromString(false));
00266
00267 $form->setData('addressFieldsEnabled', $this->getAddressFieldsEnabled());
00268
00269 $user = &Request::getUser();
00270 if ($user) {
00271 $form->setData('senderEmail', $user->getEmail());
00272 $form->setData('bccSender', $this->bccSender);
00273 }
00274
00275 if ($this->attachmentsEnabled) {
00276 $form->setData('attachmentsEnabled', true);
00277 $form->setData('persistAttachments', $this->persistAttachments);
00278 }
00279
00280 $form->setData('errorMessages', $this->errorMessages);
00281
00282 if ($hiddenFormParams != null) {
00283 $form->setData('hiddenFormParams', $hiddenFormParams);
00284 }
00285
00286 foreach ($additionalParameters as $key => $value) {
00287 $form->setData($key, $value);
00288 }
00289
00290 $templateMgr = &TemplateManager::getManager();
00291 $templateMgr->assign('helpTopicId', 'journal.managementPages.emails');
00292
00293 $form->display();
00294 }
00295
00302 function send($clearAttachments = true) {
00303 if (isset($this->journal)) {
00304
00305
00306
00307
00308
00309 $searchString = '{$templateSignature}';
00310 if (strstr($this->getBody(), $searchString) === false) {
00311 $this->setBody($this->getBody() . "\n" . $this->journal->getSetting('emailSignature'));
00312 } else {
00313 $this->setBody(str_replace($searchString, $this->journal->getSetting('emailSignature'), $this->getBody()));
00314 }
00315
00316 $envelopeSender = $this->journal->getSetting('envelopeSender');
00317 if (!empty($envelopeSender) && Config::getVar('email', 'allow_envelope_sender')) $this->setEnvelopeSender($envelopeSender);
00318 }
00319
00320 if ($this->attachmentsEnabled) {
00321 foreach ($this->persistAttachments as $persistentAttachment) {
00322 $this->addAttachment(
00323 $persistentAttachment->getFilePath(),
00324 $persistentAttachment->getOriginalFileName(),
00325 $persistentAttachment->getFileType()
00326 );
00327 }
00328 }
00329
00330 $user = &Request::getUser();
00331
00332 if ($user && $this->bccSender) {
00333 $this->addBcc($user->getEmail(), $user->getFullName());
00334 }
00335
00336 if (isset($this->skip) && $this->skip) {
00337 $result = true;
00338 } else {
00339 $result = parent::send();
00340 }
00341
00342 if ($clearAttachments && $this->attachmentsEnabled) {
00343 $this->_clearAttachments($user->getUserId());
00344 }
00345
00346 return $result;
00347 }
00348
00355 function sendWithParams($paramArray) {
00356 $savedHeaders = $this->getHeaders();
00357 $savedSubject = $this->getSubject();
00358 $savedBody = $this->getBody();
00359
00360 $this->assignParams($paramArray);
00361
00362 $ret = $this->send();
00363
00364 $this->setHeaders($savedHeaders);
00365 $this->setSubject($savedSubject);
00366 $this->setBody($savedBody);
00367
00368 return $ret;
00369 }
00370
00376 function clearRecipients($clearHeaders = true) {
00377 $this->setData('recipients', null);
00378 $this->setData('ccs', null);
00379 $this->setData('bccs', null);
00380 if ($clearHeaders) {
00381 $this->setData('headers', null);
00382 }
00383 }
00384
00390 function addPersistAttachment($temporaryFile) {
00391 $this->persistAttachments[] = $temporaryFile;
00392 }
00393
00399 function _handleAttachments($userId) {
00400 import('file.TemporaryFileManager');
00401 $temporaryFileManager = &new TemporaryFileManager();
00402
00403 $this->attachmentsEnabled = true;
00404 $this->persistAttachments = array();
00405
00406 $deleteAttachment = Request::getUserVar('deleteAttachment');
00407
00408 if (Request::getUserVar('persistAttachments') != null) foreach (Request::getUserVar('persistAttachments') as $fileId) {
00409 $temporaryFile = $temporaryFileManager->getFile($fileId, $userId);
00410 if (!empty($temporaryFile)) {
00411 if ($deleteAttachment != $temporaryFile->getFileId()) {
00412 $this->persistAttachments[] = $temporaryFile;
00413 } else {
00414
00415 $temporaryFileManager->deleteFile($temporaryFile->getFileId(), $userId);
00416 }
00417 }
00418 }
00419
00420 if (Request::getUserVar('addAttachment')) {
00421 $user = &Request::getUser();
00422
00423 $this->persistAttachments[] = $temporaryFileManager->handleUpload('newAttachment', $user->getUserId());
00424 }
00425 }
00426
00427 function getAttachmentFiles() {
00428 if ($this->attachmentsEnabled) return $this->persistAttachments;
00429 return array();
00430 }
00431
00437 function _clearAttachments($userId) {
00438 import('file.TemporaryFileManager');
00439 $temporaryFileManager = &new TemporaryFileManager();
00440
00441 $persistAttachments = Request::getUserVar('persistAttachments');
00442 if (is_array($persistAttachments)) foreach ($persistAttachments as $fileId) {
00443 $temporaryFile = $temporaryFileManager->getFile($fileId, $userId);
00444 if (!empty($temporaryFile)) {
00445 $temporaryFileManager->deleteFile($temporaryFile->getFileId(), $userId);
00446 }
00447 }
00448 }
00449 }
00450
00451 ?>