ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
17 private $pathFactory;
18
23
27 private $database;
28
32 private $fileSystem;
33
38
43
53 public function __construct(
58 ilLogger $logger = null,
60 string $webDirectory = CLIENT_WEB_DIR
61 ) {
62 $this->database = $database;
63 $this->pathFactory = $pathFactory;
64 $this->templateRepository = $templateRepository;
65
66 if (null === $logger) {
67 global $DIC;
68 $logger = $DIC->logger()->cert();
69 }
70 $this->logger = $logger;
71
72 if (null === $fileSystem) {
73 global $DIC;
74 $fileSystem = $DIC->filesystem()->web();
75 }
76 $this->fileSystem = $fileSystem;
77
78 if (null === $objectHelper) {
80 }
81 $this->objectHelper = $objectHelper;
82
83 $this->webDirectory = $webDirectory;
84 }
85
96 public function cloneCertificate(
97 ilObject $oldObject,
98 ilObject $newObject,
99 string $iliasVersion = ILIAS_VERSION_NUMERIC,
100 string $webDir = CLIENT_WEB_DIR
101 ) {
102 $oldType = $oldObject->getType();
103 $newType = $newObject->getType();
104
105 if ($oldType !== $newType) {
106 throw new ilException(sprintf(
107 'The types "%s" and "%s" for cloning does not match',
108 $oldType,
109 $newType
110 ));
111 }
112
113 $certificatePath = $this->pathFactory->create($newObject);
114
115 $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId());
116
118 foreach ($templates as $template) {
119 $backgroundImagePath = $template->getBackgroundImagePath();
120 $backgroundImageFile = basename($backgroundImagePath);
121 $backgroundImageThumbnail = dirname($backgroundImagePath) . '/background.jpg.thumb.jpg';
122
123 $newBackgroundImage = '';
124 $newBackgroundImageThumbnail = '';
125 if ($this->fileSystem->has($backgroundImagePath) &&
126 !$this->fileSystem->hasDir($backgroundImagePath)
127 ) {
128 $newBackgroundImage = $certificatePath . $backgroundImageFile;
129 $newBackgroundImageThumbnail = str_replace($webDir, '', $this->getBackgroundImageThumbPath($certificatePath));
130 if ($this->fileSystem->has($newBackgroundImage) &&
131 !$this->fileSystem->hasDir($newBackgroundImage)
132 ) {
133 $this->fileSystem->delete($newBackgroundImage);
134 }
135
136 $this->fileSystem->copy(
137 $backgroundImagePath,
138 $newBackgroundImage
139 );
140 }
141
142 if (
143 strlen($newBackgroundImageThumbnail) > 0 &&
144 $this->fileSystem->has($backgroundImageThumbnail) &&
145 !$this->fileSystem->hasDir($backgroundImageThumbnail)
146 ) {
147 if ($this->fileSystem->has($newBackgroundImageThumbnail) &&
148 !$this->fileSystem->hasDir($newBackgroundImageThumbnail)
149 ) {
150 $this->fileSystem->delete($newBackgroundImageThumbnail);
151 }
152
153 $this->fileSystem->copy(
154 $backgroundImageThumbnail,
155 $newBackgroundImageThumbnail
156 );
157 }
158
159 $newCardThumbImage = '';
160 $cardThumbImagePath = (string) $template->getThumbnailImagePath();
161
162 if ($this->fileSystem->has($cardThumbImagePath) && !$this->fileSystem->hasDir($cardThumbImagePath)) {
163 $newCardThumbImage = $certificatePath . basename($cardThumbImagePath);
164 if ($this->fileSystem->has($newCardThumbImage) && !$this->fileSystem->hasDir($newCardThumbImage)) {
165 $this->fileSystem->delete($newCardThumbImage);
166 }
167 $this->fileSystem->copy(
168 $cardThumbImagePath,
169 $newCardThumbImage
170 );
171 }
172
173 $newTemplate = new ilCertificateTemplate(
174 $newObject->getId(),
175 $this->objectHelper->lookupType((int) $newObject->getId()),
176 $template->getCertificateContent(),
177 $template->getCertificateHash(),
178 $template->getTemplateValues(),
179 $template->getVersion(),
180 $iliasVersion,
181 time(),
182 $template->isCurrentlyActive(),
183 $newBackgroundImage,
184 $newCardThumbImage
185 );
186
187 $this->templateRepository->save($newTemplate);
188 }
189
190 // #10271
191 if ($this->readActive($oldObject->getId())) {
192 $this->database->replace(
193 'il_certificate',
194 array('obj_id' => array('integer', $newObject->getId())),
195 array()
196 );
197 }
198 }
199
204 private function readActive(int $objectId) : int
205 {
206 $sql = 'SELECT obj_id FROM il_certificate WHERE obj_id = ' . $this->database->quote($objectId, 'integer');
207
208 $query = $this->database->query($sql);
209
210 return $this->database->numRows($query);
211 }
212
218 private function getBackgroundImageName()
219 {
220 return "background.jpg";
221 }
222
228 private function getBackgroundImageThumbPath(string $certificatePath) : string
229 {
230 return $this->webDirectory . $certificatePath . $this->getBackgroundImageName() . ".thumb.jpg";
231 }
232}
An exception for terminatinating execution or to throw for unit testing.
getBackgroundImageThumbPath(string $certificatePath)
Returns the filesystem path of the background image thumbnail.
getBackgroundImageName()
Returns the filename of the background image.
__construct(ilDBInterface $database, ilCertificatePathFactory $pathFactory, ilCertificateTemplateRepository $templateRepository, \ILIAS\Filesystem\Filesystem $fileSystem=null, ilLogger $logger=null, ilCertificateObjectHelper $objectHelper=null, string $webDirectory=CLIENT_WEB_DIR)
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
const ILIAS_VERSION_NUMERIC
Interface ilDBInterface.
Class FlySystemFileAccessTest.
Class ChatMainBarProvider \MainMenu\Provider.
$query
$DIC
Definition: xapitoken.php:46