ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.TranslationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\UI\Factory as UIFactory;
24use ILIAS\UI\Renderer as UIRenderer;
25use ILIAS\HTTP\Services as HTTPService;
26use ILIAS\Refinery\Factory as Refinery;
30use ILIAS\UI\Component\Modal\RoundTrip as RoundtripModal;
31use ILIAS\Data\Factory as DataFactory;
32
39{
40 private const CMD_LIST_TRANSLATIONS = 'listTranslations';
41 private const CMD_ADD_TRANSLATION = 'addTranslation';
42 private const CMD_DEACTIVATE_CONTENT_MULTILANG = 'deactivateContentTranslation';
43 private const CMD_SAVE_CONTENT_TRANSLATION_ACTIVATION = 'activateContentTranslation';
44
46
47 private bool $force_content_translation = false;
48 private bool $supports_content_translation = true;
49
50 public function __construct(
51 private readonly \ilObject $object,
52 private readonly \ilLanguage $lng,
53 private readonly \ilAccess $access,
54 private readonly \ilObjUser $user,
55 private readonly \ilCtrl $ctrl,
56 private readonly \ilGlobalTemplateInterface $tpl,
57 private readonly UIFactory $ui_factory,
58 private readonly UIRenderer $ui_renderer,
59 private readonly HTTPService $http,
60 private readonly Refinery $refinery,
61 private readonly \ilToolbarGUI $toolbar
62 ) {
63 $this->translations = $this->object->getObjectProperties()->getPropertyTranslations();
64 }
65
66 public function supportContentTranslation(bool $content_translation): void
67 {
68 $this->supports_content_translation = $content_translation;
69 }
70
78 public function forceContentTranslation(): void
79 {
80 $this->force_content_translation = true;
81 }
82
83 public function executeCommand(): void
84 {
85 $commands = [
90 ];
91
92 $this->ctrl->getNextClass($this);
93 $cmd = $this->ctrl->getCmd(self::CMD_LIST_TRANSLATIONS);
94 if (!$this->access->checkAccess('write', '', $this->object->getRefId())) {
95 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_permission'));
96 $this->ctrl->redirectByClass(get_class($this->object) . 'GUI');
97 }
98 if (in_array($cmd, $commands)) {
99 $this->$cmd();
100 }
101 }
102
103 public function listTranslations(
104 ?RoundtripModal $lang_modal = null
105 ): void {
106 $this->lng->loadLanguageModule($this->object->getType());
107
108 $table = new TranslationsTable(
109 $this->ui_factory,
110 $this->ui_renderer,
111 $this->lng,
112 $this->refinery,
113 $this->tpl,
114 $this->http,
115 $this->ctrl,
116 $this->translations,
117 $this->object->getObjectProperties(),
118 (new DataFactory())->uri(
119 ILIAS_HTTP_PATH . '/' . $this->ctrl->getFormActionByClass(
120 self::class,
121 self::CMD_LIST_TRANSLATIONS
122 )
123 )
124 );
125 $table->runAction();
126 $content = [
127 'table' => $table->getTable()
128 ];
129
130 if ($this->translations->migrationMissing()) {
131 $this->tpl->setOnScreenMessage('info', $this->lng->txt('missing_migration'));
132 $this->tpl->setContent($this->ui_renderer->render($content));
133 return;
134 }
135
136 if ($this->getArrayWithAddableLanguages() !== []) {
137 $content['lang_modal'] = $this->addAddLanguagesToolbarActionAndRetrieveModal($lang_modal);
138 }
139
140 if ($this->supports_content_translation) {
141 $content['content_trans_modal'] = $this->addContentTranslationToolbarActionAndRetrieveModal();
142 }
143
144 $this->tpl->setContent($this->ui_renderer->render($content));
145 }
146
147 public function addTranslation(): void
148 {
149 $modal = $this->getAddLanguageModal()
150 ->withRequest($this->http->request());
151 $data = $modal->getData();
152 if ($data === null) {
153 $this->listTranslations($modal->withOnLoad($modal->getShowSignal()));
154 return;
155 }
156
157 $this->translations = $this->translations->withLanguage($data[0]);
158 $this->object->getObjectProperties()->storePropertyTranslations(
159 $this->translations
160 );
161 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
162 $this->ctrl->redirectByClass($this->ctrl->getCurrentClassPath());
163 }
164
165 public function activateContentTranslation(): void
166 {
167 $data = $this->getActivateMultilingualityModal()
168 ->withRequest($this->http->request())
169 ->getData();
170
171 if ($data === null) {
172 return;
173 }
174
175 $this->translations = $this->translations->withLanguage(
176 new Language(
177 $data['lang'],
178 $this->object->getTitle(),
179 $this->object->getDescription(),
180 true,
181 true
182 )
183 );
184
185 $this->object->getObjectProperties()->storePropertyTranslations(
186 $this->translations
187 );
188 $this->listTranslations();
189 }
190
191 public function deactivateContentTranslation(): void
192 {
193 $this->translations = $this->translations->withDeactivatedContentTranslation();
194 $this->object->getObjectProperties()->storePropertyTranslations(
195 $this->translations
196 );
197
198 $this->tpl->setOnScreenMessage('success', $this->lng->txt('obj_cont_transl_deactivated'), true);
199 $this->listTranslations();
200 }
201
203 ?RoundtripModal $modal = null
204 ): RoundtripModal {
205 $modal ??= $this->getAddLanguageModal();
206 $this->toolbar->addComponent(
207 $this->ui_factory->button()->standard(
208 $this->lng->txt('obj_add_language'),
209 $modal->getShowSignal()
210 )
211 );
212
213 return $modal;
214 }
215
216 private function getAddLanguageModal(): RoundtripModal
217 {
218 $ff = $this->ui_factory->input()->field();
219 return $this->ui_factory->modal()->roundtrip(
220 $this->lng->txt('obj_add_language'),
221 null,
222 [
223 $ff->group([
224 'language' => $this->buildLangSelectionInput()
225 ->withRequired(true),
226 'title' => $ff->text($this->lng->txt('title'))
227 ->withRequired(true),
228 'description' => $ff->textarea($this->lng->txt('description'))
229 ])->withAdditionalTransformation(
230 $this->refinery->custom()->transformation(
231 static fn(array $vs): Language => new Language(
232 $vs['language'],
233 $vs['title'],
234 $vs['description']
235 )
236 )
237 )
238 ],
239 $this->ctrl->getFormActionByClass(self::class, self::CMD_ADD_TRANSLATION)
240 );
241 }
242
244 {
245 $lang_var_postfix = '_multilang';
246 $deactivation_modal_text_tag = 'obj_deactivate_multilang_conf';
247 if (!$this->force_content_translation) {
248 $lang_var_postfix = '_content_lang';
249 $deactivation_modal_text_tag = 'obj_deactivate_content_transl_conf';
250 }
251
252 if (!$this->force_content_translation && !$this->translations->getContentTranslationActivated()) {
253 $this->tpl->setOnScreenMessage('info', $this->lng->txt('obj_multilang_title_descr_only'));
254 }
255
256 if (!$this->translations->getContentTranslationActivated()) {
257 $activate_modal = $this->getActivateMultilingualityModal();
258 $this->toolbar->addComponent(
259 $this->ui_factory->button()->standard(
260 $this->lng->txt('obj_activate' . $lang_var_postfix),
261 $activate_modal->getShowSignal()
262 )
263 );
264 return $activate_modal;
265 }
266
267 $deactivate_modal = $this->getConfirmDeactivateMultilingualityModal($deactivation_modal_text_tag);
268 $this->toolbar->addComponent(
269 $this->ui_factory->button()->standard(
270 $this->lng->txt('obj_deactivate' . $lang_var_postfix),
271 $deactivate_modal->getShowSignal()
272 )
273 );
274 return $deactivate_modal;
275 }
276
277 private function getConfirmDeactivateMultilingualityModal(string $text_tag): Modal
278 {
279 return $this->ui_factory->modal()->interruptive(
280 $this->lng->txt('confirm'),
281 $this->lng->txt($text_tag),
282 $this->ctrl->getFormActionByClass(self::class, self::CMD_DEACTIVATE_CONTENT_MULTILANG)
283 )->withActionButtonLabel($this->lng->txt('confirm'));
284 }
285
286 private function getActivateMultilingualityModal(): RoundtripModal
287 {
288 return $this->ui_factory->modal()->roundtrip(
289 $this->lng->txt('confirm'),
290 $this->ui_factory->legacy()->content($this->lng->txt('obj_select_base_lang')),
291 [
292 'lang' => $this->getMasterLangSelectionInput()
293 ],
294 $this->ctrl->getFormActionByClass(self::class, self::CMD_SAVE_CONTENT_TRANSLATION_ACTIVATION)
295 );
296 }
297
298 private function buildLangSelectionInput(): SelectInput
299 {
300 return $this->ui_factory->input()->field()->select(
301 $this->lng->txt('language'),
302 $this->getArrayWithAddableLanguages()
303 );
304 }
305
307 {
308 $options = array_reduce(
309 $this->lng->getInstalledLanguages(),
310 function (array $c, string $v): array {
311 $c[$v] = $this->lng->txt("meta_l_{$v}");
312 return $c;
313 },
314 []
315 );
316
317 return $this->ui_factory->input()->field()->select(
318 $this->lng->txt('obj_base_lang'),
320 )->withAdditionalTransformation(
321 $this->refinery->custom()->transformation(
322 fn($v) => in_array($v, array_keys($options)) ? $v : $this->lng->getDefaultLanguage()
323 )
324 )->withValue($this->user->getLanguage());
325 }
326
327 private function getArrayWithAddableLanguages(): array
328 {
329 $enabled_langs = $this->translations->getLanguages();
330 return array_reduce(
331 $this->lng->getInstalledLanguages(),
332 function (array $c, string $v) use ($enabled_langs): array {
333 if (!array_key_exists($v, $enabled_langs)) {
334 $c[$v] = $this->lng->txt("meta_l_{$v}");
335 }
336 return $c;
337 },
338 []
339 );
340 }
341}
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:38
forceContentTranslation()
Some objects like learning modules do not support to translate only the title and the description.
__construct(private readonly \ilObject $object, private readonly \ilLanguage $lng, private readonly \ilAccess $access, private readonly \ilObjUser $user, private readonly \ilCtrl $ctrl, private readonly \ilGlobalTemplateInterface $tpl, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly HTTPService $http, private readonly Refinery $refinery, private readonly \ilToolbarGUI $toolbar)
Class handles translation mode for an object.
Class ilAccessHandler Checks access for ILIAS objects.
Class ilCtrl provides processing control methods.
language handling
User class.
Class ilObject Basic functions for all objects.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$http
Definition: deliver.php:30
$c
Definition: deliver.php:25
This describes select field.
Definition: Select.php:29
This describes commonalities between all inputs.
Definition: Input.php:47
This describes commonalities between the different modals.
Definition: Modal.php:35
An entity that renders components to a string output.
Definition: Renderer.php:31
static http()
Fetches the global http state from ILIAS.
global $lng
Definition: privfeed.php:26