ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
TranslationWorkflowForm.php
Go to the documentation of this file.
1 <?php
2 
20 
26 use ILIAS\Data\URI;
31 
33 {
34  private const STEP = 'step';
35  private const STEP_SELECTED_LNGS = 'selectedLngs';
36  private const STEP_SAVE_TRANSLATIONS = 'saveTranslations';
37  private readonly Factory $ui_factory;
38  private readonly \ilLanguage $lng;
39  private readonly ServerRequestInterface $request;
40 
41  private bool $all_languages = false;
42  private readonly Translations $translations;
43  private readonly \ILIAS\UI\Component\Input\Field\Factory $fields;
44  private readonly \ILIAS\Refinery\Factory $refinery;
45  private readonly \ILIAS\UI\Component\Prompt\State\Factory $states;
46 
47  public function __construct(
48  private readonly ServicesInterface $lom_services,
49  private readonly UIServices $ui,
50  private readonly TranslationsRepository $repository,
51  private readonly TranslatableItem $item
52  ) {
53  global $DIC;
54  $this->request = $DIC->http()->request();
55  $this->refinery = $DIC->refinery();
56  $this->ui_factory = $ui->factory();
57  $this->lng = $DIC->language();
58  $this->translations = $repository->get($item);
59  $this->fields = $this->ui_factory->input()->field();
60  $this->states = $this->ui_factory->prompt()->state();
61  }
62 
63  public function asTranslationWorkflow(
64  URI $here_uri,
65  URI $success_target
66  ): State {
67  $step = $this->request->getQueryParams()[self::STEP] ?? null;
68 
69  $language_slection = $this->getLanguageSelectionForm(
70  $here_uri->withParameter(self::STEP, self::STEP_SELECTED_LNGS)
71  );
72 
73  switch ($step) {
74  default:
75  return $this->states->show($language_slection);
76  case self::STEP_SELECTED_LNGS:
77  // store
78  $data = $language_slection->withRequest($this->request)->getData();
79  return $this->states
80  ->show(
81  $this->getTranslationForm(
82  $here_uri->withParameter(self::STEP, self::STEP_SAVE_TRANSLATIONS),
83  $data
84  )
85  );
86  case self::STEP_SAVE_TRANSLATIONS:
87  $active_language_keys = [];
88  foreach ($this->translations->getLanguageKeys() as $language_key) {
89  $active_language_keys[$language_key] = true;
90  }
91 
92  $tranlation_form = $this
93  ->getTranslationForm(
94  $here_uri->withParameter(self::STEP, self::STEP_SAVE_TRANSLATIONS),
95  $active_language_keys
96  )
97  ->withRequest($this->request);
98 
99  $data = $tranlation_form->getData();
100  if ($data !== null) {
101  return $this->states
102  ->redirect($success_target->withParameter(self::STEP, ''));
103  }
104  return $this->states->show($tranlation_form);
105  }
106  }
107 
108  public function getTranslationForm(URI $form_target, array $language_keys): Standard
109  {
110  $inputs = $this->getTranslationInputs($language_keys);
111  return $this->ui_factory->input()->container()->form()->standard(
112  (string) $form_target,
113  $inputs
114  )->withAdditionalTransformation(
115  $this->refinery->custom()->transformation(function ($value) {
116  $this->repository->store($this->translations);
117  return $value;
118  })
119  );
120  }
121 
122  protected function getTranslationInputs(array $language_keys, bool $required = true): array
123  {
124  $default_value = match (true) {
125  ($this->item instanceof GroupDTO) => $this->item->getTitle(),
126  ($this->item instanceof EntryDTO) => $this->item->getTitle(),
127  default => ''
128  };
129 
130  $languages = [];
131  foreach ($this->lom_services->dataHelper()->getAllLanguages() as $language) {
132  $languages[$language->value()] = $language->presentableLabel();
133  }
134 
135  $inputs = [];
136  foreach ($language_keys as $language_key => $active) {
137  if (!$active) {
138  continue;
139  }
140  $translation = $this->translations->getLanguageCode($language_key)?->getTranslation();
141  $language_title = $languages[$language_key] ?? null;
142  if ($language_title === null) {
143  continue;
144  }
145  $inputs[$language_key] = $this->fields
146  ->text($language_title)
147  ->withRequired($required)
148  ->withValue(
149  $translation ?? $default_value
150  )
151  ->withAdditionalTransformation(
152  $this->refinery->custom()->transformation(function ($value) use ($language_key) {
153  $this->translations->add(
154  $this->repository->blank($this->item, $language_key, $value)
155  );
156  return $value;
157  })
158  );
159  }
160  return $inputs;
161  }
162 
163  public function getLanguageSelectionForm(URI $form_target): Standard
164  {
166 
167  return $this->ui_factory->input()->container()->form()->standard(
168  (string) $form_target,
169  $inputs
170  )->withAdditionalTransformation(
171  $this->refinery->custom()->transformation(function ($value) {
172  $this->repository->store($this->translations);
173  return $value;
174  })
175  );
176  }
177 
178  private function getLanguageSelectionInputs(): array
179  {
180  $inputs = [];
181  $all_languages = $this->lom_services->dataHelper()->getAllLanguages();
182  $installed_languages = $this->lng->getInstalledLanguages();
183 
184  foreach ($all_languages as $language) {
185  $language_code = $language->value();
186  if (!$this->all_languages && !in_array($language_code, $installed_languages, true)) {
187  continue;
188  }
189  $inputs[$language_code] = $this->fields
190  ->checkbox($language->presentableLabel())
191  ->withValue(
192  $this->translations->getLanguageCode($language_code) !== null
194  $this->refinery->custom()->transformation(function ($value) use ($language_code) {
195  if (!$value) {
196  $this->translations->remove($language_code);
197  } else {
198  $this->translations->add(
199  $this->repository->blank(
200  $this->item,
201  $language_code,
202  $this->translations->getLanguageCode($language_code)?->getTranslation() ?? ''
203  )
204  );
205  }
206  return $value;
207  })
208  );
209  }
210  return $inputs;
211  }
212 
213  // ALTERNATIVE
214 
215  public function asTranslationModal(URI $form_target): RoundTrip
216  {
217  $languages = [];
218  foreach ($this->lng->getInstalledLanguages() as $installed_language) {
219  $languages[$installed_language] = true;
220  }
221 
222  return $this->ui_factory->modal()->roundtrip(
223  $this->lng->txt('translations'),
224  null,
225  $this->getTranslationInputs($languages, false),
226  $form_target
228  $this->refinery->custom()->transformation(function ($value) {
229  $this->repository->store($this->translations);
230  return $value;
231  })
232  );
233  }
234 
235 }
repository()
description: > Example for rendering a repository card
Definition: repository.php:17
__construct(private readonly ServicesInterface $lom_services, private readonly UIServices $ui, private readonly TranslationsRepository $repository, private readonly TranslatableItem $item)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Provides fluid interface to RBAC services.
Definition: UIServices.php:23
This is how the factory for UI elements looks.
Definition: Factory.php:37
global $DIC
Definition: shib_login.php:25
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:34
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
This describes a standard form.
Definition: Standard.php:28
readonly ILIAS UI Component Prompt State Factory $states
withParameter(string $key, $value)
Get URI with modified parameters.
Definition: URI.php:388