ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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;
33 use ilStartUpGUI;
34 use ilObjUser;
35 use Exception;
45 
46 class Conductor
47 {
48  private readonly Internal $internal;
49  private readonly Routing $routing;
50 
52  private array $modals = [];
53 
54  public function __construct(
55  private readonly Container $container,
56  ?Internal $internal = null,
57  ?Routing $routing = null
58  ) {
59  $this->internal = $internal ?? $this->createInternal();
60  $this->routing = $routing ?? new Routing(
61  $this->container->ctrl(),
62  new SelectSetting(new SessionStore(), new Marshal($this->container->refinery())),
65  );
66  }
67 
68  public function provide(string $id): Provide
69  {
70  return new Provide($id, $this->internal, $this->container);
71  }
72 
73  public function loginPageHTML(string $id): string
74  {
75  $create = $this->internal->get('show-on-login-page', $id);
76  if (!$create) {
77  return '';
78  }
79  return $this->container->ui()->renderer()->render($create());
80  }
81 
82  public function logoutText(): string
83  {
84  try {
85  $id = $this->container->http()->wrapper()->query()->retrieve(
86  'withdraw_consent',
87  $this->container->refinery()->to()->string()
88  );
89  } catch (Exception) {
90  return '';
91  }
92 
93  $logout_text = $this->internal->get('logout-text', $id);
94 
95  return null === $logout_text ? '' : $this->container->ui()->renderer()->render($logout_text());
96  }
97 
98  public function logoutTarget(LogoutTarget $target): LogoutTarget
99  {
101  $target,
102  $this->container->http()->wrapper()->query()->has('withdraw_consent'),
103  $this->container->ctrl()
104  );
105  }
106 
107  public function modifyFooter(Closure $footer): Closure
108  {
109  return array_reduce($this->internal->all('footer'), fn(Closure $footer, Closure $proc) => $proc($footer), $footer);
110  }
111 
112  public function agree(string $gui, string $cmd): void
113  {
114  $this->setMainTemplateContent($this->agreeContent($gui, $cmd));
115  }
116 
117  public function agreeContent(string $gui, string $cmd): string
118  {
119  $key = ilLegalDocumentsAgreementGUI::class === $gui ? 'agreement-form' : 'public-page';
120  $result = $this->byQueryParams($gui, $cmd, $key)->then($this->renderPageFragment($gui, $cmd));
121 
122  if (!$result->isOk() && $result->error() === 'Not available.') {
123  $this->routing->redirectToOriginalTarget();
124  }
125 
126  return $result->value();
127  }
128 
129  public function withdraw(string $gui, string $cmd): void
130  {
131  $this->setMainTemplateContent($this->byQueryParams($gui, $cmd, 'withdraw')->then($this->renderPageFragment($gui, $cmd))->value());
132  }
133 
138  public function usersWithHiddenOnlineStatus(array $users): array
139  {
140  $filters = $this->internal->all('filter-online-users');
141 
142  $visible_users = array_reduce(
143  $filters,
144  fn($users, $only_visible_users) => $only_visible_users($users),
145  $users,
146  );
147 
148  return array_values(array_diff($users, $visible_users));
149  }
150 
152  {
153  return $this->container->refinery()->in()->series(array_values($this->internal->all('constrain-internal-mail')));
154  }
155 
156  public function canUseSoapApi(): Transformation
157  {
158  return $this->container->refinery()->in()->series(array_values($this->internal->all('use-soap-api')));
159  }
160 
161  public function afterLogin(): void
162  {
163  array_map(fn($proc) => $proc(), $this->internal->all('after-login'));
164  }
165 
169  public function findGotoLink(string $goto_target): Result
170  {
171  return $this->find(
172  fn($goto_link) => $goto_link->name() === $goto_target,
173  $this->internal->all('goto')
174  )->map(fn($goto_link) => $goto_link->target());
175  }
176 
180  public function intercepting(): array
181  {
182  return $this->internal->all('intercept');
183  }
184 
186  {
187  return new Bundle($this->internal->all('self-registration'));
188  }
189 
193  public function userManagementFields(ilObjUser $user): array
194  {
195  $this->modals = [];
196  return array_reduce(
197  $this->internal->all('user-management-fields'),
198  fn(array $prev, callable $f): array => [
199  ...$prev,
200  ...array_map(function ($val) {
201  if (is_array($val)) {
202  $this->find(fn($x) => $x instanceof Modal, $val)
203  ->map(fn($modal) => array_push($this->modals, $modal));
204  return $this->find(fn($x) => $x instanceof ilNonEditableValueGUI, $val)->value();
205  }
206  return $val;
207  }, $f($user))
208  ],
209  []
210  );
211  }
212 
213  public function userManagementModals(): string
214  {
215  $string = $this->container->ui()->renderer()->render($this->modals);
216  $this->modals = [];
217 
218  return $string;
219  }
220 
229  private function find(Closure $predicate, array $array): Result
230  {
231  foreach ($array as $x) {
232  if ($predicate($x)) {
233  return new Ok($x);
234  }
235  }
236 
237  return new Error('Not found.');
238  }
239 
245  private function any(Closure $predicate, array $array): bool
246  {
247  return $this->find($predicate, $array)->isOk();
248  }
249 
255  private function all(Closure $predicate, array $array): bool
256  {
257  return !$this->any(static fn($x) => !$predicate($x), $array);
258  }
259 
260  private function byQueryParams(string $gui, string $cmd, string $key): Result
261  {
262  try {
263  $id = $this->container->http()->wrapper()->query()->retrieve('id', $this->container->refinery()->to()->string());
264  } catch (Exception) {
265  return new Error('No provider ID given.');
266  }
267 
268  $this->container->ctrl()->setParameterByClass($gui, 'id', $id);
269 
270  $value = $this->internal->get($key, $id);
271 
272  if (null === $value) {
273  return new Error('Field not defined.');
274  }
275 
276  return new Ok($value);
277  }
278 
282  private function renderPageFragment(string $gui, string $cmd): Closure
283  {
284  return fn(Closure $proc) => $proc($gui, $cmd)->map(fn($fragment) => $fragment->render(
285  $this->container->ui()->mainTemplate(),
286  $this->container->ui()->renderer()
287  ));
288  }
289 
290  private function setMainTemplateContent(string $content): void
291  {
292  $this->container->ui()->mainTemplate()->setContent($content);
293  }
294 
295  private function createInternal(): Internal
296  {
297  $clock = (new DataFactory())->clock()->system();
298  $action = new UserAction($this->container->user(), $clock);
299 
300  return new Internal($this->provide(...), fn(string $id) => new Wiring(new SlotConstructor(
301  $id,
302  $this->container,
303  $action
304  )));
305  }
306 }
This describes commonalities between the different modals.
Definition: Modal.php:34
readonly Internal $internal
Definition: Conductor.php:48
__construct(private readonly Container $container, ?Internal $internal=null, ?Routing $routing=null)
Definition: Conductor.php:54
find(Closure $predicate, array $array)
A
Definition: Conductor.php:229
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
all(Closure $predicate, array $array)
A
Definition: Conductor.php:255
$container
Definition: wac.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static logoutUrl(array $parameters=[])
Return the logout URL with a valid CSRF token.
logoutTarget(LogoutTarget $target)
Definition: Conductor.php:98
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
renderPageFragment(string $gui, string $cmd)
Definition: Conductor.php:282
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30
readonly Routing $routing
Definition: Conductor.php:49
any(Closure $predicate, array $array)
A
Definition: Conductor.php:245
setMainTemplateContent(string $content)
Definition: Conductor.php:290
static redirectToStartingPage(string $target='')
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
modifyFooter(Closure $footer)
Definition: Conductor.php:107
A transformation is a function from one datatype to another.
findGotoLink(string $goto_target)
Definition: Conductor.php:169
agree(string $gui, string $cmd)
Definition: Conductor.php:112
agreeContent(string $gui, string $cmd)
Definition: Conductor.php:117
withdraw(string $gui, string $cmd)
Definition: Conductor.php:129
userManagementFields(ilObjUser $user)
Definition: Conductor.php:193
usersWithHiddenOnlineStatus(array $users)
Definition: Conductor.php:138
byQueryParams(string $gui, string $cmd, string $key)
Definition: Conductor.php:260