ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAdvancedMDFieldTranslations.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
25{
29 private static array $instances = [];
30
31 private int $record_id;
33
37 private array $definitions;
38
42 private array $translations = [];
43
45 private string $default_language = '';
46
51
56
57 private function __construct(int $record_id)
58 {
59 global $DIC;
60
61 $this->db = $DIC->database();
62 $this->lng = $DIC->language();
63 $this->lng->loadLanguageModule('meta');
64
65 $this->record_id = $record_id;
66 $this->record_translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($this->record_id);
67 $this->read();
68 }
69
70 public static function getInstanceByRecordId(int $record_id): ilAdvancedMDFieldTranslations
71 {
72 if (!isset(self::$instances[$record_id])) {
73 self::$instances[$record_id] = new self($record_id);
74 }
75 return self::$instances[$record_id];
76 }
77
78 public function getRecordId(): int
79 {
80 return $this->record_id;
81 }
82
83 public function getDefaultLanguage(): string
84 {
86 }
87
93 public function getActivatedLanguages(int $field_id, bool $with_default = true): array
94 {
95 $activated = [];
96 foreach ($this->getTranslations($field_id) as $language => $translation) {
97 if ($language == self::getDefaultLanguage() && !$with_default) {
98 continue;
99 }
100 $activated[] = $language;
101 }
102 return $activated;
103 }
104
105 public function isConfigured(int $field_id, string $lang_key): bool
106 {
107 if (!$this->record_translations->isConfigured($lang_key)) {
108 return false;
109 }
110 return isset($this->translations[$field_id][$lang_key]);
111 }
112
113 public function getTranslation(int $field_id, string $lang_key): ?ilAdvancedMDFieldTranslation
114 {
115 if (!$this->isConfigured($field_id, $lang_key)) {
116 return null;
117 }
118 return $this->translations[$field_id][$lang_key];
119 }
120
124 public function getTranslations(int $field_id): array
125 {
126 if (isset($this->translations[$field_id])) {
127 return $this->translations[$field_id];
128 }
129 return [];
130 }
131
136 {
137 foreach ($this->getTranslations($field_id) as $translation) {
138 if ($translation->getLangKey() == $this->default_language) {
139 return $translation;
140 }
141 }
142 return null;
143 }
144
145 public function read(): void
146 {
147 $query = 'select fi.field_id tfield, de.field_id ofield, fi.title, fi.description, ri.lang_code ' .
148 'from adv_md_record_int ri join adv_mdf_definition de on ri.record_id = de.record_id ' .
149 'left join adv_md_field_int fi on (ri.lang_code = fi.lang_code and de.field_id = fi.field_id) ' .
150 'where ri.record_id = ' . $this->db->quote($this->getRecordId(), ilDBConstants::T_INTEGER);
151 $res = $this->db->query($query);
152
153 $this->record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
154 $this->default_language = $this->record->getDefaultLanguage();
155
156 $this->translations = [];
157 $this->definitions = ilAdvancedMDFieldDefinition::getInstancesByRecordId($this->record_id, false);
158 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
159 $this->translations[$row->ofield][$row->lang_code] = new ilAdvancedMDFieldTranslation(
160 (int) $row->ofield,
161 (string) $row->title,
162 (string) $row->description,
163 (string) $row->lang_code
164 );
165 if ((string) $row->lang_code == $this->default_language && $row->tfield == null) {
166 $this->translations[(int) $row->ofield][(string) $row->lang_code]->setTitle($this->definitions[(int) $row->ofield]->getTitle());
167 $this->translations[(int) $row->ofield][(string) $row->lang_code]->setDescription(
168 $this->definitions[(int) $row->ofield]->getDescription()
169 );
170 }
171 }
172 }
173
174 public function getFormTranslationInfo(int $field_id, string $active_language): string
175 {
176 if (count($this->getTranslations($field_id)) <= 1) {
177 return '';
178 }
179
180 $txt = '';
181 $txt = $this->lng->txt('md_adv_int_current') . ' ' . $this->lng->txt('meta_l_' . $active_language);
182 $txt .= ', ';
183 foreach ($this->getTranslations($field_id) as $translation) {
184 if ($translation->getLangKey() == $this->getDefaultLanguage()) {
185 $txt .= ($this->lng->txt('md_adv_int_default') . ' ' . $this->lng->txt('meta_l_' . $translation->getLangKey()));
186 break;
187 }
188 }
189 return $txt;
190 }
191
193 int $field_id,
194 ilPropertyFormGUI $form,
195 ilTextInputGUI $title,
196 string $active_language
197 ): void {
198 if (!strlen($active_language)) {
199 return;
200 }
201 $show_info = ($active_language !== $this->getDefaultLanguage() && $this->getDefaultLanguage());
202 $default = $this->getDefaultTranslation($field_id);
203 if ($default instanceof ilAdvancedMDFieldTranslation && $show_info) {
204 $title->setInfo($default->getLangKey() . ': ' . $default->getTitle());
205 }
206 if ($this->getTranslation($field_id, $active_language) instanceof ilAdvancedMDFieldTranslation) {
207 $title->setValue($this->getTranslation($field_id, $active_language)->getTitle());
208 }
209 }
210
212 int $field_id,
213 ilPropertyFormGUI $form,
214 ilTextAreaInputGUI $description,
215 string $active_language
216 ): void {
217 if (!strlen($active_language)) {
218 return;
219 }
220 $show_info = ($active_language !== $this->getDefaultLanguage() && $this->getDefaultLanguage());
221 $default = $this->getDefaultTranslation($field_id);
222 if ($default instanceof ilAdvancedMDFieldTranslation && $show_info) {
223 $description->setInfo($default->getLangKey() . ': ' . $default->getDescription());
224 }
225 if ($this->getTranslation($field_id, $active_language) instanceof ilAdvancedMDFieldTranslation) {
226 $description->setValue($this->getTranslation($field_id, $active_language)->getDescription());
227 }
228 }
229
230 public function updateFromForm(int $field_id, string $active_language, ilPropertyFormGUI $form): void
231 {
232 $translation = $this->getTranslation($field_id, $active_language);
233 if (!$translation instanceof ilAdvancedMDFieldTranslation) {
234 return;
235 }
236 $translation->setTitle($form->getInput('title'));
237 $translation->setDescription($form->getInput('description'));
238 $translation->update();
239 }
240
241 public function getTitleForLanguage(int $field_id, string $language): string
242 {
243 if ($this->getTranslation($field_id, $language) && strlen($this->getTranslation(
244 $field_id,
245 $language
246 )->getTitle())) {
247 return $this->getTranslation($field_id, $language)->getTitle();
248 }
249 if (
250 $this->getTranslation($field_id, $this->getDefaultLanguage()) &&
251 strlen(($this->getTranslation($field_id, $this->getDefaultLanguage())->getTitle()))
252 ) {
253 return $this->getTranslation($field_id, $this->getDefaultLanguage())->getTitle();
254 }
255 if ($this->definitions[$field_id] instanceof ilAdvancedMDFieldDefinition) {
256 return $this->definitions[$field_id]->getTitle();
257 }
258 return '';
259 }
260
261 public function getDescriptionForLanguage(int $field_id, string $language): string
262 {
263 if ($this->getTranslation($field_id, $language) && strlen($this->getTranslation(
264 $field_id,
265 $language
266 )->getDescription())) {
267 return $this->getTranslation($field_id, $language)->getDescription();
268 }
269 if ($this->getTranslation($field_id, $this->getDefaultLanguage()) &&
270 strlen($this->getTranslation($field_id, $this->getDefaultLanguage())->getDescription())
271 ) {
272 return $this->getTranslation($field_id, $this->getDefaultLanguage())->getDescription();
273 }
274 if ($this->definitions[$field_id] instanceof ilAdvancedMDFieldDefinition) {
275 return $this->definitions[$field_id]->getDescription();
276 }
277 return '';
278 }
279}
static getInstancesByRecordId( $a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
Class ilAdvancedMDFieldTranslation.
Class ilAdvancedMDFieldTranslations.
ilAdvancedMDRecordTranslations $record_translations
getTranslation(int $field_id, string $lang_key)
getDescriptionForLanguage(int $field_id, string $language)
modifyTranslationInfoForDescription(int $field_id, ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
getTitleForLanguage(int $field_id, string $language)
isConfigured(int $field_id, string $lang_key)
getFormTranslationInfo(int $field_id, string $active_language)
modifyTranslationInfoForTitle(int $field_id, ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
updateFromForm(int $field_id, string $active_language, ilPropertyFormGUI $form)
getActivatedLanguages(int $field_id, bool $with_default=true)
static _getInstanceByRecordId(int $a_record_id)
language handling
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
This class represents a text area property in a property form.
This class represents a text property in a property form.
$txt
Definition: error.php:31
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26