ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 HasDynamicInputs 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(
54  Language $language,
55  DataFactory $data_factory,
56  Factory $field_factory,
58  UploadLimitResolver $upload_limit_resolver,
60  string $label,
61  ?FormInput $metadata_input,
62  ?string $byline
63  ) {
64  $this->upload_limit_resolver = $upload_limit_resolver;
65  $this->max_file_size_in_bytes = $upload_limit_resolver->getBestPossibleUploadLimitInBytes($handler);
66  $this->upload_handler = $handler;
67  $this->value = [];
68 
70  $language,
71  $data_factory,
72  $refinery,
73  $this->createDynamicInputsTemplate($field_factory, $metadata_input),
74  $label,
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  $clone = clone $this;
87  $clone->max_file_size_in_bytes = $clone->upload_limit_resolver->getBestPossibleUploadLimitInBytes(
88  $clone->upload_handler,
89  $size_in_bytes
90  );
91 
92  return $clone;
93  }
94 
95  public function getMaxFileSize(): int
96  {
98  }
99 
100  public function withMaxFiles(int $max_file_amount): FileUpload
101  {
102  $clone = clone $this;
103  $clone->max_file_amount = $max_file_amount;
104 
105  return $clone;
106  }
107 
108  public function getMaxFiles(): int
109  {
110  return $this->max_file_amount;
111  }
112 
113  public function withAcceptedMimeTypes(array $mime_types): FileUpload
114  {
115  $clone = clone $this;
116  $clone->accepted_mime_types = $mime_types;
117 
118  return $clone;
119  }
120 
121  public function getAcceptedMimeTypes(): array
122  {
124  }
125 
126  // ===============================================
127  // END IMPLEMENTATION OF FileUpload
128  // ===============================================
129 
130  // ===============================================
131  // BEGIN OVERWRITTEN METHODS OF HasDynamicInputs
132  // ===============================================
133 
139  {
140  $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
141 
142  $clone = clone $this;
143  foreach ($value as $data) {
144  $file_id = ($clone->hasMetadataInputs()) ? $data[0] : $data;
145 
146  // that was not implicitly intended, but mapping dynamic inputs
147  // to the file-id is also a duplicate protection.
148  $clone->generated_dynamic_inputs[$file_id] = $clone->getTemplateForDynamicInputs()->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) {
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  // if a dynamic input template was provided, the values
211  // must be valid for the template input.
212  if ($this->hasMetadataInputs() && !$this->dynamic_input_template->isClientSideValueOk($data)) {
213  return false;
214  }
215  }
216 
217  return true;
218  }
219 
220  protected function createDynamicInputsTemplate(Factory $field_factory, ?FormInput $metadata_input): FormInput
221  {
222  $file_id_input = $field_factory->hidden();
223 
224  if (null === $metadata_input) {
225  return $file_id_input;
226  }
227 
228  $this->has_metadata_inputs = true;
229 
230  return $field_factory->group([$file_id_input, $metadata_input]);
231  }
232 }
This implements commonalities between inputs.
Definition: Input.php:42
__construct(Language $language, DataFactory $data_factory, Factory $field_factory, Refinery $refinery, UploadLimitResolver $upload_limit_resolver, C\Input\Field\UploadHandler $handler, string $label, ?FormInput $metadata_input, ?string $byline)
Definition: File.php:53
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
Factory for Date Formats.
Definition: Factory.php:26
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
createDynamicInputsTemplate(Factory $field_factory, ?FormInput $metadata_input)
Definition: File.php:220
Describes an Input Field which dynamically generates inputs according to a template.
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:29
withValue($value)
Maps generated dynamic inputs to their file-id, which must be provided in or as $value.
Definition: File.php:138
__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