ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCertificateCloneAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
30 {
31  private readonly Filesystem $fileSystem;
33  private readonly string $global_certificate_path;
35 
36  public function __construct(
37  private readonly ilDBInterface $database,
38  private readonly ilCertificatePathFactory $pathFactory,
39  private readonly ilCertificateTemplateRepository $templateRepository,
40  private readonly string $webDirectory = CLIENT_WEB_DIR,
41  ?Filesystem $fileSystem = null,
42  ?ilCertificateObjectHelper $objectHelper = null,
43  ?ilObjCertificateSettings $global_certificate_settings = null,
44  string $global_certificate_path = null
45  ) {
46  if (null === $fileSystem) {
47  global $DIC;
48  $fileSystem = $DIC->filesystem()->web();
49  }
50  $this->fileSystem = $fileSystem;
51 
52  if (null === $objectHelper) {
53  $objectHelper = new ilCertificateObjectHelper();
54  }
55  $this->objectHelper = $objectHelper;
56 
57  if (!$global_certificate_settings) {
58  $global_certificate_settings = new ilObjCertificateSettings();
59  }
60  $this->global_certificate_settings = $global_certificate_settings;
61 
62  if (null === $global_certificate_path) {
63  $global_certificate_path = $this->global_certificate_settings->getDefaultBackgroundImagePath(true);
64  }
65  $this->global_certificate_path = $global_certificate_path;
66  }
67 
75  public function cloneCertificate(
76  ilObject $oldObject,
77  ilObject $newObject,
78  string $iliasVersion = ILIAS_VERSION_NUMERIC,
79  string $webDir = CLIENT_WEB_DIR
80  ): void {
81  $oldType = $oldObject->getType();
82  $newType = $newObject->getType();
83 
84  if ($oldType !== $newType) {
85  throw new ilException(sprintf(
86  'The types "%s" and "%s" for cloning does not match',
87  $oldType,
88  $newType
89  ));
90  }
91 
92  $certificatePath = $this->pathFactory->create($newObject);
93 
94  $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId());
95 
97  foreach ($templates as $template) {
98  $backgroundImagePath = $template->getBackgroundImagePath();
99  $backgroundImageFile = basename($backgroundImagePath);
100  $backgroundImageThumbnail = dirname($backgroundImagePath) . '/background.jpg.thumb.jpg';
101 
102  $newBackgroundImage = '';
103  $newBackgroundImageThumbnail = '';
104  if ($this->global_certificate_path !== $backgroundImagePath) {
105  if ($this->fileSystem->has($backgroundImagePath) &&
106  !$this->fileSystem->hasDir($backgroundImagePath)
107  ) {
108  $newBackgroundImage = $certificatePath . $backgroundImageFile;
109  $newBackgroundImageThumbnail = str_replace(
110  $webDir,
111  '',
112  $this->getBackgroundImageThumbPath($certificatePath)
113  );
114  if ($this->fileSystem->has($newBackgroundImage) &&
115  !$this->fileSystem->hasDir($newBackgroundImage)
116  ) {
117  $this->fileSystem->delete($newBackgroundImage);
118  }
119 
120  $this->fileSystem->copy(
121  $backgroundImagePath,
122  $newBackgroundImage
123  );
124  }
125 
126  if (
127  $newBackgroundImageThumbnail !== '' &&
128  $this->fileSystem->has($backgroundImageThumbnail) &&
129  !$this->fileSystem->hasDir($backgroundImageThumbnail)
130  ) {
131  if ($this->fileSystem->has($newBackgroundImageThumbnail) &&
132  !$this->fileSystem->hasDir($newBackgroundImageThumbnail)
133  ) {
134  $this->fileSystem->delete($newBackgroundImageThumbnail);
135  }
136 
137  $this->fileSystem->copy(
138  $backgroundImageThumbnail,
139  $newBackgroundImageThumbnail
140  );
141  }
142  } else {
143  $newBackgroundImage = $this->global_certificate_path;
144  }
145 
146  $newCardThumbImage = '';
147  $cardThumbImagePath = $template->getThumbnailImagePath();
148 
149  if ($this->fileSystem->has($cardThumbImagePath) && !$this->fileSystem->hasDir($cardThumbImagePath)) {
150  $newCardThumbImage = $certificatePath . basename($cardThumbImagePath);
151  if ($this->fileSystem->has($newCardThumbImage) && !$this->fileSystem->hasDir($newCardThumbImage)) {
152  $this->fileSystem->delete($newCardThumbImage);
153  }
154  $this->fileSystem->copy(
155  $cardThumbImagePath,
156  $newCardThumbImage
157  );
158  }
159 
160  $newTemplate = new ilCertificateTemplate(
161  $newObject->getId(),
162  $this->objectHelper->lookupType($newObject->getId()),
163  $template->getCertificateContent(),
164  $template->getCertificateHash(),
165  $template->getTemplateValues(),
166  $template->getVersion(),
167  $iliasVersion,
168  time(),
169  $template->isCurrentlyActive(),
170  $newBackgroundImage,
171  $newCardThumbImage
172  );
173 
174  $this->templateRepository->save($newTemplate);
175  }
176 
177  // #10271
178  if ($this->isActive($oldObject->getId())) {
179  $this->database->replace(
180  'il_certificate',
181  ['obj_id' => ['integer', $newObject->getId()]],
182  []
183  );
184  }
185  }
186 
187  private function isActive(int $objectId): bool
188  {
189  $sql = 'SELECT 1 FROM il_certificate WHERE obj_id = ' . $this->database->quote($objectId, 'integer');
190 
191  return (bool) $this->database->fetchAssoc($this->database->query($sql));
192  }
193 
194  private function getBackgroundImageName(): string
195  {
196  return "background.jpg";
197  }
198 
199  private function getBackgroundImageThumbPath(string $certificatePath): string
200  {
201  return $this->webDirectory . $certificatePath . $this->getBackgroundImageName() . ".thumb.jpg";
202  }
203 }
const ILIAS_VERSION_NUMERIC
readonly ilObjCertificateSettings $global_certificate_settings
global $DIC
Definition: feed.php:28
Class ilObjCertificateSettings.
const CLIENT_WEB_DIR
Definition: constants.php:47
readonly ilCertificateObjectHelper $objectHelper
getDefaultBackgroundImagePath(bool $relativePath=false)
getBackgroundImageThumbPath(string $certificatePath)
__construct(private readonly ilDBInterface $database, private readonly ilCertificatePathFactory $pathFactory, private readonly ilCertificateTemplateRepository $templateRepository, private readonly string $webDirectory=CLIENT_WEB_DIR, ?Filesystem $fileSystem=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilObjCertificateSettings $global_certificate_settings=null, string $global_certificate_path=null)