Skip to content

Commit

Permalink
Certificate: Fix template import
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansenDatabay committed Apr 15, 2024
1 parent e3007a0 commit 86492c4
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ilCertificateSettingsFormRepository implements ilCertificateFormRepository
private readonly WrapperFactory $httpWrapper;
private readonly Refinery $refinery;
private readonly ilObjCertificateSettings $global_certificate_settings;
private readonly ilGlobalTemplateInterface $page_template;

public function __construct(
private readonly int $objectId,
Expand All @@ -65,6 +66,7 @@ public function __construct(

$this->httpWrapper = $DIC->http()->wrapper();
$this->refinery = $DIC->refinery();
$this->page_template = $DIC->ui()->mainTemplate();

$this->ui_factory = $ui_factory ?? $DIC->ui()->factory();
$this->ui_renderer = $ui_renderer ?? $DIC->ui()->renderer();
Expand Down Expand Up @@ -124,9 +126,19 @@ public function createForm(ilCertificateGUI $certificateGUI): ilPropertyFormGUI
$_FILES['certificate_import']['name']
);
if ($result) {
$this->page_template->setOnScreenMessage(
$this->page_template::MESSAGE_TYPE_SUCCESS,
$this->language->txt('saved_successfully'),
true
);
$this->ctrl->redirect($certificateGUI, 'certificateEditor');
} else {
$import->setAlert($this->language->txt('certificate_error_import'));
$this->page_template->setOnScreenMessage(
$this->page_template::MESSAGE_TYPE_FAILURE,
$this->language->txt('certificate_error_import'),
true
);
$this->ctrl->redirect($certificateGUI, 'certificateEditor');
}
}
$form->addItem($import);
Expand Down
20 changes: 13 additions & 7 deletions Services/Certificate/classes/Helper/ilCertificateUtilHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

declare(strict_types=1);

use ILIAS\Filesystem\Util\Convert\ImageConversionOptions;
use ILIAS\Filesystem\Util\Convert\ImageOutputOptions;
use ILIAS\Filesystem\Util\Convert\LegacyImages;
use ILIAS\Filesystem\Filesystem;
use ILIAS\Filesystem\Stream\Streams;
use ILIAS\Filesystem\Util\Archive\Unzip;
use ILIAS\Filesystem\Util\Archive\ZipDirectoryHandling;

