Skip to content

Commit

Permalink
#2163 Tidy code in SubmissionDAO in response to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
NateWr committed Jul 24, 2017
1 parent a1f5149 commit 629e7ec
Showing 1 changed file with 46 additions and 87 deletions.
133 changes: 46 additions & 87 deletions classes/submission/SubmissionDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 629e7ec

Please sign in to comment.