Skip to content

Commit

Permalink
pkp#10328 Fix dates as Carbon, fillables and garded attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Sep 30, 2024
1 parent 0f5c2aa commit 070aa6f
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 296 deletions.
6 changes: 3 additions & 3 deletions api/v1/announcements/PKPAnnouncementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function add(Request $illuminateRequest): JsonResponse
$sendEmail = (bool) filter_var($params['sendEmail'], FILTER_VALIDATE_BOOLEAN);

if ($context) {
$this->notifyUsers($request, $context, $announcement->getKey(), $sendEmail);
$this->notifyUsers($request, $context, $announcement->id, $sendEmail);
}

return response()->json(Repo::announcement()->getSchemaMap()->map($announcement), Response::HTTP_OK);
Expand Down Expand Up @@ -255,7 +255,7 @@ public function edit(Request $illuminateRequest): JsonResponse
}

$params = $this->convertStringsToSchema(PKPSchemaService::SCHEMA_ANNOUNCEMENT, $illuminateRequest->input());
$params['id'] = $announcement->getKey();
$params['id'] = $announcement->id;
$params['typeId'] ??= null;

$primaryLocale = $context ? $context->getPrimaryLocale() : $request->getSite()->getPrimaryLocale();
Expand All @@ -275,7 +275,7 @@ public function edit(Request $illuminateRequest): JsonResponse
], Response::HTTP_BAD_REQUEST);
}

$announcement = Announcement::find($announcement->getKey());
$announcement = Announcement::find($announcement->id);

return response()->json(Repo::announcement()->getSchemaMap()->map($announcement), Response::HTTP_OK);
}
Expand Down
33 changes: 18 additions & 15 deletions classes/announcement/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ class Announcement extends Model
*
* @var array
*/
protected $fillable = ['assocType', 'assocId', 'typeId', 'title', 'image', 'description', 'descriptionShort'];
protected $guarded = ['announcementId', 'datePosted'];

/**
* @inheritDoc
*/
public static function getSchemaName(): string
public static function getSchemaName(): ?string
{
return PKPSchemaService::SCHEMA_ANNOUNCEMENT;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public function save(array $options = [])
}

// If it's updated model and a new image is uploaded, first, delete an old one
$hasNewImage = $this->hasAttribute('temporaryFileId');
$hasNewImage = $this?->image?->temporaryFileId;
if ($saved && !$newlyCreated && $hasNewImage) {
$this->deleteImage();
$this->handleImageUpload();
Expand Down Expand Up @@ -270,17 +270,7 @@ protected function imageUrl(bool $withTimestamp = true): Attribute
protected function imageAltText(): Attribute
{
return Attribute::make(
get: fn () => $this->image->altText ?? ''
);
}

/**
* Get the date announcement was posted
*/
protected function datePosted(): Attribute
{
return Attribute::make(
get: fn (string $value) => date('Y-m-d', strtotime($value))
get: fn () => $this->image?->altText ?? ''
);
}

Expand Down Expand Up @@ -362,7 +352,7 @@ protected function getImageFilename(TemporaryFile $temporaryFile): string
{
$fileManager = new FileManager();

return $this->getAttribute('announcementId')
return $this->id
. $fileManager->getImageExtension($temporaryFile->getFileType());
}

Expand Down Expand Up @@ -419,4 +409,17 @@ protected function storeTemporaryFile(TemporaryFile $temporaryFile, string $newP

return $result;
}

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'dateExpire' => 'datetime',
'datePosted' => 'datetime',
];
}
}
242 changes: 0 additions & 242 deletions classes/announcement/Collector.php

This file was deleted.

