ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilCertificateBackgroundImageUpload Class Reference
+ Collaboration diagram for ilCertificateBackgroundImageUpload:

Public Member Functions

 __construct (private readonly FileUpload $fileUpload, private readonly string $certificatePath, private readonly ilLanguage $language, private readonly string $rootDirectory=CLIENT_WEB_DIR, private readonly string $clientId=CLIENT_ID, ?Filesystem $fileSystem=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilCertificateFileUtilsHelper $certificateFileUtilsHelper=null, ?LegacyPathHelperHelper $legacyPathHelper=null, ?Filesystem $tmp_file_system=null)
 
 uploadBackgroundImage (string $imageTempFilename, int $version, ?array $pending_file=null)
 Uploads a background image for the certificate. More...
 

Private Member Functions

 uploadFile (string $temporaryFilename, ?array $pending_file=null)
 
 getTargetFilesystem (string $target)
 
 getTargetDir (string $target)
 
 createBackgroundImageTempfilePath (string $extension)
 Returns the filesystem path of the background image temp file during upload. More...
 
 createBackgroundImageThumbPath ()
 Returns the filesystem path of the background image thumbnail. More...
 

Private Attributes

const BACKGROUND_IMAGE_NAME = 'background.jpg'
 
const BACKGROUND_THUMBNAIL_IMAGE_NAME = 'background.jpg.thumb.jpg'
 
const BACKGROUND_TEMPORARY_FILENAME = 'background_upload_tmp'
 
readonly Filesystem $fileSystem
 
readonly ilCertificateUtilHelper $utilHelper
 
readonly ilCertificateFileUtilsHelper $fileUtilsHelper
 
readonly LegacyPathHelperHelper $legacyPathHelper
 
readonly Filesystem $tmp_file_system
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilCertificateBackgroundImageUpload::__construct ( private readonly FileUpload  $fileUpload,
private readonly string  $certificatePath,
private readonly ilLanguage  $language,
private readonly string  $rootDirectory = CLIENT_WEB_DIR,
private readonly string  $clientId = CLIENT_ID,
?Filesystem  $fileSystem = null,
?ilCertificateUtilHelper  $utilHelper = null,
?ilCertificateFileUtilsHelper  $certificateFileUtilsHelper = null,
?LegacyPathHelperHelper  $legacyPathHelper = null,
?Filesystem  $tmp_file_system = null 
)

Definition at line 42 of file class.ilCertificateBackgroundImageUpload.php.

References $DIC, $fileSystem, $legacyPathHelper, $tmp_file_system, and $utilHelper.

53  {
54  if (null === $fileSystem) {
55  global $DIC;
56  $fileSystem = $DIC->filesystem()->web();
57  }
58  $this->fileSystem = $fileSystem;
59 
60  if (null === $utilHelper) {
62  }
63  $this->utilHelper = $utilHelper;
64 
65  if (null === $certificateFileUtilsHelper) {
66  $certificateFileUtilsHelper = new ilCertificateFileUtilsHelper();
67  }
68  $this->fileUtilsHelper = $certificateFileUtilsHelper;
69 
70  if (null === $legacyPathHelper) {
72  }
73  $this->legacyPathHelper = $legacyPathHelper;
74 
75  if (null === $tmp_file_system) {
76  global $DIC;
77  $tmp_file_system = $DIC->filesystem()->temp();
78  }
79  $this->tmp_file_system = $tmp_file_system;
80  }
global $DIC
Definition: feed.php:28
Just a wrapper class to create Unit Test for other classes.

Member Function Documentation

◆ createBackgroundImageTempfilePath()

ilCertificateBackgroundImageUpload::createBackgroundImageTempfilePath ( string  $extension)
private

Returns the filesystem path of the background image temp file during upload.

Returns
string The filesystem path of the background image temp file

Definition at line 227 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by uploadFile().

227  : string
228  {
229  return implode('', [
230  $this->rootDirectory,
231  $this->certificatePath,
232  self::BACKGROUND_TEMPORARY_FILENAME,
233  '.' . $extension
234  ]);
235  }
+ Here is the caller graph for this function:

◆ createBackgroundImageThumbPath()

ilCertificateBackgroundImageUpload::createBackgroundImageThumbPath ( )
private

Returns the filesystem path of the background image thumbnail.

Returns
string The filesystem path of the background image thumbnail

Definition at line 241 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by uploadBackgroundImage().

241  : string
242  {
243  return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_THUMBNAIL_IMAGE_NAME;
244  }
+ Here is the caller graph for this function:

◆ getTargetDir()

