From 3213aa168f7cee63e495a47f993768b6c87691bd Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Mon, 17 Jul 2017 17:51:38 +0200 Subject: [PATCH] more efficient sql code in getDocumentList() --- SeedDMS_Core/Core/inc.ClassDMS.php | 180 ++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 3 deletions(-) diff --git a/SeedDMS_Core/Core/inc.ClassDMS.php b/SeedDMS_Core/Core/inc.ClassDMS.php index 8f13c9952..ceffde224 100644 --- a/SeedDMS_Core/Core/inc.ClassDMS.php +++ b/SeedDMS_Core/Core/inc.ClassDMS.php @@ -784,6 +784,7 @@ class SeedDMS_Core_DMS { /* The following statement retrieves the status of the last version of all * documents. It must be restricted by further where clauses. */ +/* $queryStr = "SELECT `tblDocuments`.*, `tblDocumentLocks`.`userID` as `lockUser`, ". "`tblDocumentContent`.`version`, `tblDocumentStatus`.*, `tblDocumentStatusLog`.`status`, ". "`tblDocumentStatusLog`.`comment` AS `statusComment`, `tblDocumentStatusLog`.`date` as `statusDate`, ". @@ -799,9 +800,33 @@ class SeedDMS_Core_DMS { "LEFT JOIN `tblUsers` AS `sTbl` on `sTbl`.`id` = `tblDocumentStatusLog`.`userID` ". "WHERE `ttstatid`.`maxLogID`=`tblDocumentStatusLog`.`statusLogID` ". "AND `ttcontentid`.`maxVersion` = `tblDocumentContent`.`version` "; + */ + /* New sql statement which retrieves all documents, its latest version and + * status, the owner and user initiating the latest status. + * It doesn't need the where clause anymore. Hence the statement could be + * extended with further left joins. + */ + $selectStr = "SELECT `tblDocuments`.*, `tblDocumentLocks`.`userID` as `lockUser`, ". + "`tblDocumentContent`.`version`, `tblDocumentStatus`.*, `tblDocumentStatusLog`.`status`, ". + "`tblDocumentStatusLog`.`comment` AS `statusComment`, `tblDocumentStatusLog`.`date` as `statusDate`, ". + "`tblDocumentStatusLog`.`userID`, `oTbl`.`fullName` AS `ownerName`, `sTbl`.`fullName` AS `statusName` "; + $queryStr = + "FROM `ttcontentid` ". + "LEFT JOIN `tblDocuments` ON `tblDocuments`.`id` = `ttcontentid`.`document` ". + "LEFT JOIN `tblDocumentContent` ON `tblDocumentContent`.`document` = `ttcontentid`.`document` AND `tblDocumentContent`.`version` = `ttcontentid`.`maxVersion` ". + "LEFT JOIN `tblDocumentStatus` ON `tblDocumentStatus`.`documentID`=`ttcontentid`.`document` AND `tblDocumentStatus`.`version`=`ttcontentid`.`maxVersion` ". + "LEFT JOIN `ttstatid` ON `ttstatid`.`statusID` = `tblDocumentStatus`.`statusID` ". + "LEFT JOIN `tblDocumentStatusLog` ON `ttstatid`.`statusID` = `tblDocumentStatusLog`.`statusID` AND `ttstatid`.`maxLogID` = `tblDocumentStatusLog`.`statusLogID` ". + "LEFT JOIN `tblDocumentLocks` ON `ttcontentid`.`document`=`tblDocumentLocks`.`document` ". + "LEFT JOIN `tblUsers` `oTbl` ON `oTbl`.`id` = `tblDocuments`.`owner` ". + "LEFT JOIN `tblUsers` `sTbl` ON `sTbl`.`id` = `tblDocumentStatusLog`.`userID` "; + +// echo $queryStr; switch($listtype) { case 'AppRevByMe': // Documents I have to review/approve {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; // Get document list for the current user. $reviewStatus = $user->getReviewStatus(); @@ -845,6 +870,9 @@ class SeedDMS_Core_DMS { } break; // }}} case 'ReviewByMe': // Documents I have to review {{{ + if (!$this->db->createTemporaryTable("ttreviewid")) { + return false; + } $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -852,6 +880,33 @@ class SeedDMS_Core_DMS { else $orderdir = 'ASC'; + $groups = array(); + $tmp = $user->getGroups(); + foreach($tmp as $group) + $groups[] = $group->getID(); + + $selectStr .= ", `tblDocumentReviewLog`.`date` as `duedate` "; + $queryStr .= + "LEFT JOIN `tblDocumentReviewers` on `ttcontentid`.`document`=`tblDocumentReviewers`.`documentID` AND `ttcontentid`.`maxVersion`=`tblDocumentReviewers`.`version` ". + "LEFT JOIN `ttreviewid` ON `ttreviewid`.`reviewID` = `tblDocumentReviewers`.`reviewID` ". + "LEFT JOIN `tblDocumentReviewLog` ON `tblDocumentReviewLog`.`reviewLogID`=`ttreviewid`.`maxLogId` "; + + if(0) { + $queryStr .= "WHERE (`tblDocumentReviewers`.`type` = 0 AND `tblDocumentReviewers`.`required` = ".$user->getID()." "; + if($groups) + $queryStr .= "OR `tblDocumentReviewers`.`type` = 1 AND `tblDocumentReviewers`.`required` IN (".implode(',', $groups).") "; + $queryStr .= ") "; + $queryStr .= "AND `tblDocumentStatusLog`.`status` IN (".S_DRAFT_REV.", ".S_EXPIRED.") "; + if(!$param2) + $queryStr .= " AND `tblDocumentReviewLog`.`status` = 0 "; + if ($orderby=='e') $queryStr .= "ORDER BY `expires`"; + else if ($orderby=='u') $queryStr .= "ORDER BY `statusDate`"; + else if ($orderby=='s') $queryStr .= "ORDER BY `status`"; + else $queryStr .= "ORDER BY `name`"; + $queryStr .= " ".$orderdir; + } else { + $queryStr .= "WHERE 1=1 "; + // Get document list for the current user. $reviewStatus = $user->getReviewStatus(); @@ -887,8 +942,12 @@ class SeedDMS_Core_DMS { } else { $queryStr = ''; } + } break; // }}} case 'ApproveByMe': // Documents I have to approve {{{ + if (!$this->db->createTemporaryTable("ttapproveid")) { + return false; + } $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -896,6 +955,33 @@ class SeedDMS_Core_DMS { else $orderdir = 'ASC'; + $groups = array(); + $tmp = $user->getGroups(); + foreach($tmp as $group) + $groups[] = $group->getID(); + + $selectStr .= ", `tblDocumentApproveLog`.`date` as `duedate` "; + $queryStr .= + "LEFT JOIN `tblDocumentApprovers` on `ttcontentid`.`document`=`tblDocumentApprovers`.`documentID` AND `ttcontentid`.`maxVersion`=`tblDocumentApprovers`.`version` ". + "LEFT JOIN `ttapproveid` ON `ttapproveid`.`approveID` = `tblDocumentApprovers`.`approveID` ". + "LEFT JOIN `tblDocumentApproveLog` ON `tblDocumentApproveLog`.`approveLogID`=`ttapproveid`.`maxLogId` "; + + if(0) { + $queryStr .= "WHERE (`tblDocumentApprovers`.`type` = 0 AND `tblDocumentApprovers`.`required` = ".$user->getID()." "; + if($groups) + $queryStr .= "OR `tblDocumentApprovers`.`type` = 1 AND `tblDocumentApprovers`.`required` IN (".implode(',', $groups).")"; + $queryStr .= ") "; + $queryStr .= "AND `tblDocumentStatusLog`.`status` IN (".S_DRAFT_APP.", ".S_EXPIRED.") "; + if(!$param2) + $queryStr .= " AND `tblDocumentApproveLog`.`status` = 0 "; + if ($orderby=='e') $queryStr .= "ORDER BY `expires`"; + else if ($orderby=='u') $queryStr .= "ORDER BY `statusDate`"; + else if ($orderby=='s') $queryStr .= "ORDER BY `status`"; + else $queryStr .= "ORDER BY `name`"; + $queryStr .= " ".$orderdir; + } else { + $queryStr .= "WHERE 1=1 "; + // Get document list for the current user. $approvalStatus = $user->getApprovalStatus(); @@ -931,9 +1017,45 @@ class SeedDMS_Core_DMS { } else { $queryStr = ''; } + } break; // }}} case 'ReceiptByMe': // Documents I have to receipt {{{ + if (!$this->db->createTemporaryTable("ttreceiptid")) { + return false; + } $user = $param1; + $orderby = $param3; + if($param4 == 'desc') + $orderdir = 'DESC'; + else + $orderdir = 'ASC'; + + $groups = array(); + $tmp = $user->getGroups(); + foreach($tmp as $group) + $groups[] = $group->getID(); + + $selectStr .= ", `tblDocumentReceiptLog`.`date` as `duedate` "; + $queryStr .= + "LEFT JOIN `tblDocumentRecipients` on `ttcontentid`.`document`=`tblDocumentRecipients`.`documentID` AND `ttcontentid`.`maxVersion`=`tblDocumentRecipients`.`version` ". + "LEFT JOIN `ttreceiptid` ON `ttreceiptid`.`receiptID` = `tblDocumentRecipients`.`receiptID` ". + "LEFT JOIN `tblDocumentReceiptLog` ON `tblDocumentReceiptLog`.`receiptLogID`=`ttreceiptid`.`maxLogId` "; + + if(1) { + $queryStr .= "WHERE (`tblDocumentRecipients`.`type` = 0 AND `tblDocumentRecipients`.`required` = ".$user->getID()." "; + if($groups) + $queryStr .= "OR `tblDocumentRecipients`.`type` = 1 AND `tblDocumentRecipients`.`required` IN (".implode(',', $groups).")"; + $queryStr .= ") "; + if(!$param2) + $queryStr .= " AND `tblDocumentReceiptLog`.`status` = 0 "; + if ($orderby=='e') $queryStr .= "ORDER BY `expires`"; + else if ($orderby=='u') $queryStr .= "ORDER BY `statusDate`"; + else if ($orderby=='s') $queryStr .= "ORDER BY `status`"; + else $queryStr .= "ORDER BY `name`"; + $queryStr .= " ".$orderdir; + } else { + $queryStr .= "WHERE 1=1 "; + // Get document list for the current user. $receiptStatus = $user->getReceiptStatus(); @@ -958,13 +1080,22 @@ class SeedDMS_Core_DMS { } if (strlen($docCSV)>0) { - $queryStr .= "AND `tblDocuments`.`id` IN (" . $docCSV . ") ". - "ORDER BY `statusDate` DESC"; + $queryStr .= "AND `tblDocuments`.`id` IN (" . $docCSV . ") "; +// $queryStr .= "ORDER BY `statusDate` DESC"; + if ($orderby=='e') $queryStr .= "ORDER BY `expires`"; + else if ($orderby=='u') $queryStr .= "ORDER BY `statusDate`"; + else if ($orderby=='s') $queryStr .= "ORDER BY `status`"; + else $queryStr .= "ORDER BY `name`"; + $queryStr .= " ".$orderdir; } else { $queryStr = ''; } + } break; // }}} case 'ReviseByMe': // Documents I have to receipt {{{ + if (!$this->db->createTemporaryTable("ttrevisionid")) { + return false; + } $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -972,6 +1103,32 @@ class SeedDMS_Core_DMS { else $orderdir = 'ASC'; + $groups = array(); + $tmp = $user->getGroups(); + foreach($tmp as $group) + $groups[] = $group->getID(); + + $selectStr .= ", `tblDocumentRevisionLog`.`date` as `duedate` "; + $queryStr .= + "LEFT JOIN `tblDocumentRevisors` on `ttcontentid`.`document`=`tblDocumentRevisors`.`documentID` AND `ttcontentid`.`maxVersion`=`tblDocumentRevisors`.`version` ". + "LEFT JOIN `ttrevisionid` ON `ttrevisionid`.`revisionID` = `tblDocumentRevisors`.`revisionID` ". + "LEFT JOIN `tblDocumentRevisionLog` ON `tblDocumentRevisionLog`.`revisionLogID`=`ttrevisionid`.`maxLogId` "; + + if(0) { + $queryStr .= "WHERE (`tblDocumentRevisors`.`type` = 0 AND `tblDocumentRevisors`.`required` = ".$user->getID()." "; + if($groups) + $queryStr .= "OR `tblDocumentRevisors`.`type` = 1 AND `tblDocumentRevisors`.`required` IN (".implode(',', $groups).")"; + $queryStr .= ") "; + if(!$param2) + $queryStr .= " AND `tblDocumentRevisionLog`.`status` = 0 "; + if ($orderby=='e') $queryStr .= "ORDER BY `expires`"; + else if ($orderby=='u') $queryStr .= "ORDER BY `statusDate`"; + else if ($orderby=='s') $queryStr .= "ORDER BY `status`"; + else $queryStr .= "ORDER BY `name`"; + $queryStr .= " ".$orderdir; + } else { + $queryStr .= "WHERE 1=1 "; + // Get document list for the current user. $revisionStatus = $user->getRevisionStatus(); @@ -1004,8 +1161,11 @@ class SeedDMS_Core_DMS { } else { $queryStr = ''; } + } break; // }}} case 'WorkflowByMe': // Documents I to trigger in Worklflow {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; // Get document list for the current user. $workflowStatus = $user->getWorkflowStatus(); @@ -1038,6 +1198,8 @@ class SeedDMS_Core_DMS { } break; // }}} case 'AppRevOwner': // Documents waiting for review/approval/revision I'm owning {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -1056,6 +1218,8 @@ class SeedDMS_Core_DMS { // "ORDER BY `statusDate` DESC"; break; // }}} case 'ReceiveOwner': // Documents waiting for reception I'm owning {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -1086,6 +1250,8 @@ class SeedDMS_Core_DMS { } break; // }}} case 'RejectOwner': // Documents that has been rejected and I'm owning {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -1102,6 +1268,8 @@ class SeedDMS_Core_DMS { $queryStr .= " ".$orderdir; break; // }}} case 'LockedByMe': // Documents locked by me {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -1131,12 +1299,16 @@ class SeedDMS_Core_DMS { } break; // }}} case 'WorkflowOwner': // Documents waiting for workflow trigger I'm owning {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $queryStr .= "AND `tblDocuments`.`owner` = '".$user->getID()."' ". "AND `tblDocumentStatusLog`.`status` IN (".S_IN_WORKFLOW.") ". "ORDER BY `statusDate` DESC"; break; // }}} case 'MyDocs': // Documents owned by me {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -1151,6 +1323,8 @@ class SeedDMS_Core_DMS { $queryStr .= " ".$orderdir; break; // }}} case 'CheckedOutByMe': // Documents I have checked out {{{ + $queryStr .= "WHERE 1=1 "; + $user = $param1; $orderby = $param3; if($param4 == 'desc') @@ -1182,7 +1356,7 @@ class SeedDMS_Core_DMS { } if($queryStr) { - $resArr = $this->db->getResultArray($queryStr); + $resArr = $this->db->getResultArray($selectStr.$queryStr); if (is_bool($resArr) && !$resArr) { return false; }