Skip to content

Commit

Permalink
#10189 Convert Queries to an Eloquent Model (#10384)
Browse files Browse the repository at this point in the history
* #10189 Convert Queries to an Eloquent Model

* #10189 Convert Queries to an Eloquent Model

* #10189 Convert Queries to an Eloquent Model

* #10189 cleanup indentation issues

* #10189 review feedback

* #10189 review feedback
  • Loading branch information
kaitlinnewson committed Sep 23, 2024
1 parent 1584fce commit b8bd6fd
Show file tree
Hide file tree
Showing 35 changed files with 719 additions and 931 deletions.
25 changes: 13 additions & 12 deletions classes/controllers/grid/GridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
namespace PKP\controllers\grid;

use APP\template\TemplateManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use PKP\controllers\grid\files\FilesGridDataProvider;
use PKP\core\ItemIterator;
use PKP\core\JSONMessage;
use PKP\core\PKPRequest;
use PKP\db\DAOResultFactory;
use PKP\db\DBResultRange;
use PKP\form\Form;
use PKP\handler\PKPHandler;
Expand Down Expand Up @@ -398,17 +400,12 @@ public function setGridDataElements($data)
{
$this->callFeaturesHook('setGridDataElements', ['grid' => &$this, 'data' => &$data]);

if ($data instanceof Enumerable) {
$this->_data = $this->toAssociativeArray($data);
} elseif (is_iterable($data)) {
$this->_data = $data;
} elseif ($data instanceof \PKP\db\DAOResultFactory) {
$this->_data = $data->toAssociativeArray();
} elseif ($data instanceof ItemIterator) {
$this->_data = $data->toArray();
} else {
assert(false);
}
$this->_data = match (true) {
$data instanceof Enumerable => $this->toAssociativeArray($data),
$data instanceof DAOResultFactory => $data->toAssociativeArray(),
$data instanceof ItemIterator => $data->toArray(),
is_iterable($data) => $data
};
}

