Index: core/units/helpers/mailing_list_helper.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/helpers/mailing_list_helper.php (revision 15682) +++ core/units/helpers/mailing_list_helper.php (revision ) @@ -241,7 +241,7 @@ */ function _updateSentTotals($mailing_totals) { - if (array_key_exists(0, $mailing_totals)) { + if ( array_key_exists(0, $mailing_totals) ) { // don't update sent email count for mails queued directly (not via mailing lists) unset($mailing_totals[0]); } @@ -267,37 +267,45 @@ /** * Sent given messages from email queue * - * @param Array $messages + * @return int + * @access public */ - function processQueue(&$messages) + function processQueue() { $esender = $this->Application->recallObject('EmailSender'); /* @var $esender kEmailSendingHelper */ - $queue_table = $this->Application->getUnitOption('email-queue', 'TableName'); + $messages = $this->getMessages(); + $message_count = count($messages); + if ( !$message_count ) { + return 0; + } + $i = 0; $message = Array (); $mailing_totals = Array (); - $message_count = count($messages); + $queue_table = $this->Application->getUnitOption('email-queue', 'TableName'); while ($i < $message_count) { $message[0] = unserialize($messages[$i]['MessageHeaders']); $message[1] =& $messages[$i]['MessageBody']; - $esender->setLogData( unserialize($messages[$i]['LogData']) ); + $esender->setLogData(unserialize($messages[$i]['LogData'])); $delivered = $esender->Deliver($message, true); // immediate send! - if ($delivered) { + if ( $delivered ) { // send succeeded, delete from queue $sql = 'DELETE FROM ' . $queue_table . ' WHERE EmailQueueId = ' . $messages[$i]['EmailQueueId']; $this->Conn->Query($sql); $mailing_id = $messages[$i]['MailingId']; + - if (!array_key_exists($mailing_id, $mailing_totals)) { + if ( !array_key_exists($mailing_id, $mailing_totals) ) { $mailing_totals[$mailing_id] = 0; } + $mailing_totals[$mailing_id]++; } else { @@ -311,5 +319,66 @@ } $this->_updateSentTotals($mailing_totals); + + return $message_count; + } + + /** + * Returns queued messages (or their count), that can be sent + * + * @param bool $count_only + * @return Array|int + * @access public + */ + public function getMessages($count_only = false) + { + $deliver_count = $this->getSetting('MailingListSendPerStep'); + + if ( !is_numeric($deliver_count) ) { + return $count_only ? 0 : Array (); + } + + $queue_table = $this->Application->getUnitOption('email-queue', 'TableName'); + + if ( $count_only ) { + $sql = 'SELECT COUNT(*) + FROM ' . $queue_table . ' + WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ')'; + + return $this->Conn->GetOne($sql); + } + + // regular e-mails are pressed before mailing generated ones ! + $sql = 'SELECT * + FROM ' . $queue_table . ' + WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ') + ORDER BY MailingId ASC + LIMIT 0,' . $deliver_count; + + return $this->Conn->Query($sql); + } + + /** + * Allows to safely get mailing configuration variable + * + * @param string $variable_name + * @return int + * @access public + */ + public function getSetting($variable_name) + { + $value = $this->Application->ConfigValue($variable_name); + + if ( $value === false ) { + // ensure default value, when configuration variable is missing + return 10; + } + + if ( !$value ) { + // configuration variable found, but it's value is empty or zero + return false; + } + + return $value; } } \ No newline at end of file Index: core/units/email_templates/email_template_eh.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/email_templates/email_template_eh.php (revision 15682) +++ core/units/email_templates/email_template_eh.php (revision ) @@ -30,7 +30,6 @@ $permissions = Array ( 'OnFrontOnly' => Array ('self' => 'edit'), 'OnSaveSelected' => Array ('self' => 'view'), - 'OnProcessEmailQueue' => Array ('self' => 'add|edit'), 'OnExportEmailTemplates' => Array ('self' => 'view'), 'OnSuggestAddress' => Array ('self' => 'add|edit'), @@ -217,72 +216,6 @@ function OnSaveSelected($event) { $this->StoreSelectedIDs($event); - } - - /** - * Process emails from queue - * - * @param kEvent $event - * @todo Move to MailingList - */ - function OnProcessEmailQueue($event) - { - $deliver_count = $event->getEventParam('deliver_count'); - if ( $deliver_count === false ) { - $deliver_count = $this->Application->ConfigValue('MailingListSendPerStep'); - if ( $deliver_count === false ) { - $deliver_count = 10; // 10 emails per script run (if not specified directly) - } - } - - $processing_type = $this->Application->GetVar('type'); - if ( $processing_type = 'return_progress' ) { - $email_queue_progress = $this->Application->RecallVar('email_queue_progress'); - if ( $email_queue_progress === false ) { - $emails_sent = 0; - $sql = 'SELECT COUNT(*) - FROM ' . TABLE_PREFIX . 'EmailQueue - WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ')'; - $total_emails = $this->Conn->GetOne($sql); - $this->Application->StoreVar('email_queue_progress', $emails_sent . ':' . $total_emails); - } - else { - list ($emails_sent, $total_emails) = explode(':', $email_queue_progress); - } - } - - $sql = 'SELECT * - FROM ' . TABLE_PREFIX . 'EmailQueue - WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ') - LIMIT 0,' . $deliver_count; - $messages = $this->Conn->Query($sql); - - $message_count = count($messages); - if ( !$message_count ) { - // no messages left to send in queue - if ( $processing_type = 'return_progress' ) { - $this->Application->RemoveVar('email_queue_progress'); - $this->Application->Redirect($this->Application->GetVar('finish_template')); - } - return; - } - - $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); - /* @var $mailing_list_helper MailingListHelper */ - - $mailing_list_helper->processQueue($messages); - - if ( $processing_type = 'return_progress' ) { - $emails_sent += $message_count; - if ( $emails_sent >= $total_emails ) { - $this->Application->RemoveVar('email_queue_progress'); - $this->Application->Redirect($this->Application->GetVar('finish_template')); - } - - $this->Application->StoreVar('email_queue_progress', $emails_sent . ':' . $total_emails); - $event->status = kEvent::erSTOP; - echo ($emails_sent / $total_emails) * 100; - } } /** \ No newline at end of file Index: core/units/mailing_lists/mailing_lists_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/mailing_lists/mailing_lists_config.php (revision 15682) +++ core/units/mailing_lists/mailing_lists_config.php (revision ) @@ -33,7 +33,6 @@ 'ScheduledTasks' => Array ( 'generate_mailing_queue' => Array ('EventName' => 'OnGenerateEmailQueue', 'RunSchedule' => '*/30 * * * *'), - 'process_mailing_queue' => Array ('EventName' => 'OnProcessEmailQueue', 'RunSchedule' => '*/30 * * * *'), ), 'IDField' => 'MailingId', \ No newline at end of file Index: core/units/email_templates/email_templates_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/email_templates/email_templates_config.php (revision 15682) +++ core/units/email_templates/email_templates_config.php (revision ) @@ -62,8 +62,6 @@ // for mass mailing 'email_send_form' => Array ('prefixes' => Array (), 'format' => '!la_title_SendEmail!'), - 'email_send' => Array ('prefixes' => Array (), 'format' => '!la_title_SendingPreparedEmails!. !la_title_PleaseWait!'), - 'email_send_complete' => Array ('prefixes' => Array (), 'format' => '!la_title_SendMailComplete!'), ), 'EditTabPresets' => Array ( \ No newline at end of file Index: core/kernel/utility/email.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/kernel/utility/email.php (revision 15682) +++ core/kernel/utility/email.php (revision ) @@ -204,7 +204,7 @@ protected function _getCustomParams() { $ret = $this->params; - $send_keys = Array ('from_email', 'from_name', 'to_email', 'to_name', 'overwrite_to_email', 'language_id', 'use_custom_design'); + $send_keys = Array ('from_email', 'from_name', 'to_email', 'to_name', 'overwrite_to_email', 'language_id', 'use_custom_design', 'delivery'); foreach ($send_keys as $send_key) { unset($ret[$send_key]); @@ -217,11 +217,10 @@ * Sends e-mail now or puts it in queue * * @param int $recipient_user_id - * @param bool $immediate_send * @return bool * @access public */ - public function send($recipient_user_id = null, $immediate_send = true) + public function send($recipient_user_id = null) { $this->recipientUserId = $recipient_user_id; @@ -283,7 +282,9 @@ $this->sender->setLogData($log_fields_hash); } - return $this->sender->Deliver(null, $immediate_send); + $delivery = isset($this->params['delivery']) ? $this->params['delivery'] : $this->Application->ConfigValue('EmailDelivery'); + + return $this->sender->Deliver(null, $delivery == EmailDelivery::IMMEDIATE); } /** \ No newline at end of file Index: core/install/english.lang IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/english.lang (revision 15682) +++ core/install/english.lang (revision ) @@ -164,6 +164,7 @@ RGVmYXVsdCAiUGVyIFBhZ2UiIHNldHRpbmcgaW4gR3JpZHM= RGVmYXVsdCBSZWdpc3RyYXRpb24gQ291bnRyeQ== RGVmYXVsdCBBbmFseXRpY3MgVHJhY2tpbmcgQ29kZQ== + RW1haWwgRGVsaXZlcnk= S2VlcCAiRS1tYWlsIExvZyIgZm9y RW5hYmxlICJFLW1haWwgTG9nIg== RW5hYmxlIFJldmlzaW9uIENvbnRyb2wgZm9yIFNlY3Rpb24gQ29udGVudA== @@ -882,6 +883,8 @@ RWRpdG9yJ3MgUGljaw== RS1tYWls RS1tYWlsIEJvZHk= + SW1tZWRpYXRl + RW1haWwgUXVldWU= Rm9yZXZlciAobmV2ZXIgZGVsZXRlZCBhdXRvbWF0aWNhbGx5KQ== RS1tYWlsIFN1YmplY3Q= RS1tYWlsIFRlbXBsYXRlcw== Index: core/units/email_queue/email_queue_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/email_queue/email_queue_config.php (revision 15682) +++ core/units/email_queue/email_queue_config.php (revision ) @@ -18,7 +18,7 @@ 'Prefix' => 'email-queue', 'ItemClass' => Array('class' => 'kDBItem', 'file' => '', 'build_event' => 'OnItemBuild'), 'ListClass' => Array('class' => 'kDBList', 'file' => '', 'build_event' => 'OnListBuild'), - 'EventHandlerClass' => Array ('class' => 'kDBEventHandler', 'file' => '', 'build_event' => 'OnBuild'), + 'EventHandlerClass' => Array ('class' => 'EmailQueueEventHandler', 'file' => 'email_queue_eh.php', 'build_event' => 'OnBuild'), 'TagProcessorClass' => Array ('class' => 'EmailQueueTagProcessor', 'file' => 'email_queue_tp.php', 'build_event' => 'OnBuild'), 'AutoLoad' => true, @@ -31,12 +31,19 @@ 5 => 'mode', ), + 'ScheduledTasks' => Array ( + 'process_email_queue' => Array ('EventName' => 'OnProcess', 'RunSchedule' => '* * * * *'), + ), + 'IDField' => 'EmailQueueId', 'TableName' => TABLE_PREFIX . 'EmailQueue', 'TitlePresets' => Array ( 'email_queue_list' => Array ('prefixes' => Array('email-queue_List'), 'format' => '!la_tab_EmailQueue!',), + + 'email_send' => Array ('prefixes' => Array (), 'format' => '!la_title_SendingPreparedEmails!. !la_title_PleaseWait!'), + 'email_send_complete' => Array ('prefixes' => Array (), 'format' => '!la_title_SendMailComplete!'), ), 'PermSection' => Array ('main' => 'in-portal:email_queue'), \ No newline at end of file Index: core/install/install_data.sql IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/install_data.sql (revision 15682) +++ core/install/install_data.sql (revision ) @@ -89,6 +89,7 @@ INSERT INTO SystemSettings VALUES(DEFAULT, 'MailingListQueuePerStep', '10', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsMailling', 'la_config_MailingListQueuePerStep', 'text', NULL, NULL, 50.08, 0, 0, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'MailingListSendPerStep', '10', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsMailling', 'la_config_MailingListSendPerStep', 'text', NULL, NULL, 50.09, 0, 0, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'DefaultEmailRecipients', '', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsMailling', 'la_config_DefaultEmailRecipients', 'text', NULL, NULL, 50.10, 0, 0, NULL); +INSERT INTO SystemSettings VALUES(DEFAULT, 'EmailDelivery', '1', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsMailling', 'la_config_EmailDelivery', 'radio', NULL, '1=la_opt_EmailDeliveryQueue||2=la_opt_EmailDeliveryImmediate', 50.11, 0, 1, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'UseOutputCompression', '1', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsSystem', 'la_config_UseOutputCompression', 'checkbox', '', '', 60.01, 0, 1, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'OutputCompressionLevel', '7', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsSystem', 'la_config_OutputCompressionLevel', 'text', '', '', 60.02, 0, 1, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'UseTemplateCompression', '0', 'In-Portal', 'in-portal:configure_advanced', 'la_section_SettingsSystem', 'la_config_UseTemplateCompression', 'checkbox', '', '', 60.03, 0, 1, NULL); \ No newline at end of file Index: core/units/mailing_lists/mailing_list_eh.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/mailing_lists/mailing_list_eh.php (revision 15682) +++ core/units/mailing_lists/mailing_list_eh.php (revision ) @@ -30,7 +30,6 @@ $permissions = Array ( 'OnCancelMailing' => Array ('self' => 'edit'), 'OnGenerateEmailQueue' => Array ('self' => true), - 'OnProcessEmailQueue' => Array ('self' => true), 'OnGetHtmlBody' => Array ('self' => 'edit'), ); @@ -255,28 +254,6 @@ } /** - * Allows to safely get mailing configuration variables - * - * @param string $variable_name - * @return int - */ - function _ensureDefault($variable_name) - { - $value = $this->Application->ConfigValue($variable_name); - if ( $value === false ) { - // ensure default value, when configuration variable is missing - return 10; - } - - if ( !$value ) { - // configuration variable found, but it's value is empty or zero - return false; - } - - return $value; - } - - /** * Generates email queue for active mailing lists * * @param kEvent $event @@ -302,16 +279,15 @@ return; } - // queue 10 emails per step summary from all mailing lists (FIFO logic) - $to_queue = $this->_ensureDefault('MailingListQueuePerStep'); + $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); + /* @var $mailing_list_helper MailingListHelper */ - if ( $to_queue === false ) { + $to_queue = $mailing_list_helper->getSetting('MailingListQueuePerStep'); + + if ( !is_numeric($to_queue) ) { return; } - $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); - /* @var $mailing_list_helper MailingListHelper */ - foreach ($mailing_lists as $mailing_id => $mailing_data) { if ( $mailing_data['EmailsTotal'] == 0 ) { // no work performed on this mailing list -> calculate totals @@ -353,38 +329,6 @@ break; } } - } - - /** - * Process email queue from cron - * - * @param kEvent $event - */ - function OnProcessEmailQueue($event) - { - $deliver_count = $this->_ensureDefault('MailingListSendPerStep'); - if ($deliver_count === false) { - return ; - } - - $queue_table = $this->Application->getUnitOption('email-queue', 'TableName'); - - // get queue part to send - $sql = 'SELECT * - FROM ' . $queue_table . ' - WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ') - LIMIT 0,' . $deliver_count; - $messages = $this->Conn->Query($sql); - - if (!$messages) { - // no more messages left in queue - return ; - } - - $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); - /* @var $mailing_list_helper MailingListHelper */ - - $mailing_list_helper->processQueue($messages); } /** \ No newline at end of file Index: core/admin_templates/mailing_lists/send_complete.tpl IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/admin_templates/mailing_lists/send_complete.tpl (revision 15682) +++ core/admin_templates/mailing_lists/send_complete.tpl (revision ) @@ -1,7 +1,7 @@ - + \ No newline at end of file Index: core/units/email_queue/email_queue_eh.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/email_queue/email_queue_eh.php (revision ) +++ core/units/email_queue/email_queue_eh.php (revision ) @@ -0,0 +1,97 @@ + Array ('self' => 'view'), + 'OnProcess' => Array ('self' => true), + ); + + $this->permMapping = array_merge($this->permMapping, $permissions); + } + + /** + * Process emails from queue + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnProcessAjax(kEvent $event) + { + $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); + /* @var $mailing_list_helper MailingListHelper */ + + $emails_sent = 0; + $email_queue_progress = $this->Application->RecallVar('email_queue_progress'); + + if ( $email_queue_progress === false ) { + $total_emails = $mailing_list_helper->getMessages(true); + + $this->Application->StoreVar('email_queue_progress', $emails_sent . ':' . $total_emails); + } + else { + list ($emails_sent, $total_emails) = explode(':', $email_queue_progress); + } + + $message_count = $mailing_list_helper->processQueue(); + + if ( !$message_count ) { + // no messages left to send in queue + $this->Application->RemoveVar('email_queue_progress'); + $this->Application->Redirect($this->Application->GetVar('finish_template')); + + return; + } + + $emails_sent += $message_count; + + if ( $emails_sent >= $total_emails ) { + $this->Application->RemoveVar('email_queue_progress'); + $this->Application->Redirect($this->Application->GetVar('finish_template')); + } + + $this->Application->StoreVar('email_queue_progress', $emails_sent . ':' . $total_emails); + $event->status = kEvent::erSTOP; + + echo ($emails_sent / $total_emails) * 100; + } + + /** + * [SCHEDULED TASK] Process email queue from cron + * + * @param kEvent $event + */ + function OnProcess($event) + { + $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); + /* @var $mailing_list_helper MailingListHelper */ + + $mailing_list_helper->processQueue(); + } +}