ilCertificateBackgroundImageUpload::getTargetDir ( string  $target)
private

Definition at line 217 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by uploadFile().

217  : string
218  {
219  $absTargetDir = dirname($target);
220  return $this->legacyPathHelper->createRelativePath($absTargetDir);
221  }
+ Here is the caller graph for this function:

◆ getTargetFilesystem()

ilCertificateBackgroundImageUpload::getTargetFilesystem ( string  $target)
private

Definition at line 201 of file class.ilCertificateBackgroundImageUpload.php.

References CLIENT_DATA_DIR.

Referenced by uploadFile().

201  : int
202  {
203  return match (true) {
204  str_starts_with($target, $this->rootDirectory . '/' . $this->clientId), str_starts_with(
205  $target,
206  './' . $this->rootDirectory . '/' . $this->clientId
207  ), str_starts_with($target, $this->rootDirectory) => Location::WEB,
208  str_starts_with($target, CLIENT_DATA_DIR . "/temp") => Location::TEMPORARY,
209  str_starts_with($target, CLIENT_DATA_DIR) => Location::STORAGE,
210  str_starts_with($target, ILIAS_ABSOLUTE_PATH . '/Customizing') => Location::CUSTOMIZING,
211  default => throw new InvalidArgumentException(
212  "Can not move files to \"$target\" because path can not be mapped to web, storage or customizing location."
213  ),
214  };
215  }
const CLIENT_DATA_DIR
Definition: constants.php:46
+ Here is the caller graph for this function:

◆ uploadBackgroundImage()

ilCertificateBackgroundImageUpload::uploadBackgroundImage ( string  $imageTempFilename,
int  $version,
?array  $pending_file = null 
)

Uploads a background image for the certificate.

Creates a new directory for the certificate if needed. Removes an existing certificate image if necessary

Parameters
string$imageTempFilenameName of the temporary uploaded image file
int$version- Version of the current certifcate template
Returns
string An errorcode if the image upload fails, 0 otherwise
Exceptions
IllegalStateException
FileNotFoundException
IOException
ilException
ilFileUtilsException

Definition at line 94 of file class.ilCertificateBackgroundImageUpload.php.

References createBackgroundImageThumbPath(), ilFileUtils\makeDirParents(), ilFileUtils\moveUploadedFile(), and uploadFile().

94  : string
95  {
96  $imagepath = $this->rootDirectory . $this->certificatePath;
97 
98  if (!$this->fileSystem->hasDir($imagepath)) {
99  ilFileUtils::makeDirParents($imagepath);
100  }
101 
102  $backgroundImageTempFilePath = $this->uploadFile($imageTempFilename, $pending_file);
103 
104  $backgroundImagePath = $this->certificatePath . 'background_' . $version . '.jpg';
105 
106  $this->utilHelper->convertImage(
107  $backgroundImageTempFilePath,
108  $this->rootDirectory . $backgroundImagePath
109  );
110 
111  $backgroundImageThumbnailPath = $this->createBackgroundImageThumbPath();
112 
113  $this->utilHelper->convertImage(
114  $backgroundImageTempFilePath,
115  $backgroundImageThumbnailPath,
116  "100"
117  );
118 
119  $convert_filename = self::BACKGROUND_IMAGE_NAME;
120 
121  // something went wrong converting the file. use the original file and hope, that PDF can work with it
122  if (!$this->fileSystem->has($backgroundImagePath) && !ilFileUtils::moveUploadedFile(
123  $backgroundImageTempFilePath,
124  $convert_filename,
125  $this->rootDirectory . $backgroundImagePath
126  )) {
127  throw new ilException('Unable to convert the file and the original file');
128  }
129 
130  if ($this->fileSystem->has($backgroundImageTempFilePath)) {
131  $this->fileSystem->delete($backgroundImageTempFilePath);
132  }
133 
134  if ($this->fileSystem->has($backgroundImagePath)) {
135  return $this->certificatePath . 'background_' . $version . '.jpg';
136  }
137 
138  throw new ilException('The given temporary filename is empty');
139  }
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
uploadFile(string $temporaryFilename, ?array $pending_file=null)
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
createBackgroundImageThumbPath()
Returns the filesystem path of the background image thumbnail.
$version
Definition: plugin.php:24
+ Here is the call graph for this function:

◆ uploadFile()

ilCertificateBackgroundImageUpload::uploadFile ( string  $temporaryFilename,
?array  $pending_file = null 
)
private
Exceptions
FileNotFoundException
IOException
IllegalStateException
ilException
ilFileUtilsException

