Index: branches/5.3.x/core/kernel/utility/email.php
===================================================================
diff -u -N -r15578 -r15677
--- branches/5.3.x/core/kernel/utility/email.php (.../email.php) (revision 15578)
+++ branches/5.3.x/core/kernel/utility/email.php (.../email.php) (revision 15677)
@@ -1,6 +1,6 @@
Array (),
- EmailEvent::RECIPIENT_TYPE_CC => Array (),
- EmailEvent::RECIPIENT_TYPE_BCC => Array (),
+ EmailTemplate::RECIPIENT_TYPE_TO => Array (),
+ EmailTemplate::RECIPIENT_TYPE_CC => Array (),
+ EmailTemplate::RECIPIENT_TYPE_BCC => Array (),
);
/**
@@ -113,78 +113,79 @@
$this->Application->removeObject('u.email-from');
$this->recipients = Array (
- EmailEvent::RECIPIENT_TYPE_TO => Array (),
- EmailEvent::RECIPIENT_TYPE_CC => Array (),
- EmailEvent::RECIPIENT_TYPE_BCC => Array (),
+ EmailTemplate::RECIPIENT_TYPE_TO => Array (),
+ EmailTemplate::RECIPIENT_TYPE_CC => Array (),
+ EmailTemplate::RECIPIENT_TYPE_BCC => Array (),
);
$this->toEmail = $this->toEmail = '';
$this->Application->removeObject('u.email-to');
}
/**
- * Finds e-mail event matching user data
+ * Finds e-mail template matching user data
*
* @param string $name
* @param int $type
* @return bool
* @throws InvalidArgumentException
* @access public
*/
- public function findEvent($name, $type)
+ public function findTemplate($name, $type)
{
if ( !$name || !preg_match('/^[A-Z\.]+$/', $name) ) {
- throw new InvalidArgumentException('Invalid e-mail event name "' . $name . '". Only UPPERCASE characters and dots are allowed.');
+ throw new InvalidArgumentException('Invalid e-mail template name "' . $name . '". Only UPPERCASE characters and dots are allowed.');
}
- if ( $type != EmailEvent::EVENT_TYPE_ADMIN && $type != EmailEvent::EVENT_TYPE_FRONTEND ) {
- throw new InvalidArgumentException('Invalid e-mail event type');
+ if ( $type != EmailTemplate::TEMPLATE_TYPE_ADMIN && $type != EmailTemplate::TEMPLATE_TYPE_FRONTEND ) {
+ throw new InvalidArgumentException('Invalid e-mail template type');
}
- $this->emailEvent = $this->Application->recallObject('emailevents', null, Array ('skip_autoload' => true));
+ // use "-item" special prevent error, when e-mail sent out from e-mail templates list
+ $this->emailTemplate = $this->Application->recallObject('email-template.-item', null, Array ('skip_autoload' => true));
- if ( !$this->emailEvent->isLoaded() || !$this->_sameEvent($name, $type) ) {
- // get event parameters by name & type
- $this->emailEvent->Load(Array ('Event' => $name, 'Type' => $type));
+ if ( !$this->emailTemplate->isLoaded() || !$this->_sameTemplate($name, $type) ) {
+ // get template parameters by name & type
+ $this->emailTemplate->Load(Array ('TemplateName' => $name, 'Type' => $type));
}
- return $this->_eventUsable();
+ return $this->_templateUsable();
}
/**
- * Detects, that given event data matches currently used event
+ * Detects, that given e-mail template data matches currently used e-mail template
*
* @param string $name
* @param int $type
* @return bool
* @access protected
*/
- protected function _sameEvent($name, $type)
+ protected function _sameTemplate($name, $type)
{
- return $this->emailEvent->GetDBField('Event') == $name && $this->emailEvent->GetDBField('Type') == $type;
+ return $this->emailTemplate->GetDBField('TemplateName') == $name && $this->emailTemplate->GetDBField('Type') == $type;
}
/**
- * Determines if we can use e-mail event we've found based on user data
+ * Determines if we can use e-mail template we've found based on user data
*
* @return bool
* @access protected
*/
- protected function _eventUsable()
+ protected function _templateUsable()
{
- if ( !$this->emailEvent->isLoaded() || $this->emailEvent->GetDBField('Enabled') == STATUS_DISABLED ) {
+ if ( !$this->emailTemplate->isLoaded() || $this->emailTemplate->GetDBField('Enabled') == STATUS_DISABLED ) {
return false;
}
- if ( $this->emailEvent->GetDBField('FrontEndOnly') && $this->Application->isAdmin ) {
+ if ( $this->emailTemplate->GetDBField('FrontEndOnly') && $this->Application->isAdmin ) {
return false;
}
return true;
}
/**
- * Sets e-mail event params
+ * Sets e-mail template params
*
* @param Array $params
* @access public
@@ -195,7 +196,7 @@
}
/**
- * Returns any custom parameters, that are passed when invoked e-mail event sending
+ * Returns any custom parameters, that are passed when invoked e-mail template sending
*
* @return Array
* @access protected
@@ -246,8 +247,8 @@
'OtherRecipients' => serialize($this->recipients),
'Subject' => $message_subject,
'SentOn' => TIMENOW,
- 'EventName' => $this->emailEvent->GetDBField('Event'),
- 'EventType' => $this->emailEvent->GetDBField('Type'),
+ 'TemplateName' => $this->emailTemplate->GetDBField('TemplateName'),
+ 'EventType' => $this->emailTemplate->GetDBField('Type'),
'EventParams' => serialize($this->_getCustomParams()),
);
@@ -323,7 +324,7 @@
*/
protected function _processSender()
{
- if ( $this->emailEvent->GetDBField('CustomSender') ) {
+ if ( $this->emailTemplate->GetDBField('CustomSender') ) {
$this->_processCustomSender();
}
@@ -349,15 +350,15 @@
*/
protected function _processCustomSender()
{
- $address = $this->emailEvent->GetDBField('SenderAddress');
- $address_type = $this->emailEvent->GetDBField('SenderAddressType');
+ $address = $this->emailTemplate->GetDBField('SenderAddress');
+ $address_type = $this->emailTemplate->GetDBField('SenderAddressType');
switch ($address_type) {
- case EmailEvent::ADDRESS_TYPE_EMAIL:
+ case EmailTemplate::ADDRESS_TYPE_EMAIL:
$this->fromEmail = $address;
break;
- case EmailEvent::ADDRESS_TYPE_USER:
+ case EmailTemplate::ADDRESS_TYPE_USER:
$sql = 'SELECT FirstName, LastName, Email, PortalUserId
FROM ' . TABLE_PREFIX . 'Users
WHERE Username = ' . $this->Conn->qstr($address);
@@ -376,8 +377,8 @@
break;
}
- if ( $this->emailEvent->GetDBField('SenderName') ) {
- $this->fromName = $this->emailEvent->GetDBField('SenderName');
+ if ( $this->emailTemplate->GetDBField('SenderName') ) {
+ $this->fromName = $this->emailTemplate->GetDBField('SenderName');
}
}
@@ -409,9 +410,9 @@
$this->_collectRecipients();
$header_mapping = Array (
- EmailEvent::RECIPIENT_TYPE_TO => 'To',
- EmailEvent::RECIPIENT_TYPE_CC => 'Cc',
- EmailEvent::RECIPIENT_TYPE_BCC => 'Bcc',
+ EmailTemplate::RECIPIENT_TYPE_TO => 'To',
+ EmailTemplate::RECIPIENT_TYPE_CC => 'Cc',
+ EmailTemplate::RECIPIENT_TYPE_BCC => 'Bcc',
);
$default_email = $this->Application->ConfigValue('DefaultEmailSender');
@@ -423,7 +424,7 @@
continue;
}
- if ( $recipient_type == EmailEvent::RECIPIENT_TYPE_TO ) {
+ if ( $recipient_type == EmailTemplate::RECIPIENT_TYPE_TO ) {
$this->toEmail = $recipients[0]['email'] ? $recipients[0]['email'] : $default_email;
$this->toName = $recipients[0]['name'] ? $recipients[0]['name'] : $this->toEmail;
}
@@ -447,13 +448,13 @@
*/
protected function _collectRecipients()
{
- $this->_addRecipientsFromXml($this->emailEvent->GetDBField('Recipients'));
+ $this->_addRecipientsFromXml($this->emailTemplate->GetDBField('Recipients'));
$this->_overwriteToRecipient();
$this->_addRecipientByUserId();
$this->_addRecipientFromParams();
- if ( ($this->emailEvent->GetDBField('Type') == EmailEvent::EVENT_TYPE_ADMIN) && !$this->recipients[EmailEvent::RECIPIENT_TYPE_TO] ) {
- // admin email event without direct recipient -> send to admin
+ if ( ($this->emailTemplate->GetDBField('Type') == EmailTemplate::TEMPLATE_TYPE_ADMIN) && !$this->recipients[EmailTemplate::RECIPIENT_TYPE_TO] ) {
+ // admin email template without direct recipient -> send to admin
$this->_addDefaultRecipient();
}
}
@@ -494,8 +495,8 @@
{
$overwrite_to_email = isset($this->params['overwrite_to_email']) ? $this->params['overwrite_to_email'] : false;
- if ( !$this->emailEvent->GetDBField('CustomRecipient') || $overwrite_to_email ) {
- $this->recipients[EmailEvent::RECIPIENT_TYPE_TO] = Array ();
+ if ( !$this->emailTemplate->GetDBField('CustomRecipient') || $overwrite_to_email ) {
+ $this->recipients[EmailTemplate::RECIPIENT_TYPE_TO] = Array ();
}
}
@@ -518,7 +519,7 @@
return;
}
- $language_field = $this->emailEvent->GetDBField('Type') == EmailEvent::EVENT_TYPE_FRONTEND ? 'FrontLanguage' : 'AdminLanguage';
+ $language_field = $this->emailTemplate->GetDBField('Type') == EmailTemplate::TEMPLATE_TYPE_FRONTEND ? 'FrontLanguage' : 'AdminLanguage';
$sql = 'SELECT FirstName, LastName, Email, ' . $language_field . ' AS Language
FROM ' . TABLE_PREFIX . 'Users
@@ -530,7 +531,7 @@
}
$add_recipient = Array (
- 'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL,
+ 'RecipientAddressType' => EmailTemplate::ADDRESS_TYPE_EMAIL,
'RecipientAddress' => $user_info['Email'],
'RecipientName' => trim($user_info['FirstName'] . ' ' . $user_info['LastName']),
);
@@ -539,7 +540,7 @@
$this->params['language_id'] = $user_info['Language'];
}
- array_unshift($this->recipients[EmailEvent::RECIPIENT_TYPE_TO], $add_recipient);
+ array_unshift($this->recipients[EmailTemplate::RECIPIENT_TYPE_TO], $add_recipient);
$user = $this->Application->recallObject('u.email-to', null, Array('skip_autoload' => true));
/* @var $user UsersItem */
@@ -559,7 +560,7 @@
if ( isset($this->params['to_email']) && $this->params['to_email'] ) {
$add_recipient['RecipientName'] = '';
- $add_recipient['RecipientAddressType'] = EmailEvent::ADDRESS_TYPE_EMAIL;
+ $add_recipient['RecipientAddressType'] = EmailTemplate::ADDRESS_TYPE_EMAIL;
$add_recipient['RecipientAddress'] = $this->params['to_email'];
}
@@ -568,7 +569,7 @@
}
if ( $add_recipient ) {
- array_unshift($this->recipients[EmailEvent::RECIPIENT_TYPE_TO], $add_recipient);
+ array_unshift($this->recipients[EmailTemplate::RECIPIENT_TYPE_TO], $add_recipient);
}
}
@@ -585,11 +586,11 @@
if ( !$this->_addRecipientsFromXml($xml) ) {
$recipient = Array (
'RecipientName' => $this->Application->ConfigValue('DefaultEmailSender'),
- 'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL,
+ 'RecipientAddressType' => EmailTemplate::ADDRESS_TYPE_EMAIL,
'RecipientAddress' => $this->Application->ConfigValue('DefaultEmailSender'),
);
- array_unshift($this->recipients[EmailEvent::RECIPIENT_TYPE_TO], $recipient);
+ array_unshift($this->recipients[EmailTemplate::RECIPIENT_TYPE_TO], $recipient);
}
}
@@ -614,11 +615,11 @@
$recipient_name = $recipient['RecipientName'];
switch ($address_type) {
- case EmailEvent::ADDRESS_TYPE_EMAIL:
+ case EmailTemplate::ADDRESS_TYPE_EMAIL:
$pairs[] = Array ('email' => $address, 'name' => $recipient_name);
break;
- case EmailEvent::ADDRESS_TYPE_USER:
+ case EmailTemplate::ADDRESS_TYPE_USER:
$sql = 'SELECT FirstName, LastName, Email
FROM ' . TABLE_PREFIX . 'Users
WHERE Username = ' . $this->Conn->qstr($address);
@@ -635,7 +636,7 @@
}
break;
- case EmailEvent::ADDRESS_TYPE_GROUP:
+ case EmailTemplate::ADDRESS_TYPE_GROUP:
$sql = 'SELECT u.FirstName, u.LastName, u.Email
FROM ' . TABLE_PREFIX . 'UserGroups g
JOIN ' . TABLE_PREFIX . 'UserGroupRelations ug ON ug.GroupId = g.GroupId
@@ -697,8 +698,8 @@
*/
protected function _getHeaders()
{
- $headers = $this->emailEvent->GetDBField('Headers');
- $headers = 'Subject: ' . $this->emailEvent->GetField('Subject') . ($headers ? "\n" . $headers : '');
+ $headers = $this->emailTemplate->GetDBField('Headers');
+ $headers = 'Subject: ' . $this->emailTemplate->GetField('Subject') . ($headers ? "\n" . $headers : '');
$headers = explode("\n", $this->_parseText($headers));
$ret = Array ();
@@ -709,9 +710,9 @@
}
if ( $this->Application->isDebugMode() ) {
- // set special header with event name, so it will be easier to determine what's actually was received
- $event_type = $this->emailEvent->GetDBField('Type') == EmailEvent::EVENT_TYPE_ADMIN ? 'ADMIN' : 'USER';
- $ret['X-Event-Name'] = $this->emailEvent->GetDBField('Event') . ' - ' . $event_type;
+ // set special header with template name, so it will be easier to determine what's actually was received
+ $template_type = $this->emailTemplate->GetDBField('Type') == EmailTemplate::TEMPLATE_TYPE_ADMIN ? 'ADMIN' : 'USER';
+ $ret['X-Template-Name'] = $this->emailTemplate->GetDBField('TemplateName') . ' - ' . $template_type;
}
return $ret;
@@ -744,7 +745,7 @@
$design_templates[$design_key] = $design_template;
}
- return $this->_parseText(str_replace('$body', $text, $design_templates[$design_key]));
+ return $this->_parseText(str_replace('$body', $text, $design_templates[$design_key]), $is_html);
}
/**
@@ -756,19 +757,19 @@
*/
protected function _getMessageBody($is_html = false)
{
- $message_body = $this->emailEvent->GetField($is_html ? 'HtmlBody' : 'PlainTextBody');
+ $message_body = $this->emailTemplate->GetField($is_html ? 'HtmlBody' : 'PlainTextBody');
if ( !trim($message_body) && !$is_html ) {
// no plain text part available -> make it from html part then
- $message_body = $this->sender->ConvertToText($this->emailEvent->GetField('HtmlBody'), true);
+ $message_body = $this->sender->ConvertToText($this->emailTemplate->GetField('HtmlBody'), true);
}
if ( !trim($message_body) ) {
return false;
}
if ( isset($this->params['use_custom_design']) && $this->params['use_custom_design'] ) {
- $message_body = $this->_parseText($message_body);
+ $message_body = $this->_parseText($message_body, $is_html);
}
else {
$message_body = $this->_applyMessageDesign($message_body, $is_html);
@@ -781,10 +782,11 @@
* Parse message template and return headers (as array) and message body part
*
* @param string $text
+ * @param bool $is_html
* @return string
* @access protected
*/
- protected function _parseText($text)
+ protected function _parseText($text, $is_html = true)
{
$text = $this->_substituteReplacementTags($text);
@@ -796,15 +798,15 @@
$this->Application->InitParser();
$parser_params = $this->Application->Parser->Params; // backup parser params
- $this->Application->Parser->SetParams( array_merge($parser_params, $this->params) );
+ $this->Application->Parser->SetParams($this->params);
- $text = $this->Application->Parser->Parse($text, 'email_template');
+ $text = $this->Application->Parser->Parse($this->_normalizeLineEndings($text), 'email_template');
$this->Application->Parser->SetParams($parser_params); // restore parser params
$category_helper = $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
- return $category_helper->replacePageIds($this->_normalizeLineEndings($text));
+ return $category_helper->replacePageIds($is_html ? $this->_removeTrailingLineEndings($text) : $text);
}
/**
@@ -822,7 +824,7 @@
' 'emailEvent->GetDBField('ReplacementTags');
+ $replacement_tags = $this->emailTemplate->GetDBField('ReplacementTags');
$replacement_tags = $replacement_tags ? unserialize($replacement_tags) : Array ();
$replacement_tags = array_merge($default_replacement_tags, $replacement_tags);
@@ -834,18 +836,26 @@
}
/**
- * Normalizes line endings in given text
+ * Convert Unix/Windows/Mac line ending into Unix line endings
*
* @param string $text
* @return string
* @access protected
*/
protected function _normalizeLineEndings($text)
{
- // remove trailing line endings
- $text = preg_replace('/(\n|\r)+/', "\\1", $text);
-
- // convert Unix/Windows/Mac line ending into Unix line endings
return str_replace(Array ("\r\n", "\r"), "\n", $text);
}
+
+ /**
+ * Remove trailing line endings
+ *
+ * @param $text
+ * @return string
+ * @access protected
+ */
+ protected function _removeTrailingLineEndings($text)
+ {
+ return preg_replace('/(\n|\r)+/', "\\1", $text);
+ }
}
\ No newline at end of file