ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilAdvancedMDFieldTranslations.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
13 private static $instances = null;
14
18 private $record_id;
19
23 private $record;
24
28 private $definitions;
29
33 private $translations = [];
34
39
40
44 private $default_language = '';
45
46
50 private $db;
51
55 private $lng;
56
57
58 private function __construct(int $record_id)
59 {
60 global $DIC;
61
62 $this->db = $DIC->database();
63 $this->lng = $DIC->language();
64 $this->lng->loadLanguageModule('meta');
65
66 $this->record_id = $record_id;
67 $this->record_translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($this->record_id);
68 $this->read();
69 }
70
75 public static function getInstanceByRecordId(int $record_id)
76 {
77 if (!isset(self::$instances[$record_id])) {
78 self::$instances[$record_id] = new self($record_id);
79 }
80 return self::$instances[$record_id];
81 }
82
86 public function getRecordId() : int
87 {
88 return $this->record_id;
89 }
90
91
95 public function getDefaultLanguage() :string
96 {
98 }
99
100 public function getActivatedLanguages(int $field_id, bool $with_default = true)
101 {
102 $activated = [];
103 foreach ($this->getTranslations($field_id) as $language => $translation)
104 {
105 if ($language == self::getDefaultLanguage() && !$with_default) {
106 continue;
107 }
108 $activated[] = $language;
109 }
110 return $activated;
111 }
112
113
119 public function isConfigured(int $field_id, string $lang_key)
120 {
121 if (!$this->record_translations->isConfigured($lang_key)) {
122 return false;
123 }
124 return isset($this->translations[$field_id][$lang_key]);
125 }
126
127
132 public function getTranslation(int $field_id, string $lang_key) :? ilAdvancedMDFieldTranslation
133 {
134 if (!$this->isConfigured($field_id, $lang_key)) {
135 return null;
136 }
137 return $this->translations[$field_id][$lang_key];
138 }
139
143 public function getTranslations(int $field_id)
144 {
145 if (isset($this->translations[$field_id])) {
146 return $this->translations[$field_id];
147 }
148 return [];
149 }
150
154 public function getDefaultTranslation(int $field_id) : ?ilAdvancedMDFieldTranslation
155 {
156 foreach ($this->getTranslations($field_id) as $translation) {
157 if ($translation->getLangKey() == $this->default_language) {
158 return $translation;
159 }
160 }
161 return null;
162 }
163
164 public function read()
165 {
166 $query = 'select fi.field_id tfield, de.field_id ofield, fi.title, fi.description, ri.lang_code ' .
167 'from adv_md_record_int ri join adv_mdf_definition de on ri.record_id = de.record_id ' .
168 'left join adv_md_field_int fi on (ri.lang_code = fi.lang_code and de.field_id = fi.field_id) ' .
169 'where ri.record_id = ' . $this->db->quote($this->getRecordId(), ilDBConstants::T_INTEGER);
170 $res = $this->db->query($query);
171
172 $this->record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
173 $this->default_language = $this->record->getDefaultLanguage();
174
175 $this->translations = [];
176 $this->definitions = ilAdvancedMDFieldDefinition::getInstancesByRecordId($this->record_id, false);
177 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
178 $this->translations[$row->ofield][$row->lang_code] = new ilAdvancedMDFieldTranslation(
179 (int) $row->ofield,
180 (string) $row->title,
181 (string) $row->description,
182 (string) $row->lang_code
183 );
184 if (
185 $row->lang_code == $this->default_language &&
186 $row->tfield == null
187 ) {
188 $this->translations[$row->ofield][$row->lang_code]->setTitle($this->definitions[$row->ofield]->getTitle());
189 $this->translations[$row->ofield][$row->lang_code]->setDescription((string) $this->definitions[$row->ofield]->getDescription());
190 }
191 }
192 }
193
197 public function getFormTranslationInfo(int $field_id, string $active_language) : string
198 {
199 if (count($this->getTranslations($field_id)) <= 1) {
200 return '';
201 }
202
203 $txt = '';
204 $txt = $this->lng->txt('md_adv_int_current'). ' ' . $this->lng->txt('meta_l_' . $active_language);
205 $txt .= ', ';
206 foreach ($this->getTranslations($field_id) as $translation) {
207 if ($translation->getLangKey() == $this->getDefaultLanguage()) {
208 $txt .= ($this->lng->txt('md_adv_int_default') . ' ' . $this->lng->txt('meta_l_' . $translation->getLangKey()));
209 break;
210 }
211 }
212 return $txt;
213 }
214
219 public function modifyTranslationInfoForTitle(int $field_id, ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
220 {
221 if (!strlen($active_language)) {
222 return;
223 }
224 $show_info = ($active_language !== $this->getDefaultLanguage() && $this->getDefaultLanguage());
225 $default = $this->getDefaultTranslation($field_id);
226 if ($default instanceof ilAdvancedMDFieldTranslation && $show_info) {
227 $title->setInfo($default->getLangKey() . ': ' . $default->getTitle());
228 }
229 if ($this->getTranslation($field_id, $active_language) instanceof ilAdvancedMDFieldTranslation) {
230 $title->setValue($this->getTranslation($field_id, $active_language)->getTitle());
231 }
232 }
233
238 public function modifyTranslationInfoForDescription(int $field_id, ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
239 {
240 if (!strlen($active_language)) {
241 return;
242 }
243 $show_info = ($active_language !== $this->getDefaultLanguage() && $this->getDefaultLanguage());
244 $default = $this->getDefaultTranslation($field_id);
245 if ($default instanceof ilAdvancedMDFieldTranslation && $show_info) {
246 $description->setInfo($default->getLangKey() . ': ' . $default->getDescription());
247 }
248 if ($this->getTranslation($field_id, $active_language) instanceof ilAdvancedMDFieldTranslation) {
249 $description->setValue($this->getTranslation($field_id, $active_language)->getDescription());
250 }
251 }
252
258 public function updateFromForm(int $field_id, string $active_language, ilPropertyFormGUI $form)
259 {
260 $translation = $this->getTranslation($field_id, $active_language);
261 if (!$translation instanceof ilAdvancedMDFieldTranslation) {
262 return;
263 }
264 $translation->setTitle($form->getInput('title'));
265 $translation->setDescription($form->getInput('description'));
266 $translation->update();
267 return;
268 }
269
275 public function updateTranslations(string $active_language, string $title, string $description)
276 {
277 $translation = $this->getTranslation($active_language);
278 if (!$translation instanceof ilAdvancedMDRecordTranslation) {
279 return false;
280 }
281 $translation->setTitle($title);
282 $translation->setDescription($description);
283 $translation->update();
284 }
285
290 public function getTitleForLanguage(int $field_id, string $language) : string
291 {
292 if ($this->getTranslation($field_id, $language) && strlen($this->getTranslation($field_id, $language)->getTitle())) {
293 return $this->getTranslation($field_id, $language)->getTitle();
294 }
295 if (
296 $this->getTranslation($field_id, $this->getDefaultLanguage()) &&
297 strlen(($this->getTranslation($field_id, $this->getDefaultLanguage())->getTitle()))
298 ) {
299 return $this->getTranslation($field_id, $this->getDefaultLanguage())->getTitle();
300 }
301 if ($this->definitions[$field_id] instanceof ilAdvancedMDFieldDefinition) {
302 return $this->definitions[$field_id]->getTitle();
303 }
304 return '';
305 }
310 public function getDescriptionForLanguage(int $field_id, string $language)
311 {
312 if ($this->getTranslation($field_id, $language) && strlen($this->getTranslation($field_id, $language)->getDescription())) {
313 return $this->getTranslation($field_id, $language)->getDescription();
314 }
315 if ($this->getTranslation($field_id, $this->getDefaultLanguage()) &&
316 strlen($this->getTranslation($field_id, $this->getDefaultLanguage())->getDescription())
317 ) {
318 return $this->getTranslation($field_id, $this->getDefaultLanguage())->getDescription();
319 }
320 if ($this->definitions[$field_id] instanceof ilAdvancedMDFieldDefinition) {
321 return $this->definitions[$field_id]->getDescription();
322 }
323 return '';
324 }
325}
An exception for terminatinating execution or to throw for unit testing.
static getInstancesByRecordId($a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
Class ilAdvancedMDFieldTranslation.
Class ilAdvancedMDFieldTranslations.
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)
updateTranslations(string $active_language, string $title, string $description)
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)
Class ilAdvancedMDRecordTranslation.
static _getInstanceByRecordId($a_record_id)
Get instance by record id.
setInfo($a_info)
Set Information Text.
This class represents a property form user interface.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
This class represents a text area property in a property form.
setValue($a_value)
Set Value.
This class represents a text property in a property form.
setValue($a_value)
Set Value.
$txt
Definition: error.php:13
global $DIC
Definition: goto.php:24
$query
foreach($_POST as $key=> $value) $res