From 629e7ecc8b25429abe01ea51dc088ec465cdbef3 Mon Sep 17 00:00:00 2001 From: Nate Wright Date: Tue, 18 Jul 2017 16:57:36 +0100 Subject: [PATCH] pkp/pkp-lib#2163 Tidy code in SubmissionDAO in response to code review --- classes/submission/SubmissionDAO.inc.php | 133 ++++++++--------------- 1 file changed, 46 insertions(+), 87 deletions(-) diff --git a/classes/submission/SubmissionDAO.inc.php b/classes/submission/SubmissionDAO.inc.php index 173858f9c26..818637085ac 100644 --- a/classes/submission/SubmissionDAO.inc.php +++ b/classes/submission/SubmissionDAO.inc.php @@ -356,12 +356,15 @@ public function get($caller, $params, $contextId) { )); $order = isset($params['order']) ? $params['order'] : 'DESC'; - if (!is_null($params['orderBy']) && $params['orderBy'] === 'id') { - $query->orderBy('s.submission_id ' . $order); - } elseif (!is_null($params['orderBy']) && $params['orderBy'] === 'lastModified') { - $query->orderBy('s.last_modified ' . $order); - } else { - $query->orderBy('s.date_submitted ' . $order); + switch ($order) { + case 'id': + $query->orderBy('s.submission_id ' . $order); + break; + case 'last_modified': + $query->orderBy('s.last_modified ' . $order); + break; + default: + $query->orderBy('s.date_submitted ' . $order); } if (!is_null($params['status'])) { @@ -686,7 +689,7 @@ function getBySubEditorId($contextId, $subEditorId = null, $includeDeclined = tr * @param $stageId int? Optional stage ID to limit results * @param $sectionId int? Optional section ID to limit results * @param $rangeInfo DBResultRange optional - * @param $search string General search parameters + * @param $search string|null General search parameters * @return array Submissions */ function getUnpublishedByUserId($userId, $contextId = null, $submissionId = null, $title = null, $stageId = null, $sectionId = null, $rangeInfo = null, $search = null) { @@ -708,26 +711,7 @@ function getUnpublishedByUserId($userId, $contextId = null, $submissionId = null if ($contextId) $params[] = (int) $contextId; if ($sectionId) $params[] = (int) $sectionId; - $searchWhere = ''; - if ($search) { - $words = explode(" ", trim($search)); - if (count($words)) { - $searchWhere = ' AND ('; - $searchClauses = array(); - foreach($words as $word) { - $clause = '('; - $clause .= '(ss.setting_name = ? AND ss.setting_value LIKE ?)'; - $params[] = 'title'; - $params[] = '%' . $word . '%'; - $clause .= ' OR (au.first_name LIKE ? OR au.middle_name LIKE ? OR au.last_name LIKE ?)'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $searchClauses[] = $clause . ')'; - } - $searchWhere .= join(' AND ', $searchClauses) . ')'; - } - } + $searchWhere = $this->getSearchWhere($search); $result = $this->retrieveRange( 'SELECT s.*, ps.date_published, @@ -855,26 +839,7 @@ function getByStatus($status, $userId = null, $contextId = null, $submissionId = if ($stageId) $params[] = (int) $stageId; if ($sectionId) $params[] = (int) $sectionId; - $searchWhere = ''; - if ($search) { - $words = explode(" ", trim($search)); - if (count($words)) { - $searchWhere = ' AND ('; - $searchClauses = array(); - foreach($words as $word) { - $clause = '('; - $clause .= '(ss.setting_name = ? AND ss.setting_value LIKE ?)'; - $params[] = 'title'; - $params[] = '%' . $word . '%'; - $clause .= ' OR (au.first_name LIKE ? OR au.middle_name LIKE ? OR au.last_name LIKE ?)'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $searchClauses[] = $clause . ')'; - } - $searchWhere .= join(' AND ', $searchClauses) . ')'; - } - } + $searchWhere = $this->getSearchWhere($search); $result = $this->retrieveRange( 'SELECT s.*, ps.date_published, @@ -943,26 +908,7 @@ function getAssignedToUser($userId, $contextId = null, $submissionId = null, $ti if ($stageId) $params[] = (int) $stageId; if ($sectionId) $params[] = (int) $sectionId; - $searchWhere = ''; - if ($search) { - $words = explode(" ", trim($search)); - if (count($words)) { - $searchWhere = ' AND ('; - $searchClauses = array(); - foreach($words as $word) { - $clause = '('; - $clause .= '(ss.setting_name = ? AND ss.setting_value LIKE ?)'; - $params[] = 'title'; - $params[] = '%' . $word . '%'; - $clause .= ' OR (ra.submission_id IS NULL AND (au.first_name LIKE ? OR au.middle_name LIKE ? OR au.last_name LIKE ?))'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $searchClauses[] = $clause . ')'; - } - $searchWhere .= join(' AND ', $searchClauses) . ')'; - } - } + $searchWhere = $this->getSearchWhere($search); $result = $this->retrieveRange($sql = 'SELECT s.*, ps.date_published, @@ -1032,26 +978,7 @@ function getActiveSubmissions($contextId = null, $submissionId = null, $title = if ($sectionId) $params[] = (int) $sectionId; if ($editor) array_push($params, (int) ROLE_ID_MANAGER, (int) ROLE_ID_SUB_EDITOR, $editorQuery = '%' . $editor . '%', $editorQuery); - $searchWhere = ''; - if ($search) { - $words = explode(" ", trim($search)); - if (count($words)) { - $searchWhere = ' AND ('; - $searchClauses = array(); - foreach($words as $word) { - $clause = '('; - $clause .= '(ss.setting_name = ? AND ss.setting_value LIKE ?)'; - $params[] = 'title'; - $params[] = '%' . $word . '%'; - $clause .= ' OR (au.first_name LIKE ? OR au.middle_name LIKE ? OR au.last_name LIKE ?)'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $params[] = '%' . $word . '%'; - $searchClauses[] = $clause . ')'; - } - $searchWhere .= join(' AND ', $searchClauses) . ')'; - } - } + $searchWhere = $this->getSearchWhere($search); $result = $this->retrieveRange( 'SELECT s.*, ps.date_published, @@ -1116,6 +1043,38 @@ function deletePermissions($contextId) { $this->flushCache(); } + /** + * Helper function to collect the SQL WHERE statement for searching + * submissions by word(s) + * + * @param $phrase string The search phrase. One or more words. + * @return string + */ + public function getSearchWhere($phrase) { + $searchWhere = ''; + if ($search) { + $words = explode(" ", trim($phrase)); + if (count($words)) { + $searchWhere = ' AND ('; + $searchClauses = array(); + foreach($words as $word) { + $clause = '('; + $clause .= '(ss.setting_name = ? AND ss.setting_value LIKE ?)'; + $params[] = 'title'; + $params[] = '%' . $word . '%'; + $clause .= ' OR (au.first_name LIKE ? OR au.middle_name LIKE ? OR au.last_name LIKE ?)'; + $params[] = '%' . $word . '%'; + $params[] = '%' . $word . '%'; + $params[] = '%' . $word . '%'; + $searchClauses[] = $clause . ')'; + } + $searchWhere .= join(' AND ', $searchClauses) . ')'; + } + } + + return $searchWhere; + } + // // Protected functions