Definition at line 148 of file class.ilCertificateBackgroundImageUpload.php.

References createBackgroundImageTempfilePath(), getTargetDir(), getTargetFilesystem(), ILIAS\UI\examples\Symbol\Glyph\Language\language(), and ILIAS\FileUpload\DTO\ProcessingStatus\REJECTED.

Referenced by uploadBackgroundImage().

148  : string
149  {
150  if (!$this->fileUpload->hasBeenProcessed()) {
151  $this->fileUpload->process();
152  }
153 
154  if (!$this->fileUpload->hasUploads()) {
155  throw new ilException($this->language->txt('upload_error_file_not_found'));
156  }
157 
158  $uploadResults = $this->fileUpload->getResults();
159  if (isset($uploadResults[$temporaryFilename])) {
160  $uploadResult = $uploadResults[$temporaryFilename];
161  $processingStatus = $uploadResult->getStatus();
162  if ($processingStatus->getCode() === ILIAS\FileUpload\DTO\ProcessingStatus::REJECTED) {
163  throw new ilException($processingStatus->getMessage());
164  }
165 
166  $extension = pathinfo($uploadResult->getName(), PATHINFO_EXTENSION);
167  $temp_file_path = $this->createBackgroundImageTempfilePath($extension);
168  $target_file_name = basename($temp_file_path);
169  $target_file_name = $this->fileUtilsHelper->getValidFilename($target_file_name);
170 
171  $target_file_system = $this->getTargetFilesystem($temp_file_path);
172  $target_directory = $this->getTargetDir($temp_file_path);
173 
174  $this->fileUpload->moveOneFileTo(
175  $uploadResult,
176  $target_directory,
177  $target_file_system,
178  $target_file_name,
179  true
180  );
181 
182  return $temp_file_path;
183  } elseif (is_array($pending_file) && $pending_file !== []) {
184  $extension = pathinfo($pending_file['name'], PATHINFO_EXTENSION);
185  $temp_file_path = $this->createBackgroundImageTempfilePath($extension);
186 
187  $target_file_name = basename($temp_file_path);
188  $target_file_name = $this->fileUtilsHelper->getValidFilename($target_file_name);
189 
190  $target_directory = $this->getTargetDir($temp_file_path);
191 
192  $stream = $this->tmp_file_system->readStream(basename($pending_file['tmp_name']));
193  $this->fileSystem->writeStream($target_directory . '/' . $target_file_name, $stream);
194 
195  return $temp_file_path;
196  } else {
197  throw new ilException($this->language->txt('upload_error_file_not_found'));
198  }
199  }
const REJECTED
Upload got rejected by a processor.
createBackgroundImageTempfilePath(string $extension)
Returns the filesystem path of the background image temp file during upload.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $fileSystem

readonly Filesystem ilCertificateBackgroundImageUpload::$fileSystem
private

Definition at line 36 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by __construct().

◆ $fileUtilsHelper

readonly ilCertificateFileUtilsHelper ilCertificateBackgroundImageUpload::$fileUtilsHelper
private

Definition at line 38 of file class.ilCertificateBackgroundImageUpload.php.

◆ $legacyPathHelper

readonly LegacyPathHelperHelper ilCertificateBackgroundImageUpload::$legacyPathHelper
private

Definition at line 39 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by __construct().

◆ $tmp_file_system

readonly Filesystem ilCertificateBackgroundImageUpload::$tmp_file_system
private

Definition at line 40 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by __construct().

◆ $utilHelper

readonly ilCertificateUtilHelper ilCertificateBackgroundImageUpload::$utilHelper
private

Definition at line 37 of file class.ilCertificateBackgroundImageUpload.php.

Referenced by __construct().

◆ BACKGROUND_IMAGE_NAME

const ilCertificateBackgroundImageUpload::BACKGROUND_IMAGE_NAME = 'background.jpg'
private

Definition at line 33 of file class.ilCertificateBackgroundImageUpload.php.

◆ BACKGROUND_TEMPORARY_FILENAME

const ilCertificateBackgroundImageUpload::BACKGROUND_TEMPORARY_FILENAME = 'background_upload_tmp'
private

Definition at line 35 of file class.ilCertificateBackgroundImageUpload.php.

◆ BACKGROUND_THUMBNAIL_IMAGE_NAME

const ilCertificateBackgroundImageUpload::BACKGROUND_THUMBNAIL_IMAGE_NAME = 'background.jpg.thumb.jpg'
private

Definition at line 34 of file class.ilCertificateBackgroundImageUpload.php.


The documentation for this class was generated from the following file: