Skip to content

Commit

Permalink
Merge pull request pkp#10787 from touhidurabir/i10668_main
Browse files Browse the repository at this point in the history
pkp#10668 Announcement image handling fixed
  • Loading branch information
touhidurabir authored Jan 23, 2025
2 parents be2343c + 69319c0 commit fdd66d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 16 additions & 4 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 = $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 as part of update
// 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 @@ -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
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 fdd66d9

Please sign in to comment.