ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.FormAdapterGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
25 
30 {
31  protected const DEFAULT_SECTION = "@internal_default_section";
32  protected \ilLanguage $lng;
33  protected string $title = "";
37  protected $raw_data = null;
38  protected \ILIAS\HTTP\Services $http;
39  protected \ilCtrlInterface $ctrl;
40  protected \ILIAS\DI\UIServices $ui;
41  protected array $fields = [];
42  protected array $sections = [self::DEFAULT_SECTION => ["title" => "", "description" => ""]];
43  protected string $current_section = self::DEFAULT_SECTION;
44  protected array $section_of_field = [];
45  protected $class_path;
46  protected string $cmd = self::DEFAULT_SECTION;
47  protected ?Form\Standard $form = null;
48  protected array $upload_handler = [];
49 
53  public function __construct(
55  string $cmd
56  ) {
57  global $DIC;
58  $this->class_path = $class_path;
59  $this->cmd = $cmd;
60  $this->ui = $DIC->ui();
61  $this->ctrl = $DIC->ctrl();
62  $this->http = $DIC->http();
63  $this->lng = $DIC->language();
64  }
65 
66  public function getTitle(): string
67  {
68  return $this->title;
69  }
70 
71  public function section(
72  string $key,
73  string $title,
74  string $description = ""
75  ): self {
76  if ($this->title == "") {
77  $this->title = $title;
78  }
79 
80  $this->sections[$key] = [
81  "title" => $title,
82  "description" => $description
83  ];
84  $this->current_section = $key;
85  return $this;
86  }
87 
88  public function text(
89  string $key,
90  string $title,
91  string $description = "",
92  ?string $value = null
93  ): self {
94  $field = $this->ui->factory()->input()->field()->text($title, $description);
95  if (!is_null($value)) {
96  $field = $field->withValue($value);
97  }
98  $this->addField($key, $field);
99  return $this;
100  }
101 
102  public function textarea(
103  string $key,
104  string $title,
105  string $description = "",
106  ?string $value = null
107  ): self {
108  $field = $this->ui->factory()->input()->field()->textarea($title, $description);
109  if (!is_null($value)) {
110  $field = $field->withValue($value);
111  }
112  $this->addField($key, $field);
113  return $this;
114  }
115 
116  public function select(
117  string $key,
118  string $title,
119  array $options,
120  string $description = "",
121  ?string $value = null
122  ): self {
123  $field = $this->ui->factory()->input()->field()->select($title, $options, $description);
124  if (!is_null($value)) {
125  $field = $field->withValue($value);
126  }
127  $this->addField(
128  $key,
129  $field
130  );
131  return $this;
132  }
133 
134  public function file(
135  string $key,
136  string $title,
137  \Closure $result_handler,
138  string $id_parameter,
139  string $description = "",
140  int $max_files = 1,
141  array $mime_types = [],
142  bool $required = false
143  ): self {
144  $this->upload_handler[$key] = new \ilRepoStandardUploadHandlerGUI(
145  $result_handler,
146  $id_parameter
147  );
148 
149  if (count($mime_types) > 0) {
150  $description.= $this->lng->txt("rep_allowed_types") . ": " .
151  implode(", ", $mime_types);
152  }
153 
154  $field = $this->ui->factory()->input()->field()->file(
155  $this->upload_handler[$key],
156  $title,
157  $description
158  )
159  ->withMaxFileSize((int) \ilFileUtils::getUploadSizeLimitBytes())
160  ->withMaxFiles($max_files);
161  if (count($mime_types) > 0) {
162  $field = $field->withAcceptedMimeTypes($mime_types);
163  }
164  if ($required) {
165  $field = $field->withRequired(true);
166  }
167 
168  $this->addField(
169  $key,
170  $field
171  );
172  return $this;
173  }
174 
176  {
177  if (!isset($this->upload_handler[$key])) {
178  throw new \ilException("Unknown file upload field: " . $key);
179  }
180  return $this->upload_handler[$key];
181  }
182 
183 
184  protected function addField(string $key, FormInput $field): void
185  {
186  if (isset($this->section_of_field[$key])) {
187  throw new \ilException("Duplicate Input Key: " . $key);
188  }
189  if ($key === "") {
190  throw new \ilException("Missing Input Key: " . $key);
191  }
192  $this->section_of_field[$key] = $this->current_section;
193  $this->fields[$this->current_section][$key] = $field;
194  $this->form = null;
195  }
196 
197  protected function getForm(): Form\Standard
198  {
199  $ctrl = $this->ctrl;
200 
201  if (is_null($this->form)) {
202  $action = $ctrl->getLinkTargetByClass($this->class_path, $this->cmd);
203  $inputs = [];
204  foreach ($this->sections as $sec_key => $section) {
205  if ($sec_key === self::DEFAULT_SECTION) {
206  if (isset($this->fields[$sec_key])) {
207  foreach ($this->fields[$sec_key] as $f_key => $field) {
208  $inputs[$f_key] = $field;
209  }
210  }
211  } else {
212  if (isset($this->fields[$sec_key]) && count($this->fields[$sec_key]) > 0) {
213  $inputs[$sec_key] = $this->ui->factory()->input()->field()->section(
214  $this->fields[$sec_key],
215  $section["title"],
216  $section["description"]
217  );
218  }
219  }
220  }
221  $this->form = $this->ui->factory()->input()->container()->form()->standard(
222  $action,
223  $inputs
224  );
225  }
226  return $this->form;
227  }
228 
232  public function getData(string $key)
233  {
234  if (is_null($this->raw_data)) {
235  $request = $this->http->request();
236  $this->raw_data = $this->getForm()->withRequest($request)->getData();
237  }
238 
239  if (!isset($this->section_of_field[$key])) {
240  throw new \ilException("Unknown Key: " . $key);
241  }
242 
243  $section_data = ($this->section_of_field[$key] === self::DEFAULT_SECTION)
244  ? $this->raw_data
245  : $this->raw_data[$this->section_of_field[$key]] ?? null;
246 
247  if (!isset($section_data[$key])) {
248  return null;
249  }
250  return $section_data[$key];
251  }
252 
253  public function render(): string
254  {
255  return $this->ui->renderer()->render($this->getForm());
256  }
257 
258  protected function _getData(): void
259  {
260  if (is_null($this->raw_data)) {
261  $request = $this->http->request();
262  $this->form = $this->getForm()->withRequest($request);
263  $this->raw_data = $this->form->getData();
264  }
265  }
266 
267  public function isValid(): bool
268  {
269  $this->_getData();
270  return !(is_null($this->raw_data));
271  }
272 }
file(string $key, string $title, \Closure $result_handler, string $id_parameter, string $description="", int $max_files=1, array $mime_types=[], bool $required=false)
$errors fields
Definition: imgupload.php:67
addField(string $key, FormInput $field)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
static getUploadSizeLimitBytes()
global $DIC
Definition: feed.php:28
section(string $key, string $title, string $description="")
string $key
Consumer key/client ID value.
Definition: System.php:193
textarea(string $key, string $title, string $description="", ?string $value=null)
form( $class_path, string $cmd)
This describes a standard form.
Definition: Standard.php:26
getLinkTargetByClass( $a_class, string $a_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
Returns a link target for the given information.
text(string $key, string $title, string $description="", ?string $value=null)
select(string $key, string $title, array $options, string $description="", ?string $value=null)
This describes inputs that can be used in forms.
Definition: FormInput.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct( $class_path, string $cmd)