ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMMItemTranslationGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 {
29  use DIC;
30 
31  public const P_TRANSLATIONS = 'translations';
32  public const P_DELETE = 'delete';
33  public const CMD_ADD_LANGUAGE = "addLanguages";
34  public const CMD_SAVE_LANGUAGES = "saveLanguages";
35  public const CMD_SAVE_TRANSLATIONS = "saveTranslations";
36  public const CMD_DELETE_TRANSLATIONS = "deleteTranslations";
37  public const CMD_DEFAULT = 'index';
38  public const IDENTIFIER = 'identifier';
39 
41 
44 
48  public function __construct(ilMMItemFacadeInterface $item_facade, ilMMItemRepository $repository)
49  {
50  global $DIC;
51  $this->main_tpl = $DIC->ui()->mainTemplate();
52  $this->item_facade = $item_facade;
53  $this->repository = $repository;
54  $this->lng()->loadLanguageModule("mme");
55  }
56 
57  public function executeCommand(): void
58  {
59  $this->ctrl()->saveParameter($this, self::IDENTIFIER);
60  switch ($this->ctrl()->getNextClass()) {
61  default:
62  $cmd = $this->ctrl()->getCmd(self::CMD_DEFAULT);
63  $this->{$cmd}();
64  }
65  }
66 
67  protected function index(): void
68  {
69  $this->initToolbar();
70 
71  $table = new ilMMItemTranslationTableGUI($this, $this->item_facade);
72  $this->tpl()->setContent($table->getHTML());
73  }
74 
75  protected function initToolbar(): void
76  {
77  $this->toolbar()->addButton(
78  $this->lng()->txt("add_languages"),
79  $this->ctrl()
80  ->getLinkTarget($this, self::CMD_ADD_LANGUAGE)
81  );
82  }
83 
84  protected function saveTranslations(): void
85  {
86  $to_translate = (array) $this->http()->request()->getParsedBody()[self::P_TRANSLATIONS];
87  foreach ($to_translate as $id => $data) {
91  $translation = ilMMItemTranslationStorage::find($id);
92  $translation->setTranslation($data['translation']);
93  $translation->update();
94  }
95  $this->repository->clearCache();
96  $this->main_tpl->setOnScreenMessage('info', $this->lng()->txt('msg_translations_saved'), true);
97  $this->cancel();
98  }
99 
100  protected function deleteTranslations(): void
101  {
102  $to_delete = (array) ($this->http()->request()->getParsedBody()[self::P_DELETE] ?? []);
103  foreach ($to_delete as $id) {
104  ilMMItemTranslationStorage::find($id)->delete();
105  }
106  $this->repository->updateItem($this->item_facade);
107  $this->main_tpl->setOnScreenMessage('info', $this->lng()->txt('msg_translations_deleted'), true);
108  $this->cancel();
109  }
110 
111  protected function addLanguages(): void
112  {
113  $form = $this->getLanguagesForm();
114 
115  $this->tpl()->setContent($form->getHTML());
116  }
117 
118  protected function saveLanguages(): void
119  {
120  $form = $this->getLanguagesForm();
121  if ($form->checkInput()) {
122  $ad = $form->getInput("additional_langs");
123  if (is_array($ad)) {
124  foreach ($ad as $language_key) {
125  ilMMItemTranslationStorage::storeTranslation($this->item_facade->identification(), $language_key, "");
126  }
127  }
128  $this->repository->updateItem($this->item_facade);
129  $this->main_tpl->setOnScreenMessage('info', $this->lng()->txt("msg_languages_added"), true);
130  $this->cancel();
131  }
132 
133  $this->main_tpl->setOnScreenMessage('failure', $this->lng()->txt('err_check_input'));
134  $form->setValuesByPost();
135  $this->tpl()->setContent($form->getHTML());
136  }
137 
141  protected function getLanguagesForm(): ilPropertyFormGUI
142  {
143  $form = new ilPropertyFormGUI();
144  $form->setFormAction($this->ctrl()->getFormAction($this));
145 
146  // additional languages
147  $options = ilMDLanguageItem::_getLanguages();
148  $options = array("" => $this->lng()->txt("please_select")) + $options;
149  $si = new ilSelectInputGUI($this->lng()->txt("additional_langs"), "additional_langs");
150  $si->setOptions($options);
151  $si->setMulti(true);
152  $form->addItem($si);
153 
154  $form->setTitle($this->lng()->txt("add_languages"));
155  $form->addCommandButton(self::CMD_SAVE_LANGUAGES, $this->lng()->txt("save"));
156  $form->addCommandButton(self::CMD_DEFAULT, $this->lng()->txt("cancel"));
157 
158  return $form;
159  }
160 
161  protected function cancel(): void
162  {
163  $this->ctrl()->redirect($this, self::CMD_DEFAULT);
164  }
165 }
ilGlobalTemplateInterface $main_tpl
Class ilMMItemRepository.
global $DIC
Definition: feed.php:28
__construct(ilMMItemFacadeInterface $item_facade, ilMMItemRepository $repository)
ilMMItemTranslationGUI constructor.
static http()
Fetches the global http state from ILIAS.
Class ilMMItemTranslationGUI.
Class ilMMItemTranslationTableGUI.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ilMMItemFacadeInterface $item_facade
static storeTranslation(IdentificationInterface $identification, string $language_key, string $translation)
Interface ilMMItemFacadeInterface.