ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilCertificateCloneAction.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
12 private $logger;
13
18
23
27 private $database;
28
32 private $fileSystem;
33
38
48 public function __construct(
53 ilLogger $logger = null,
55 ) {
56 $this->database = $database;
57 $this->certificateFactory = $certificateFactory;
58 $this->templateRepository = $templateRepository;
59
60 if (null === $logger) {
61 global $DIC;
62 $logger = $DIC->logger()->cert();
63 }
64 $this->logger = $logger;
65
66 if (null === $fileSystem) {
67 global $DIC;
68 $fileSystem = $DIC->filesystem()->web();
69 }
70 $this->fileSystem = $fileSystem;
71
72 if (null === $objectHelper) {
74 }
75 $this->objectHelper = $objectHelper;
76 }
77
88 public function cloneCertificate(
89 ilObject $oldObject,
90 ilObject $newObject,
91 string $iliasVersion = ILIAS_VERSION_NUMERIC,
92 string $webDir = CLIENT_WEB_DIR
93 ) {
94 $oldType = $oldObject->getType();
95 $newType = $newObject->getType();
96
97 if ($oldType !== $newType) {
98 throw new ilException(sprintf(
99 'The types "%s" and "%s" for cloning does not match',
100 $oldType,
101 $newType
102 ));
103 }
104
105 $newCertificate = $this->certificateFactory->create($newObject);
106
107 $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId());
108
110 foreach ($templates as $template) {
111 $backgroundImagePath = $template->getBackgroundImagePath();
112 $backgroundImageFile = basename($backgroundImagePath);
113 $backgroundImageThumbnail = dirname($backgroundImagePath) . '/background.jpg.thumb.jpg';
114
115 $newBackgroundImage = '';
116 $newBackgroundImageThumbnail = '';
117
118 if ($this->fileSystem->has($backgroundImagePath) &&
119 !$this->fileSystem->hasDir($backgroundImagePath)
120 ) {
121 $newBackgroundImage = $newCertificate->getBackgroundImageDirectory() . $backgroundImageFile;
122 $newBackgroundImageThumbnail = str_replace($webDir, '', $newCertificate->getBackgroundImageThumbPath());
123
124 if ($this->fileSystem->has($newBackgroundImage) &&
125 !$this->fileSystem->hasDir($newBackgroundImage)
126 ) {
127 $this->fileSystem->delete($newBackgroundImage);
128 }
129
130 $this->fileSystem->copy(
131 $backgroundImagePath,
132 $newBackgroundImage
133 );
134 }
135
136 if (
137 strlen($newBackgroundImageThumbnail) > 0 &&
138 $this->fileSystem->has($backgroundImageThumbnail) &&
139 !$this->fileSystem->hasDir($backgroundImageThumbnail)
140 ) {
141 if ($this->fileSystem->has($newBackgroundImageThumbnail) &&
142 !$this->fileSystem->hasDir($newBackgroundImageThumbnail)
143 ) {
144 $this->fileSystem->delete($newBackgroundImageThumbnail);
145 }
146
147 $this->fileSystem->copy(
148 $backgroundImageThumbnail,
149 $newBackgroundImageThumbnail
150 );
151 }
152
153 $newCardThumbImage = '';
154 $cardThumbImagePath = $template->getThumbnailImagePath();
155
156 if ($this->fileSystem->has($cardThumbImagePath) && !$this->fileSystem->hasDir($cardThumbImagePath)) {
157 $newCardThumbImage = $newCertificate->getBackgroundImageDirectory() . basename($cardThumbImagePath);
158 if ($this->fileSystem->has($newCardThumbImage) && !$this->fileSystem->hasDir($newCardThumbImage)) {
159 $this->fileSystem->delete($newCardThumbImage);
160 }
161 $this->fileSystem->copy(
162 $cardThumbImagePath,
163 $newCardThumbImage
164 );
165 }
166
167 $newTemplate = new ilCertificateTemplate(
168 $newObject->getId(),
169 $this->objectHelper->lookupType($newObject->getId()),
170 $template->getCertificateContent(),
171 $template->getCertificateHash(),
172 $template->getTemplateValues(),
173 $template->getVersion(),
174 $iliasVersion,
175 time(),
176 $template->isCurrentlyActive(),
177 $newBackgroundImage,
178 $newCardThumbImage
179 );
180
181 $this->templateRepository->save($newTemplate);
182 }
183
184 // #10271
185 if ($this->readActive($oldObject->getId())) {
186 $this->database->replace(
187 'il_certificate',
188 array('obj_id' => array('integer', $newObject->getId())),
189 array()
190 );
191 }
192 }
193
198 private function readActive(int $objectId) : int
199 {
200 $sql = 'SELECT obj_id FROM il_certificate WHERE obj_id = ' . $this->database->quote($objectId, 'integer');
201
202 $query = $this->database->query($sql);
203
204 return $this->database->numRows($query);
205 }
206}
An exception for terminatinating execution or to throw for unit testing.
__construct(ilDBInterface $database, ilCertificateFactory $certificateFactory, ilCertificateTemplateRepository $templateRepository, \ILIAS\Filesystem\Filesystem $fileSystem=null, ilLogger $logger=null, ilCertificateObjectHelper $objectHelper=null)
Base class for ILIAS Exception handling.
Component logger with individual log levels by component id.
Class ilObject Basic functions for all objects.
getType()
get object type @access public
getId()
get object id @access public
$template
const ILIAS_VERSION_NUMERIC
Interface ilDBInterface.
Class FlySystemFileAccessTest.
Class BaseForm.
$query
global $DIC
Definition: saml.php:7