ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjectCommonSettingFormAdapter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
35 {
36  public function __construct(
37  private ilLanguage $language,
38  private FileUpload $upload,
39  private ResourceStorageServices $storage,
40  private ilObjectTileImageStakeholder $stakeholder,
41  private ilObjectTileImageFlavourDefinition $flavour,
42  private ilObjectCommonSettings $common_settings,
43  private Services $http,
44  private ?ilPropertyFormGUI $legacy_form = null
45  ) {
46  $this->language->loadLanguageModule('obj');
47  $this->language->loadLanguageModule('cntr');
48  }
49 
50  public function addIcon(): ?ilPropertyFormGUI
51  {
52  $icon = $this->common_settings->getPropertyIcon()->toLegacyForm($this->language);
53  if (!is_null($this->legacy_form) && $icon !== null) {
54  $this->legacy_form->addItem($icon);
55  }
56 
57  return $this->legacy_form;
58  }
59 
60  public function saveIcon(): void
61  {
62  if (is_null($this->legacy_form)) {
63  return;
64  }
65 
66  $item = $this->legacy_form->getItemByPostVar('icon');
67  if ($item && $item->getDeletionFlag()) {
68  $this->common_settings->storePropertyIcon(
69  $this->common_settings->getPropertyIcon()->withDeletedFlag()
70  );
71  return;
72  }
73 
74  $file_data = (array) $this->legacy_form->getInput('icon');
75  if (isset($file_data['tmp_name']) && $file_data['tmp_name']) {
76  $tempfile = ilFileUtils::ilTempnam();
77  if (!$this->upload->hasBeenProcessed()) {
78  $this->upload->process();
79  }
80 
81  rename($file_data['tmp_name'], $tempfile);
82 
83  $this->common_settings->storePropertyIcon(
84  $this->common_settings->getPropertyIcon()->withTempFileName(basename($tempfile))
85  );
86  }
87  }
88 
89  public function addTileImage(): ?ilPropertyFormGUI
90  {
91  if (!is_null($this->legacy_form)) {
92  $timg = $this->common_settings->getPropertyTileImage()->toLegacyForm($this->language);
93  $this->legacy_form->addItem($timg);
94  }
95 
96  return $this->legacy_form;
97  }
98 
99  public function saveTileImage(): void
100  {
101  if (is_null($this->legacy_form)) {
102  return;
103  }
104 
105  $item = $this->legacy_form->getItemByPostVar('tile_image');
106  if ($item && $item->getDeletionFlag()) {
107  $this->common_settings->storePropertyTileImage(
108  $this->common_settings->getPropertyTileImage()->withDeletedFlag()
109  );
110  return;
111  }
112 
113  // In case of a parallel oather upload, the uploads may already have been processed. otherwise we have to do it
114  if (!$this->upload->hasBeenProcessed()) {
115  $this->upload->process();
116  }
117  $result_array = $this->upload->getResults();
118 
119  // Determine Legacy Upload (since there can be more than one in this form). This is not best practice, because
120  // on the one hand we should actually use the wrapper, on the other hand with the conversion to the new forms
121  // this problem no longer have anyway. But at the moment we have to pick the right one out of all uploadResults,
122  // that's only possible with this way
123 
125  $file = $this->http->request()->getUploadedFiles()['tile_image']; // Direct Access to the Psr7/UploadedFile
126  if ($file->getClientFilename() === '') {
127  return;
128  }
129 
130  $temp_name = $file->getStream()->getMetadata('uri'); // Get the Path of the Temp. File
131  $result = $result_array[$temp_name] ?? null;
132 
133  if (!($result instanceof UploadResult) || !$result->isOK()) {
134  return;
135  }
136 
137  if ($item->getValue() === null || $item->getValue() === '') {
138  $i = $this->storage->manage()->upload($result, $this->stakeholder);
139  $this->storage->flavours()->ensure($i, $this->flavour);
140  $new_tile_image = $this->common_settings->getPropertyTileImage()
141  ->getTileImage()->withRid($i->serialize());
142  $this->common_settings->storePropertyTileImage(
143  $this->common_settings->getPropertyTileImage()->withTileImage($new_tile_image)
144  );
145  } else {
146  $i = $this->storage->manage()->find($item->getValue());
147  $this->storage->manage()->replaceWithUpload(
148  $i,
149  $result,
150  $this->stakeholder
151  );
152  }
153  }
154 
156  {
157  $title_and_icon_visibility_input = $this->common_settings->getPropertyTitleAndIconVisibility()
158  ->toLegacyForm($this->language);
159  $this->legacy_form->addItem($title_and_icon_visibility_input);
160 
161  return $this->legacy_form;
162  }
163 
164  public function saveTitleIconVisibility(): void
165  {
166  if (is_null($this->legacy_form)) {
167  return;
168  }
169 
170  $this->common_settings->storePropertyTitleAndIconVisibility(
171  new ilObjectPropertyTitleAndIconVisibility((bool) $this->legacy_form->getInput('show_header_icon_and_title'))
172  );
173  }
174 
176  {
177  $top_actions_visibility_input = $this->common_settings->getPropertyHeaderActionVisibility()
178  ->toLegacyForm($this->language);
179  $this->legacy_form->addItem($top_actions_visibility_input);
180 
181  return $this->legacy_form;
182  }
183 
184  public function saveTopActionsVisibility(): void
185  {
186  if (is_null($this->legacy_form)) {
187  return;
188  }
189 
190  $this->common_settings->storePropertyHeaderActionVisibility(
191  new ilObjectPropertyHeaderActionVisibility((bool) $this->legacy_form->getInput('show_top_actions'))
192  );
193  }
194 }
addTopActionsVisibility()
Add top actions visibility setting to form.
__construct(private ilLanguage $language, private FileUpload $upload, private ResourceStorageServices $storage, private ilObjectTileImageStakeholder $stakeholder, private ilObjectTileImageFlavourDefinition $flavour, private ilObjectCommonSettings $common_settings, private Services $http, private ?ilPropertyFormGUI $legacy_form=null)
saveTitleIconVisibility()
Save title icon visibility setting from form.
addTitleIconVisibility()
Add title icon visibility setting to form.
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
saveTopActionsVisibility()
Save top actions visibility setting from form.
$http
Definition: raiseError.php:7
Class FileUpload.
Definition: FileUpload.php:34
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
saveTileImage()
Save tile image setting from form.