Skip to content

Commit

Permalink
pkp#8700 Updated code to make use of the Laravel Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed May 28, 2024
1 parent 6fca5cb commit a844d61
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 122 deletions.
95 changes: 40 additions & 55 deletions classes/galley/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
37 changes: 14 additions & 23 deletions classes/log/EmailLogDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

/**
Expand Down
21 changes: 9 additions & 12 deletions classes/reviewForm/ReviewFormElementDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
27 changes: 10 additions & 17 deletions classes/submission/SubmissionCommentDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace PKP\submission;

use DB;
use Illuminate\Database\Query\Builder;
use PKP\db\DAOResultFactory;
use PKP\plugins\Hook;

Expand Down Expand Up @@ -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
}

/**
Expand Down
24 changes: 9 additions & 15 deletions classes/submission/reviewRound/ReviewRoundDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit a844d61

Please sign in to comment.