ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ModalAdapterGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26
28{
30 protected string $cancel_label;
32 protected \ILIAS\Refinery\Factory $refinery;
33 protected string $title = "";
34 protected \ILIAS\HTTP\Services $http;
35 protected \ilCtrlInterface $ctrl;
36 protected \ILIAS\DI\UIServices $ui;
37 protected ?array $ui_content = null;
38 protected array $action_buttons = [];
39
43 public function __construct(
44 string $title,
45 string $cancel_label,
47 ) {
48 global $DIC;
49 $this->ui = $DIC->ui();
50 $this->ctrl = $DIC->ctrl();
51 $this->http = $DIC->http();
52 $this->refinery = $DIC->refinery();
53 $this->title = $title;
54 $this->cancel_label = $cancel_label;
55 $this->http_util = $http_util;
56 }
57
61 protected function _send(string $output): void
62 {
63 $this->http_util->sendString($output);
64 }
65
66 public function getTitle(): string
67 {
68 return $this->title;
69 }
70
71 public function legacy(string $content): self
72 {
73 $this->ui_content = [$this->ui->factory()->legacy()->content($content)];
74 $this->form = null;
75 return $this;
76 }
77
78 public function content(array $components): self
79 {
80 $this->ui_content = $components;
81 $this->form = null;
82 return $this;
83 }
84
85 public function button(string $text, string $url, bool $replace_modal = true, $onclick = ""): self
86 {
87 $target = $replace_modal
88 ? "#"
89 : $url;
90
91 $button = $this->ui->factory()->button()->standard(
92 $text,
93 $target
94 );
95
96 if ($replace_modal) {
97 $button = $button->withOnLoadCode(function ($id) use ($url, $onclick) {
98 return
99 "$('#$id').click(function(event) { $onclick; il.repository.ui.redirect('$url'); return false;});";
100 });
101 } elseif ($onclick !== "") {
102 $button = $button->withOnLoadCode(function ($id) use ($url, $onclick) {
103 return "$('#$id').click(function(event) { $onclick });";
104 });
105 }
106 $this->action_buttons[] = $button;
107 return $this;
108 }
109
110 public function form(
112 string $on_form_submit_click = ""
113 ): self {
114 if ($this->ctrl->isAsynch()) {
115 $this->form = $form->asyncModal();
116 } else {
117 $this->form = $form->syncModal();
118 }
119
120 $async = $this->form->isSentAsync()
121 ? "true"
122 : "false";
123 if ($on_form_submit_click === "") {
124 $on_form_submit_click = "il.repository.ui.submitModalForm(event,$async); return false;";
125 }
126 $button = $this->ui->factory()->button()->standard(
127 $this->form->getSubmitLabel(),
128 "#"
129 )->withOnLoadCode(function ($id) use ($on_form_submit_click) {
130 return
131 "$('#$id').click(function(event) {" . $on_form_submit_click . "});";
132 });
133 $this->action_buttons[] = $button;
134 $this->ui_content = null;
135 return $this;
136 }
137
138 protected function getModalWithContent(): Modal\RoundTrip
139 {
140 $modal = [];
141 if (!is_null($this->form)) {
142 $modal = $this->ui->factory()->modal()->roundtrip(
143 $this->getTitle(),
144 null,
145 $this->form->getForm()->getInputs(),
146 $this->form->getForm()->getPostURL()
147 );
148 } else {
149 $modal = $this->ui->factory()->modal()->roundtrip($this->getTitle(), $this->ui_content);
150 }
151 if (count($this->action_buttons) > 0) {
152 $modal = $modal->withActionButtons($this->action_buttons);
153 }
154 if ($this->cancel_label !== "") {
155 $modal = $modal->withCancelButtonLabel($this->cancel_label);
156 }
157 $modal = $modal->withAdditionalOnLoadCode(function ($id) {
158 return "il.repository.ui.initModal('$id');";
159 });
160 return $modal;
161 }
162
166 public function send(): void
167 {
168 $modal = $this->getModalWithContent();
169 $this->_send($this->ui->renderer()->renderAsync($modal));
170 }
171
172 public function renderAsyncTriggerButton(string $button_title, string $url, $shy = true): string
173 {
174 $ui = $this->ui;
175 $components = $this->getAsyncTriggerButtonComponents(
176 $button_title,
177 $url,
178 $shy
179 );
180 return $ui->renderer()->render($components);
181 }
182
183 public function getAsyncTriggerButtonComponents(string $button_title, string $url, $shy = true): array
184 {
185 $ui = $this->ui;
186 $modal = $ui->factory()->modal()->roundtrip("", $ui->factory()->legacy()->content(""));
187 $url .= '&replaceSignal=' . $modal->getReplaceSignal()->getId();
188 $modal = $modal->withAsyncRenderUrl($url);
189 if ($shy) {
190 $button = $ui->factory()->button()->shy($button_title, "#")
191 ->withOnClick($modal->getShowSignal());
192 } else {
193 $button = $ui->factory()->button()->standard($button_title, "#")
194 ->withOnClick($modal->getShowSignal());
195 }
196 return ["button" => $button, "modal" => $modal, "signal" => $modal->getShowSignal()->getId()];
197 }
198
199 public function getTriggerButtonComponents(string $button_title, $shy = true): array
200 {
201 $ui = $this->ui;
202
203 $modal = $this->getModalWithContent();
204 if ($shy) {
205 $button = $ui->factory()->button()->shy($button_title, "#")
206 ->withOnClick($modal->getShowSignal());
207 } else {
208 $button = $ui->factory()->button()->standard($button_title, "#")
209 ->withOnClick($modal->getShowSignal());
210 }
211 return ["button" => $button, "modal" => $modal, "signal" => $modal->getShowSignal()->getId()];
212 }
213}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$components
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
ILIAS Repository Form FormAdapterGUI $form
renderAsyncTriggerButton(string $button_title, string $url, $shy=true)
form(\ILIAS\Repository\Form\FormAdapterGUI $form, string $on_form_submit_click="")
button(string $text, string $url, bool $replace_modal=true, $onclick="")
__construct(string $title, string $cancel_label, HTTPUtil $http_util)
getTriggerButtonComponents(string $button_title, $shy=true)
getAsyncTriggerButtonComponents(string $button_title, string $url, $shy=true)
This describes commonalities between the different modals.
Definition: Modal.php:35
form( $class_path, string $cmd, string $submit_caption="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68