From 5e867b378e7004f4d858c78363334eec7a176194 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 12 Oct 2014 02:37:43 -0700 Subject: [PATCH] Corrected Admin->Mail 'Send now' functionality - replacing old prototype.js code. --- e107_admin/mailout.php | 179 +++++++++++++++----------- e107_handlers/form_handler.php | 19 ++- e107_handlers/mail.php | 17 ++- e107_handlers/mail_manager_class.php | 60 ++++++--- e107_handlers/mailout_admin_class.php | 28 ++-- e107_plugins/forum/forum_update.php | 2 +- e107_web/js/core/admin.jquery.js | 10 +- 7 files changed, 199 insertions(+), 116 deletions(-) diff --git a/e107_admin/mailout.php b/e107_admin/mailout.php index 30c6e9cf5..3b0fb8823 100644 --- a/e107_admin/mailout.php +++ b/e107_admin/mailout.php @@ -66,7 +66,7 @@ Valid subparameters (where required): $_GET['m'] - id of mail info in db $_GET['t'] - id of target info in db */ -header('Content-Encoding: none'); // turn off gzip. +// header('Content-Encoding: none'); // turn off gzip. require_once('../class2.php'); if (!getperms('W')) @@ -85,6 +85,73 @@ require_once(e_HANDLER.'mailout_admin_class.php'); // Admin tasks handler require_once(e_HANDLER.'mail_manager_class.php'); // Mail DB API +/** + * Display Progress-bar of real-time mail-out. + * @return + */ +function sendProgress($id) +{ + $pref = e107::getPref(); + + $mailManager = new e107MailManager(); + $mailManager->doEmailTask(varset($pref['mail_workpertick'],5)); + + $sqld = e107::getDb(); + + $sqld->select("mail_content","mail_togo_count,mail_sent_count,mail_fail_count","mail_source_id= ".intval($id) ); + $row = $sqld->fetch(); + + $rand = ($row['mail_sent_count'] + $row['mail_fail_count']); + $total = ($row['mail_togo_count'] + $row['mail_sent_count'] + $row['mail_fail_count']); + + // $rand = rand(90,100); + // return $rand; + + $inc = round(($rand / $total) * 100); + + return $inc; + + +} + +if(e_AJAX_REQUEST) +{ + $id = intval($_GET['mode']); + // echo rand(92,100); + echo sendProgress($id); + exit; + +} + + + +if(vartrue($_GET['mode']) == "progress") +{ +// session_write_close(); +// sendProgress(); +// exit; +} + + +$mes = e107::getMessage(); +$tp = e107::getParser(); +/* +if($_GET['mode']=="process") +{ + session_write_close(); // allow other scripts to run in parallel. + header('Content-Encoding: none'); + ignore_user_abort(true); + set_time_limit(0); + + header("Content-Length: $size"); + header('Connection: close'); + + $mailManager = new e107MailManager(); + $mailManager->doEmailTask(999999); + echo "Completed Mailout ID: ".$_GET['id']; + exit; +} +*/ class mailout_admin extends e_admin_dispatcher @@ -354,10 +421,10 @@ class mailout_main_ui extends e_admin_ui { $ret = $this->processData($new_data); - $ret['mail_create_date'] = time(); - $ret['mail_creator'] = USERID; - $ret['mail_create_app'] = 'core'; - $ret['mail_content_status'] = 22; // Default status is 'Saved'; + $ret['mail_create_date'] = time(); + $ret['mail_creator'] = USERID; + $ret['mail_create_app'] = 'core'; + $ret['mail_content_status'] = MAIL_STATUS_TEMP; return $ret; @@ -409,9 +476,15 @@ class mailout_main_ui extends e_admin_ui } - private function emailSendNow($mailId) + private function emailSendNow($id) { - e107::getMessage()->addInfo("'Send Now' is Under Construction"); + $mes = e107::getMessage(); + $text = e107::getForm()->progressBar('mail-progress',1, array('btn-label'=>'Start', 'url'=> e_SELF)); + + $mes->setTitle('Ready to Process Mail Queue', E_MESSAGE_INFO)->addInfo($text); + return ''; + + //; e107::getRender()->tablerender("Sending...", $mes->render()); } @@ -1174,7 +1247,7 @@ class mailout_recipients_ui extends e_admin_ui 'mail_recipient_id' => array('title' => LAN_MAILOUT_142, 'thclass' => 'center'), 'mail_recipient_name' => array('title' => LAN_MAILOUT_141, 'forced' => TRUE), 'mail_recipient_email' => array('title' => LAN_MAILOUT_140, 'thclass' => 'left', 'forced' => TRUE), - 'mail_status' => array('title' => LAN_MAILOUT_138, 'thclass' => 'left', 'class'=>'left', 'proc' => 'contentstatus'), + 'mail_status' => array('title' => LAN_MAILOUT_138, 'type'=>'method', 'thclass' => 'left', 'class'=>'left', 'proc' => 'contentstatus'), 'mail_detail_id' => array('title' => LAN_MAILOUT_137, 'type'=>'dropdown', 'filter'=>true), 'mail_send_date' => array('title' => LAN_MAILOUT_139, 'proc' => 'sdatetime'), 'mail_target_info' => array('title' => LAN_MAILOUT_148, 'proc' => 'array'), @@ -1184,15 +1257,22 @@ class mailout_recipients_ui extends e_admin_ui protected $fieldpref = array('checkboxes', 'mail_target_id', 'mail_recipient_name', 'mail_recipient_email', 'mail_detail_id', 'mail_status', 'options'); + public $mailManager = null; function init() { + + $this->mailManager = new e107MailManager; + + + $sql = e107::getDb(); $sql->gen("SELECT r.mail_detail_id,c.mail_title FROM #mail_recipients AS r LEFT JOIN #mail_content as c ON r.mail_detail_id = c.mail_source_id GROUP BY r.mail_detail_id"); - + while($row = $sql->fetch()) { - $array[] = $row['mail_title']; + $id = $row['mail_detail_id']; + $array[$id] = varset($row['mail_title'], "(No Name)"); } $this->fields['mail_detail_id']['writeParms'] = $array; @@ -1218,7 +1298,19 @@ class mailout_recipients_ui extends e_admin_ui class mailout_recipients_form_ui extends e_admin_form_ui { - + public function mail_status($curVal,$mode) + { + if($mode == 'read') + { + return $this->getController()->mailManager->statusToText($curVal); + } + + if($mode == 'write') + { + return $curVal; + } + + } } @@ -1241,33 +1333,8 @@ new mailout_admin(); $e_sub_cat = 'mail'; -if(vartrue($_GET['mode']) == "progress") -{ - session_write_close(); - sendProgress(); - exit; -} -$mes = e107::getMessage(); -$tp = e107::getParser(); - -if($_GET['mode']=="process") -{ - session_write_close(); // allow other scripts to run in parallel. - header('Content-Encoding: none'); - ignore_user_abort(true); - set_time_limit(0); - - header("Content-Length: $size"); - header('Connection: close'); - - $mailManager = new e107MailManager(); - $mailManager->doEmailTask(999999); - echo "Completed Mailout ID: ".$_GET['id']; - exit; -} - @@ -1751,6 +1818,7 @@ require_once(e_ADMIN.'footer.php'); * @param integer $id (mailing id) * @return */ + /* function sendImmediately($id) { $mes = e107::getMessage(); @@ -1794,45 +1862,8 @@ function sendImmediately($id) e107::getRender()->tablerender("Sending...", $mes->render()); } +*/ -/** - * Display Progress-bar of real-time mail-out. - * @return - */ -function sendProgress() -{ - $sqld = e107::getDb(); - - $sqld->db_Select("mail_content","mail_togo_count,mail_sent_count,mail_fail_count","mail_source_id= ".intval($_GET['id']) ); - $row = $sqld -> db_Fetch(); - - $rand = $row['mail_sent_count'] + $row['mail_fail_count']; - - $total = $row['mail_togo_count'] + $row['mail_sent_count'] + $row['mail_fail_count']; - - // $rand = rand(1,20); - //$total = 20; - - $inc = round(($rand / $total) * 100); - - if($rand >= $total && $total !=0) - { - echo ""; - echo "
- Complete
"; - echo "
".$rand." / ".$total."
"; - return; - } - - echo "
"; - for($j=1;$j<=$inc;$j++) - { - echo ""; - } - echo " $inc %
"; - echo "
".$rand." / ".$total."
"; - return; -} diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php index 68d076420..802eb13f4 100644 --- a/e107_handlers/form_handler.php +++ b/e107_handlers/form_handler.php @@ -1104,11 +1104,24 @@ class e_form } $class = vartrue($options['class'],''); + $target = $this->name2id($name); - return "
-
+ $striped = (vartrue($options['btn-label'])) ? ' progress-striped active' : ''; + + $text = "
+
"; - + + $loading = vartrue($options['loading'],'Please wait...'); + + if(vartrue($options['btn-label'])) + { + $text .= ''.$options['btn-label'].''; + } + + + return $text; + } diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index 4e76e6900..1f6252126 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -138,7 +138,7 @@ define('MAIL_LOG_PATH',e_LOG); class e107Email extends PHPMailer { private $general_opts = array(); - private $logEnable = 0; // 0 = log disabled, 1 = 'dry run' (debug and log, no send). 2 = 'log all' (send, and log result) + private $logEnable = 1; // 0 = log disabled, 1 = 'dry run' (debug and log, no send). 2 = 'log all' (send, and log result) private $logHandle = FALSE; // Save handle of log file if opened private $localUseVerp = FALSE; // Use our own variable - PHPMailer one doesn't work with all mailers @@ -601,8 +601,6 @@ class e107Email extends PHPMailer */ public function arraySet($eml) { - - $tp = e107::getParser(); if(vartrue($eml['template'])) // @see e107_core/templates/email_template.php @@ -637,6 +635,8 @@ class e107Email extends PHPMailer { echo "

