Index: branches/RC/core/units/email_messages/email_messages_event_handler.php =================================================================== diff -u -r8929 -r9047 --- branches/RC/core/units/email_messages/email_messages_event_handler.php (.../email_messages_event_handler.php) (revision 8929) +++ branches/RC/core/units/email_messages/email_messages_event_handler.php (.../email_messages_event_handler.php) (revision 9047) @@ -151,6 +151,167 @@ { return preg_replace('/(\n|\r)+/',"\\1",$string); } + + /** + * Prepares selected user(-s) or group(-s) for message sending + * + * @param kEvent $event + */ + function OnPrepareMassRecipients(&$event) + { + $object =& $event->getObject( Array('skip_autoload' => true) ); + /* @var $object kDBItem */ + + $object->Clear(0); + + $event->redirect = false; + + $this->Application->RemoveVar('recipient_ids'); + $this->Application->RemoveVar('recipient_type'); + + $this->saveMassRecipients('u'); + $this->saveMassRecipients('g', 'total'); + } + + function saveMassRecipients($prefix, $special = '') + { + $recipients = $this->Application->GetVar(rtrim($prefix.'_'.$special, '_')); + if ($recipients) { + $this->Application->StoreVar('recipient_ids', implode(',', array_keys($recipients))); + $this->Application->StoreVar('recipient_type', $prefix); + } + } + + /** + * Sends mass mail + * + * @param kEvent $event + */ + function OnMassMail(&$event) + { + $object =& $event->getObject( Array('skip_autoload' => true) ); + /* @var $object kDBItem */ + + $object->setRequired('MassSubject', true); + + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + if ($items_info) { + list($id, $field_values) = each($items_info); + $object->SetFieldsFromHash($field_values); + } + + if (!$object->Validate()) { + $event->redirect = false; + $event->status = erFAIL; + $object->setID($id); + return ; + } + + $esender =& $this->Application->recallObject('EmailSender'); + /* @var $esender kEmailSendingHelper */ + + $fields_hash = $object->GetFieldValues(); + list ($fields_hash['FromEmail'], $fields_hash['FromName']) = $this->getSenderData(); + + if ($fields_hash['MassAttachment']) { + $field_options = $object->GetFieldOptions('MassAttachment'); + $fields_hash['MassAttachment'] = $field_options['upload_dir'].$fields_hash['MassAttachment']; + } + + $recipients = $this->getRecipientEmails(); + foreach ($recipients as $recipient_email) { + $this->sendMessage($fields_hash, $recipient_email, $esender); + } + + // remove any temporary data + $this->Application->RemoveVar('recipient_ids'); + $this->Application->RemoveVar('recipient_type'); + $this->Application->RemoveVar('email_queue_progress'); + if ($fields_hash['MassAttachment']) { + unlink(FULL_PATH.$fields_hash['MassAttachment']); + } + + $event->redirect = 'emails/send_queue'; + } + + + function getRecipientEmails() + { + $recipient_type = $this->Application->RecallVar('recipient_type'); + $recipient_ids = $this->Application->RecallVar('recipient_ids'); + + if ($recipient_type == 'u') { + $sql = 'SELECT Email + FROM '.TABLE_PREFIX.'PortalUser + WHERE PortalUserId IN ('.$recipient_ids.')'; + } + else { + $sql = 'SELECT u.Email + FROM '.TABLE_PREFIX.'UserGroup ug + LEFT JOIN '.TABLE_PREFIX.'PortalUser u ON ug.PortalUserId = u.PortalUserId + WHERE ug.GroupId IN ('.$recipient_ids.')'; + } + + return $this->Conn->GetCol($sql); + } + + /** + * Puts message to email queue for sending + * + * @param Array $fields_hash + * @param string $to_email recipient's email address + * @param kEmailSendingHelper $esender + */ + function sendMessage($fields_hash, $to_email, &$esender) + { + $esender->SetFrom($fields_hash['FromEmail'], $fields_hash['FromName']); + + $esender->AddTo($to_email); + $esender->SetSubject($fields_hash['MassSubject']); + $esender->SetBody($fields_hash['MassHtmlMessage'], $fields_hash['MassTextMessage']); + + // add attachment if any + if ($fields_hash['MassAttachment']) { + $esender->AddAttachment(FULL_PATH.$fields_hash['MassAttachment']); + } + + $status = $esender->Deliver(null, false); + + if ($status) { + // write to log + $fields_hash = Array ( + 'fromuser' => $fields_hash['FromName'], + 'addressto' => $to_email, + 'subject' => $fields_hash['MassSubject'], + 'timestamp' => adodb_mktime(), + 'event' => '', + ); + + $this->Conn->doInsert($fields_hash, TABLE_PREFIX.'EmailLog'); + } + } + + /** + * Returns mass mail sender name & email + * + * @return Array + */ + function getSenderData() + { + $user =& $this->Application->recallObject('u.current'); + /* @var $user UsersItem */ + + if ($user->GetID() > 0) { + $email_address = $user->GetDBField('Email'); + $name = $user->GetDBField('FirstName').' '.$user->GetDBField('LastName'); + } + else { + $email_address = $this->Application->ConfigValue('Smtp_AdminMailFrom'); + $name = strip_tags( $this->Application->ConfigValue('Site_Name') ); + } + + return Array ($email_address, $name); + } } ?> \ No newline at end of file