/**
* Just a wrapper class to create Unit Test for other classes.
Expand Down Expand Up @@ -75,6 +75,9 @@ public function stripSlashes(string $string): string
return ilUtil::stripSlashes($string);
}

/**
* @param list<Streams> $streams
*/
public function zipAndDeliver(array $streams, string $download_filename): void
{
$this->delivery->delivery()->attached(
Expand All @@ -88,13 +91,16 @@ public function getDir(string $copyDirectory): array
return ilFileUtils::getDir($copyDirectory);
}

public function unzip(string $file, bool $overwrite): void
public function unzip(string $file, string $zip_output_path, bool $overwrite): Unzip
{
$this->archives->unzip(
$file,
return $this->archives->unzip(
Streams::ofResource(fopen($file, 'rb')),
$this->archives->unzipOptions()
->withOverwrite($overwrite)
)->extract();
->withZipOutputPath($zip_output_path)
->withDirectoryHandling(ZipDirectoryHandling::KEEP_STRUCTURE)

);
}

public function delDir(string $path): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,44 @@ public function import(
): bool {
$importPath = $this->createArchiveDirectory($installationID);

$result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
$clean_up_import_dir = function () use (&$importPath) {
if ($this->filesystem->hasDir($importPath)) {
$this->filesystem->deleteDir($importPath);
}
};

$result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
if (!$result) {
$this->filesystem->deleteDir($importPath);
$clean_up_import_dir();
return false;
}

$this->utilHelper->unzip(
$destination_dir = $rootDir . $importPath;
$unzip = $this->utilHelper->unzip(
$rootDir . $importPath . $filename,
$destination_dir,
true
);

$subDirectoryName = str_replace('.zip', '', strtolower($filename)) . '/';
$subDirectoryAbsolutePath = $rootDir . $importPath . $subDirectoryName;

$copyDirectory = $importPath;
if (is_dir($subDirectoryAbsolutePath)) {
$copyDirectory = $subDirectoryAbsolutePath;
$unzipped = $unzip->extract();
if (!$unzipped) {
$clean_up_import_dir();
return false;
}

$directoryInformation = $this->utilHelper->getDir($copyDirectory);
if ($this->filesystem->has($importPath . $filename)) {
$this->filesystem->delete($importPath . $filename);
}

$xmlFiles = 0;
foreach ($directoryInformation as $file) {
if (strcmp($file['type'], 'file') === 0 && str_contains((string) $file['entry'], '.xml')) {
$contents = $this->filesystem->listContents($importPath);
foreach ($contents as $file) {
if ($file->isFile() && str_contains($file->getPath(), '.xml')) {
$xmlFiles++;
}
}

if (0 === $xmlFiles) {
$this->filesystem->deleteDir($importPath);
return false;
}

Expand All @@ -130,58 +137,58 @@ public function import(
$newVersion = $currentVersion + 1;
$backgroundImagePath = $certificate->getBackgroundImagePath();
$cardThumbnailImagePath = $certificate->getThumbnailImagePath();

$xsl = $certificate->getCertificateContent();

foreach ($directoryInformation as $file) {
if (strcmp($file['type'], 'file') === 0) {
$filePath = $importPath . $subDirectoryName . $file['entry'];
if (str_contains((string) $file['entry'], '.xml')) {
$xsl = $this->filesystem->read($filePath);
// as long as we cannot make RPC calls in a given directory, we have
// to add the complete path to every url
$xsl = preg_replace_callback(
"/url\([']{0,1}(.*?)[']{0,1}\)/",
function (array $matches) use ($rootDir): string {
$basePath = rtrim(dirname($this->fileService->getBackgroundImageDirectory($rootDir)), '/');
$fileName = basename($matches[1]);

if ('[BACKGROUND_IMAGE]' === $fileName) {
$basePath = '';
} elseif ($basePath !== '') {
$basePath .= '/';
}

return 'url(' . $basePath . $fileName . ')';
},
$xsl
);
} elseif (str_contains((string) $file['entry'], '.jpg')) {
$newBackgroundImageName = 'background_' . $newVersion . '.jpg';
$newPath = $this->certificatePath . $newBackgroundImageName;
$this->filesystem->copy($filePath, $newPath);

$backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
// upload of the background image, create a thumbnail

$backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();

$thumbnailImagePath = $rootDir . $backgroundImageThumbPath;

$originalImagePath = $rootDir . $newPath;
$this->utilHelper->convertImage(
$originalImagePath,
$thumbnailImagePath,
"100"
);
} elseif (str_contains((string) $file['entry'], '.svg')) {
$newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
$newPath = $this->certificatePath . $newCardThumbnailName;

$this->filesystem->copy($filePath, $newPath);

$cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
}
foreach ($contents as $file) {
if (!$file->isFile()) {
continue;
}

if (str_contains($file->getPath(), '.xml')) {
$xsl = $this->filesystem->read($file->getPath());
// as long as we cannot make RPC calls in a given directory, we have
// to add the complete path to every url
$xsl = preg_replace_callback(
"/url\([']{0,1}(.*?)[']{0,1}\)/",
function (array $matches) use ($rootDir): string {
$basePath = rtrim(dirname($this->fileService->getBackgroundImageDirectory($rootDir)), '/');
$fileName = basename($matches[1]);

if ('[BACKGROUND_IMAGE]' === $fileName) {
$basePath = '';
} elseif ($basePath !== '') {
$basePath .= '/';
}

return 'url(' . $basePath . $fileName . ')';
},
$xsl
);
} elseif (str_contains($file->getPath(), '.jpg')) {
$newBackgroundImageName = 'background_' . $newVersion . '.jpg';
$newPath = $this->certificatePath . $newBackgroundImageName;
$this->filesystem->copy($file->getPath(), $newPath);

$backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
// upload of the background image, create a thumbnail

$backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();

$thumbnailImagePath = $rootDir . $backgroundImageThumbPath;

$originalImagePath = $rootDir . $newPath;
$this->utilHelper->convertImage(
$originalImagePath,
$thumbnailImagePath,
'100'
);
} elseif (str_contains($file->getPath(), '.svg')) {
$newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
$newPath = $this->certificatePath . $newCardThumbnailName;

$this->filesystem->copy($file->getPath(), $newPath);

$cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
}
}

Expand Down Expand Up @@ -216,7 +223,7 @@ function (array $matches) use ($rootDir): string {

$this->templateRepository->save($template);

$this->filesystem->deleteDir($importPath);
$clean_up_import_dir();

return true;
}
Expand All @@ -232,6 +239,9 @@ private function createArchiveDirectory(string $installationID): string
$certificateId = $this->objectId;

$dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
if ($this->filesystem->hasDir($dir)) {
$this->filesystem->deleteDir($dir);
}
$this->filesystem->createDir($dir);

return $dir;
Expand Down
Loading

0 comments on commit 86492c4

Please sign in to comment.