ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 = null;
51 
52  public function __construct(
54  DataFactory $data_factory,
56  UploadLimitResolver $upload_limit_resolver,
57  C\Input\Field\UploadHandler $handler,
58  string $label,
59  ?FormInput $metadata_input,
60  ?string $byline
61  ) {
62  $this->upload_limit_resolver = $upload_limit_resolver;
63  $this->language = $language;
64  $this->data_factory = $data_factory;
65  $this->refinery = $refinery;
66  $this->upload_handler = $handler;
67  $this->value = [];
68 
70  $language,
71  $data_factory,
72  $refinery,
73  $label,
74  $this->createDynamicInputsTemplate($metadata_input),
75  $byline
76  );
77  }
78 
79  public function getUploadHandler(): UploadHandler
80  {
81  return $this->upload_handler;
82  }
83 
84  public function withMaxFileSize(int $size_in_bytes): FileUpload
85  {
86  $size_in_bytes = $this->upload_limit_resolver->min($size_in_bytes);
87 
88  $clone = clone $this;
89  $clone->max_file_size = $size_in_bytes;
90 
91  return $clone;
92  }
93 
94  public function getMaxFileSize(): int
95  {
96  return $this->max_file_size ?? $this->upload_limit_resolver->getUploadLimit();
97  }
98 
99  public function withMaxFiles(int $max_file_amount): FileUpload
100  {
101  $clone = clone $this;
102  $clone->max_file_amount = $max_file_amount;
103 
104  return $clone;
105  }
106 
107  public function getMaxFiles(): int
108  {
109  return $this->max_file_amount;
110  }
111 
112  public function withAcceptedMimeTypes(array $mime_types): FileUpload
113  {
114  $clone = clone $this;
115  $clone->accepted_mime_types = $mime_types;
116 
117  return $clone;
118  }
119 
120  public function getAcceptedMimeTypes(): array
121  {
123  }
124 
125  // ===============================================
126  // END IMPLEMENTATION OF FileUpload
127  // ===============================================
128 
129  // ===============================================
130  // BEGIN OVERWRITTEN METHODS OF HasDynamicInputs
131  // ===============================================
132 
137  public function withValue($value): HasDynamicInputsBase
138  {
139  $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
140 
141  $clone = clone $this;
142  $identifier_key = $clone->upload_handler->getFileIdentifierParameterName();
143  foreach ($value as $data) {
144  $file_id = ($clone->hasMetadataInputs()) ? $data[$identifier_key] : $data;
145 
146  // that was not implicitly intended, but mapping dynamic inputs
147  // to the file-id is also a duplicate protection.
148  $clone->dynamic_inputs[$file_id] = $clone->dynamic_input_template->withValue($data);
149  }
150 
151  return $clone;
152  }
153 
154  // ===============================================
155  // END OVERWRITTEN METHODS OF HasDynamicInputs
156  // ===============================================
157 
158  public function hasMetadataInputs(): bool
159  {
161  }
162 
166  public function getTranslations(): array
167  {
168  return [
169  'invalid_mime' => $this->language->txt('ui_file_input_invalid_mime'),
170  'invalid_size' => $this->language->txt('ui_file_input_invalid_size'),
171  'invalid_amount' => $this->language->txt('ui_file_input_invalid_amount'),
172  'general_error' => $this->language->txt('ui_file_input_general_error'),
173  ];
174  }
175 
176  public function getUpdateOnLoadCode(): Closure
177  {
178  return static function () {
179  };
180  }
181 
183  {
184  if ($this->requirement_constraint !== null) {
185  return $this->requirement_constraint;
186  }
187 
188  return $this->refinery->custom()->constraint(
189  function ($value) {
190  return (is_array($value) && count($value) > 0);
191  },
192  function ($txt, $value) {
193  return $txt("msg_no_files_selected");
194  },
195  );
196  }
197 
198  protected function isClientSideValueOk($value): bool
199  {
200  if (!is_array($value)) {
201  return false;
202  }
203 
204  foreach ($value as $data) {
205  // if no dynamic input template was provided, the values
206  // must all be strings (possibly file-ids).
207  if (!is_string($data) && !$this->hasMetadataInputs()) {
208  return false;
209  }
210 
211  if ($this->hasMetadataInputs()) {
212  // if a dynamic input template was provided, the values
213  // must all contain the file-id as an array entry.
214  if (!array_key_exists($this->upload_handler->getFileIdentifierParameterName(), $data)) {
215  return false;
216  }
217 
218  // if a dynamic input template was provided, the values
219  // must be valid for the template input.
220  if (!$this->dynamic_input_template->isClientSideValueOk($data)) {
221  return false;
222  }
223  }
224  }
225 
226  return true;
227  }
228 
229  protected function createDynamicInputsTemplate(?FormInput $metadata_input): FormInput
230  {
231  $default_metadata_input = new Hidden(
232  $this->data_factory,
233  $this->refinery
234  );
235 
236  if (null === $metadata_input) {
237  return $default_metadata_input;
238  }
239 
240  $inputs = ($metadata_input instanceof C\Input\Field\Group) ?
241  $metadata_input->getInputs() : [
242  $metadata_input,
243  ];
244 
245  // map the file-id input to the UploadHandlers identifier key.
246  $inputs[$this->upload_handler->getFileIdentifierParameterName()] = $default_metadata_input;
247 
248  // tell the input that it contains actual metadata inputs.
249  $this->has_metadata_inputs = true;
250 
251  return new Group(
252  $this->data_factory,
253  $this->refinery,
254  $this->language,
255  $inputs,
256  ''
257  );
258  }
259 }
createDynamicInputsTemplate(?FormInput $metadata_input)
Definition: File.php:229
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
$txt
Definition: error.php:13
withValue($value)
Maps generated dynamic inputs to their file-id, which must be provided in or as $value.
Definition: File.php:137
__construct(Container $dic, ilPlugin $plugin)
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
Refinery Factory $refinery