Couldn't find email template: ".$eml['template']."

"; } + $emailBody = $eml['email_body']; + if (vartrue($eml['email_subject'])) $this->Subject = $tp->parseTemplate($eml['email_subject'], true, varset($eml['shortcodes'],null)); e107::getMessage()->addDebug("Couldn't find email template: ".$eml['template']); } @@ -645,6 +645,7 @@ class e107Email extends PHPMailer else { if (vartrue($eml['email_subject'])) $this->Subject = $tp->parseTemplate($eml['email_subject'], true, varset($eml['shortcodes'],null)); + // $eml['email_body'] = ($tp->toEmail($tmpl['header']). str_replace('{BODY}', $eml['email_body'], $tmpl['body']). $tp->toEmail($tmpl['footer'])); } @@ -695,6 +696,8 @@ class e107Email extends PHPMailer if (varset($eml['wordwrap'])) $this->WordWrap = $eml['wordwrap']; if (vartrue($eml['split'])) $this->SingleTo = ($eml['split'] != FALSE); + $this->logLine("ArraySet Data:".print_r($eml,true)); + return 0; // No error } @@ -741,7 +744,7 @@ class e107Email extends PHPMailer { if (count($eml)) { // Set parameters from list - $ret = $this->arraySet($eml); + $ret = $this->arraySet($eml); // returns error on fail and nothing if all is okay. if ($ret) return $ret; } @@ -785,6 +788,12 @@ class e107Email extends PHPMailer $this->logLine("Send to {$to_name} at {$send_to} Mail-ID={$this->MessageID} - ".($result ? 'Success' : 'Fail')); + if(!$result) + { + $this->logLine(print_r($eml,true)); + } + + $this->ClearAddresses(); // In case we send another email $this->ClearCustomHeaders(); diff --git a/e107_handlers/mail_manager_class.php b/e107_handlers/mail_manager_class.php index b10a3fbe1..dc9f2c6d0 100644 --- a/e107_handlers/mail_manager_class.php +++ b/e107_handlers/mail_manager_class.php @@ -422,10 +422,9 @@ class e107MailManager } // Now array fields - $array = new ArrayData; if (isset($data['mail_other'])) { - $tmp = $array->ReadArray(str_replace('\\\'', '\'',$data['mail_other'])); // May have escaped data + $tmp = e107::unserialize(str_replace('\\\'', '\'',$data['mail_other'])); // May have escaped data if (is_array($tmp)) { $res = array_merge($res,$tmp); @@ -441,7 +440,7 @@ class e107MailManager } if (isset($data['mail_target_info'])) { - $tmp = $array->ReadArray(str_replace('\\\'', '\'',$data['mail_target_info'])); // May have escaped data + $tmp = e107::unserialize(str_replace('\\\'', '\'',$data['mail_target_info'])); // May have escaped data $res['mail_target_info'] = $tmp; } return $res; @@ -853,6 +852,9 @@ class e107MailManager } } if (isset($email['mail_overrides']) && is_array($email['mail_overrides'])) $result = array_merge($result, $email['mail_overrides']); + + e107::getAdminLog()->addDebug(print_a($email,true),false); + return $result; } @@ -878,6 +880,10 @@ class e107MailManager $this->mailer->allSent(); // Tidy up on completion } } + else + { + e107::getAdminLog()->addDebug("Couldn't select emails", false); + } } @@ -902,8 +908,7 @@ class e107MailManager { unset($dbData['mail_source_id']); // Just in case - there are circumstances where might be set $result = $this->db2->db_Insert('mail_content', array('data' => $dbData, - '_FIELD_TYPES' => $this->dbTypes['mail_content'], - '_NOTNULL' => $this->dbNull['mail_content'])); + '_FIELD_TYPES' => $this->dbTypes['mail_content'], '_NOTNULL' => $this->dbNull['mail_content'])); } else { @@ -1483,30 +1488,47 @@ class e107MailManager public function sendEmails($templateName, $emailData, $recipientData, $extra = FALSE) { - if (!is_array($emailData)) return FALSE; + $log = e107::getAdminLog(); + $log->addDebug(print_a($emailData, true),false); + $log->addDebug(print_a($recipientData, true),false); + $log->toFile('mail_manager','Main Manager Log',true); + + + if (!is_array($emailData)) + { + return FALSE; + } + if (!is_array($recipientData)) { $recipientData = array('mail_recipient_email' => $recipientData, 'mail_recipient_name' => $recipientData); } + $emailData['mail_content_status'] = MAIL_STATUS_TEMP; if ($templateName == '') { - $templateName = varset($email['mail_send_style'], 'textonly'); // Safest default if nothing specified + $templateName = varset($emailData['mail_send_style'], 'textonly'); // Safest default if nothing specified } + + $templateName = trim($templateName); if ($templateName == '') return FALSE; - + // Get template data, override email settings as appropriate - require_once(e_HANDLER.'mail_template_class.php'); - $ourTemplate = new e107MailTemplate(); - if (!$ourTemplate->setNewTemplate($templateName) && empty($emailData['template'])) return FALSE; // Probably template not found if error - if (!$ourTemplate->makeEmailBody($emailData['mail_body'], varset($emailData['mail_include_images'], TRUE))) return FALSE; // Create body text - $emailData['mail_body_templated'] = $ourTemplate->mainBodyText; - $this->currentMailBody = $emailData['mail_body_templated']; // In case we send immediately - $emailData['mail_body_alt'] = $ourTemplate->altBodyText; - $this->currentTextBody = $emailData['mail_body_alt']; + // require_once(e_HANDLER.'mail_template_class.php'); + // $ourTemplate = new e107MailTemplate(); + // if (!$ourTemplate->setNewTemplate($templateName) && empty($emailData['template'])) return FALSE; // Probably template not found if error + // if (!$ourTemplate->makeEmailBody($emailData['mail_body'], varset($emailData['mail_include_images'], TRUE))) return FALSE; // Create body text + + + +// $emailData['mail_body_templated'] = $ourTemplate->mainBodyText; + $this->currentMailBody = $emailData['mail_body']; // In case we send immediately +// $emailData['mail_body_alt'] = $ourTemplate->altBodyText; + $this->currentTextBody = strip_tags($emailData['mail_body']); + if (!isset($emailData['mail_overrides'])) { $emailData['mail_overrides'] = $ourTemplate->lastTemplateData['email_overrides']; @@ -1522,9 +1544,13 @@ class e107MailManager echo "

