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