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 15908) +++ core/units/helpers/mailing_list_helper.php (revision ) @@ -113,10 +113,12 @@ } /** - * Generates recipients emails based on "To" field value + * Generates recipients emails based on "To" field value. * - * @param int $id - * @param Array $fields_hash + * @param int $id Id. + * @param Array $fields_hash Fields hash. + * + * @return array */ function generateRecipients($id, $fields_hash) { @@ -242,7 +244,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]); } @@ -251,7 +253,7 @@ $table_name = $config->getTableName(); // update sent email count for each processed mailing - foreach ($mailing_totals as $mailing_id => $mailing_total) { + foreach ( $mailing_totals as $mailing_id => $mailing_total ) { $sql = 'UPDATE ' . $table_name . ' SET EmailsSent = EmailsSent + ' . $mailing_total . ' WHERE ' . $config->getIDField() . ' = ' . $mailing_id; @@ -266,37 +268,42 @@ } /** - * Sent given messages from email queue + * Sent given messages from email queue. * - * @param Array $messages + * @return integer */ - function processQueue(&$messages) + function processQueue() { $esender = $this->Application->recallObject('EmailSender'); /* @var $esender kEmailSendingHelper */ - $queue_table = $this->Application->getUnitConfig('email-queue')->getTableName(); + $messages = $this->getMessages(); + $message_count = count($messages); + if ( !$message_count ) { + return 0; + } + $i = 0; - $message = Array (); + $message = Array(); - $mailing_totals = Array (); + $mailing_totals = Array(); - $message_count = count($messages); + $queue_table = $this->Application->getUnitConfig('email-queue')->getTableName(); - while ($i < $message_count) { + 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]++; @@ -312,5 +319,68 @@ } $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->getUnitConfig('email-queue')->getTableName(); + + 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/kernel/constants.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/kernel/constants.php (revision 15908) +++ core/kernel/constants.php (revision ) @@ -199,12 +199,17 @@ const PS_USER = 'USER'; } -class CategoryPermissionRebuild { - const MANUAL = 1; - const SILENT = 2; - const AUTOMATIC = 3; -} + class CategoryPermissionRebuild { + const MANUAL = 1; + const SILENT = 2; + const AUTOMATIC = 3; + } + class EmailDelivery { + const QUEUE = 1; + const IMMEDIATE = 2; + } + class TranslationSaveMode { const SYNC_WITH_PRIMARY = 1; const MAKE_PRIMARY = 2; \ 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 15908) +++ 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'), @@ -215,72 +214,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/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,99 @@ + 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 + * + * @return void + */ + protected function OnProcess(kEvent $event) + { + $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); + /* @var $mailing_list_helper MailingListHelper */ + + $mailing_list_helper->processQueue(); + } +} Index: core/install/upgrades.sql IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/upgrades.sql (revision 15913) +++ core/install/upgrades.sql (revision ) @@ -2944,3 +2944,5 @@ UPDATE LanguageLabels SET l1_HintTranslation = '