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