ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDidacticTemplateIconHandler.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 declare(strict_types=1);
5 
13 
22 {
23  protected const WEBDIR_PREFIX = 'ilDidacticTemplateIcons';
24 
28  protected $settings;
29 
33  protected $logger;
34 
38  protected $webDirectory;
39 
40 
45  public function __construct(ilDidacticTemplateSetting $setting)
46  {
47  global $DIC;
48 
49  $this->settings = $setting;
50  $this->webDirectory = $DIC->filesystem()->web();
51  $this->logger = $DIC->logger()->otpl();
52  }
53 
57  public function handleUpload(FileUpload $upload, string $tmpname)
58  {
59  if ($upload->hasUploads() && !$upload->hasBeenProcessed()) {
60  try {
61  $upload->process();
62  } catch (IllegalStateException $e) {
63  $this->logger->warning('File upload already processed: ' . $e->getMessage());
64  }
65  }
66  $this->initWebDir();
67  $result = isset($upload->getResults()[$tmpname]) ? $upload->getResults()[$tmpname] : false;
68  if ($result instanceof UploadResult && $result->isOK() && $result->getSize()) {
69  $this->delete();
70  $upload->moveOneFileTo(
71  $result,
72  self::WEBDIR_PREFIX,
73  Location::WEB,
74  $this->settings->getId() . '.svg'
75  );
76 
77  $this->settings->setIconIdentifier((string) $this->settings->getId());
78  $this->settings->update();
79  }
80  }
81 
85  public function writeSvg(string $svg)
86  {
87  try {
88  $this->webDirectory->write(
89  self::WEBDIR_PREFIX . '/' . $this->settings->getId() . '.svg',
90  trim($svg)
91  );
92  $this->settings->setIconIdentifier((string) $this->settings->getId());
93  $this->settings->update();
94  } catch (Exception $e) {
95  $this->logger->warning('Error writing svg image from xml: ' . $e->getMessage());
96  }
97  }
98 
102  public function getAbsolutePath() : string
103  {
104  if ($this->webDirectory->has(self::WEBDIR_PREFIX . '/' . $this->settings->getIconIdentifier() . '.svg')) {
105  return ilUtil::getWebspaceDir() . '/' . self::WEBDIR_PREFIX . '/' . $this->settings->getIconIdentifier() . '.svg';
106  }
107  return '';
108  }
109 
113  public function copy(ilDidacticTemplateSetting $original)
114  {
115  if ($original->getIconHandler()->getAbsolutePath()) {
116 
117  try {
118  $this->webDirectory->copy(
119  self::WEBDIR_PREFIX . '/' . $original->getIconIdentifier() . '.svg',
120  self::WEBDIR_PREFIX . '/' . $this->settings->getId() . '.svg'
121  );
122  } catch (Exception $e) {
123  $this->logger->warning('Copying icon failed with message: ' . $e->getMessage());
124  }
125  $this->settings->setIconIdentifier((string) $this->settings->getId());
126  $this->settings->update();
127  } else {
128  $this->settings->setIconIdentifier((string) 0 );
129  $this->settings->update();
130  }
131  }
132 
136  public function delete()
137  {
138  if ($this->webDirectory->has(self::WEBDIR_PREFIX . '/' . $this->settings->getIconIdentifier() . '.svg')) {
139  try {
140  $this->webDirectory->delete(self::WEBDIR_PREFIX . '/' . $this->settings->getIconIdentifier() . '.svg');
141  $this->settings->setIconIdentifier('');
142  $this->settings->update();
143  } catch (Exception $e) {
144  $this->logger->warning('Deleting icon dfailed with message: ' . $e->getMessage());
145  }
146  }
147  }
148 
152  private function initWebDir()
153  {
154  if (!$this->webDirectory->has(self::WEBDIR_PREFIX)) {
155  try {
156  $this->webDirectory->createDir(self::WEBDIR_PREFIX);
157  } catch (IOException $e) {
158  $this->logger->error('Creating icon directory failed with message: ' . $e->getMessage());
159  } catch (IllegalStateException $e) {
160  $this->logger->warning('Creating icon directory failed with message: ' . $e->getMessage());
161  }
162  }
163  }
164 
169  public function toXml(ilXmlWriter $writer)
170  {
171  if ($this->settings->getIconIdentifier()) {
172  try {
173  if ($this->webDirectory->has(self::WEBDIR_PREFIX . '/' . $this->settings->getIconIdentifier() . '.svg')) {
174  $writer->xmlElement('icon', [], $this->webDirectory->read(
175  self::WEBDIR_PREFIX . '/' . $this->settings->getIconIdentifier() . '.svg'
176  ));
177  }
178  } catch (Exception $e) {
179  $this->logger->warning('Export xml failed with message: ' . $e->getMessage());
180  }
181  }
182  return $writer;
183  }
184 
185 }
settings()
Definition: settings.php:2
process()
Invokes all preprocessors for each uploaded file in the sequence they got registered.
$result
Class IOException Indicates general problems with the input or output operations. ...
Definition: IOException.php:12
XML writer class.
Class IllegalStateException The IllegalStateException indicates a wrong state of the object...
hasUploads()
Return (bool)true if one ore more file-uploads are in the current request, (bool)false if not...
moveOneFileTo(UploadResult $UploadResult, $destination, $location=Location::STORAGE, $file_name='', $override_existing=false)
Moves a single File (the attributes, metadata and upload-status of which are contained in UploadResul...
global $DIC
Definition: goto.php:24
__construct(ilDidacticTemplateSetting $setting)
ilDidacticTemplateIconHandler constructor.
getResults()
Returns the results of the processing and moving operation of the uploaded files. ...
handleUpload(FileUpload $upload, string $tmpname)
Class FileUpload.
Definition: FileUpload.php:21
Icon handler for didactic template custom icons.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
hasBeenProcessed()
Return (bool)true if the current upload has already been processed.
copy(ilDidacticTemplateSetting $original)
static getWebspaceDir($mode="filesystem")
get webspace directory