Skip to content

Commit

Permalink
pkp#10188 Convert Notes to an Eloquent model (pkp#10268)
Browse files Browse the repository at this point in the history
* pkp#10188 Convert Notes to an Eloquent model

* pkp#10188 review feedback for converting Notes to Eloquent

* pkp#10188 further review feedback for converting Notes to Eloquent
  • Loading branch information
kaitlinnewson authored Aug 8, 2024
1 parent 7e81f7c commit d24432e
Show file tree
Hide file tree
Showing 34 changed files with 326 additions and 660 deletions.
4 changes: 3 additions & 1 deletion classes/controllers/grid/DataObjectGridCellProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace PKP\controllers\grid;

use Illuminate\Database\Eloquent\Model;
use PKP\core\DataObject;
use PKP\facades\Locale;

class DataObjectGridCellProvider extends GridCellProvider
Expand Down Expand Up @@ -74,7 +76,7 @@ public function getTemplateVarsFromRowColumn($row, $column)
{
$element = $row->getData();
$columnId = $column->getId();
assert($element instanceof \PKP\core\DataObject && !empty($columnId));
assert(($element instanceof DataObject || $element instanceof Model) && !empty($columnId));

$data = $element->getData($columnId);
// For localized fields, $data will be an array; otherwise,
Expand Down
1 change: 0 additions & 1 deletion classes/core/PKPApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ public function getDAOMap()
'NavigationMenuDAO' => 'PKP\navigationMenu\NavigationMenuDAO',
'NavigationMenuItemDAO' => 'PKP\navigationMenu\NavigationMenuItemDAO',
'NavigationMenuItemAssignmentDAO' => 'PKP\navigationMenu\NavigationMenuItemAssignmentDAO',
'NoteDAO' => 'PKP\note\NoteDAO',
'NotificationSettingsDAO' => 'PKP\notification\NotificationSettingsDAO',
'NotificationSubscriptionSettingsDAO' => 'PKP\notification\NotificationSubscriptionSettingsDAO',
'PluginGalleryDAO' => 'PKP\plugins\PluginGalleryDAO',
Expand Down
4 changes: 2 additions & 2 deletions classes/decision/types/traits/IsRecommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function addRecommendationQuery(EmailData $email, Submission $submissi
$newSubmissionFile->setData('fileStage', SubmissionFile::SUBMISSION_FILE_QUERY);
$newSubmissionFile->setData('sourceSubmissionFileId', $submissionFile->getId());
$newSubmissionFile->setData('assocType', Application::ASSOC_TYPE_NOTE);
$newSubmissionFile->setData('assocId', $note->getId());
$newSubmissionFile->setData('assocId', $note->id);
Repo::submissionFile()->add($newSubmissionFile);
$mailable->attachSubmissionFile($newSubmissionFile->getId(), $newSubmissionFile->getLocalizedData('name'));
} elseif (isset($attachment[Mailable::ATTACHMENT_LIBRARY_FILE])) {
Expand Down Expand Up @@ -220,7 +220,7 @@ protected function addSubmissionFileToNoteFromFilePath(string $filepath, string
'submissionId' => $submission->getId(),
'uploaderUserId' => $uploader->getId(),
'assocType' => Application::ASSOC_TYPE_NOTE,
'assocId' => $note->getId(),
'assocId' => $note->id,
]);
Repo::submissionFile()->add($submissionFile);
}
Expand Down
6 changes: 6 additions & 0 deletions classes/facades/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use PKP\job\repositories\Job as JobRepository;
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\stageAssignment\Repository as StageAssignmentRepository;
use PKP\submissionFile\Repository as SubmissionFileRepository;
Expand Down Expand Up @@ -128,4 +129,9 @@ public static function notification(): NotificationRepository
{
return app(NotificationRepository::class);
}

public static function note(): NoteRepository
{
return app(NoteRepository::class);
}
}
184 changes: 78 additions & 106 deletions classes/note/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,176 +3,148 @@
/**
* @file classes/note/Note.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2000-2021 John Willinsky
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Note
*
* @ingroup note
*
* @see NoteDAO
*
* @brief Class for Note.
*/

namespace PKP\note;

use APP\facades\Repo;
use Eloquence\Behaviours\HasCamelCasing;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use PKP\db\DAO;

