Skip to content

Commit

Permalink
pkp#10668 Announcement image handling fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Jan 9, 2025
1 parent 7b40613 commit 2bd0920
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 18 additions & 6 deletions classes/announcement/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function save(array $options = [])

Hook::call('Announcement::add', [$this]);

$hasNewImage = $this?->image?->temporaryFileId;
$hasNewImage = isset($this?->image?->temporaryFileId) ? $this->image->temporaryFileId : null;

// if announcement is being inserted and includes new image, upload it
if ($newlyCreated) {
Expand All @@ -135,6 +135,16 @@ public function save(array $options = [])
$this->handleImageUpload();
}

// If there is no new image and image data exists in DB and it's now removed
// need to delete the image for this announcement model instance
if (!$hasNewImage && !$this?->image && $this->fresh()->image) {
$this->deleteImage();
DB::table($this->getSettingsTable())
->where($this->getkeyName(), $this->getKey())
->where('setting_name', 'image')
->delete();
}

return $saved;
}

Expand Down Expand Up @@ -280,7 +290,7 @@ protected function imageUrl(bool $withTimestamp = true): Attribute
protected function imageAltText(): Attribute
{
return Attribute::make(
get: fn () => $this->image?->altText ?? ''
get: fn () => isset($this->image?->altText) ? $this->image->altText : ''
);
}

Expand All @@ -289,8 +299,9 @@ protected function imageAltText(): Attribute
*/
protected function deleteImage(): void
{
$image = $this->getAttribute('image');
if ($image?->uploadName) {
$image = $this->fresh()->image;

if ($image && isset($image->uploadName)) {
$publicFileManager = new PublicFileManager();
$filesPath = $this->hasAttribute('assocId')
? $publicFileManager->getContextFilesPath($this->getAttribute('assocId'))
Expand All @@ -314,7 +325,8 @@ protected function deleteImage(): void
protected function handleImageUpload(): void
{
$image = $this->getAttribute('image');
if (!$image?->temporaryFileId) {

if (!isset($image?->temporaryFileId)) {
return;
}

Expand Down Expand Up @@ -351,7 +363,7 @@ protected function getImageData(TemporaryFile $temporaryFile): array
'name' => $temporaryFile->getOriginalFileName(),
'uploadName' => $this->getImageFilename($temporaryFile),
'dateUploaded' => Core::getCurrentDate(),
'altText' => $image->altText ?? '',
'altText' => isset($image->altText) ? $image->altText : '',
];
}

Expand Down
5 changes: 5 additions & 0 deletions classes/services/PKPSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ public function coerce($value, $type, $schema)
return $newArray;
case 'object':
$newObject = []; // we handle JSON objects as assoc arrays in PHP

if (isValidJson($value)) {
$value = json_decode($value, true);
}

foreach ($schema->properties as $propName => $propSchema) {
if (!isset($value[$propName]) || !empty($propSchema->readOnly)) {
continue;
Expand Down

0 comments on commit 2bd0920

Please sign in to comment.