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