ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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->ctrl->redirectByClass($this->ctrl->getCurrentClassPath());
162 }
163
164 public function activateContentTranslation(): void
165 {
166 $data = $this->getActivateMultilingualityModal()
167 ->withRequest($this->http->request())
168 ->getData();
169
170 if ($data === null) {
171 return;
172 }
173
174 $this->translations = $this->translations->withLanguage(
175 new Language(
176 $data['lang'],
177 $this->object->getTitle(),
178 $this->object->getDescription(),
179 true,
180 true
181 )
182 );
183
184 $this->object->getObjectProperties()->storePropertyTranslations(
185 $this->translations
186 );
187 $this->listTranslations();
188 }
189
190 public function deactivateContentTranslation(): void
191 {
192 $this->translations = $this->translations->withDeactivatedContentTranslation();
193 $this->object->getObjectProperties()->storePropertyTranslations(
194 $this->translations
195 );
196
197 $this->tpl->setOnScreenMessage('success', $this->lng->txt('obj_cont_transl_deactivated'), true);
198 $this->listTranslations();
199 }
200
202 ?RoundtripModal $modal = null
203 ): RoundtripModal {
204 $modal ??= $this->getAddLanguageModal();
205 $this->toolbar->addComponent(
206 $this->ui_factory->button()->standard(
207 $this->lng->txt('obj_add_language'),
208 $modal->getShowSignal()
209 )
210 );
211
212 return $modal;
213 }
214
215 private function getAddLanguageModal(): RoundtripModal
216 {
217 $ff = $this->ui_factory->input()->field();
218 return $this->ui_factory->modal()->roundtrip(
219 $this->lng->txt('obj_add_language'),
220 null,
221 [
222 $ff->group([
223 'language' => $this->buildLangSelectionInput()
224 ->withRequired(true),
225 'title' => $ff->text($this->lng->txt('title'))
226 ->withRequired(true),
227 'description' => $ff->textarea($this->lng->txt('description'))
228 ])->withAdditionalTransformation(
229 $this->refinery->custom()->transformation(
230 static fn(array $vs): Language => new Language(
231 $vs['language'],
232 $vs['title'],
233 $vs['description']
234 )
235 )
236 )
237 ],
238 $this->ctrl->getFormActionByClass(self::class, self::CMD_ADD_TRANSLATION)
239 );
240 }
241
243 {
244 $lang_var_postfix = '_multilang';
245 $deactivation_modal_text_tag = 'obj_deactivate_multilang_conf';
246 if (!$this->force_content_translation) {
247 $lang_var_postfix = '_content_lang';
248 $deactivation_modal_text_tag = 'obj_deactivate_content_transl_conf';
249 }
250
251 if (!$this->force_content_translation && !$this->translations->getContentTranslationActivated()) {
252 $this->tpl->setOnScreenMessage('info', $this->lng->txt('obj_multilang_title_descr_only'));
253 }
254
255 if (!$this->translations->getContentTranslationActivated()) {
256 $activate_modal = $this->getActivateMultilingualityModal();
257 $this->toolbar->addComponent(
258 $this->ui_factory->button()->standard(
259 $this->lng->txt('obj_activate' . $lang_var_postfix),
260 $activate_modal->getShowSignal()
261 )
262 );
263 return $activate_modal;
264 }
265
266 $deactivate_modal = $this->getConfirmDeactivateMultilingualityModal($deactivation_modal_text_tag);
267 $this->toolbar->addComponent(
268 $this->ui_factory->button()->standard(
269 $this->lng->txt('obj_deactivate' . $lang_var_postfix),
270 $deactivate_modal->getShowSignal()
271 )
272 );
273 return $deactivate_modal;
274 }
275
276 private function getConfirmDeactivateMultilingualityModal(string $text_tag): Modal
277 {
278 return $this->ui_factory->modal()->interruptive(
279 $this->lng->txt('confirm'),
280 $this->lng->txt($text_tag),
281 $this->ctrl->getFormActionByClass(self::class, self::CMD_DEACTIVATE_CONTENT_MULTILANG)
282 )->withActionButtonLabel($this->lng->txt('confirm'));
283 }
284
285 private function getActivateMultilingualityModal(): RoundtripModal
286 {
287 return $this->ui_factory->modal()->roundtrip(
288 $this->lng->txt('confirm'),
289 $this->ui_factory->legacy()->content($this->lng->txt('obj_select_base_lang')),
290 [
291 'lang' => $this->getMasterLangSelectionInput()
292 ],
293 $this->ctrl->getFormActionByClass(self::class, self::CMD_SAVE_CONTENT_TRANSLATION_ACTIVATION)
294 );
295 }
296
297 private function buildLangSelectionInput(): SelectInput
298 {
299 return $this->ui_factory->input()->field()->select(
300 $this->lng->txt('language'),
301 $this->getArrayWithAddableLanguages()
302 );
303 }
304
306 {
307 $options = array_reduce(
308 $this->lng->getInstalledLanguages(),
309 function (array $c, string $v): array {
310 $c[$v] = $this->lng->txt("meta_l_{$v}");
311 return $c;
312 },
313 []
314 );
315
316 return $this->ui_factory->input()->field()->select(
317 $this->lng->txt('obj_base_lang'),
318 $options
320 $this->refinery->custom()->transformation(
321 fn($v) => in_array($v, array_keys($options)) ? $v : $this->lng->getDefaultLanguage()
322 )
323 )->withValue($this->user->getLanguage());
324 }
325
326 private function getArrayWithAddableLanguages(): array
327 {
328 $enabled_langs = $this->translations->getLanguages();
329 return array_reduce(
330 $this->lng->getInstalledLanguages(),
331 function (array $c, string $v) use ($enabled_langs): array {
332 if (!array_key_exists($v, $enabled_langs)) {
333 $c[$v] = $this->lng->txt("meta_l_{$v}");
334 }
335 return $c;
336 },
337 []
338 );
339 }
340}
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
withAdditionalTransformation(Transformation $trafo)
Apply a transformation to the content of the input.
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:31