".$emailData['template']." Template detected

"; } } + + + - $forceQueue = FALSE; + + if (is_array($extra) && isset($extra['mail_force_queue'])) { $forceQueue = $extra['mail_force_queue']; diff --git a/e107_handlers/mailout_admin_class.php b/e107_handlers/mailout_admin_class.php index 4d26f9585..6edccb8e5 100644 --- a/e107_handlers/mailout_admin_class.php +++ b/e107_handlers/mailout_admin_class.php @@ -1470,6 +1470,7 @@ class mailoutAdminClass extends e107MailManager else { // Start by saving the email + /* $mailData['mail_content_status'] = MAIL_STATUS_TEMP; $mailData['mail_create_app'] = 'core'; $result = $this->saveEmail($mailData, TRUE); @@ -1482,11 +1483,11 @@ class mailoutAdminClass extends e107MailManager { e107::getMessage()->addDebug("Couldn't save email. (".__FILE__." Line: ".__LINE__.")"); } + */ - + $mailMainID = $mailData['mail_source_id']; - $this->mailInitCounters($mailMainID); - // Initialise counters for emails added + $this->mailInitCounters($mailMainID); // Initialise counters for emails added foreach($this->mailHandlers as $key => $m) { @@ -1494,16 +1495,15 @@ class mailoutAdminClass extends e107MailManager // all can use the $sql data object if($m->mailerEnabled && isset($mailData['mail_selectors'][$key])) { - // Initialise - $mailerCount = $m->selectInit($mailData['mail_selectors'][$key]); + + $mailerCount = $m->selectInit($mailData['mail_selectors'][$key]); // Initialise if($mailerCount > 0) { - // Get email addresses - add to list, strip duplicates - while($row = $m->selectAdd()) - { - // Add email addresses to the database ready for sending (the body is never saved - // in the DB - it gets passed as a $_POST value) - $result = $this->mailAddNoDup($mailMainID, $row, MAIL_STATUS_TEMP); + + while($row = $m->selectAdd()) // Get email addresses - add to list, strip duplicates + { + $result = $this->mailAddNoDup($mailMainID, $row, MAIL_STATUS_TEMP); // Add email addresses to the database ready for sending (the body is never saved // in the DB - it gets passed as a $_POST value) + if($result === FALSE) { // Error @@ -1660,6 +1660,7 @@ class mailoutAdminClass extends e107MailManager return e107::getForm()->datepicker($calName, $calVal, $options); } + /** * Show recipients of an email * @@ -1667,6 +1668,7 @@ class mailoutAdminClass extends e107MailManager * @param $nextPage - 'mode' specification for page to return to following delete * @return text for display */ + /* public function showmailRecipients($mailID, $nextPage = 'saved') { $gen = new convert; @@ -1804,7 +1806,9 @@ class mailoutAdminClass extends e107MailManager $ns->tablerender(ADLAN_136 . SEP . LAN_MAILOUT_181, $mes->render() . $text); } - + */ + + /** * Clean up mailout DB * Dump array of results to admin log diff --git a/e107_plugins/forum/forum_update.php b/e107_plugins/forum/forum_update.php index 849ca103e..db6d8b1be 100644 --- a/e107_plugins/forum/forum_update.php +++ b/e107_plugins/forum/forum_update.php @@ -547,7 +547,7 @@ function renderProgress($caption, $step)
- '.$caption.' + '.$caption.' '; diff --git a/e107_web/js/core/admin.jquery.js b/e107_web/js/core/admin.jquery.js index 947a819fa..6b24ca0aa 100644 --- a/e107_web/js/core/admin.jquery.js +++ b/e107_web/js/core/admin.jquery.js @@ -205,12 +205,12 @@ $(document).ready(function() - + $('.e-progress').on('click', function(e) { // alert('Process Started'); - - var target = 'progress'; + + var target = $(this).attr('data-progress-target'); var script = $(this).attr('data-progress'); var show = $(this).attr('data-progress-show'); var hide = $(this).attr('data-progress-hide'); @@ -235,8 +235,8 @@ $(document).ready(function() if(data > 99.999) { clearInterval(progresspump); - - $("#progressouter").removeClass("active"); + $("#"+target).closest('.progress').removeClass("active"); + $("#"+target).html("Done"); if(hide !== 'undefined')