/**
Expand Down Expand Up @@ -1378,7 +1375,11 @@ public static function toAssociativeArray(LazyCollection $lazyCollection, string
{
$returner = [];
foreach ($lazyCollection as $item) {
$returner[$item->getData($idField)] = $item;
if ($item instanceof Model) {
$returner[$item->$idField] = $item;
} else {
$returner[$item->getData($idField)] = $item;
}
}
return $returner;
}
Expand Down
5 changes: 2 additions & 3 deletions classes/core/PKPApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @file classes/core/PKPApplication.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PKPApplication
Expand Down Expand Up @@ -487,7 +487,6 @@ public function getDAOMap(): array
'SubmissionCommentDAO' => 'PKP\submission\SubmissionCommentDAO',
'SubmissionDisciplineDAO' => 'PKP\submission\SubmissionDisciplineDAO',
'SubmissionDisciplineEntryDAO' => 'PKP\submission\SubmissionDisciplineEntryDAO',
'QueryDAO' => 'PKP\query\QueryDAO',
'SubmissionKeywordDAO' => 'PKP\submission\SubmissionKeywordDAO',
'SubmissionKeywordEntryDAO' => 'PKP\submission\SubmissionKeywordEntryDAO',
'SubmissionSubjectDAO' => 'PKP\submission\SubmissionSubjectDAO',
Expand Down
14 changes: 6 additions & 8 deletions classes/decision/types/traits/IsRecommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* @file classes/decision/types/traits/IsRecommendation.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class decision
Expand Down Expand Up @@ -31,7 +31,7 @@
use PKP\mail\Mailable;
use PKP\mail\mailables\RecommendationNotifyEditors;
use PKP\note\Note;
use PKP\query\QueryDAO;
use PKP\query\Query;
use PKP\security\Role;
use PKP\stageAssignment\StageAssignment;
use PKP\submission\reviewRound\ReviewRound;
Expand Down Expand Up @@ -132,9 +132,7 @@ protected function addRecommendationQuery(EmailData $email, Submission $submissi
}
}

/** @var QueryDAO $queryDao */
$queryDao = DAORegistry::getDAO('QueryDAO');
$queryId = $queryDao->addQuery(
$queryId = Repo::query()->addQuery(
$submission->getId(),
$this->getStageId(),
$email->subject,
Expand All @@ -145,8 +143,8 @@ protected function addRecommendationQuery(EmailData $email, Submission $submissi
false
);

$query = $queryDao->getById($queryId);
$note = $query->getHeadNote();
$query = Query::find($queryId);
$note = Repo::note()->getHeadNote($query->id);
$mailable = new Mailable();
foreach ($email->attachments as $attachment) {
if (isset($attachment[Mailable::ATTACHMENT_TEMPORARY_FILE])) {
Expand Down
12 changes: 9 additions & 3 deletions classes/facades/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @file classes/facades/Repo.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Repo
Expand Down Expand Up @@ -38,7 +38,8 @@
use PKP\log\event\Repository as EventLogRepository;
use PKP\log\Repository as EmailLogEntryRepository;
use PKP\note\Repository as NoteRepository;
use PKP\notification\Notification as NotificationRepository;
use PKP\notification\Repository as NotificationRepository;
use PKP\query\Repository as QueryRepository;
use PKP\stageAssignment\Repository as StageAssignmentRepository;
use PKP\submissionFile\Repository as SubmissionFileRepository;
use PKP\userGroup\Repository as UserGroupRepository;
Expand Down Expand Up @@ -134,4 +135,9 @@ public static function note(): NoteRepository
{
return app(NoteRepository::class);
}

public static function query(): QueryRepository
{
return app(QueryRepository::class);
}
}
2 changes: 1 addition & 1 deletion classes/note/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getData(?string $field)
*/
public function scopeWithUserId(Builder $query, int $userId): Builder
{
return $query->where('userId', $userId);
return $query->where('user_id', $userId);
}

/**
Expand Down
29 changes: 13 additions & 16 deletions classes/note/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@

namespace PKP\note;

use PKP\db\DAO;
use PKPApplication;

class Repository
{
/**
* Fetch a note by symbolic info, building it if needed.
*/
public function build(int $assocType, int $assocId, int $userId, ?string $contents, ?string $title): Note
{
return Note::withUserId($userId)
->withAssoc($assocType, $assocId)
->firstOr(fn() => Note::create([
'assocType' => $assocType,
'assocId' => $assocId,
'userId' => $userId,
'contents' => $contents,
'title' => $title,
]));
}

public function transfer(int $oldUserId, int $newUserId): int
{
return Note::withUserId($oldUserId)
->update(['user_id' => $newUserId]);
}

/**
* Get the "head" (first) note for a Query.
*/
public function getHeadNote(int $queryId): ?Note
{
return Note::withAssoc(PKPApplication::ASSOC_TYPE_QUERY, $queryId)
->withSort(Note::NOTE_ORDER_DATE_CREATED, DAO::SORT_DIRECTION_ASC)
->first();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* @file classes/notification/managerDelegate/PKPEditingProductionStatusNotificationManager.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PKPEditingProductionStatusNotificationManager
*
* @ingroup classses_notification_managerDelegate
* @ingroup classes_notification_managerDelegate
*
* @brief Editing and productionstatus notifications types manager delegate.
* @brief Editing and production status notifications types manager delegate.
*/

namespace PKP\notification\managerDelegate;
Expand All @@ -21,9 +21,9 @@
use APP\notification\NotificationManager;
use PKP\core\PKPApplication;
use PKP\core\PKPRequest;
use PKP\db\DAORegistry;
use PKP\notification\Notification;
use PKP\notification\NotificationManagerDelegate;
use PKP\query\Query;
use PKP\security\Role;
use PKP\stageAssignment\StageAssignment;
use PKP\submissionFile\SubmissionFile;
Expand All @@ -35,7 +35,7 @@ class PKPEditingProductionStatusNotificationManager extends NotificationManagerD
*/
public function getNotificationMessage(PKPRequest $request, Notification $notification): string|array|null
{
return match($notification->type) {
return match ($notification->type) {
Notification::NOTIFICATION_TYPE_ASSIGN_COPYEDITOR => __('notification.type.assignCopyeditors'),
Notification::NOTIFICATION_TYPE_AWAITING_COPYEDITS => __('notification.type.awaitingCopyedits'),
Notification::NOTIFICATION_TYPE_ASSIGN_PRODUCTIONUSER => __('notification.type.assignProductionUser'),
Expand Down Expand Up @@ -91,10 +91,10 @@ public function updateNotification(PKPRequest $request, ?array $userIds, int $as
->withRoleIds([Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR])
->get();

// Get the copyediting and production discussions
$queryDao = DAORegistry::getDAO('QueryDAO'); /** @var \PKP\query\QueryDAO $queryDao */
$productionQueries = $queryDao->getByAssoc(Application::ASSOC_TYPE_SUBMISSION, $submissionId, WORKFLOW_STAGE_ID_PRODUCTION);
$productionQuery = $productionQueries->next();
// Get the production discussions
$productionQuery = Query::withAssoc(Application::ASSOC_TYPE_SUBMISSION, $submissionId)
->withStageId(WORKFLOW_STAGE_ID_PRODUCTION)
->first();

// Get the copyedited files
$countCopyeditedFiles = Repo::submissionFile()
Expand Down Expand Up @@ -165,8 +165,10 @@ public function updateNotification(PKPRequest $request, ?array $userIds, int $as
$this->_removeNotification($submissionId, $editorStageAssignment->userId, $notificationType, $contextId);
} else {
// If a copyeditor is assigned i.e. there is a copyediting discussion
$editingQueries = $queryDao->getByAssoc(Application::ASSOC_TYPE_SUBMISSION, $submissionId, WORKFLOW_STAGE_ID_EDITING);
if ($editingQueries->next()) {
$editingQueries = Query::withAssoc(Application::ASSOC_TYPE_SUBMISSION, $submissionId)
->withStageId(WORKFLOW_STAGE_ID_EDITING)
->first();
if ($editingQueries) {
if ($notificationType == Notification::NOTIFICATION_TYPE_AWAITING_COPYEDITS) {
// Add 'awaiting copyedits' notification
$this->_createNotification(
Expand Down Expand Up @@ -239,7 +241,3 @@ public function _createNotification(PKPRequest $request, int $submissionId, int
}
}
}

if (!PKP_STRICT_MODE) {
class_alias('\PKP\notification\managerDelegate\PKPEditingProductionStatusNotificationManager', '\PKPEditingProductionStatusNotificationManager');
}
34 changes: 12 additions & 22 deletions classes/notification/managerDelegate/QueryNotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
use PKP\core\PKPApplication;
use PKP\core\PKPRequest;
use PKP\core\PKPString;
use PKP\db\DAO;
use PKP\db\DAORegistry;
use PKP\note\Note;
use PKP\notification\Notification;
use PKP\notification\NotificationManagerDelegate;
use PKP\query\Query;
use PKP\query\QueryDAO;

class QueryNotificationManager extends NotificationManagerDelegate
{
Expand All @@ -41,10 +38,9 @@ public function getNotificationMessage(PKPRequest $request, Notification $notifi
if ($notification->assocType != Application::ASSOC_TYPE_QUERY) {
throw new \Exception('Unexpected assoc type!');
}
$queryDao = DAORegistry::getDAO('QueryDAO'); /** @var QueryDAO $queryDao */
$query = $queryDao->getById($notification->assocId);
$query = Query::find($notification->assocId);

$headNote = $query->getHeadNote();
$headNote = Repo::note()->getHeadNote($query->id);
if (!$headNote) {
throw new \Exception('Unable to retrieve head note for query!');
}
Expand All @@ -58,14 +54,14 @@ public function getNotificationMessage(PKPRequest $request, Notification $notifi
'noteTitle' => Str::limit($headNote->title, 200),
]);
case Notification::NOTIFICATION_TYPE_QUERY_ACTIVITY:
$latestNote = Note::withAssoc(PKPApplication::ASSOC_TYPE_QUERY, $query->getAssocId())
$latestNote = Note::withAssoc(PKPApplication::ASSOC_TYPE_QUERY, $query->id)
->withSort(Note::NOTE_ORDER_ID)
->first();
$user = $latestNote->user;
return __('submission.query.activity', [
'responderName' => $user->getFullName(),
'noteContents' => Str::limit(PKPString::html2text($latestNote->contents), 200),
'noteTitle' => Str::limit($headNote->title,200),
'noteTitle' => Str::limit($headNote->title, 200),
]);
}
throw new \Exception('Unexpected notification type!');
Expand All @@ -76,12 +72,12 @@ public function getNotificationMessage(PKPRequest $request, Notification $notifi
*/
protected function getQuerySubmission(Query $query): Submission
{
switch ($query->getAssocType()) {
switch ($query->assocType) {
case Application::ASSOC_TYPE_SUBMISSION:
return Repo::submission()->get($query->getAssocId());
return Repo::submission()->get($query->assocId);
case Application::ASSOC_TYPE_REPRESENTATION:
$representationDao = Application::getRepresentationDAO();
$representation = $representationDao->getById($query->getAssocId());
$representation = $representationDao->getById($query->assocId);
$publication = Repo::publication()->get($representation->getData('publicationId'));
return Repo::submission()->get($publication->getData('submissionId'));
}
Expand All @@ -97,8 +93,7 @@ public function getNotificationUrl(PKPRequest $request, Notification $notificati
throw new \Exception('Unexpected query assoc type!');
}

$queryDao = DAORegistry::getDAO('QueryDAO'); /** @var QueryDAO $queryDao */
$query = $queryDao->getById($notification->assocId);
$query = Query::find($notification->assocId);
if (!$query) {
return null;
}
Expand All @@ -112,27 +107,26 @@ public function getNotificationUrl(PKPRequest $request, Notification $notificati
*/
public function getNotificationContents(PKPRequest $request, Notification $notification): mixed
{
if($notification->assocType != Application::ASSOC_TYPE_QUERY) {
if ($notification->assocType != Application::ASSOC_TYPE_QUERY) {
throw new \Exception('Unexpected assoc type!');
}
$queryDao = DAORegistry::getDAO('QueryDAO'); /** @var QueryDAO $queryDao */
$query = $queryDao->getById($notification->assocId);
$query = Query::find($notification->assocId);
$submission = $this->getQuerySubmission($query);

switch ($notification->type) {
case Notification::NOTIFICATION_TYPE_NEW_QUERY:
return __(
'submission.query.new.contents',
[
'queryTitle' => $query->getHeadNote()->title,
'queryTitle' => Repo::note()->getHeadNote($query->id)->title,
'submissionTitle' => $submission->getCurrentPublication()->getLocalizedTitle(null, 'html'),
]
);
case Notification::NOTIFICATION_TYPE_QUERY_ACTIVITY:
return __(
'submission.query.activity.contents',
[
'queryTitle' => $query->getHeadNote()->title,
'queryTitle' => Repo::note()->getHeadNote($query->id)->title,
'submissionTitle' => $submission->getCurrentPublication()->getLocalizedTitle(null, 'html'),
]
);
Expand All @@ -148,7 +142,3 @@ public function getStyleClass(Notification $notification): string
return NOTIFICATION_STYLE_CLASS_WARNING;
}
}

if (!PKP_STRICT_MODE) {
class_alias('\PKP\notification\managerDelegate\QueryNotificationManager', '\QueryNotificationManager');
}
Loading

0 comments on commit b8bd6fd

Please sign in to comment.