ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
PersonalSettingsTableActions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use ILIAS\UI\Renderer as UIRenderer;
28use ILIAS\UI\Factory as UIFactory;
31
33{
34 public const string ROW_ID_PARAMETER = 't_id';
35 public const string ACTION_PARAMETER = 'action';
36 public const string ACTION_TYPE_PARAMETER = 'action_type';
37 public const string SUBMIT_ACTION = 'submitTableAction';
38
42 public function __construct(
43 private readonly RequestDataCollector $test_request,
44 private readonly ResponseHandler $test_response,
45 private readonly UIRenderer $ui_renderer,
46 private readonly UIFactory $ui_factory,
47 private readonly Language $lng,
48 private readonly \ilObjUser $user,
49 private readonly PersonalSettingsRepository $repository,
50 private readonly array $actions
51 ) {
52 }
53
54 public function getActions(
55 URLBuilder $url_builder,
56 URLBuilderToken $row_id_token,
57 URLBuilderToken $action_token,
58 URLBuilderToken $action_type_token
59 ): array {
60 return array_map(fn(TableAction $action) => $action->buildTableAction(
61 $url_builder,
62 $row_id_token,
63 $action_token,
64 $action_type_token
65 ), $this->actions);
66 }
67
68 public function getAction(string $action_id): ?TableAction
69 {
70 return $this->actions[$action_id] ?? null;
71 }
72
73 public function perform(
74 URLBuilder $url_builder,
75 URLBuilderToken $row_id_token,
76 URLBuilderToken $action_token,
77 URLBuilderToken $action_type_token
78 ): ?Modal {
79 $selection_ids = $this->test_request->getMultiSelectionIds($row_id_token->getName());
80
81 $selection = $selection_ids === 'ALL_OBJECTS'
82 ? $this->repository->getForUser()
83 : $this->repository->getByIds($selection_ids);
84
85 if ($selection === []) {
86 return $this->fail('personal_settings_invalid_selection');
87 }
88
89 if (!$this->checkAccess($selection)) {
90 return $this->fail('no_permission');
91 }
92
93 $action = $this->getAction($this->test_request->strVal($action_token->getName()));
94 try {
95 $url_builder = $url_builder
96 ->withParameter($row_id_token, $selection_ids)
97 ->withParameter($action_token, $action->getActionId())
98 ->withParameter($action_type_token, self::SUBMIT_ACTION);
99
100 return match ($this->test_request->strVal($action_type_token->getName())) {
101 self::SUBMIT_ACTION => $this->submit(
102 $action,
103 $url_builder,
104 $selection,
105 ),
106 default => $this->showModal(
107 $action,
108 $url_builder,
109 $selection,
110 )
111 };
112 } catch (\InvalidArgumentException $e) {
113 return $this->fail($e->getMessage());
114 }
115 }
116
117 private function submit(
118 TableAction $action,
119 URLBuilder $url_builder,
120 array $selection,
121 ): ?Modal {
122 return $action->onSubmit(
123 $url_builder,
124 $this->test_request->getRequest(),
125 $selection,
126 );
127 }
128
129 private function showModal(
130 TableAction $action,
131 URLBuilder $url_builder,
132 array $selection,
133 ): null {
134 $this->test_response->sendAsync(
135 $this->ui_renderer->renderAsync(
136 $action->buildModal(
137 $url_builder,
138 $selection,
139 )
140 )
141 );
142 return null;
143 }
144
148 private function checkAccess(array $selection): bool
149 {
150 foreach ($selection as $template) {
151 if ($this->user->getId() !== $template->getUserId()) {
152 return false;
153 }
154 }
155 return true;
156 }
157
158 private function fail(string $message_key): null
159 {
160 $this->test_response->sendAsync(
161 $this->ui_renderer->renderAsync(
162 $this->ui_factory->messageBox()->failure($this->lng->txt($message_key))
163 )
164 );
165 return null;
166 }
167}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
getActions(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
perform(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
submit(TableAction $action, URLBuilder $url_builder, array $selection,)
showModal(TableAction $action, URLBuilder $url_builder, array $selection,)
__construct(private readonly RequestDataCollector $test_request, private readonly ResponseHandler $test_response, private readonly UIRenderer $ui_renderer, private readonly UIFactory $ui_factory, private readonly Language $lng, private readonly \ilObjUser $user, private readonly PersonalSettingsRepository $repository, private readonly array $actions)
getName()
Get the full name of the token including its namespace.
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter's value if the supplied token is valid.
Definition: URLBuilder.php:166
User class.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes commonalities between the different modals.
Definition: Modal.php:35
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:31