ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilAdvancedMDRecordTranslationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use Psr\Http\Message\RequestInterface;
22use ILIAS\HTTP\Services as HTTP;
23use ILIAS\Data\Factory as DataFactory;
24use ILIAS\UI\Factory as UIFactory;
25use ILIAS\UI\Renderer as UIRenderer;
26use ILIAS\Refinery\Factory as RefineryFactory;
31
36{
37 protected const string CMD_DEFAULT = 'translations';
38 protected const string CMD_TABLE_ACTION = 'handleTableAction';
39
40 protected ilCtrl $ctrl;
43 protected ilTabsGUI $tabs;
45 protected RequestInterface $request;
47 protected UIFactory $ui_factory;
48 protected UIRenderer $ui_renderer;
49 protected HTTP $http;
50 protected RefineryFactory $refinery;
51 protected DataFactory $data_factory;
53
55 {
56 global $DIC;
57
58 $this->ctrl = $DIC->ctrl();
59 $this->tpl = $DIC->ui()->mainTemplate();
60 $this->toolbar = $DIC->toolbar();
61 $this->tabs = $DIC->tabs();
62 $this->language = $DIC->language();
63 $this->language->loadLanguageModule('obj');
64 $this->request = $DIC->http()->request();
65 $this->record = $record;
66 $this->ui_factory = $DIC->ui()->factory();
67 $this->ui_renderer = $DIC->ui()->renderer();
68 $this->http = $DIC->http();
69 $this->refinery = $DIC->refinery();
70 $this->data_factory = new DataFactory();
72 $this->record->getRecordId()
73 );
74 }
75
79 public function executeCommand(): void
80 {
81 $this->ctrl->setParameterByClass(
82 strtolower(get_class($this)),
83 'record_id',
84 $this->record->getRecordId()
85 );
86
87 $next_class = $this->ctrl->getNextClass($this);
88 $cmd = $this->ctrl->getCmd(self::CMD_DEFAULT);
89
90 switch ($next_class) {
91 default:
92 switch ($cmd) {
94 $this->translations();
95 break;
97 $this->handleTableAction();
98 break;
99 }
100 break;
101 }
102 }
103
104 protected function setTabs(string $active_tab)
105 {
106 $this->tabs->activateTab($active_tab);
107 }
108
109 protected function translations(): void
110 {
111 $this->setTabs(self::CMD_DEFAULT);
112
113 $data_retrieval = new DataRetrieval(
114 $this->record,
115 $this->language,
116 $this->record_translations
117 );
118
119 $table_builder = new TableBuilder(
120 $data_retrieval,
121 $this->language,
122 $this->ui_factory,
123 $this->http
124 );
125
126 [$url_builder, $id_token, $action_token] = $this->getTableActionURLBuilder();
127 $table = $table_builder->get($url_builder, $id_token, $action_token);
128
129 $this->tpl->setContent($this->ui_renderer->render($table));
130 }
131
132 protected function getTableActionURLBuilder(): array
133 {
134 $link = ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
135 ilAdvancedMDRecordTranslationGUI::class,
136 $this::CMD_TABLE_ACTION
137 );
138 $url_builder = new URLBuilder($this->data_factory->uri($link));
139
140 return $url_builder->acquireParameters(['advmd_ml'], 'table_ids', 'table_action');
141 }
142
143 protected function getLanguageCodesFromQuery(URLBuilderToken $id_token, DataRetrieval $data_retrieval): array
144 {
145 $parameter_name = $id_token->getName();
146 $wrapper = $this->http->wrapper()->query();
147
148 $raw_ids = null;
149 if ($wrapper->has($parameter_name)) {
150 $raw_ids = $wrapper->retrieve(
151 $parameter_name,
152 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
153 );
154 }
155
156 if ($raw_ids === ['ALL_OBJECTS']) {
157 return $data_retrieval->getAllIDs();
158 }
159
160 return $raw_ids ?? [];
161 }
162
163 protected function getTableActionFromQuery(URLBuilderToken $action_token): string
164 {
165 $name = $action_token->getName();
166 $wrapper = $this->http->wrapper()->query();
167
168 if ($wrapper->has($name)) {
169 return (string) $wrapper->retrieve(
170 $name,
171 $this->refinery->kindlyTo()->string()
172 );
173 }
174
175 return '';
176 }
177
178 public function handleTableAction(): void
179 {
180 $data_retrieval = new DataRetrieval(
181 $this->record,
182 $this->language,
183 $this->record_translations
184 );
185 [$url_builder, $id_token, $action_token] = $this->getTableActionURLBuilder();
186 $ids = $this->getLanguageCodesFromQuery($id_token, $data_retrieval);
187 $action = $this->getTableActionFromQuery($action_token);
188
189 switch ($action) {
190 case TableBuilder::ACTION_ACTIVATE:
191 $this->activate($ids);
192 break;
193 case TableBuilder::ACTION_DEACTIVATE:
194 $this->deactivate($ids);
195 break;
196 case TableBuilder::ACTION_SET_DEFAULT:
197 $this->setDefault($ids);
198 break;
199 }
200 }
201
202
203 protected function activate(array $lang_keys): void
204 {
205 $record_id = $this->record->getRecordId();
207
208 foreach ($lang_keys as $lang_key) {
209 if (!$translations->isConfigured($lang_key)) {
210 $translations->addTranslationEntry($lang_key);
211 }
212 }
213
214 if (count($lang_keys) > 0) {
215 $this->tpl->setOnScreenMessage('success', $this->language->txt('settings_saved'), true);
216 }
217
218 $this->ctrl->redirect($this, self::CMD_DEFAULT);
219 }
220
221 protected function deactivate(array $lang_keys): void
222 {
223 $record_id = $this->record->getRecordId();
225 $success = false;
226
227 foreach ($lang_keys as $lang_key) {
228 if ($lang_key === $this->record->getDefaultLanguage()) {
229 $this->tpl->setOnScreenMessage('failure', $this->language->txt('md_adn_int_error_no_default'), true);
230 } else {
231 $translation = $translations->getTranslation($lang_key);
232 if ($translation) {
233 $translation->delete();
234 $success = true;
235 }
236 }
237 }
238
239 if ($success) {
240 $this->tpl->setOnScreenMessage('success', $this->language->txt('settings_saved'), true);
241 }
242
243 $this->ctrl->redirect($this, self::CMD_DEFAULT);
244 }
245
246 protected function setDefault(array $lang_keys): void
247 {
248 $lang_key = reset($lang_keys);
249
250 if (!$lang_key) {
251 $this->ctrl->redirect($this, self::CMD_DEFAULT);
252 }
253
254 $translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($this->record->getRecordId());
255 if (!$translations->isConfigured($lang_key)) {
256 $this->tpl->setOnScreenMessage('failure', $this->language->txt('err_check_input'), true);
257 $this->ctrl->redirect($this, self::CMD_DEFAULT);
258 }
259
260 $this->record->setDefaultLanguage($lang_key);
261 $this->record->update();
262
263 $this->tpl->setOnScreenMessage('success', $this->language->txt('settings_saved'), true);
264 $this->ctrl->redirect($this, self::CMD_DEFAULT);
265 }
266}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:43
getName()
Get the full name of the token including its namespace.
@ilCtrl_IsCalledBy ilAdvancedMDRecordTranslationGUI: ilAdvancedMDSettingsGUI
getLanguageCodesFromQuery(URLBuilderToken $id_token, DataRetrieval $data_retrieval)
executeCommand()
Execute command and save parameter record id.
Class ilCtrl provides processing control methods.
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An entity that renders components to a string output.
Definition: Renderer.php:31
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26