class Note extends \PKP\core\DataObject
class Note extends Model
{
/**
* get user id of the note's author
*
* @return int
*/
public function getUserId()
{
return $this->getData('userId');
}
use HasCamelCasing;

/**
* set user id of the note's author
*
* @param int $userId
*/
public function setUserId($userId)
{
$this->setData('userId', $userId);
}
public const NOTE_ORDER_DATE_CREATED = 1;
public const NOTE_ORDER_ID = 2;

/**
* Return the user of the note's author.
*
* @return \PKP\user\User
*/
public function getUser()
{
return Repo::user()->get($this->getUserId(), true);
}
const CREATED_AT = 'date_created';
const UPDATED_AT = 'date_modified';

/**
* get date note was created
*
* @return string (YYYY-MM-DD HH:MM:SS)
*/
public function getDateCreated()
{
return $this->getData('dateCreated');
}
protected $table = 'notes';
protected $primaryKey = 'note_id';

/**
* set date note was created
*
* @param string $dateCreated (YYYY-MM-DD HH:MM:SS)
*/
public function setDateCreated($dateCreated)
{
$this->setData('dateCreated', $dateCreated);
}
protected $fillable = [
'assocType', 'assocId', 'userId',
'dateCreated', 'dateModified',
'title', 'contents'
];

/**
* get date note was modified
*
* @return string (YYYY-MM-DD HH:MM:SS)
*/
public function getDateModified()
protected function casts(): array
{
return $this->getData('dateModified');
return [
'assocType' => 'int',
'assocId' => 'int',
'userId' => 'int',
'dateCreated' => 'datetime',
'dateModified' => 'datetime'
];
}

/**
* set date note was modified
*
* @param string $dateModified (YYYY-MM-DD HH:MM:SS)
* Accessor and Mutator for primary key => id
*/
public function setDateModified($dateModified)
protected function id(): Attribute
{
$this->setData('dateModified', $dateModified);
return Attribute::make(
get: fn($value, $attributes) => $attributes[$this->primaryKey] ?? null,
set: fn($value) => [$this->primaryKey => $value],
);
}

/**
* get note contents
*
* @return string
* Accessor for user. Can be replaced with relationship once User is converted to an Eloquent Model.
*/
public function getContents()
protected function user(): Attribute
{
return $this->getData('contents');
return Attribute::make(
get: function () {
return Repo::user()->get($this->userId, true);
},
);
}

/**
* set note contents
* Compatibility function for including note IDs in grids.
*
* @param string $contents
* @deprecated 3.5 Use $model->id instead. Can be removed once the DataObject pattern is removed.
*/
public function setContents($contents)
public function getId(): int
{
$this->setData('contents', $contents);
return $this->id;
}

/**
* get note title
* Compatibility function for including notes in grids.
*
* @return string
* @deprecated 3.5. Use $model or $model->$field instead. Can be removed once the DataObject pattern is removed.
*/
public function getTitle()
public function getData(?string $field)
{
return $this->getData('title');
return $field ? $this->$field : $this;
}

// Scopes

/**
* set note title
*
* @param string $title
* Scope a query to only include notes with a specific user ID.
*/
public function setTitle($title)
public function scopeWithUserId(Builder $query, int $userId): Builder
{
$this->setData('title', $title);
return $query->where('userId', $userId);
}

/**
* get note type
*
* @return int
* Scope a query to only include notes with a specific assoc type and assoc ID.
*/
public function getAssocType()
public function scopeWithAssoc(Builder $query, int $assocType, int $assocId): Builder
{
return $this->getData('assocType');
return $query->where('assoc_type', $assocType)
->where('assoc_id', $assocId);
}

/**
* set note type
*
* @param int $assocType
* Scope a query to only include notes with a specific type.
*/
public function setAssocType($assocType)
public function scopeWithType(Builder $query, int $type): Builder
{
$this->setData('assocType', $assocType);
return $query->where('type', $type);
}

/**
* get note assoc id
*
* @return int
* Scope a query to only include notes with a specific type.
*/
public function getAssocId()
public function scopeWithContents(Builder $query, string $contents): Builder
{
return $this->getData('assocId');
return $query->where('contents', $contents);
}

/**
* set note assoc id
*
* @param int $assocId
* Scope a query to a specific sort order.
*/
public function setAssocId($assocId)
public function scopeWithSort(Builder $query, int $orderBy = self::NOTE_ORDER_DATE_CREATED, int $sortDirection = DAO::SORT_DIRECTION_DESC): Builder
{
$this->setData('assocId', $assocId);
// Sanitize sort ordering
$orderSanitized = match ($orderBy) {
self::NOTE_ORDER_ID => 'note_id',
self::NOTE_ORDER_DATE_CREATED => 'date_created',
};

$directionSanitized = match ($sortDirection) {
DAO::SORT_DIRECTION_ASC => 'ASC',
DAO::SORT_DIRECTION_DESC => 'DESC',
};

return $query->orderBy($orderSanitized, $directionSanitized);
}
}

if (!PKP_STRICT_MODE) {
class_alias('\PKP\note\Note', '\Note');
}
Loading

0 comments on commit d24432e

Please sign in to comment.