ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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(private readonly Container $container, ?Internal $internal = null, Routing $routing = null)
55  {
56  $this->internal = $internal ?? $this->createInternal();
57  $this->routing = $routing ?? new Routing(
58  $this->container->ctrl(),
59  new SelectSetting(new SessionStore(), new Marshal($this->container->refinery())),
62  );
63  }
64 
65  public function provide(string $id): Provide
66  {
67  return new Provide($id, $this->internal, $this->container);
68  }
69 
70  public function loginPageHTML(string $id): string
71  {
72  $create = $this->internal->get('show-on-login-page', $id);
73  if (!$create) {
74  return '';
75  }
76  return $this->container->ui()->renderer()->render($create());
77  }
78 
79  public function logoutText(): string
80  {
81  try {
82  $id = $this->container->http()->wrapper()->query()->retrieve(
83  'withdraw_consent',
84  $this->container->refinery()->to()->string()
85  );
86  } catch (Exception) {
87  return '';
88  }
89 
90  $logout_text = $this->internal->get('logout-text', $id);
91 
92  return null === $logout_text ? '' : $this->container->ui()->renderer()->render($logout_text());
93  }
94 
95  public function logoutTarget(LogoutTarget $target): LogoutTarget
96  {
98  $target,
99  $this->container->http()->wrapper()->query()->has('withdraw_consent'),
100  $this->container->ctrl()
101  );
102  }
103 
104  public function modifyFooter(Closure $footer): Closure
105  {
106  return array_reduce($this->internal->all('footer'), fn(Closure $footer, Closure $proc) => $proc($footer), $footer);
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 
177  public function intercepting(): array
178  {
179  return $this->internal->all('intercept');
180  }
181 
183  {
184  return new Bundle($this->internal->all('self-registration'));
185  }
186 
190  public function userManagementFields(ilObjUser $user): array
191  {
192  $this->modals = [];
193  return array_reduce(
194  $this->internal->all('user-management-fields'),
195  fn(array $prev, callable $f): array => [
196  ...$prev,
197  ...array_map(function ($val) {
198  if (is_array($val)) {
199  $this->find(fn($x) => $x instanceof Modal, $val)
200  ->map(fn($modal) => array_push($this->modals, $modal));
201  return $this->find(fn($x) => $x instanceof ilNonEditableValueGUI, $val)->value();
202  }
203  return $val;
204  }, $f($user))
205  ],
206  []
207  );
208  }
209 
210  public function userManagementModals(): string
211  {
212  $string = $this->container->ui()->renderer()->render($this->modals);
213  $this->modals = [];
214 
215  return $string;
216  }
217 
226  private function find(Closure $predicate, array $array): Result
227  {
228  foreach ($array as $x) {
229  if ($predicate($x)) {
230  return new Ok($x);
231  }
232  }
233 
234  return new Error('Not found.');
235  }
236 
242  private function any(Closure $predicate, array $array): bool
243  {
244  return $this->find($predicate, $array)->isOk();
245  }
246 
252  private function all(Closure $predicate, array $array): bool
253  {
254  return !$this->any(static fn($x) => !$predicate($x), $array);
255  }
256 
257  private function byQueryParams(string $gui, string $cmd, string $key): Result
258  {
259  try {
260  $id = $this->container->http()->wrapper()->query()->retrieve('id', $this->container->refinery()->to()->string());
261  } catch (Exception) {
262  return new Error('No provider ID given.');
263  }
264 
265  $this->container->ctrl()->setParameterByClass($gui, 'id', $id);
266 
267  $value = $this->internal->get($key, $id);
268 
269  if (null === $value) {
270  return new Error('Field not defined.');
271  }
272 
273  return new Ok($value);
274  }
275 
279  private function renderPageFragment(string $gui, string $cmd): Closure
280  {
281  return fn(Closure $proc) => $proc($gui, $cmd)->map(fn($fragment) => $fragment->render(
282  $this->container->ui()->mainTemplate(),
283  $this->container->ui()->renderer()
284  ));
285  }
286 
287  private function setMainTemplateContent(string $content): void
288  {
289  $this->container->ui()->mainTemplate()->setContent($content);
290  }
291 
292  private function createInternal(): Internal
293  {
294  $clock = (new DataFactory())->clock()->system();
295  $action = new UserAction($this->container->user(), $clock);
296 
297  return new Internal($this->provide(...), fn(string $id) => new Wiring(new SlotConstructor(
298  $id,
299  $this->container,
300  $action
301  )));
302  }
303 }
This describes commonalities between the different modals.
Definition: Modal.php:34
readonly Internal $internal
Definition: Conductor.php:48
find(Closure $predicate, array $array)
A
Definition: Conductor.php:226
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
all(Closure $predicate, array $array)
A
Definition: Conductor.php:252
$container
Definition: wac.php:13
static logoutUrl(array $parameters=[])
Return the logout URL with a valid CSRF token.
logoutTarget(LogoutTarget $target)
Definition: Conductor.php:95
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:279
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
readonly Routing $routing
Definition: Conductor.php:49
__construct(private readonly Container $container, ?Internal $internal=null, Routing $routing=null)
Definition: Conductor.php:54
any(Closure $predicate, array $array)
A
Definition: Conductor.php:242
setMainTemplateContent(string $content)
Definition: Conductor.php:287
static redirectToStartingPage(string $target='')
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
modifyFooter(Closure $footer)
Definition: Conductor.php:104
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:190
usersWithHiddenOnlineStatus(array $users)
Definition: Conductor.php:135
byQueryParams(string $gui, string $cmd, string $key)
Definition: Conductor.php:257