2 changes: 1 addition & 1 deletion classes/core/PKPApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class_alias('\PKP\payment\QueuedPayment', '\QueuedPayment'); // QueuedPayment in
Hook::addUnsupportedHooks('APIHandler::endpoints'); // pkp/pkp-lib#9434 Unavailable since stable-3_4_0; remove for 3.6.0 development branch
Hook::addUnsupportedHooks('Mail::send', 'EditorAction::modifyDecisionOptions', 'EditorAction::recordDecision', 'Announcement::getProperties', 'Author::getProperties::values', 'EmailTemplate::getProperties', 'Galley::getProperties::values', 'Issue::getProperties::fullProperties', 'Issue::getProperties::summaryProperties', 'Issue::getProperties::values', 'Publication::getProperties', 'Section::getProperties::fullProperties', 'Section::getProperties::summaryProperties', 'Section::getProperties::values', 'Submission::getProperties::values', 'SubmissionFile::getProperties', 'User::getProperties::fullProperties', 'User::getProperties::reviewerSummaryProperties', 'User::getProperties::summaryProperties', 'User::getProperties::values', 'Announcement::getMany::queryBuilder', 'Announcement::getMany::queryObject', 'Author::getMany::queryBuilder', 'Author::getMany::queryObject', 'EmailTemplate::getMany::queryBuilder', 'EmailTemplate::getMany::queryObject::custom', 'EmailTemplate::getMany::queryObject::default', 'Galley::getMany::queryBuilder', 'Issue::getMany::queryBuilder', 'Publication::getMany::queryBuilder', 'Publication::getMany::queryObject', 'Stats::getOrderedObjects::queryBuilder', 'Stats::getRecords::queryBuilder', 'Stats::queryBuilder', 'Stats::queryObject', 'Submission::getMany::queryBuilder', 'Submission::getMany::queryObject', 'SubmissionFile::getMany::queryBuilder', 'SubmissionFile::getMany::queryObject', 'User::getMany::queryBuilder', 'User::getMany::queryObject', 'User::getReviewers::queryBuilder', 'CategoryDAO::_fromRow', 'IssueDAO::_fromRow', 'IssueDAO::_returnIssueFromRow', 'SectionDAO::_fromRow', 'UserDAO::_returnUserFromRow', 'UserDAO::_returnUserFromRowWithData', 'UserDAO::_returnUserFromRowWithReviewerStats', 'UserGroupDAO::_returnFromRow', 'ReviewerSubmissionDAO::_fromRow', 'API::stats::publication::abstract::params', 'API::stats::publication::galley::params', 'API::stats::publications::abstract::params', 'API::stats::publications::galley::params', 'PKPLocale::installLocale', 'PKPLocale::registerLocaleFile', 'PKPLocale::registerLocaleFile::isValidLocaleFile', 'PKPLocale::translate', 'API::submissions::files::params', 'ArticleGalleyDAO::getLocalizedGalleysByArticle', 'PluginGridHandler::plugin', 'PluginGridHandler::plugin', 'SubmissionFile::assignedFileStages', 'SubmissionHandler::saveSubmit'); // From the 3.4.0 Release Notebook; remove for 3.6.0 development branch
Hook::addUnsupportedHooks('AcronPlugin::parseCronTab'); // pkp/pkp-lib#9678 Unavailable since stable-3_5_0;
Hook::addUnsupportedHooks('Announcement::delete::before', 'Announcement::delete'); // pkp/pkp-lib#10328 Unavailable since stable-3_5_0, use Eloquent Model events instead
Hook::addUnsupportedHooks('Announcement::delete::before', 'Announcement::delete', 'Announcement::Collector'); // pkp/pkp-lib#10328 Unavailable since stable-3_5_0, use Eloquent Model events instead

// If not in strict mode, globally expose constants on this class.
if (!PKP_STRICT_MODE) {
Expand Down
2 changes: 1 addition & 1 deletion classes/core/SettingsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function update(array $values)
$settingCount = DB::table($us)->whereIn($us . '.' . $primaryKey, $newQuery->select($primaryKey))
->update([$us . '.setting_value' => DB::raw($sql)]);

return $count ? $count + $settingCount : $settingCount; // TODO Return the count of updated setting rows?
return ($count ?? 0) + $settingCount;
}

/**
Expand Down
Loading

0 comments on commit 070aa6f

Please sign in to comment.