From 8be39a9e43514ad4c783e2559ecbcaf49fa7e36d Mon Sep 17 00:00:00 2001 From: Guido Vollbach Date: Thu, 7 Dec 2023 11:56:40 +0100 Subject: [PATCH] Refactoring --- Services/Badge/classes/class.ilBadge.php | 13 +++++++++- .../classes/class.ilBadgeManagementGUI.php | 24 +++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Services/Badge/classes/class.ilBadge.php b/Services/Badge/classes/class.ilBadge.php index 9d682f73af81..3fe4a67e1faf 100644 --- a/Services/Badge/classes/class.ilBadge.php +++ b/Services/Badge/classes/class.ilBadge.php @@ -16,6 +16,9 @@ * *********************************************************************/ +use ILIAS\ResourceStorage\Services; +use ILIAS\ResourceStorage\Identification\ResourceIdentification; + /** * @author Jörg Lützenkirchen */ @@ -36,6 +39,8 @@ class ilBadge protected ?array $config = null; protected string $criteria = ""; + private ?Services $resource_storage = null; + public function __construct( int $a_id = null ) { @@ -43,6 +48,7 @@ public function __construct( $this->lng = $DIC->language(); $this->db = $DIC->database(); + $this->resource_storage = $DIC->resourceStorage(); if ($a_id) { $this->read($a_id); } @@ -465,6 +471,8 @@ public function delete(): void if (file_exists($this->getImagePath())) { unlink($this->getImagePath()); + } else { + $this->resource_storage->manage()->remove($this->getImageRid(), new ilBadgeFileStakeholder()); } $this->deleteStaticFiles(); @@ -572,7 +580,10 @@ public static function getExtendedTypeCaption( : $lng->txt("badge_subtype_manual")) . ")"; } - public function getImageRid() : ?string + /** + * @return string|null|ResourceIdentification + */ + public function getImageRid() { return $this->image_rid; } diff --git a/Services/Badge/classes/class.ilBadgeManagementGUI.php b/Services/Badge/classes/class.ilBadgeManagementGUI.php index 33a066c98be9..2f6642397d0d 100644 --- a/Services/Badge/classes/class.ilBadgeManagementGUI.php +++ b/Services/Badge/classes/class.ilBadgeManagementGUI.php @@ -17,6 +17,8 @@ *********************************************************************/ use ILIAS\Badge\ilBadgeImage; +use ILIAS\ResourceStorage\Services; +use ILIAS\FileUpload\FileUpload; /** * Class ilBadgeManagementGUI @@ -40,6 +42,8 @@ class ilBadgeManagementGUI private string $parent_obj_type; private ?ilBadgeImage $badge_image = null; + private ?Services $resource_storage = null; + private ?FileUpload $upload_service = null; public function __construct( private readonly int $parent_ref_id, @@ -54,7 +58,9 @@ public function __construct( $this->access = $DIC->access(); $this->toolbar = $DIC->toolbar(); $this->ui_factory = $DIC->ui()->factory(); - $this->tpl = $DIC['tpl']; + $this->resource_storage = $DIC->resourceStorage(); + $this->upload_service = $DIC->upload(); + $this->tpl = $DIC->ui()->mainTemplate(); $this->user = $DIC->user(); $lng = $DIC->language(); $this->parent_obj_id = $a_parent_obj_id @@ -391,13 +397,11 @@ protected function saveBadge(): void if ($form->getInput('img_mode') === 'up') { # $badge->uploadImage($_FILES['img']); - global $DIC; - $upload_service = $DIC->upload(); - $upload_service->process(); - $array_result = $upload_service->getResults(); + $this->upload_service->process(); + $array_result = $this->upload_service->getResults(); $array_result = array_pop($array_result); $stakeholder = new ilBadgeFileStakeholder(); - $identification = $DIC['resource_storage']->manage()->upload($array_result, $stakeholder); + $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder); $badge->setImageRid($identification); $badge->update(); } else { @@ -501,7 +505,13 @@ protected function updateBadge(): void $badge->update(); - $badge->uploadImage($_FILES['img']); + $this->upload_service->process(); + $array_result = $this->upload_service->getResults(); + $array_result = array_pop($array_result); + $stakeholder = new ilBadgeFileStakeholder(); + $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder); + $badge->setImageRid($identification); + $badge->update(); $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true); $ilCtrl->redirect($this, 'listBadges');