ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMultilingualismGUI.php
Go to the documentation of this file.
1<?php
2
20
27{
28 protected \ILIAS\DidacticTemplate\Multilingualism\StandardGUIRequest $request;
29 protected ilLanguage $lng;
30 protected ilCtrl $ctrl;
33 protected ilObjUser $user;
34 protected LOMServices $lom_services;
36 protected bool $title_descr_only = true;
37 protected string $start_title = "";
38 protected string $start_description = "";
39
40 public function __construct(
41 int $a_obj_id,
42 string $a_type
43 ) {
44 global $DIC;
45
46 $this->toolbar = $DIC->toolbar();
47 $this->user = $DIC->user();
48 $this->lng = $DIC->language();
49 $this->lng->loadLanguageModule('obj');
50 $this->ctrl = $DIC->ctrl();
51 $this->tpl = $DIC->ui()->mainTemplate();
52 $this->lom_services = $DIC->learningObjectMetadata();
53
54 $this->obj_trans = ilMultilingualism::getInstance($a_obj_id, $a_type);
55 $this->request = new \ILIAS\Multilingualism\StandardGUIRequest(
56 $DIC->http(),
57 $DIC->refinery()
58 );
59 }
60
64 public function setTitleDescrOnlyMode(bool $a_val): void
65 {
66 $this->title_descr_only = $a_val;
67 }
68
72 public function getTitleDescrOnlyMode(): bool
73 {
75 }
76
77 public function executeCommand(): void
78 {
79 $next_class = $this->ctrl->getNextClass($this);
80
81 switch ($next_class) {
82 default:
83 $cmd = $this->ctrl->getCmd("listTranslations");
84 if (in_array($cmd, array("listTranslations", "saveTranslations",
85 "addTranslation", "deleteTranslations", "activateContentMultilinguality",
86 "confirmRemoveLanguages", "removeLanguages", "confirmDeactivateContentMultiLang", "saveLanguages",
87 "saveContentTranslationActivation", "deactivateContentMultiLang", "addLanguages"))) {
88 $this->$cmd();
89 }
90 break;
91 }
92 }
93
94 public function listTranslations(
95 bool $a_get_post_values = false,
96 bool $a_add = false
97 ): void {
98 $this->lng->loadLanguageModule("translation");
99
100
101 $this->addToolbar();
102
103 $titles = $this->request->getTitles();
104 $langs = $this->request->getLanguages();
105 $descs = $this->request->getDescriptions();
106 $default = $this->request->getDefault();
107
108 $table = new ilMultilingualismTableGUI(
109 $this,
110 "listTranslations",
111 true,
112 "Translation"
113 );
114 if ($a_get_post_values) {
115 $vals = array();
116 foreach ($titles as $k => $v) {
117 $vals[] = array("title" => $v,
118 "desc" => $descs[$k],
119 "lang" => $langs[$k],
120 "default" => ($default == $k));
121 }
122 $table->setData($vals);
123 } else {
124 $k = 0;
125 $data = $this->obj_trans->getLanguages();
126 foreach ($data as $k => $v) {
127 $data[$k]["default"] = $v["lang_default"];
128 $data[$k]["desc"] = $v["description"];
129 $data[$k]["lang"] = $v["lang_code"];
130 }
131 if ($a_add) {
132 $data["Fobject"][++$k]["title"] = "";
133 }
134 $table->setData($data);
135 }
136 $this->tpl->setContent($table->getHTML());
137 }
138
139 public function addToolbar(): void
140 {
141 $ilToolbar = $this->toolbar;
142 if ($this->getTitleDescrOnlyMode()) {
143 $ilToolbar->addButton(
144 $this->lng->txt("obj_add_languages"),
145 $this->ctrl->getLinkTarget($this, "addLanguages")
146 );
147 }
148 }
149
150 public function saveTranslations(bool $delete_checked = false): void
151 {
152 $default = $this->request->getDefault();
153 $langs = $this->request->getLanguages();
154 $titles = $this->request->getTitles();
155 $descs = $this->request->getDescriptions();
156
157 // default language set?
158 if ($default === "") {
159 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_no_default_language"));
160 $this->listTranslations(true);
161 return;
162 }
163
164 // all languages set?
165 if (array_key_exists("", $langs)) {
166 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_no_language_selected"));
167 $this->listTranslations(true);
168 return;
169 }
170
171 // no single language is selected more than once?
172 if (count(array_unique($langs)) < count($langs)) {
173 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_multi_language_selected"));
174 $this->listTranslations(true);
175 return;
176 }
177
178 $check = $this->request->getCheck();
179
180 // save the stuff
181 $this->obj_trans->setLanguages(array());
182
183 foreach ($titles as $k => $v) {
184 // update object data if default
185 $is_default = ($default == $k);
186 if ($delete_checked && !$is_default && isset($check[$k])) {
187 continue;
188 }
189
190 $this->obj_trans->addLanguage(
191 $langs[$k],
192 $v,
193 $descs[$k],
194 $is_default
195 );
196 }
197 $this->obj_trans->save();
198
199 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
200 $this->ctrl->redirect($this, "listTranslations");
201 }
202
203 public function deleteTranslations(): void
204 {
205 $default = $this->request->getDefault();
206 $langs = $this->request->getLanguages();
207 $titles = $this->request->getTitles();
208 $descs = $this->request->getDescriptions();
209 $check = $this->request->getCheck();
210 foreach ($titles as $k => $v) {
211 if ($check[$k] ?? false) {
212 // default translation cannot be deleted
213 if ($k != $default) {
214 unset($titles[$k], $descs[$k], $langs[$k]);
215 } else {
216 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("msg_no_default_language"));
217 $this->listTranslations();
218 return;
219 }
220 }
221 }
222 $this->saveTranslations(true);
223 }
224
228
232 public function getMultiLangForm(bool $a_add = false): ilPropertyFormGUI
233 {
235 $ilCtrl = $this->ctrl;
236 $ilUser = $this->user;
237
238 $form = new ilPropertyFormGUI();
239
240 $options = [];
241 foreach ($this->lom_services->dataHelper()->getAllLanguages() as $language) {
242 $options[$language->value()] = $language->presentableLabel();
243 }
244
245 // master language
246 if (!$a_add) {
247 $si = new ilSelectInputGUI($lng->txt("obj_base_lang"), "master_lang");
248 $si->setOptions($options);
249 $si->setValue($ilUser->getLanguage());
250 $form->addItem($si);
251 }
252
253 // additional languages
254 if ($a_add) {
255 $options = array("" => $lng->txt("please_select")) + $options;
256 $si = new ilSelectInputGUI($lng->txt("obj_additional_langs"), "additional_langs");
257 $si->setOptions($options);
258 $si->setMulti(true);
259 $form->addItem($si);
260 }
261
262 if ($a_add) {
263 $form->setTitle($lng->txt("obj_add_languages"));
264 $form->addCommandButton("saveLanguages", $lng->txt("save"));
265 } else {
266 if ($this->getTitleDescrOnlyMode()) {
267 $form->setTitle($lng->txt("obj_activate_content_lang"));
268 } else {
269 $form->setTitle($lng->txt("obj_activate_multilang"));
270 }
271 $form->addCommandButton("saveContentTranslationActivation", $lng->txt("save"));
272 }
273 $form->addCommandButton("listTranslations", $lng->txt("cancel"));
274 $form->setFormAction($ilCtrl->getFormAction($this));
275
276 return $form;
277 }
278
282 public function confirmDeactivateContentMultiLang(): void
283 {
284 $ilCtrl = $this->ctrl;
285 $tpl = $this->tpl;
287
288 $cgui = new ilConfirmationGUI();
289 $cgui->setFormAction($ilCtrl->getFormAction($this));
290 if ($this->getTitleDescrOnlyMode()) {
291 $cgui->setHeaderText($lng->txt("obj_deactivate_content_transl_conf"));
292 } else {
293 $cgui->setHeaderText($lng->txt("obj_deactivate_multilang_conf"));
294 }
295
296 $cgui->setCancel($lng->txt("cancel"), "listTranslations");
297 $cgui->setConfirm($lng->txt("confirm"), "deactivateContentMultiLang");
298 $tpl->setContent($cgui->getHTML());
299 }
300
304 public function addLanguages(?ilPropertyFormGUI $form = null): void
305 {
306 $tpl = $this->tpl;
307
308 if (!$form instanceof ilPropertyFormGUI) {
309 $form = $this->getMultiLangForm(true);
310 }
311 $tpl->setContent($form->getHTML());
312 }
313
314 public function saveLanguages(): void
315 {
316 $ilCtrl = $this->ctrl;
318
319 ilLoggerFactory::getLogger('otpl')->debug('Save languages');
320
321 $form = $this->getMultiLangForm(true);
322 if ($form->checkInput()) {
323 $ad = $form->getInput("additional_langs");
324
325 ilLoggerFactory::getLogger('otpl')->dump($ad);
326
327 if (is_array($ad)) {
328 foreach ($ad as $l) {
329 if ($l != "") {
330 $std = false;
331
332 //if no other language is set, set this one as standard
333 if (!count($this->obj_trans->getLanguages())) {
334 $std = true;
335 }
336
337 $this->obj_trans->addLanguage($l, $this->start_title, $this->start_description, $std);
338 }
339 }
340 }
341 $this->obj_trans->save();
342 $this->tpl->setOnScreenMessage('info', $lng->txt("msg_obj_modified"), true);
343 $ilCtrl->redirect($this, "listTranslations");
344 } else {
345 $form->setValuesByPost();
346 $this->tpl->setOnScreenMessage('failure', $lng->txt('err_check_input'));
347 $this->addLanguages($form);
348 }
349 }
350
354 public function confirmRemoveLanguages(): void
355 {
356 $ilCtrl = $this->ctrl;
357 $tpl = $this->tpl;
359
360 $lng->loadLanguageModule("meta");
361 $langs = $this->request->getLanguages();
362
363 if (count($langs) === 0) {
364 $this->tpl->setOnScreenMessage('info', $lng->txt("no_checkbox"), true);
365 $ilCtrl->redirect($this, "listTranslations");
366 } else {
367 $cgui = new ilConfirmationGUI();
368 $cgui->setFormAction($ilCtrl->getFormAction($this));
369 $cgui->setHeaderText($lng->txt("obj_conf_delete_lang"));
370 $cgui->setCancel($lng->txt("cancel"), "listTranslations");
371 $cgui->setConfirm($lng->txt("remove"), "removeLanguages");
372
373 foreach ($langs as $i) {
374 $cgui->addItem("lang[]", $i, $lng->txt("meta_l_" . $i));
375 }
376
377 $tpl->setContent($cgui->getHTML());
378 }
379 }
380
384 public function removeLanguages(): void
385 {
387 $ilCtrl = $this->ctrl;
388
389 $post_langs = $this->request->getLanguages();
390
391 if (count($post_langs) > 0) {
392 $langs = $this->obj_trans->getLanguages();
393 foreach ($langs as $k => $l) {
394 if (in_array($l, $post_langs)) {
395 $this->obj_trans->removeLanguage($l);
396 }
397 }
398 $this->obj_trans->save();
399 $this->tpl->setOnScreenMessage('info', $lng->txt("msg_obj_modified"), true);
400 }
401 $ilCtrl->redirect($this, "listTranslations");
402 }
403
404 public function setStartValues(
405 string $a_title,
406 string $a_description
407 ): void {
408 $this->start_title = $a_title;
409 $this->start_description = $a_description;
410 }
411}
$check
Definition: buildRTE.php:81
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilCtrl provides processing control methods.
language handling
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setStartValues(string $a_title, string $a_description)
listTranslations(bool $a_get_post_values=false, bool $a_add=false)
confirmDeactivateContentMultiLang()
Confirm page translation creation.
ILIAS DidacticTemplate Multilingualism StandardGUIRequest $request
setTitleDescrOnlyMode(bool $a_val)
Set enable title/description only mode.
confirmRemoveLanguages()
Confirm remove languages.
getMultiLangForm(bool $a_add=false)
Get multi language form.
__construct(int $a_obj_id, string $a_type)
saveTranslations(bool $delete_checked=false)
addLanguages(?ilPropertyFormGUI $form=null)
Add language.
getTitleDescrOnlyMode()
Get enable title/description only mode.
ilGlobalTemplateInterface $tpl
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...
static getInstance(int $a_obj_id, string $a_type)
User class.
This class represents a property form user interface.
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setContent(string $a_html)
Sets content for standard template.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26