From b10b13b4b86370438741bcea35c939ef748ebe0f Mon Sep 17 00:00:00 2001 From: wangyidong Date: Mon, 8 Jan 2018 15:50:53 +0800 Subject: [PATCH] * finish task #3562. --- config/zentaopms.php | 3 +-- db/update9.7.sql | 16 ++++++++++++ db/zentao.sql | 16 ++++++++++++ module/mail/control.php | 26 +++++++++---------- module/mail/lang/en.php | 4 +-- module/mail/lang/zh-cn.php | 4 +-- module/mail/model.php | 37 ++++++++++++++------------- module/mail/view/browse.html.php | 18 ++++++------- module/upgrade/model.php | 44 ++++++++++++++++++++++++++++++++ module/webhook/control.php | 7 ++--- module/webhook/model.php | 7 ++--- 11 files changed, 130 insertions(+), 52 deletions(-) diff --git a/config/zentaopms.php b/config/zentaopms.php index a7149c59c1..949a5a8146 100644 --- a/config/zentaopms.php +++ b/config/zentaopms.php @@ -95,7 +95,6 @@ define('TABLE_FILE', '`' . $config->db->prefix . 'file`'); define('TABLE_HISTORY', '`' . $config->db->prefix . 'history`'); define('TABLE_EXTENSION', '`' . $config->db->prefix . 'extension`'); define('TABLE_CRON', '`' . $config->db->prefix . 'cron`'); -define('TABLE_MAILQUEUE', '`' . $config->db->prefix . 'mailqueue`'); define('TABLE_BLOCK', '`' . $config->db->prefix . 'block`'); define('TABLE_DOCCONTENT', '`' . $config->db->prefix . 'doccontent`'); define('TABLE_TESTSUITE', '`' . $config->db->prefix . 'testsuite`'); @@ -104,9 +103,9 @@ define('TABLE_TESTREPORT', '`' . $config->db->prefix . 'testreport`'); define('TABLE_ENTRY', '`' . $config->db->prefix . 'entry`'); define('TABLE_WEBHOOK', '`' . $config->db->prefix . 'webhook`'); -define('TABLE_WEBHOOKDATAS', '`' . $config->db->prefix . 'webhookdatas`'); define('TABLE_LOG', '`' . $config->db->prefix . 'log`'); define('TABLE_SCORE', '`' . $config->db->prefix . 'score`'); +define('TABLE_NOTIFY', '`' . $config->db->prefix . 'notify`'); if(!defined('TABLE_LANG')) define('TABLE_LANG', '`' . $config->db->prefix . 'lang`'); $config->objectTables['product'] = TABLE_PRODUCT; diff --git a/db/update9.7.sql b/db/update9.7.sql index fc4d315116..bedfa78591 100644 --- a/db/update9.7.sql +++ b/db/update9.7.sql @@ -3,3 +3,19 @@ ALTER TABLE `zt_todo` ADD `config` varchar(255) NOT NULL; ALTER TABLE `zt_todo` ADD `cycle` tinyint unsigned NOT NULL DEFAULT '0' AFTER `type`; INSERT INTO `zt_cron` (`m`, `h`, `dom`, `mon`, `dow`, `command`, `remark`, `type`, `buildin`, `status`, `lastTime`) VALUES ('1', '1', '*', '*', '*', 'moduleName=todo&methodName=createCycle', '生成周期性待办', 'zentao', 1, 'normal', '0000-00-00 00:00:00'); + +CREATE TABLE IF NOT EXISTS `zt_notify` ( + `id` mediumint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, + `objectType` varchar(50) NOT NULL, + `objectID` mediumint unsigned NOT NULL, + `action` mediumint NOT NULL, + `toList` varchar(255) NOT NULL, + `ccList` text NOT NULL, + `subject` varchar(255) NOT NULL, + `data` text NOT NULL, + `createdBy` char(30) NOT NULL, + `createdDate` datetime NOT NULL, + `sendTime` datetime NOT NULL, + `status` varchar(10) NOT NULL DEFAULT 'wait', + `failReason` text NOT NULL +) ENGINE='MyISAM' COLLATE 'utf8_general_ci'; diff --git a/db/zentao.sql b/db/zentao.sql index 93970e4650..a97519813c 100644 --- a/db/zentao.sql +++ b/db/zentao.sql @@ -407,6 +407,22 @@ CREATE TABLE IF NOT EXISTS `zt_module` ( PRIMARY KEY (`id`), KEY `module` (`root`,`type`,`path`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; +-- DROP TABLE IF EXISTS `zt_notify`; +CREATE TABLE IF NOT EXISTS `zt_notify` ( + `id` mediumint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, + `objectType` varchar(50) NOT NULL, + `objectID` mediumint unsigned NOT NULL, + `action` mediumint NOT NULL, + `toList` varchar(255) NOT NULL, + `ccList` text NOT NULL, + `subject` varchar(255) NOT NULL, + `data` text NOT NULL, + `createdBy` char(30) NOT NULL, + `createdDate` datetime NOT NULL, + `sendTime` datetime NOT NULL, + `status` varchar(10) NOT NULL DEFAULT 'wait', + `failReason` text NOT NULL +) ENGINE='MyISAM' COLLATE 'utf8_general_ci'; -- DROP TABLE IF EXISTS `zt_product`; CREATE TABLE IF NOT EXISTS `zt_product` ( `id` mediumint(8) unsigned NOT NULL auto_increment, diff --git a/module/mail/control.php b/module/mail/control.php index cdc4a5ddd1..63e4d80490 100755 --- a/module/mail/control.php +++ b/module/mail/control.php @@ -316,35 +316,35 @@ class mail extends control { if(!isset($queue->merge) or $queue->merge == false) { - $mailStatus = $this->dao->select('*')->from(TABLE_MAILQUEUE)->where('id')->eq($queue->id)->fetch('status'); + $mailStatus = $this->dao->select('*')->from(TABLE_NOTIFY)->where('id')->eq($queue->id)->fetch('status'); if(empty($mailStatus) or $mailStatus != 'wait') continue; } - $this->dao->update(TABLE_MAILQUEUE)->set('status')->eq('sending')->where('id')->in($queue->id)->exec(); - $this->mail->send($queue->toList, $queue->subject, $queue->body, $queue->ccList, true); + $this->dao->update(TABLE_NOTIFY)->set('status')->eq('sending')->where('id')->in($queue->id)->exec(); + $this->mail->send($queue->toList, $queue->subject, $queue->data, $queue->ccList, true); $data = new stdclass(); $data->sendTime = $now; - $data->status = 'send'; + $data->status = 'sended'; if($this->mail->isError()) { $data->status = 'fail'; $data->failReason = join("\n", $this->mail->getError()); } - $this->dao->update(TABLE_MAILQUEUE)->data($data)->where('id')->in($queue->id)->exec(); + $this->dao->update(TABLE_NOTIFY)->data($data)->where('id')->in($queue->id)->exec(); $log .= "Send #$queue->id result is $data->status\n"; if($data->status == 'fail') $log .= "reason is $data->failReason\n"; } /* Delete sended mail. */ - $lastMail = $this->dao->select('id,status')->from(TABLE_MAILQUEUE)->orderBy('id_desc')->limit(1)->fetch(); + $lastMail = $this->dao->select('id,status')->from(TABLE_NOTIFY)->where('objectType')->eq('mail')->orderBy('id_desc')->limit(1)->fetch(); if(!empty($lastMail) and $lastMail->id > 1000000) { - $unSendNum = $this->dao->select('count(id) as count')->from(TABLE_MAILQUEUE)->where('status')->eq('wait')->fetch('count'); - if($unSendNum == 0) $this->dao->exec('TRUNCATE table ' . TABLE_MAILQUEUE); + $unSendNum = $this->dao->select('count(id) as count')->from(TABLE_NOTIFY)->where('status')->eq('wait')->fetch('count'); + if($unSendNum == 0) $this->dao->exec('TRUNCATE table ' . TABLE_NOTIFY); } - $this->dao->delete()->from(TABLE_MAILQUEUE)->where('status')->eq('send')->andWhere('sendTime')->le(date('Y-m-d H:i:s', time() - 2 * 24 * 3600))->exec(); + $this->dao->delete()->from(TABLE_NOTIFY)->where('status')->eq('sended')->andWhere('sendTime')->le(date('Y-m-d H:i:s', time() - 2 * 24 * 3600))->exec(); echo $log; echo "OK\n"; @@ -366,7 +366,7 @@ class mail extends control } if(isset($this->config->mail->async)) $this->config->mail->async = 0; - $this->mail->send($queue->toList, $queue->subject, $queue->body, $queue->ccList); + $this->mail->send($queue->toList, $queue->subject, $queue->data, $queue->ccList); $data = new stdclass(); $data->sendTime = helper::now(); @@ -377,7 +377,7 @@ class mail extends control $data->status = 'fail'; $data->failReason = join("\n", $this->mail->getError()); } - $this->dao->update(TABLE_MAILQUEUE)->data($data)->where('id')->in($queue->id)->exec(); + $this->dao->update(TABLE_NOTIFY)->data($data)->where('id')->in($queue->id)->exec(); if($data->status == 'fail') die(js::alert($data->failReason)); echo js::alert($this->lang->mail->noticeResend); @@ -422,7 +422,7 @@ class mail extends control { if($confirm == 'no') die(js::confirm($this->lang->mail->confirmDelete, inlink('delete', "id=$id&confirm=yes"))); - $this->dao->delete()->from(TABLE_MAILQUEUE)->where('id')->eq($id)->exec(); + $this->dao->delete()->from(TABLE_NOTIFY)->where('id')->eq($id)->exec(); die(js::reload('parent')); } @@ -444,7 +444,7 @@ class mail extends control $idList = array(); if(isset($_GET['idList'])) $idList = explode('|', $_GET['idList']); - if($idList) $this->dao->delete()->from(TABLE_MAILQUEUE)->where('id')->in($idList)->exec(); + if($idList) $this->dao->delete()->from(TABLE_NOTIFY)->where('id')->in($idList)->exec(); die(js::reload('parent')); } diff --git a/module/mail/lang/en.php b/module/mail/lang/en.php index 72797bb7cc..08def3969e 100755 --- a/module/mail/lang/en.php +++ b/module/mail/lang/en.php @@ -45,8 +45,8 @@ $lang->mail->remove = 'Remove'; $lang->mail->toList = 'Addressee'; $lang->mail->ccList = 'Copy to'; $lang->mail->subject = 'Subject'; -$lang->mail->addedBy = 'Sender'; -$lang->mail->addedDate = 'Added Date'; +$lang->mail->createdBy = 'Sender'; +$lang->mail->createdDate = 'Added Date'; $lang->mail->sendTime = 'Send Date'; $lang->mail->status = 'Status'; $lang->mail->failReason = 'Fail Reason'; diff --git a/module/mail/lang/zh-cn.php b/module/mail/lang/zh-cn.php index 2e3795408b..0f44839a0a 100755 --- a/module/mail/lang/zh-cn.php +++ b/module/mail/lang/zh-cn.php @@ -45,8 +45,8 @@ $lang->mail->remove = '移除'; $lang->mail->toList = '收信人'; $lang->mail->ccList = '抄送给'; $lang->mail->subject = '主题'; -$lang->mail->addedBy = '发送者'; -$lang->mail->addedDate = '创建时间'; +$lang->mail->createdBy = '发送者'; +$lang->mail->createdDate = '创建时间'; $lang->mail->sendTime = '发送时间'; $lang->mail->status = '状态'; $lang->mail->failReason = '失败原因'; diff --git a/module/mail/model.php b/module/mail/model.php index e77779bb41..d9b056c212 100644 --- a/module/mail/model.php +++ b/module/mail/model.php @@ -514,13 +514,14 @@ class mailModel extends model if(empty($toList) or empty($subject)) return true; $data = new stdclass(); - $data->toList = $toList; - $data->ccList = $ccList; - $data->subject = $subject; - $data->body = $body; - $data->addedBy = $this->config->mail->fromName; - $data->addedDate = helper::now(); - $this->dao->insert(TABLE_MAILQUEUE)->data($data)->autocheck()->exec(); + $data->objectType = 'mail'; + $data->toList = $toList; + $data->ccList = $ccList; + $data->subject = $subject; + $data->data = $body; + $data->createdBy = $this->config->mail->fromName; + $data->createdDate = helper::now(); + $this->dao->insert(TABLE_NOTIFY)->data($data)->autocheck()->exec(); } /** @@ -532,8 +533,8 @@ class mailModel extends model */ public function getQueue($status = '', $orderBy = 'id_desc', $pager = null) { - $mails = $this->dao->select('*')->from(TABLE_MAILQUEUE) - ->where('1=1') + $mails = $this->dao->select('*')->from(TABLE_NOTIFY) + ->where('objectType')->eq('mail') ->beginIF($status)->andWhere('status')->eq($status)->fi() ->orderBy($orderBy) ->page($pager) @@ -562,7 +563,7 @@ class mailModel extends model public function getQueueById($queueID) { - return $this->dao->select('*')->from(TABLE_MAILQUEUE)->where('id')->eq($queueID)->fetch(); + return $this->dao->select('*')->from(TABLE_NOTIFY)->where('id')->eq($queueID)->fetch(); } /** @@ -599,8 +600,8 @@ class mailModel extends model } /* Remove html tail for first mail. */ - $endPos = strripos($firstMail->body, ''); - $mail->body = trim(substr($firstMail->body, 0, $endPos)); + $endPos = strripos($firstMail->data, ''); + $mail->data = trim(substr($firstMail->data, 0, $endPos)); /* Merge middle mails. */ if($mails) @@ -610,21 +611,21 @@ class mailModel extends model $mail->id .= ',' . $middleMail->id; /* Remove html head and tail for middle mails. */ - $beginPos = strpos($middleMail->body, ''); - $mailBody = trim(substr($middleMail->body, $beginPos)); + $beginPos = strpos($middleMail->data, ''); + $mailBody = trim(substr($middleMail->data, $beginPos)); $endPos = strripos($mailBody, ''); $mailBody = trim(substr($mailBody, 0, $endPos)); - $mail->body .= ltrim($mailBody, ''); + $mail->data .= ltrim($mailBody, ''); } } $mail->id .= ',' . $lastMail->id; /* Remove html head for last mail. */ - $beginPos = strpos($lastMail->body, ''); - $mailBody = substr($lastMail->body, $beginPos); - $mail->body .= trim(ltrim($mailBody, '')); + $beginPos = strpos($lastMail->data, ''); + $mailBody = substr($lastMail->data, $beginPos); + $mail->data .= trim(ltrim($mailBody, '')); return $mail; } diff --git a/module/mail/view/browse.html.php b/module/mail/view/browse.html.php index bd05ea74a5..92eb39e186 100644 --- a/module/mail/view/browse.html.php +++ b/module/mail/view/browse.html.php @@ -20,13 +20,13 @@ recTotal}&recPerPage={$pager->recPerPage}"; ?> - idAB);?> - mail->toList);?> - mail->subject);?> - mail->addedBy);?> - mail->addedDate);?> - mail->sendTime);?> - mail->status);?> + idAB);?> + mail->toList);?> + mail->subject);?> + mail->createdBy);?> + mail->createdDate);?> + mail->sendTime);?> + mail->status);?> mail->failReason;?> actions;?> @@ -40,8 +40,8 @@ toList, $queue->toList)?> subject?> - addedBy, $queue->addedBy)?> - addedDate?> + createdBy)?> + createdDate?> sendTime?> mail->statusList, $queue->status, '')?> failReason?> diff --git a/module/upgrade/model.php b/module/upgrade/model.php index 28bb16b4e2..b2e8434ff3 100644 --- a/module/upgrade/model.php +++ b/module/upgrade/model.php @@ -1986,4 +1986,48 @@ class upgradeModel extends model $this->dao->exec("ALTER TABLE `" . TABLE_TEAM . "` DROP `task`"); return true; } + + /** + * Move data to notify. + * + * @access public + * @return bool + */ + public function moveData2Notify() + { + $this->dao->exec('TRUNCATE TABLE ' . TABLE_NOTIFY); + $mailQueueTable = '`' . $this->config->db->prefix . 'mailqueue`'; + $stmt = $this->dao->select('*')->from($mailQueueTable)->query(); + while($mailQueue = $stmt->fetch()) + { + $notify = new stdclass(); + $notify->objectType = 'mail'; + $notify->toList = $mailQueue->toList; + $notify->ccList = $mailQueue->ccList; + $notify->subject = $mailQueue->subject; + $notify->data = $mailQueue->body; + $notify->createdBy = $mailQueue->addedBy; + $notify->createdDate = $mailQueue->addedDate; + $notify->sendTime = $mailQueue->sendTime; + $notify->status = $mailQueue->status; + $notify->failReason = $mailQueue->failReason; + $this->dao->insert(TABLE_NOTIFY)->data($notify)->exec(); + } + + $webhookDataTable = '`' . $this->config->db->prefix . 'webhookdatas`'; + $stmt = $this->dao->select('*')->from($webhookDataTable)->query(); + while($webhookData = $stmt->fetch()) + { + $notify = new stdclass(); + $notify->objectType = 'webhook'; + $notify->objectID = $webhookData->webhook; + $notify->action = $webhookData->action; + $notify->data = $webhookData->data; + $notify->createdBy = $webhookData->createdBy; + $notify->createdDate = $webhookData->createdDate; + $notify->status = $webhookData->status; + $this->dao->insert(TABLE_NOTIFY)->data($notify)->exec(); + } + return true; + } } diff --git a/module/webhook/control.php b/module/webhook/control.php index 4aecf53d51..c700a8ce01 100644 --- a/module/webhook/control.php +++ b/module/webhook/control.php @@ -157,19 +157,20 @@ class webhook extends control return true; } + $now = helper::now(); foreach($dataList as $data) { - $webhook = zget($webhooks, $data->webhook, ''); + $webhook = zget($webhooks, $data->objectID, ''); if($webhook) { $result = $this->webhook->fetchHook($webhook, $data->data); $this->webhook->saveLog($webhook, $data->action, $data->data, $result); } - $this->dao->update(TABLE_WEBHOOKDATAS)->set('status')->eq('sended')->where('id')->eq($data->id)->exec(); + $this->dao->update(TABLE_NOTIFY)->set('status')->eq('sended')->set('sendTime')->eq($now)->where('id')->eq($data->id)->exec(); } - $this->dao->delete()->from(TABLE_WEBHOOKDATAS)->where('status')->eq('sended')->exec(); + $this->dao->delete()->from(TABLE_NOTIFY)->where('status')->eq('sended')->exec(); echo "OK\n"; return true; diff --git a/module/webhook/model.php b/module/webhook/model.php index ce466af534..b3d7a1032c 100644 --- a/module/webhook/model.php +++ b/module/webhook/model.php @@ -96,7 +96,7 @@ class webhookModel extends model */ public function getDataList() { - return $this->dao->select('*')->from(TABLE_WEBHOOKDATAS)->where('status')->eq('wait')->orderBy('id')->fetchAll('id'); + return $this->dao->select('*')->from(TABLE_NOTIFY)->where('status')->eq('wait')->andWhere('objectType')->eq('webhook')->orderBy('id')->fetchAll('id'); } /** @@ -435,13 +435,14 @@ class webhookModel extends model public function saveData($webhookID, $actionID, $data) { $webhookData = new stdclass(); - $webhookData->webhook = $webhookID; + $webhookData->objectType = 'webhook'; + $webhookData->objectID = $webhookID; $webhookData->action = $actionID; $webhookData->data = $data; $webhookData->createdBy = $this->app->user->account; $webhookData->createdDate = helper::now(); - $this->dao->insert(TABLE_WEBHOOKDATAS)->data($webhookData)->exec(); + $this->dao->insert(TABLE_NOTIFY)->data($webhookData)->exec(); return !dao::isError(); }