ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Conductor.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\LegalDocuments;
22 
28 use Closure;
34 use ilStartUpGUI;
35 use ilObjUser;
36 use Exception;
45 
46 class Conductor
47 {
48  private readonly Internal $internal;
49  private readonly Routing $routing;
50 
51  public function __construct(private readonly Container $container, ?Internal $internal = null, Routing $routing = null)
52  {
53  $this->internal = $internal ?? $this->createInternal();
54  $this->routing = $routing ?? new Routing(
55  $this->container->ctrl(),
56  new SelectSetting(new SessionStore(), new Marshal($this->container->refinery())),
59  );
60  }
61 
62  public function provide(string $id): Provide
63  {
64  return new Provide($id, $this->internal, $this->container);
65  }
66 
67  public function onLogout(string $gui): void
68  {
69  try {
70  $id = $this->container->http()->wrapper()->query()->retrieve('withdraw_consent', $this->container->refinery()->to()->string());
71  } catch (Exception $e) {
72  return;
73  }
74 
75  $logout = $this->internal->get('logout', $id);
76  if (null !== $logout) {
77  $this->container->ctrl()->setParameterByClass($gui, 'withdraw_from', $id);
78  $logout();
79  }
80  }
81 
82  public function loginPageHTML(string $id): string
83  {
84  $create = $this->internal->get('show-on-login-page', $id);
85  if (!$create) {
86  return '';
87  }
88  return $this->container->ui()->renderer()->render($create());
89  }
90 
91  public function logoutText(): string
92  {
93  try {
94  $id = $this->container->http()->wrapper()->query()->retrieve('withdraw_from', $this->container->refinery()->to()->string());
95  } catch (Exception $e) {
96  return '';
97  }
98 
99  $logout_text = $this->internal->get('logout-text', $id);
100 
101  return null === $logout_text ? '' : $this->container->ui()->renderer()->render($logout_text());
102  }
103 
104  public function modifyFooter(Footer $footer): Footer
105  {
106  return $this->footerBridge($footer, array_reduce($this->internal->all('footer'), fn(Closure $footer, Closure $proc) => $proc($footer), $this->collectFooterItems([]))());
107  }
108 
109  public function agree(string $gui, string $cmd): void
110  {
111  $this->setMainTemplateContent($this->agreeContent($gui, $cmd));
112  }
113 
114  public function agreeContent(string $gui, string $cmd): string
115  {
116  $key = ilLegalDocumentsAgreementGUI::class === $gui ? 'agreement-form' : 'public-page';
117  $result = $this->byQueryParams($gui, $cmd, $key)->then($this->renderPageFragment($gui, $cmd));
118 
119  if (!$result->isOk() && $result->error() === 'Not available.') {
120  $this->routing->redirectToOriginalTarget();
121  }
122 
123  return $result->value();
124  }
125 
126  public function withdraw(string $gui, string $cmd): void
127  {
128  $this->setMainTemplateContent($this->byQueryParams($gui, $cmd, 'withdraw')->then($this->renderPageFragment($gui, $cmd))->value());
129  }
130 
135  public function usersWithHiddenOnlineStatus(array $users): array
136  {
137  $filters = $this->internal->all('filter-online-users');
138 
139  $visible_users = array_reduce(
140  $filters,
141  fn($users, $only_visible_users) => $only_visible_users($users),
142  $users,
143  );
144 
145  return array_values(array_diff($users, $visible_users));
146  }
147 
149  {
150  return $this->container->refinery()->in()->series(array_values($this->internal->all('constrain-internal-mail')));
151  }
152 
153  public function canUseSoapApi(): Transformation
154  {
155  return $this->container->refinery()->in()->series(array_values($this->internal->all('use-soap-api')));
156  }
157 
158  public function afterLogin(): void
159  {
160  array_map(fn($proc) => $proc(), $this->internal->all('after-login'));
161  }
162 
166  public function findGotoLink(string $goto_target): Result
167  {
168  return $this->find(
169  fn($goto_link) => $goto_link->name() === $goto_target,
170  $this->internal->all('goto')
171  )->map(fn($goto_link) => $goto_link->target());
172  }
173 
174  public function intercepting(): array
175  {
176  return $this->internal->all('intercept');
177  }
178 
180  {
181  return new Bundle($this->internal->all('self-registration'));
182  }
183 
184  public function userManagementFields(ilObjUser $user): array
185  {
186  return array_reduce(
187  $this->internal->all('user-management-fields'),
188  static fn(array $prev, callable $f): array => [...$prev, ...$f($user)],
189  []
190  );
191  }
192 
201  private function find(Closure $predicate, array $array): Result
202  {
203  foreach ($array as $x) {
204  if ($predicate($x)) {
205  return new Ok($x);
206  }
207  }
208 
209  return new Error('Not found.');
210  }
211 
217  private function any(Closure $predicate, array $array): bool
218  {
219  return $this->find($predicate, $array)->isOk();
220  }
221 
227  private function all(Closure $predicate, array $array): bool
228  {
229  return !$this->any(static fn($x) => !$predicate($x), $array);
230  }
231 
232  private function byQueryParams(string $gui, string $cmd, string $key): Result
233  {
234  try {
235  $id = $this->container->http()->wrapper()->query()->retrieve('id', $this->container->refinery()->to()->string());
236  } catch (Exception $e) {
237  return new Error('No provider ID given.');
238  }
239 
240  $this->container->ctrl()->setParameterByClass($gui, 'id', $id);
241 
242  $value = $this->internal->get($key, $id);
243 
244  if (null === $value) {
245  return new Error('Field not defined.');
246  }
247 
248  return new Ok($value);
249  }
250 
254  private function renderPageFragment(string $gui, string $cmd): Closure
255  {
256  return fn(Closure $proc) => $proc($gui, $cmd)->map(fn($fragment) => $fragment->render(
257  $this->container->ui()->mainTemplate(),
258  $this->container->ui()->renderer()
259  ));
260  }
261 
262  private function setMainTemplateContent(string $content): void
263  {
264  $this->container->ui()->mainTemplate()->setContent($content);
265  }
266 
267  private function createInternal(): Internal
268  {
269  $clock = (new DataFactory())->clock()->system();
270  $action = new UserAction($this->container->user(), $clock);
271 
272  return new Internal($this->provide(...), fn(string $id) => new Wiring(new SlotConstructor(
273  $id,
274  $this->container,
275  $action
276  )));
277  }
278 
279  private function footerBridge(Footer $footer, array $collection): Footer
280  {
281  $new_links = [];
282  $add_item = function (string $id, string $title, object $obj) use (&$footer, &$new_links): void {
283  if ($obj instanceof Modal) {
284  $footer = $footer->withAdditionalModalAndTrigger($obj, $this->container->ui()->factory()->button()->shy($title, ''));
285  } else {
286  $new_links[] = $this->container->ui()->factory()->link()->standard($title, (string) $obj);
287  }
288  };
289 
290  foreach ($collection as $args) {
291  $add_item(...$args);
292  }
293 
294  $old_links = $footer->getLinks();
295  $modals = $footer->getModals();
296  $new_footer = $this->container->ui()->factory()->mainControls()->footer(array_merge($old_links, $new_links), $footer->getText());
297  $new_footer = $footer->getPermanentURL() ? $new_footer->withPermanentURL($footer->getPermanentURL()) : $new_footer;
298 
299  return array_reduce($modals, static fn(Footer $f, array $m) => $f->withAdditionalModalAndTrigger(...$m), $new_footer);
300  }
301 
302  private function collectFooterItems(array $items): Closure
303  {
304  return function (...$args) use ($items) {
305  if ($args === []) {
306  return $items;
307  }
308  return $this->collectFooterItems(array_merge($items, [$args]));
309  };
310  }
311 }
This describes commonalities between the different modals.
Definition: Modal.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly Internal $internal
Definition: Conductor.php:48
modifyFooter(Footer $footer)
Definition: Conductor.php:104
withAdditionalModalAndTrigger(Modal\RoundTrip $roundTripModal, Button\Shy $shyButton)
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
collectFooterItems(array $items)
Definition: Conductor.php:302
find(Closure $predicate, array $array)
A
Definition: Conductor.php:201
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
all(Closure $predicate, array $array)
A
Definition: Conductor.php:227
$container
Definition: wac.php:14
static logoutUrl(array $parameters=[])
Return the logout URL with a valid CSRF token.
renderPageFragment(string $gui, string $cmd)
Definition: Conductor.php:254
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
string $key
Consumer key/client ID value.
Definition: System.php:193
footerBridge(Footer $footer, array $collection)
Definition: Conductor.php:279
readonly Routing $routing
Definition: Conductor.php:49
__construct(private readonly Container $container, ?Internal $internal=null, Routing $routing=null)
Definition: Conductor.php:51
any(Closure $predicate, array $array)
A
Definition: Conductor.php:217
setMainTemplateContent(string $content)
Definition: Conductor.php:262
static redirectToStartingPage(string $target='')
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
A transformation is a function from one datatype to another.
findGotoLink(string $goto_target)
Definition: Conductor.php:166
agree(string $gui, string $cmd)
Definition: Conductor.php:109
agreeContent(string $gui, string $cmd)
Definition: Conductor.php:114
withdraw(string $gui, string $cmd)
Definition: Conductor.php:126
userManagementFields(ilObjUser $user)
Definition: Conductor.php:184
usersWithHiddenOnlineStatus(array $users)
Definition: Conductor.php:135
This describes the Footer.
Definition: Footer.php:32
byQueryParams(string $gui, string $cmd, string $key)
Definition: Conductor.php:232