ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
37  private $objectHelper;
38 
42  private $webDirectory;
44 
49 
59  public function __construct(
64  ilLogger $logger = null,
68  string $global_certificate_path = null
69  ) {
70  $this->database = $database;
71  $this->pathFactory = $pathFactory;
72  $this->templateRepository = $templateRepository;
73 
74  if (null === $logger) {
75  global $DIC;
76  $logger = $DIC->logger()->cert();
77  }
78  $this->logger = $logger;
79 
80  if (null === $fileSystem) {
81  global $DIC;
82  $fileSystem = $DIC->filesystem()->web();
83  }
84  $this->fileSystem = $fileSystem;
85 
86  if (null === $objectHelper) {
88  }
89  $this->objectHelper = $objectHelper;
90 
93  }
94  $this->global_certificate_settings = $global_certificate_settings;
95 
96 
97  if (null === $global_certificate_path) {
98  $global_certificate_path = $this->global_certificate_settings->getDefaultBackgroundImagePath(true);
99  }
100  $this->global_certificate_path = $global_certificate_path;
101 
102  $this->webDirectory = $webDirectory;
103  }
104 
115  public function cloneCertificate(
116  ilObject $oldObject,
117  ilObject $newObject,
118  string $iliasVersion = ILIAS_VERSION_NUMERIC,
119  string $webDir = CLIENT_WEB_DIR
120  ) {
121  $oldType = $oldObject->getType();
122  $newType = $newObject->getType();
123 
124  if ($oldType !== $newType) {
125  throw new ilException(sprintf(
126  'The types "%s" and "%s" for cloning does not match',
127  $oldType,
128  $newType
129  ));
130  }
131 
132  $certificatePath = $this->pathFactory->create($newObject);
133 
134  $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId());
135 
137  foreach ($templates as $template) {
138  $backgroundImagePath = $template->getBackgroundImagePath();
139  $backgroundImageFile = basename($backgroundImagePath);
140  $backgroundImageThumbnail = dirname($backgroundImagePath) . '/background.jpg.thumb.jpg';
141 
142  $newBackgroundImage = '';
143  $newBackgroundImageThumbnail = '';
144  if ($this->global_certificate_path !== $backgroundImagePath) {
145  if ($this->fileSystem->has($backgroundImagePath) &&
146  !$this->fileSystem->hasDir($backgroundImagePath)
147  ) {
148  $newBackgroundImage = $certificatePath . $backgroundImageFile;
149  $newBackgroundImageThumbnail = str_replace(
150  $webDir,
151  '',
152  $this->getBackgroundImageThumbPath($certificatePath)
153  );
154  if ($this->fileSystem->has($newBackgroundImage) &&
155  !$this->fileSystem->hasDir($newBackgroundImage)
156  ) {
157  $this->fileSystem->delete($newBackgroundImage);
158  }
159 
160  $this->fileSystem->copy(
161  $backgroundImagePath,
162  $newBackgroundImage
163  );
164  }
165 
166  if (
167  $newBackgroundImageThumbnail !== '' &&
168  $this->fileSystem->has($backgroundImageThumbnail) &&
169  !$this->fileSystem->hasDir($backgroundImageThumbnail)
170  ) {
171  if ($this->fileSystem->has($newBackgroundImageThumbnail) &&
172  !$this->fileSystem->hasDir($newBackgroundImageThumbnail)
173  ) {
174  $this->fileSystem->delete($newBackgroundImageThumbnail);
175  }
176 
177  $this->fileSystem->copy(
178  $backgroundImageThumbnail,
179  $newBackgroundImageThumbnail
180  );
181  }
182  } else {
183  $newBackgroundImage = $this->global_certificate_path;
184  }
185 
186  $newCardThumbImage = '';
187  $cardThumbImagePath = $template->getThumbnailImagePath();
188  if ($this->fileSystem->has($cardThumbImagePath) && !$this->fileSystem->hasDir($cardThumbImagePath)) {
189  $newCardThumbImage = $certificatePath . basename($cardThumbImagePath);
190  if ($this->fileSystem->has($newCardThumbImage) && !$this->fileSystem->hasDir($newCardThumbImage)) {
191  $this->fileSystem->delete($newCardThumbImage);
192  }
193  $this->fileSystem->copy(
194  $cardThumbImagePath,
195  $newCardThumbImage
196  );
197  }
198 
199  $newTemplate = new ilCertificateTemplate(
200  $newObject->getId(),
201  $this->objectHelper->lookupType((int) $newObject->getId()),
202  $template->getCertificateContent(),
203  $template->getCertificateHash(),
204  $template->getTemplateValues(),
205  $template->getVersion(),
206  $iliasVersion,
207  time(),
208  $template->isCurrentlyActive(),
209  $newBackgroundImage,
210  $newCardThumbImage
211  );
212 
213  $this->templateRepository->save($newTemplate);
214  }
215 
216  // #10271
217  if ($this->readActive($oldObject->getId())) {
218  $this->database->replace(
219  'il_certificate',
220  array('obj_id' => array('integer', $newObject->getId())),
221  array()
222  );
223  }
224  }
225 
230  private function readActive(int $objectId) : int
231  {
232  $sql = 'SELECT obj_id FROM il_certificate WHERE obj_id = ' . $this->database->quote($objectId, 'integer');
233 
234  $query = $this->database->query($sql);
235 
236  return $this->database->numRows($query);
237  }
238 
244  private function getBackgroundImageName()
245  {
246  return "background.jpg";
247  }
248 
254  private function getBackgroundImageThumbPath(string $certificatePath) : string
255  {
256  return $this->webDirectory . $certificatePath . $this->getBackgroundImageName() . ".thumb.jpg";
257  }
258 }
const ILIAS_VERSION_NUMERIC
Class ChatMainBarProvider .
__construct(ilDBInterface $database, ilCertificatePathFactory $pathFactory, ilCertificateTemplateRepository $templateRepository, \ILIAS\Filesystem\Filesystem $fileSystem=null, ilLogger $logger=null, ilCertificateObjectHelper $objectHelper=null, ?ilObjCertificateSettings $global_certificate_settings=null, string $webDirectory=CLIENT_WEB_DIR, string $global_certificate_path=null)
Class ilObjCertificateSettings.
getId()
get object id public
global $DIC
Definition: goto.php:24
$query
const CLIENT_WEB_DIR
Definition: constants.php:45
getType()
get object type public
getBackgroundImageThumbPath(string $certificatePath)
Returns the filesystem path of the background image thumbnail.
Component logger with individual log levels by component id.
getBackgroundImageName()
Returns the filename of the background image.
Class FlySystemFileAccessTest disabled disabled disabled.