diff --git a/classes/galley/DAO.php b/classes/galley/DAO.php index 6052729c0e1..7b8be70ea6a 100644 --- a/classes/galley/DAO.php +++ b/classes/galley/DAO.php @@ -17,6 +17,7 @@ use APP\plugins\PubObjectsExportPlugin; use APP\publication\Publication; use Illuminate\Database\Query\Builder; +use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Illuminate\Support\LazyCollection; @@ -269,61 +270,45 @@ public function deleteAllPubIds($contextId, $pubIdType) */ public function getExportable($contextId, $pubIdType = null, $title = null, $author = null, $issueId = null, $pubIdSettingName = null, $pubIdSettingValue = null, $rangeInfo = null) { - $params = []; - if ($pubIdSettingName) { - $params[] = $pubIdSettingName; - } - $params[] = PKPSubmission::STATUS_PUBLISHED; - $params[] = (int) $contextId; - if ($pubIdType) { - $params[] = 'pub-id::' . $pubIdType; - } - if ($title) { - $params[] = 'title'; - $params[] = '%' . $title . '%'; - } - if ($author) { - array_push($params, $authorQuery = '%' . $author . '%', $authorQuery); - } - if ($issueId) { - $params[] = (int) $issueId; - } - if ($pubIdSettingName && $pubIdSettingValue && $pubIdSettingValue != PubObjectsExportPlugin::EXPORT_STATUS_NOT_DEPOSITED) { - $params[] = $pubIdSettingValue; - } - - $baseSql = ' - FROM publication_galleys g - LEFT JOIN publications p ON (p.publication_id = g.publication_id) - LEFT JOIN publication_settings ps ON (ps.publication_id = p.publication_id) - LEFT JOIN submissions s ON (s.submission_id = p.submission_id) - LEFT JOIN submission_files sf ON (g.submission_file_id = sf.submission_file_id) - ' . ($pubIdType != null ? ' LEFT JOIN publication_galley_settings gs ON (g.galley_id = gs.galley_id)' : '') - . ($title != null ? ' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)' : '') - . ($author != null ? ' LEFT JOIN authors au ON (p.publication_id = au.publication_id) - LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \'' . Identity::IDENTITY_SETTING_GIVENNAME . '\') - LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \'' . Identity::IDENTITY_SETTING_FAMILYNAME . '\') - ' : '') - . ($pubIdSettingName != null ? ' LEFT JOIN publication_galley_settings gss ON (g.galley_id = gss.galley_id AND gss.setting_name = ?)' : '') . ' - WHERE - s.status = ? AND s.context_id = ? - ' . ($pubIdType != null ? ' AND gs.setting_name = ? AND gs.setting_value IS NOT NULL' : '') - . ($title != null ? ' AND (pst.setting_name = ? AND pst.setting_value LIKE ?)' : '') - . ($author != null ? ' AND (asgs.setting_value LIKE ? OR asfs.setting_value LIKE ?)' : '') - . ($issueId != null ? ' AND (ps.setting_name = \'issueId\' AND ps.setting_value = ? AND ps.locale = \'\'' : '') - . (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == PubObjectsExportPlugin::EXPORT_STATUS_NOT_DEPOSITED) ? ' AND gss.setting_value IS NULL' : '') - . (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != PubObjectsExportPlugin::EXPORT_STATUS_NOT_DEPOSITED) ? ' AND gss.setting_value = ?' : '') - . (($pubIdSettingName != null && is_null($pubIdSettingValue)) ? ' AND (gss.setting_value IS NULL OR gss.setting_value = \'\')' : ''); - - $result = $this->deprecatedDao->retrieveRange( - "SELECT g.* - {$baseSql} - GROUP BY g.galley_id - ORDER BY p.date_published DESC, p.publication_id DESC, g.galley_id DESC", - $params, - $rangeInfo - ); - return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); + $q = DB::table('publication_galleys', 'g') + ->leftJoin('publications AS p', 'p.publication_id', '=', 'g.publication_id') + ->leftJoin('publication_settings AS ps', 'ps.publication_id', '=', 'p.publication_id') + ->leftJoin('submissions AS s', 's.submission_id', '=', 'p.submission_id') + ->leftJoin('submission_files AS sf', 'g.submission_file_id', '=', 'sf.submission_file_id') + ->when($pubIdType != null, fn (Builder $q) => $q->leftJoin('publication_galley_settings AS gs', 'g.galley_id', '=', 'gs.galley_id')) + ->when($title != null, fn (Builder $q) => $q->leftJoin('publication_settings AS pst', 'p.publication_id', '=', 'pst.publication_id')) + ->when( + $author != null, + fn (Builder $q) => $q->leftJoin('authors AS au', 'p.publication_id', '=', 'au.publication_id') + ->leftJoin('author_settings AS asgs', fn (JoinClause $j) => $j->on('asgs.author_id', '=', 'au.author_id')->where('asgs.setting_name', '=', Identity::IDENTITY_SETTING_GIVENNAME)) + ->leftJoin('author_settings AS asfs', fn (JoinClause $j) => $j->on('asfs.author_id', '=', 'au.author_id')->where('asfs.setting_name', '=', Identity::IDENTITY_SETTING_FAMILYNAME)) + ) + ->when($pubIdSettingName != null, fn (Builder $q) => $q->leftJoin('publication_galley_settings AS gss', fn (JoinClause $j) => $j->on('g.galley_id', '=', 'gss.galley_id')->where('gss.setting_name', '=', $pubIdSettingName))) + ->where('s.status', '=', PKPSubmission::STATUS_PUBLISHED) + ->where('s.context_id', '=', $contextId) + ->when($pubIdType != null, fn (Builder $q) => $q->where('gs.setting_name', '=', "pub-id::{$pubIdType}")->whereNotNull('gs.setting_value')) + ->when($title != null, fn (Builder $q) => $q->where('pst.setting_name', '=', 'title')->where('pst.setting_value', 'LIKE', "%{$title}%")) + ->when($author != null, fn (Builder $q) => $q->whereRaw("CONCAT(COALESCE(asgs.setting_value, ''), ' ', COALESCE(asfs.setting_value, '')) LIKE ?", ["%{$author}%"])) + ->when($issueId != null, fn (Builder $q) => $q->where('ps.setting_name', '=', 'issueId')->where('ps.setting_value', '=', $issueId)->where('ps.locale', '=', '')) + ->when($pubIdSettingName, fn (Builder $q) => + $q->when( + $pubIdSettingValue === null, + fn (Builder $q) => $q->whereRaw("COALESCE(gss.setting_value, '') = ''"), + fn (Builder $q) => $q->when( + $pubIdSettingValue != PubObjectsExportPlugin::EXPORT_STATUS_NOT_DEPOSITED, + fn (Builder $q) => $q->where('gss.setting_value', '=', $pubIdSettingValue), + fn (Builder $q) => $q->whereNull('gss.setting_value') + ) + ) + ) + ->groupBy('g.galley_id') + ->orderByDesc('p.date_published') + ->orderByDesc('p.publication_id') + ->orderByDesc('g.galley_id') + ->select('g.*'); + + $result = $this->deprecatedDao->retrieveRange($q, [], $rangeInfo); + return new DAOResultFactory($result, $this, '_fromRow', [], $q, [], $rangeInfo); } } diff --git a/classes/log/EmailLogDAO.php b/classes/log/EmailLogDAO.php index 28dbd3f1389..d26b6a64c50 100644 --- a/classes/log/EmailLogDAO.php +++ b/classes/log/EmailLogDAO.php @@ -19,6 +19,8 @@ namespace PKP\log; use APP\facades\Repo; +use Illuminate\Database\Query\Builder; +use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Facades\DB; use PKP\db\DAOResultFactory; use PKP\plugins\Hook; @@ -64,29 +66,18 @@ public function getById($logId, $assocType = null, $assocId = null) */ public function _getByEventType($assocType, $assocId, $eventType, $userId = null, $rangeInfo = null) { - $params = [ - (int) $assocType, - (int) $assocId, - (int) $eventType - ]; - if ($userId) { - $params[] = $userId; - } - - $baseSql = ' - FROM email_log e - ' . ($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') . ' - WHERE e.assoc_type = ? AND - e.assoc_id = ? AND - e.event_type = ? - ' . ($userId ? ' AND u.user_id = ?' : ''); - $result = $this->retrieveRange( - "SELECT e.* {$baseSql}", - $params, - $rangeInfo - ); - - return new DAOResultFactory($result, $this, 'build', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted in submissionEmails.tpl + $q = DB::table('email_log', 'e') + ->when( + $userId, + fn (Builder $q) => $q->join( + 'email_log_users AS u', + fn (JoinClause $j) => $j->on('u.email_log_id', '=', 'e.log_id') + ->where('u.user_id', $userId) + ) + ) + ->select('e.*'); + $result = $this->retrieveRange($q, [], $rangeInfo); + return new DAOResultFactory($result, $this, 'build', [], $q, [], $rangeInfo); // Counted in submissionEmails.tpl } /** diff --git a/classes/reviewForm/ReviewFormElementDAO.php b/classes/reviewForm/ReviewFormElementDAO.php index 8ea8dbea02e..f7e46d94d18 100644 --- a/classes/reviewForm/ReviewFormElementDAO.php +++ b/classes/reviewForm/ReviewFormElementDAO.php @@ -19,6 +19,8 @@ namespace PKP\reviewForm; +use DB; +use Illuminate\Database\Query\Builder; use PKP\db\DAORegistry; use PKP\db\DAOResultFactory; use PKP\db\DBResultRange; @@ -233,18 +235,13 @@ public function deleteSetting($reviewFormElementId, $name, $locale = null) */ public function getByReviewFormId($reviewFormId, $rangeInfo = null, $included = null) { - $baseSql = ' - FROM review_form_elements - WHERE review_form_id = ? - ' . ($included === true ? ' AND included = 1' : '') . ' - ' . ($included === false ? ' AND included = 0' : ''); - $result = $this->retrieveRange( - "SELECT * {$baseSql} ORDER BY seq", - $params = [(int) $reviewFormId], - $rangeInfo - ); - - return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params); + $q = DB::table('review_form_elements', 'rfe') + ->where('rfe.review_form_id', '=', $reviewFormId) + ->where($included !== null, fn (Builder $q) => $q->where('rfe.included', '=', $included)) + ->select('rfe.*') + ->orderBy('rfe.seq'); + $result = $this->retrieveRange($q, [], $rangeInfo); + return new DAOResultFactory($result, $this, '_fromRow', [], $q); } /** diff --git a/classes/submission/SubmissionCommentDAO.php b/classes/submission/SubmissionCommentDAO.php index dda0400e8d7..19402108b1e 100644 --- a/classes/submission/SubmissionCommentDAO.php +++ b/classes/submission/SubmissionCommentDAO.php @@ -18,6 +18,8 @@ namespace PKP\submission; +use DB; +use Illuminate\Database\Query\Builder; use PKP\db\DAOResultFactory; use PKP\plugins\Hook; @@ -87,24 +89,15 @@ public function getByUserId($userId) */ public function getReviewerCommentsByReviewerId($submissionId, $reviewerId = null, $reviewId = null, $viewable = null) { - $params = [(int) $submissionId]; - if ($reviewerId) { - $params[] = (int) $reviewerId; - } - if ($reviewId) { - $params[] = (int) $reviewId; - } - - $baseSql = ' - FROM submission_comments a - WHERE submission_id = ? - ' . ($reviewerId ? ' AND author_id = ?' : '') . ' - ' . ($reviewId ? ' AND assoc_id = ?' : '') . ' - ' . ($viewable === true ? ' AND viewable = 1' : '') . ' - ' . ($viewable === false ? ' AND viewable = 0' : ''); + $q = DB::table('submission_comments', 'a') + ->where('a.submission_id', '=', $submissionId) + ->when($reviewerId, fn (Builder $q) => $q->where('a.author_id', '=', $reviewerId)) + ->when($reviewId, fn (Builder $q) => $q->where('a.assoc_id', '=', $reviewId)) + ->when($viewable !== null, fn (Builder $q) => $q->where('a.viewable', '=', $viewable)) + ->orderByDesc('a.date_posted') + ->select('a.*'); - $result = $this->retrieve("SELECT a.* {$baseSql} ORDER BY date_posted DESC", $params); - return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params); // Counted in readReview.tpl and authorReadReview.tpl + return new DAOResultFactory($q->get(), $this, '_fromRow', [], $q); // Counted in readReview.tpl and authorReadReview.tpl } /** diff --git a/classes/submission/reviewRound/ReviewRoundDAO.php b/classes/submission/reviewRound/ReviewRoundDAO.php index a4fd1dbccaf..e5abe61e14d 100644 --- a/classes/submission/reviewRound/ReviewRoundDAO.php +++ b/classes/submission/reviewRound/ReviewRoundDAO.php @@ -19,6 +19,7 @@ namespace PKP\submission\reviewRound; use APP\core\Application; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Facades\DB; use PKP\db\DAOResultFactory; @@ -193,21 +194,14 @@ public function getBySubmissionFileId($submissionFileId) */ public function getBySubmissionId($submissionId, $stageId = null, $round = null) { - $params = [(int) $submissionId]; - if ($stageId) { - $params[] = $stageId; - } - if ($round) { - $params[] = $round; - } - - $baseSql = ' - FROM review_rounds - WHERE submission_id = ? - ' . ($stageId ? ' AND stage_id = ?' : '') . ' - ' . ($round ? ' AND round = ?' : ''); - $result = $this->retrieve("SELECT * {$baseSql} ORDER BY stage_id ASC, round ASC", $params); - return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params); + $q = DB::table('review_rounds', 'rr') + ->where('rr.submission_id', '=', $submissionId) + ->when($stageId, fn (Builder $q) => $q->where('rr.stage_id', '=', $stageId)) + ->when($round, fn (Builder $q) => $q->where('rr.round', '=', $round)) + ->orderBy('rr.stage_id') + ->orderBy('rr.round'); + + return new DAOResultFactory($q->get(), $this, '_fromRow', [], $q); } /**