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