Index: branches/5.2.x/core/units/email_events/email_events_event_handler.php =================================================================== diff -u -N -r14861 -r14941 --- branches/5.2.x/core/units/email_events/email_events_event_handler.php (.../email_events_event_handler.php) (revision 14861) +++ branches/5.2.x/core/units/email_events/email_events_event_handler.php (.../email_events_event_handler.php) (revision 14941) @@ -1,6 +1,6 @@ 'VariableValue', 'site-domain' => 'DefaultEmailRecipients'); + + if ( isset($mapping[$event->Special]) ) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $target_object =& $this->Application->recallObject($event->Special); + /* @var $target_object kDBList */ + + $object->SetDBField('Recipients', $target_object->GetDBField($mapping[$event->Special])); + } + } + + /** * Set default headers * * @param kEvent $event @@ -276,7 +299,7 @@ // still nothing, set defaults if (!$email) { - $email = $this->Application->ConfigValue('Smtp_AdminMailFrom'); + $email = $this->Application->ConfigValue('DefaultEmailSender'); } if (!$name) { @@ -296,6 +319,7 @@ * * @param kEvent $event * @param Array $direct_params + * @return Array */ function _processRecipients(&$event, $direct_params = Array ()) { @@ -306,47 +330,30 @@ $to_email = $to_name = ''; $all_recipients = Array (); - $recipients_xml = $object->GetDBField('Recipients'); - if ($recipients_xml) { - $minput_helper =& $this->Application->recallObject('MInputHelper'); - /* @var $minput_helper MInputHelper */ + $this->_addRecipientsFromXml($all_recipients, $object->GetDBField('Recipients')); - // group recipients by type - $records = $minput_helper->parseMInputXML($recipients_xml); - - foreach ($records as $record) { - $recipient_type = $record['RecipientType']; - - if (!array_key_exists($recipient_type, $all_recipients)) { - $all_recipients[$recipient_type] = Array (); - } - - $all_recipients[$recipient_type][] = $record; - } - } - - if (!array_key_exists(EmailEvent::RECIPIENT_TYPE_TO, $all_recipients)) { + if ( !array_key_exists(EmailEvent::RECIPIENT_TYPE_TO, $all_recipients) ) { $all_recipients[EmailEvent::RECIPIENT_TYPE_TO] = Array (); } // remove all "To" recipients, when not allowed $overwrite_to_email = array_key_exists('overwrite_to_email', $direct_params) ? $direct_params['overwrite_to_email'] : false; - if (!$object->GetDBField('CustomRecipient') || $overwrite_to_email) { + if ( !$object->GetDBField('CustomRecipient') || $overwrite_to_email ) { $all_recipients[EmailEvent::RECIPIENT_TYPE_TO] = Array (); } // update with custom data given during event execution (user_id) $to_user_id = $event->getEventParam('EmailEventToUserId'); - if ($to_user_id > 0) { + if ( $to_user_id > 0 ) { $sql = 'SELECT FirstName, LastName, Email FROM ' . TABLE_PREFIX . 'PortalUser WHERE PortalUserId = ' . $to_user_id; $user_info = $this->Conn->GetRow($sql); - if ($user_info) { + if ( $user_info ) { $add_recipient = Array ( 'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL, 'RecipientAddress' => $user_info['Email'], @@ -361,31 +368,31 @@ $user->Load($to_user_id); } } - elseif (is_numeric($to_user_id)) { + elseif ( is_numeric($to_user_id) ) { // recipient is system user with negative ID (root, guest, etc.) -> send to admin - array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $this->_getDefaultRepipient()); + $this->_addDefaultRecipient($all_recipients); } // update with custom data given during event execution (email + name) $add_recipient = Array (); - if (array_key_exists('to_email', $direct_params)) { + if ( array_key_exists('to_email', $direct_params) ) { $add_recipient['RecipientName'] = ''; $add_recipient['RecipientAddressType'] = EmailEvent::ADDRESS_TYPE_EMAIL; $add_recipient['RecipientAddress'] = $direct_params['to_email']; } - if (array_key_exists('to_name', $direct_params)) { + if ( array_key_exists('to_name', $direct_params) ) { $add_recipient['RecipientName'] = $direct_params['to_name']; } - if ($add_recipient) { + if ( $add_recipient ) { array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $add_recipient); } - if (($object->GetDBField('Type') == EmailEvent::EVENT_TYPE_ADMIN) && !$all_recipients[EmailEvent::RECIPIENT_TYPE_TO]) { + if ( ($object->GetDBField('Type') == EmailEvent::EVENT_TYPE_ADMIN) && !$all_recipients[EmailEvent::RECIPIENT_TYPE_TO] ) { // admin email event without direct recipient -> send to admin - array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $this->_getDefaultRepipient()); + $this->_addDefaultRecipient($all_recipients); } $esender =& $this->Application->recallObject('EmailSender'); @@ -397,7 +404,7 @@ EmailEvent::RECIPIENT_TYPE_BCC => 'Bcc', ); - $default_email = $this->Application->ConfigValue('Smtp_AdminMailFrom'); + $default_email = $this->Application->ConfigValue('DefaultEmailSender'); foreach ($all_recipients as $recipient_type => $recipients) { // add recipients to email @@ -406,11 +413,11 @@ foreach ($recipients as $recipient) { $address = $recipient['RecipientAddress']; $address_type = $recipient['RecipientAddressType']; - $repipient_name = $recipient['RecipientName']; + $recipient_name = $recipient['RecipientName']; switch ($address_type) { case EmailEvent::ADDRESS_TYPE_EMAIL: - $pairs[] = Array ('email' => $address, 'name' => $repipient_name); + $pairs[] = Array ('email' => $address, 'name' => $recipient_name); break; case EmailEvent::ADDRESS_TYPE_USER: @@ -425,7 +432,7 @@ $pairs[] = Array ( 'email' => $user_info['Email'], - 'name' => $name ? $name : $repipient_name, + 'name' => $name ? $name : $recipient_name, ); } break; @@ -438,23 +445,23 @@ WHERE g.Name = ' . $this->Conn->qstr($address); $users = $this->Conn->Query($sql); - foreach ($users as $user) { + foreach ($users as $user_info) { $name = trim($user_info['FirstName'] . ' ' . $user_info['LastName']); $pairs[] = Array ( 'email' => $user_info['Email'], - 'name' => $name ? $name : $repipient_name, + 'name' => $name ? $name : $recipient_name, ); } break; } } - if (!$pairs) { + if ( !$pairs ) { continue; } - if ($recipient_type == EmailEvent::RECIPIENT_TYPE_TO) { + if ( $recipient_type == EmailEvent::RECIPIENT_TYPE_TO ) { $to_email = $pairs[0]['email'] ? $pairs[0]['email'] : $default_email; $to_name = $pairs[0]['name'] ? $pairs[0]['name'] : $to_email; } @@ -475,22 +482,63 @@ /** * This is default recipient, when we can't determine actual one * - * @return Array + * @param Array $recipients + * @return void */ - function _getDefaultRepipient() + function _addDefaultRecipient(&$recipients) { - return Array ( - 'RecipientName' => $this->Application->ConfigValue('Smtp_AdminMailFrom'), - 'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL, - 'RecipientAddress' => $this->Application->ConfigValue('Smtp_AdminMailFrom'), - ); + $xml = $this->Application->ConfigValue('DefaultEmailRecipients'); + + if ( !$this->_addRecipientsFromXml($recipients, $xml) ) { + $recipient = Array ( + 'RecipientName' => $this->Application->ConfigValue('DefaultEmailSender'), + 'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL, + 'RecipientAddress' => $this->Application->ConfigValue('DefaultEmailSender'), + ); + + array_unshift($recipients[EmailEvent::RECIPIENT_TYPE_TO], $recipient); + } } /** + * Adds multiple recipients from an XML + * + * @param Array $recipients + * @param string $xml + * @return bool + * @access protected + */ + protected function _addRecipientsFromXml(&$recipients, $xml) + { + if ( !$xml ) { + return false; + } + + $minput_helper =& $this->Application->recallObject('MInputHelper'); + /* @var $minput_helper MInputHelper */ + + // group recipients by type + $records = $minput_helper->parseMInputXML($xml); + + foreach ($records as $record) { + $recipient_type = $record['RecipientType']; + + if ( !array_key_exists($recipient_type, $recipients) ) { + $recipients[$recipient_type] = Array (); + } + + $recipients[$recipient_type][] = $record; + } + + return true; + } + + /** * Returns email event message by ID (headers & body in one piece) * * @param kEvent $event * @param int $language_id + * @return string */ function _getMessageBody(&$event, $language_id = null) { @@ -1069,7 +1117,7 @@ $address = $this->Application->GetVar('value'); $limit = $this->Application->GetVar('limit'); - if (!$limit) { + if ( !$limit ) { $limit = 20; } @@ -1088,9 +1136,13 @@ $field = 'Name'; $table_name = TABLE_PREFIX . 'PortalGroup'; break; + + default: + $field = $table_name = ''; + break; } - if (isset($field)) { + if ( $field ) { $sql = 'SELECT DISTINCT ' . $field . ' FROM ' . $table_name . ' WHERE ' . $field . ' LIKE ' . $this->Conn->qstr($address . '%') . '