ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
file_upload.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use Generator;
32
50function file_upload(): string
51{
52 global $DIC;
53
54 $factory = $DIC->ui()->factory();
55 $renderer = $DIC->ui()->renderer();
56 $refinery = $DIC->refinery();
57 $http = $DIC->http();
58 $server_request = $DIC->http()->request();
59 $get_request = $DIC->http()->wrapper()->query();
60 $data_factory = new \ILIAS\Data\Factory();
61
62 $here_url = $data_factory->uri((string) $DIC->http()->request()->getUri());
63 $here_url_builder = new URLBuilder($here_url);
64
65 // will simulate a GET request to this example asking for a prompt state
66 [$form_get_url, $form_parameter] = $here_url_builder->acquireParameter([...explode(__NAMESPACE__, '\\'), __METHOD__], "get_form", "1");
67
68 // will simulate a POST request to this example that processes the file upload
69 [$form_post_url, $process_parameter] = $here_url_builder->acquireParameter([...explode(__NAMESPACE__, '\\'), __METHOD__], "process", "1");
70
71 // will simulate a shared function to create a Form that is accessible in all endpoints
72 $build_form = static fn() => $factory->input()->container()->form()->standard(
73 (string) $form_post_url->buildURI(),
74 [$factory->input()->field()->file(new ilUIDemoFileUploadHandlerGUI(), "Upload file")],
75 )->withSubmitLabel('Upload');
76
77 // simulates the POST endpoint for processing the file upload
78 if ($get_request->has($process_parameter->getName())) {
79 $form = $build_form()->withRequest($server_request);
80 $data_message_box = $factory->messageBox()->info(
81 "Files have been uploaded. You can verify the submitted data below:" .
82 "<pre>" . print_r($form->getData(), true) . "</pre>"
83 );
84 $html = $renderer->renderAsync($factory->prompt()->state()->show($form, $data_message_box));
85 $http->saveResponse(
86 $http->response()
87 ->withHeader('Content-Type', 'text/html; charset=utf-8')
88 ->withBody(Streams::ofString($html))
89 );
90 $http->sendResponse();
91 $http->close();
92 }
93
94 // simulates the GET endpoint for rendering the prompt state
95 if ($get_request->has($form_parameter->getName())) {
96 $html = $renderer->renderAsync($factory->prompt()->state()->show($build_form()));
97 $http->saveResponse(
98 $http->response()
99 ->withHeader('Content-Type', 'text/html; charset=utf-8')
100 ->withBody(Streams::ofString($html))
101 );
102 $http->sendResponse();
103 $http->close();
104 }
105
106 // create a Standard Prompt that requests the simulated GET endpoint
107 $prompt = $factory->prompt()->standard($form_get_url->buildURI())->withTitle('Upload file');
108
109 // create a Button that triggers the Standard Prompt
110 $button = $factory->button()->primary('Upload file', $prompt->getShowSignal());
111
112 return $renderer->render([$prompt, $button]);
113}
$renderer
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
Class ilUIDemoFileUploadHandlerGUI.
This describes how entities should be retrieved for different purposes.
This describes an Entity.
Definition: Entity.php:37
Flags some Component to be a valid content of a Prompt.
global $DIC
Definition: shib_login.php:26