1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * GUI class for object translation handling.
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 * @version $Id$
10 * @ingroup ServicesObject
11 */
12class ilObjectTranslationGUI
13{
14    /**
15     * @var ilLanguage
16     */
17    protected $lng;
18
19    /**
20     * @var ilCtrl
21     */
22    protected $ctrl;
23
24    /**
25     * @var ilTemplate
26     */
27    protected $tpl;
28
29    /**
30     * @var ilToolbarGUI
31     */
32    protected $toolbar;
33
34    /**
35     * @var ilObjUser
36     */
37    protected $user;
38
39    protected $obj_trans;
40    protected $title_descr_only = true;
41
42    /**
43     * Constructor
44     */
45    public function __construct($a_obj_gui)
46    {
47        global $DIC;
48
49        $this->toolbar = $DIC->toolbar();
50        $this->user = $DIC->user();
51        $lng = $DIC->language();
52        $ilCtrl = $DIC->ctrl();
53        $tpl = $DIC["tpl"];
54
55        $this->lng = $lng;
56        $this->ctrl = $ilCtrl;
57        $this->tpl = $tpl;
58        $this->obj_gui = $a_obj_gui;
59        $this->obj = $a_obj_gui->object;
60
61        include_once("./Services/Object/classes/class.ilObjectTranslation.php");
62        $this->obj_trans = ilObjectTranslation::getInstance($this->obj->getId());
63    }
64
65    /**
66     * Set enable title/description only mode
67     *
68     * @param bool $a_val enable title/description only mode
69     */
70    public function setTitleDescrOnlyMode($a_val)
71    {
72        $this->title_descr_only = $a_val;
73    }
74
75    /**
76     * Get enable title/description only mode
77     *
78     * @return bool enable title/description only mode
79     */
80    public function getTitleDescrOnlyMode()
81    {
82        return $this->title_descr_only;
83    }
84
85    /**
86     * Execute command
87     */
88    public function executeCommand()
89    {
90        $next_class = $this->ctrl->getNextClass($this);
91
92        switch ($next_class) {
93            default:
94                $cmd = $this->ctrl->getCmd("listTranslations");
95                if (in_array($cmd, array("listTranslations", "saveTranslations",
96                    "addTranslation", "deleteTranslations", "activateContentMultilinguality",
97                    "confirmRemoveLanguages", "removeLanguages", "confirmDeactivateContentMultiLang", "saveLanguages",
98                    "saveContentTranslationActivation", "deactivateContentMultiLang", "addLanguages"))) {
99                    $this->$cmd();
100                }
101                break;
102        }
103    }
104
105    /**
106     * List translations
107     */
108    public function listTranslations($a_get_post_values = false, $a_add = false)
109    {
110        $ilToolbar = $this->toolbar;
111
112        $this->lng->loadLanguageModule(ilObject::_lookupType($this->obj->getId()));
113
114
115        if ($this->getTitleDescrOnlyMode() || $this->obj_trans->getContentActivated()) {
116            $ilToolbar->addButton(
117                $this->lng->txt("obj_add_languages"),
118                $this->ctrl->getLinkTarget($this, "addLanguages")
119            );
120        }
121
122        if ($this->getTitleDescrOnlyMode()) {
123            if (!$this->obj_trans->getContentActivated()) {
124                ilUtil::sendInfo($this->lng->txt("obj_multilang_title_descr_only"));
125                $ilToolbar->addButton(
126                    $this->lng->txt("obj_activate_content_lang"),
127                    $this->ctrl->getLinkTarget($this, "activateContentMultilinguality")
128                );
129            } else {
130                $ilToolbar->addButton(
131                    $this->lng->txt("obj_deactivate_content_lang"),
132                    $this->ctrl->getLinkTarget($this, "confirmDeactivateContentMultiLang")
133                );
134            }
135        } else {
136            if ($this->obj_trans->getContentActivated()) {
137                $ilToolbar->addButton(
138                    $this->lng->txt("obj_deactivate_multilang"),
139                    $this->ctrl->getLinkTarget($this, "confirmDeactivateContentMultiLang")
140                );
141            } else {
142                $ilToolbar->addButton(
143                    $this->lng->txt("obj_activate_multilang"),
144                    $this->ctrl->getLinkTarget($this, "activateContentMultilinguality")
145                );
146                return;
147            }
148        }
149
150        include_once("./Services/Object/classes/class.ilObjectTranslation2TableGUI.php");
151        $table = new ilObjectTranslation2TableGUI(
152            $this,
153            "listTranslations",
154            true,
155            "Translation",
156            $this->obj_trans->getMasterLanguage()
157        );
158        if ($a_get_post_values) {
159            $vals = array();
160            foreach ($_POST["title"] as $k => $v) {
161                $vals[] = array("title" => $v,
162                    "desc" => $_POST["desc"][$k],
163                    "lang" => $_POST["lang"][$k],
164                    "default" => ($_POST["default"] == $k));
165            }
166            $table->setData($vals);
167        } else {
168            $data = $this->obj_trans->getLanguages();
169            foreach ($data as $k => $v) {
170                $data[$k]["default"] = $v["lang_default"];
171                $data[$k]["desc"] = $v["description"];
172                $data[$k]["lang"] = $v["lang_code"];
173            }
174            /*			if($a_add)
175                        {
176                            $data["Fobject"][++$k]["title"] = "";
177                        }*/
178            $table->setData($data);
179        }
180        $this->tpl->setContent($table->getHTML());
181    }
182
183    /**
184     * Save translations
185     */
186    public function saveTranslations()
187    {
188        // default language set?
189        if (!isset($_POST["default"]) && $this->obj_trans->getMasterLanguage() == "") {
190            ilUtil::sendFailure($this->lng->txt("msg_no_default_language"));
191            $this->listTranslations(true);
192            return;
193        }
194
195        // all languages set?
196        if (array_key_exists("", $_POST["lang"])) {
197            ilUtil::sendFailure($this->lng->txt("msg_no_language_selected"));
198            $this->listTranslations(true);
199            return;
200        }
201
202        // no single language is selected more than once?
203        if (count(array_unique($_POST["lang"])) < count($_POST["lang"])) {
204            ilUtil::sendFailure($this->lng->txt("msg_multi_language_selected"));
205            $this->listTranslations(true);
206            return;
207        }
208
209        // save the stuff
210        $this->obj_trans->setLanguages(array());
211
212        foreach ($_POST["title"] as $k => $v) {
213            // update object data if default
214            $is_default = ($_POST["default"] == $k);
215
216            // ensure master language is set as default
217            if ($this->obj_trans->getMasterLanguage() != "") {
218                $is_default = ($this->obj_trans->getMasterLanguage() == $_POST["lang"][$k]);
219            }
220            if ($is_default) {
221                $this->obj->setTitle(ilUtil::stripSlashes($v));
222                $this->obj->setDescription(ilUtil::stripSlashes($_POST["desc"][$k]));
223            }
224
225            $this->obj_trans->addLanguage(
226                ilUtil::stripSlashes($_POST["lang"][$k]),
227                ilUtil::stripSlashes($v),
228                ilUtil::stripSlashes($_POST["desc"][$k]),
229                $is_default
230            );
231        }
232        $this->obj_trans->save();
233        if (method_exists($this->obj, "setObjectTranslation")) {
234            $this->obj->setObjectTranslation($this->obj_trans);
235        }
236        $this->obj->update();
237
238        ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
239        $this->ctrl->redirect($this, "listTranslations");
240    }
241
242    /**
243     * Remove translation
244     */
245    public function deleteTranslations()
246    {
247        foreach ($_POST["title"] as $k => $v) {
248            if ($_POST["check"][$k]) {
249                // default translation cannot be deleted
250                if ($k != $_POST["default"]) {
251                    unset($_POST["title"][$k]);
252                    unset($_POST["desc"][$k]);
253                    unset($_POST["lang"][$k]);
254                } else {
255                    ilUtil::sendFailure($this->lng->txt("msg_no_default_language"));
256                    $this->listTranslations();
257                    return;
258                }
259            }
260        }
261        $this->saveTranslations();
262    }
263
264    ////
265    //// Content translation
266    ////
267
268    /**
269     * Activate multi language (-> master language selection)
270     */
271    public function activateContentMultilinguality()
272    {
273        $tpl = $this->tpl;
274        $lng = $this->lng;
275
276        ilUtil::sendInfo($lng->txt("obj_select_master_lang"));
277
278        $form = $this->getMultiLangForm();
279        $tpl->setContent($form->getHTML());
280    }
281
282    /**
283     * Get multi language form
284     */
285    public function getMultiLangForm($a_add = false)
286    {
287        $tpl = $this->tpl;
288        $lng = $this->lng;
289        $ilCtrl = $this->ctrl;
290        $ilUser = $this->user;
291
292        include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
293        $form = new ilPropertyFormGUI();
294
295        // master language
296        if (!$a_add) {
297            include_once("./Services/MetaData/classes/class.ilMDLanguageItem.php");
298            $options = ilMDLanguageItem::_getLanguages();
299            $si = new ilSelectInputGUI($lng->txt("obj_master_lang"), "master_lang");
300            $si->setOptions($options);
301            $si->setValue($ilUser->getLanguage());
302            $form->addItem($si);
303        }
304
305        // additional languages
306        if ($a_add) {
307            include_once("./Services/MetaData/classes/class.ilMDLanguageItem.php");
308            $options = ilMDLanguageItem::_getLanguages();
309            $options = array("" => $lng->txt("please_select")) + $options;
310            $si = new ilSelectInputGUI($lng->txt("obj_additional_langs"), "additional_langs");
311            $si->setOptions($options);
312            $si->setMulti(true);
313            $form->addItem($si);
314        }
315
316        if ($a_add) {
317            $form->setTitle($lng->txt("obj_add_languages"));
318            $form->addCommandButton("saveLanguages", $lng->txt("save"));
319            $form->addCommandButton("listTranslations", $lng->txt("cancel"));
320        } else {
321            if ($this->getTitleDescrOnlyMode()) {
322                $form->setTitle($lng->txt("obj_activate_content_lang"));
323            } else {
324                $form->setTitle($lng->txt("obj_activate_multilang"));
325            }
326            $form->addCommandButton("saveContentTranslationActivation", $lng->txt("save"));
327            $form->addCommandButton("listTranslations", $lng->txt("cancel"));
328        }
329        $form->setFormAction($ilCtrl->getFormAction($this));
330
331        return $form;
332    }
333
334    /**
335     * Save content translation activation
336     */
337    public function saveContentTranslationActivation()
338    {
339        $ilCtrl = $this->ctrl;
340
341        //		include_once("./Services/COPage/classes/class.ilPageMultiLang.php");
342
343        $form = $this->getMultiLangForm();
344        if ($form->checkInput()) {
345            $ml = $form->getInput("master_lang");
346            $this->obj_trans->setMasterLanguage($ml);
347            $this->obj_trans->addLanguage(
348                $ml,
349                $this->obj->getTitle(),
350                $this->obj->getDescription(),
351                true
352            );
353            $this->obj_trans->setDefaultTitle($this->obj->getTitle());
354            $this->obj_trans->setDefaultDescription($this->obj->getDescription());
355            $this->obj_trans->save();
356        }
357
358        $ilCtrl->redirect($this, "listTranslations");
359    }
360
361    /**
362     * Confirm page translation creation
363     */
364    public function confirmDeactivateContentMultiLang()
365    {
366        $ilCtrl = $this->ctrl;
367        $tpl = $this->tpl;
368        $lng = $this->lng;
369
370        include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
371        $cgui = new ilConfirmationGUI();
372        $cgui->setFormAction($ilCtrl->getFormAction($this));
373        if ($this->getTitleDescrOnlyMode()) {
374            $cgui->setHeaderText($lng->txt("obj_deactivate_content_transl_conf"));
375        } else {
376            $cgui->setHeaderText($lng->txt("obj_deactivate_multilang_conf"));
377        }
378
379        $cgui->setCancel($lng->txt("cancel"), "listTranslations");
380        $cgui->setConfirm($lng->txt("confirm"), "deactivateContentMultiLang");
381        $tpl->setContent($cgui->getHTML());
382    }
383
384    /**
385     * Deactivate multilanguage
386     */
387    public function deactivateContentMultiLang()
388    {
389        $lng = $this->lng;
390        $ilCtrl = $this->ctrl;
391
392        if (!$this->getTitleDescrOnlyMode()) {
393            $this->obj_trans->setMasterLanguage("");
394            $this->obj_trans->setLanguages(array());
395            $this->obj_trans->save();
396        }
397        $this->obj_trans->deactivateContentTranslation();
398        if ($this->getTitleDescrOnlyMode()) {
399            ilUtil::sendSuccess($lng->txt("obj_cont_transl_deactivated"), true);
400        } else {
401            ilUtil::sendSuccess($lng->txt("obj_multilang_deactivated"), true);
402        }
403
404
405        $ilCtrl->redirect($this, "listTranslations");
406    }
407
408    /**
409     * Add language
410     */
411    public function addLanguages()
412    {
413        $tpl = $this->tpl;
414
415        $form = $this->getMultiLangForm(true);
416        $tpl->setContent($form->getHTML());
417    }
418
419    /**
420     * Save languages
421     */
422    public function saveLanguages()
423    {
424        $ilCtrl = $this->ctrl;
425        $lng = $this->lng;
426        $tpl = $this->tpl;
427
428        $form = $this->getMultiLangForm(true);
429        if ($form->checkInput()) {
430            $ad = $form->getInput("additional_langs");
431            if (is_array($ad)) {
432                $ml = $this->obj_trans->getMasterLanguage();
433                foreach ($ad as $l) {
434                    if ($l != $ml && $l != "") {
435                        $this->obj_trans->addLanguage($l, false, "", "");
436                    }
437                }
438            }
439            $this->obj_trans->save();
440            ilUtil::sendInfo($lng->txt("msg_obj_modified"), true);
441            $ilCtrl->redirect($this, "listTranslations");
442        }
443
444        ilUtil::sendFailure($this->lng->txt('err_check_input'));
445        $form->setValuesByPost();
446        $tpl->setContent($form->getHTML());
447    }
448
449    /**
450     * Confirm remove languages
451     */
452    public function confirmRemoveLanguages()
453    {
454        $ilCtrl = $this->ctrl;
455        $tpl = $this->tpl;
456        $lng = $this->lng;
457
458        $lng->loadLanguageModule("meta");
459
460        if (!is_array($_POST["lang"]) || count($_POST["lang"]) == 0) {
461            ilUtil::sendInfo($lng->txt("no_checkbox"), true);
462            $ilCtrl->redirect($this, "listTranslations");
463        } else {
464            include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
465            $cgui = new ilConfirmationGUI();
466            $cgui->setFormAction($ilCtrl->getFormAction($this));
467            $cgui->setHeaderText($lng->txt("obj_conf_delete_lang"));
468            $cgui->setCancel($lng->txt("cancel"), "listTranslations");
469            $cgui->setConfirm($lng->txt("remove"), "removeLanguages");
470
471            foreach ($_POST["lang"] as $i) {
472                $cgui->addItem("lang[]", $i, $lng->txt("meta_l_" . $i));
473            }
474
475            $tpl->setContent($cgui->getHTML());
476        }
477    }
478
479    /**
480     * Remove languages
481     */
482    public function removeLanguages()
483    {
484        $lng = $this->lng;
485        $ilCtrl = $this->ctrl;
486
487        if (is_array($_POST["lang"])) {
488            $langs = $this->obj_trans->getLanguages();
489            foreach ($langs as $k => $l) {
490                if (in_array($l, $_POST["lang"])) {
491                    $this->obj_trans->removeLanguage();
492                }
493            }
494            $this->obj_trans->save();
495            ilUtil::sendInfo($lng->txt("msg_obj_modified"), true);
496        }
497        $ilCtrl->redirect($this, "listTranslations");
498    }
499}
500