From f5e7275ee5597225e34ddd0b508479768cef9c1e Mon Sep 17 00:00:00 2001 From: mjansen Date: Tue, 22 Feb 2022 16:13:49 +0100 Subject: [PATCH 1/3] Certificate: Fix cloning templates (Mantis 32127) --- .../Clone/class.ilCertificateCloneAction.php | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php b/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php index 8504623963be..78c5a9a5e47e 100644 --- a/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php +++ b/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php @@ -114,6 +114,8 @@ public function cloneCertificate( $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId()); + $globalCertificateBackgroundImagePath = ilObjCertificateSettingsAccess::getBackgroundImagePath(true); + /** @var ilCertificateTemplate $template */ foreach ($templates as $template) { $backgroundImagePath = $template->getBackgroundImagePath(); @@ -122,43 +124,49 @@ public function cloneCertificate( $newBackgroundImage = ''; $newBackgroundImageThumbnail = ''; - if ($this->fileSystem->has($backgroundImagePath) && - !$this->fileSystem->hasDir($backgroundImagePath) - ) { - $newBackgroundImage = $certificatePath . $backgroundImageFile; - $newBackgroundImageThumbnail = str_replace($webDir, '', $this->getBackgroundImageThumbPath($certificatePath)); - if ($this->fileSystem->has($newBackgroundImage) && - !$this->fileSystem->hasDir($newBackgroundImage) + if ($globalCertificateBackgroundImagePath !== $backgroundImagePath) { + if ($this->fileSystem->has($backgroundImagePath) && + !$this->fileSystem->hasDir($backgroundImagePath) ) { - $this->fileSystem->delete($newBackgroundImage); + $newBackgroundImage = $certificatePath . $backgroundImageFile; + $newBackgroundImageThumbnail = str_replace( + $webDir, '', + $this->getBackgroundImageThumbPath($certificatePath) + ); + if ($this->fileSystem->has($newBackgroundImage) && + !$this->fileSystem->hasDir($newBackgroundImage) + ) { + $this->fileSystem->delete($newBackgroundImage); + } + + $this->fileSystem->copy( + $backgroundImagePath, + $newBackgroundImage + ); } - $this->fileSystem->copy( - $backgroundImagePath, - $newBackgroundImage - ); - } - - if ( - strlen($newBackgroundImageThumbnail) > 0 && - $this->fileSystem->has($backgroundImageThumbnail) && - !$this->fileSystem->hasDir($backgroundImageThumbnail) - ) { - if ($this->fileSystem->has($newBackgroundImageThumbnail) && - !$this->fileSystem->hasDir($newBackgroundImageThumbnail) + if ( + $newBackgroundImageThumbnail !== '' && + $this->fileSystem->has($backgroundImageThumbnail) && + !$this->fileSystem->hasDir($backgroundImageThumbnail) ) { - $this->fileSystem->delete($newBackgroundImageThumbnail); + if ($this->fileSystem->has($newBackgroundImageThumbnail) && + !$this->fileSystem->hasDir($newBackgroundImageThumbnail) + ) { + $this->fileSystem->delete($newBackgroundImageThumbnail); + } + + $this->fileSystem->copy( + $backgroundImageThumbnail, + $newBackgroundImageThumbnail + ); } - - $this->fileSystem->copy( - $backgroundImageThumbnail, - $newBackgroundImageThumbnail - ); + } else { + $newBackgroundImage = $globalCertificateBackgroundImagePath; } $newCardThumbImage = ''; - $cardThumbImagePath = (string) $template->getThumbnailImagePath(); - + $cardThumbImagePath = $template->getThumbnailImagePath(); if ($this->fileSystem->has($cardThumbImagePath) && !$this->fileSystem->hasDir($cardThumbImagePath)) { $newCardThumbImage = $certificatePath . basename($cardThumbImagePath); if ($this->fileSystem->has($newCardThumbImage) && !$this->fileSystem->hasDir($newCardThumbImage)) { From 52c70a13a097d9754475102a9f64966a97db1081 Mon Sep 17 00:00:00 2001 From: mjansen Date: Tue, 22 Feb 2022 17:00:18 +0100 Subject: [PATCH 2/3] Certificate: Fix cloning templates (Mantis 32127) --- .../Clone/class.ilCertificateCloneAction.php | 19 ++++++++++++----- .../test/ilCertificateCloneActionTest.php | 21 ++++++++++++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php b/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php index 78c5a9a5e47e..49bee74edcfe 100644 --- a/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php +++ b/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php @@ -40,6 +40,7 @@ class ilCertificateCloneAction * @var string */ private $webDirectory; + private ?string $global_certificate_path; /** * @param ilDBInterface $database @@ -57,7 +58,8 @@ public function __construct( \ILIAS\Filesystem\Filesystem $fileSystem = null, ilLogger $logger = null, ilCertificateObjectHelper $objectHelper = null, - string $webDirectory = CLIENT_WEB_DIR + string $webDirectory = CLIENT_WEB_DIR, + string $global_certificate_path = null ) { $this->database = $database; $this->pathFactory = $pathFactory; @@ -80,6 +82,15 @@ public function __construct( } $this->objectHelper = $objectHelper; + if (null === $global_certificate_path) { + $global_certificate_path = str_replace( + '[CLIENT_WEB_DIR]', + '', + ilObjCertificateSettingsAccess::getBackgroundImagePath(true) + ); + } + $this->global_certificate_path = $global_certificate_path; + $this->webDirectory = $webDirectory; } @@ -114,8 +125,6 @@ public function cloneCertificate( $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId()); - $globalCertificateBackgroundImagePath = ilObjCertificateSettingsAccess::getBackgroundImagePath(true); - /** @var ilCertificateTemplate $template */ foreach ($templates as $template) { $backgroundImagePath = $template->getBackgroundImagePath(); @@ -124,7 +133,7 @@ public function cloneCertificate( $newBackgroundImage = ''; $newBackgroundImageThumbnail = ''; - if ($globalCertificateBackgroundImagePath !== $backgroundImagePath) { + if ($this->global_certificate_path !== $backgroundImagePath) { if ($this->fileSystem->has($backgroundImagePath) && !$this->fileSystem->hasDir($backgroundImagePath) ) { @@ -162,7 +171,7 @@ public function cloneCertificate( ); } } else { - $newBackgroundImage = $globalCertificateBackgroundImagePath; + $newBackgroundImage = $this->global_certificate_path; } $newCardThumbImage = ''; diff --git a/Services/Certificate/test/ilCertificateCloneActionTest.php b/Services/Certificate/test/ilCertificateCloneActionTest.php index 015aca5ca34a..5599805619a4 100644 --- a/Services/Certificate/test/ilCertificateCloneActionTest.php +++ b/Services/Certificate/test/ilCertificateCloneActionTest.php @@ -61,12 +61,26 @@ public function testCloneCertificate() '/some/where/background.jpg', '/some/where/card_thumb.jpg', $id = null + ), + new ilCertificateTemplate( + 30, + 'crs', + ' Some Content ', + md5(' Some Content '), + '[]', + '3', + 'v5.3.0', + 123456789, + true, + '/certificates/default/background.jpg', + '/some/where/card_thumb.jpg', + $id = null ) ) ); $templateRepository - ->expects($this->exactly(2)) + ->expects($this->exactly(3)) ->method('save'); $fileSystem = $this->getMockBuilder('\ILIAS\Filesystem\Filesystem') @@ -76,7 +90,7 @@ public function testCloneCertificate() ->willReturn(true); $fileSystem - ->expects($this->exactly(6)) + ->expects($this->exactly(7)) ->method('copy'); $logger = $this->getMockBuilder('ilLogger') @@ -96,7 +110,8 @@ public function testCloneCertificate() $fileSystem, $logger, $objectHelper, - 'some/web/directory' + 'some/web/directory', + '/certificates/default/background.jpg' ); $oldObject = $this->getMockBuilder('ilObject') From 5b6400290743d6eb81cd328171dd21d4c7b1ae50 Mon Sep 17 00:00:00 2001 From: mjansen Date: Tue, 22 Feb 2022 17:04:14 +0100 Subject: [PATCH 3/3] Certificate: Fix cloning templates (Mantis 32127) --- .../Template/Action/Clone/class.ilCertificateCloneAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php b/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php index 49bee74edcfe..56ca2a92bfa8 100644 --- a/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php +++ b/Services/Certificate/classes/Template/Action/Clone/class.ilCertificateCloneAction.php @@ -40,7 +40,7 @@ class ilCertificateCloneAction * @var string */ private $webDirectory; - private ?string $global_certificate_path; + private $global_certificate_path; /** * @param ilDBInterface $database