ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
File.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
28 use ILIAS\UI\Component as C;
30 use Closure;
31 use ilLanguage;
32 
39 class File extends HasDynamicInputsBase implements C\Input\Field\File
40 {
41  // ===============================================
42  // BEGIN IMPLEMENTATION OF FileUpload
43  // ===============================================
44 
47  protected array $accepted_mime_types = [];
48  protected bool $has_metadata_inputs = false;
49  protected int $max_file_amount = 1;
50  protected int $max_file_size_in_bytes;
51 
52  public function __construct(
54  DataFactory $data_factory,
56  UploadLimitResolver $upload_limit_resolver,
58  string $label,
59  ?FormInput $metadata_input,
60  ?string $byline
61  ) {
62  $this->upload_limit_resolver = $upload_limit_resolver;
63  $this->max_file_size_in_bytes = $upload_limit_resolver->getBestPossibleUploadLimitInBytes($handler);
64  $this->language = $language;
65  $this->data_factory = $data_factory;
66  $this->refinery = $refinery;
67  $this->upload_handler = $handler;
68  $this->value = [];
69 
71  $language,
72  $data_factory,
73  $refinery,
74  $label,
75  $this->createDynamicInputsTemplate($metadata_input),
76  $byline
77  );
78  }
79 
80  public function getUploadHandler(): UploadHandler
81  {
82  return $this->upload_handler;
83  }
84 
85  public function withMaxFileSize(int $size_in_bytes): FileUpload
86  {
87  $clone = clone $this;
88  $clone->max_file_size_in_bytes = $clone->upload_limit_resolver->getBestPossibleUploadLimitInBytes(
89  $clone->upload_handler,
90  $size_in_bytes
91  );
92 
93  return $clone;
94  }
95 
96  public function getMaxFileSize(): int
97  {
99  }
100 
101  public function withMaxFiles(int $max_file_amount): FileUpload
102  {
103  $clone = clone $this;
104  $clone->max_file_amount = $max_file_amount;
105 
106  return $clone;
107  }
108 
109  public function getMaxFiles(): int
110  {
111  return $this->max_file_amount;
112  }
113 
114  public function withAcceptedMimeTypes(array $mime_types): FileUpload
115  {
116  $clone = clone $this;
117  $clone->accepted_mime_types = $mime_types;
118 
119  return $clone;
120  }
121 
122  public function getAcceptedMimeTypes(): array
123  {
125  }
126 
127  // ===============================================
128  // END IMPLEMENTATION OF FileUpload
129  // ===============================================
130 
131  // ===============================================
132  // BEGIN OVERWRITTEN METHODS OF HasDynamicInputs
133  // ===============================================
134 
139  public function withValue($value): HasDynamicInputsBase
140  {
141  $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
142 
143  $clone = clone $this;
144  $identifier_key = $clone->upload_handler->getFileIdentifierParameterName();
145  foreach ($value as $data) {
146  $file_id = ($clone->hasMetadataInputs()) ? $data[$identifier_key] : $data;
147 
148  // that was not implicitly intended, but mapping dynamic inputs
149  // to the file-id is also a duplicate protection.
150  $clone->dynamic_inputs[$file_id] = $clone->dynamic_input_template->withValue($data);
151  }
152 
153  return $clone;
154  }
155 
156  // ===============================================
157  // END OVERWRITTEN METHODS OF HasDynamicInputs
158  // ===============================================
159 
160  public function hasMetadataInputs(): bool
161  {
163  }
164 
168  public function getTranslations(): array
169  {
170  return [
171  'invalid_mime' => $this->language->txt('ui_file_input_invalid_mime'),
172  'invalid_size' => $this->language->txt('ui_file_input_invalid_size'),
173  'invalid_amount' => $this->language->txt('ui_file_input_invalid_amount'),
174  'general_error' => $this->language->txt('ui_file_input_general_error'),
175  ];
176  }
177 
178  public function getUpdateOnLoadCode(): Closure
179  {
180  return static function () {
181  };
182  }
183 
185  {
186  if ($this->requirement_constraint !== null) {
187  return $this->requirement_constraint;
188  }
189 
190  return $this->refinery->custom()->constraint(
191  function ($value) {
192  return (is_array($value) && count($value) > 0);
193  },
194  function ($txt, $value) {
195  return $txt("msg_no_files_selected");
196  },
197  );
198  }
199 
200  protected function isClientSideValueOk($value): bool
201  {
202  if (!is_array($value)) {
203  return false;
204  }
205 
206  foreach ($value as $data) {
207  // if no dynamic input template was provided, the values
208  // must all be strings (possibly file-ids).
209  if (!is_string($data) && !$this->hasMetadataInputs()) {
210  return false;
211  }
212 
213  if ($this->hasMetadataInputs()) {
214  // if a dynamic input template was provided, the values
215  // must all contain the file-id as an array entry.
216  if (!array_key_exists($this->upload_handler->getFileIdentifierParameterName(), $data)) {
217  return false;
218  }
219 
220  // if a dynamic input template was provided, the values
221  // must be valid for the template input.
222  if (!$this->dynamic_input_template->isClientSideValueOk($data)) {
223  return false;
224  }
225  }
226  }
227 
228  return true;
229  }
230 
231  protected function createDynamicInputsTemplate(?FormInput $metadata_input): FormInput
232  {
233  $default_metadata_input = new Hidden(
234  $this->data_factory,
235  $this->refinery
236  );
237 
238  if (null === $metadata_input) {
239  return $default_metadata_input;
240  }
241 
242  $inputs = ($metadata_input instanceof C\Input\Field\Group) ?
243  $metadata_input->getInputs() : [
244  $metadata_input,
245  ];
246 
247  // map the file-id input to the UploadHandlers identifier key.
248  $inputs[$this->upload_handler->getFileIdentifierParameterName()] = $default_metadata_input;
249 
250  // tell the input that it contains actual metadata inputs.
251  $this->has_metadata_inputs = true;
252 
253  return new Group(
254  $this->data_factory,
255  $this->refinery,
256  $this->language,
257  $inputs,
258  ''
259  );
260  }
261 }
This implements commonalities between inputs.
Definition: Input.php:42
createDynamicInputsTemplate(?FormInput $metadata_input)
Definition: File.php:231
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
__construct(VocabulariesInterface $vocabularies)
getBestPossibleUploadLimitInBytes(UploadHandler $upload_handler, int $local_limit_in_bytes=null)
$txt
Definition: error.php:14
withValue($value)
Maps generated dynamic inputs to their file-id, which must be provided in or as $value.
Definition: File.php:139
This describes inputs that can be used in forms.
Definition: FormInput.php:31
__construct(ilLanguage $language, DataFactory $data_factory, Refinery $refinery, UploadLimitResolver $upload_limit_resolver, C\Input\Field\UploadHandler $handler, string $label, ?FormInput $metadata_input, ?string $byline)
Definition: File.php:52
$handler
Definition: index.php:18
Refinery Factory $refinery