ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilAdvancedMDRecordTranslations.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 $translations = [];
29
33 private $default_language = '';
34
38 private $db;
39
43 private $lng;
44
45
46 private function __construct(int $record_id)
47 {
48 global $DIC;
49
50 $this->db = $DIC->database();
51 $this->lng = $DIC->language();
52 $this->lng->loadLanguageModule('meta');
53
54 $this->record_id = $record_id;
55 $this->read();
56 }
57
62 public static function getInstanceByRecordId(int $record_id)
63 {
64 if (!isset(self::$instances[$record_id])) {
65 self::$instances[$record_id] = new self($record_id);
66 }
67 return self::$instances[$record_id];
68 }
69
73 public function getDefaultLanguage() :string
74 {
76 }
77
81 public function getRecordId() : int
82 {
83 return $this->record_id;
84 }
85
90 public function isConfigured(string $lang_key)
91 {
92 return isset($this->translations[$lang_key]);
93 }
94
99 public function getTranslation(string $lang_key) :? ilAdvancedMDRecordTranslation
100 {
101 if (!$this->isConfigured($lang_key)) {
102 return null;
103 }
104 return $this->translations[$lang_key];
105 }
106
110 public function getTranslations()
111 {
112 return $this->translations;
113 }
114
119 {
120 foreach ($this->getTranslations() as $translation) {
121 if ($translation->getLangKey() == $this->default_language) {
122 return $translation;
123 }
124 }
125 return null;
126 }
127
128 public function cloneRecord(int $new_record_id)
129 {
130 foreach ($this->getTranslations() as $recordTranslation) {
131 $recordTranslation->setRecordId($new_record_id);
132 $recordTranslation->insert();
133 }
134 }
135
136 private function read()
137 {
138 $query = 'select * from ' . ilAdvancedMDRecordTranslation::TABLE_NAME . ' ' .
139 'where record_id = ' . $this->db->quote($this->getRecordId(), ilDBConstants::T_INTEGER);
140 $res = $this->db->query($query);
141
142 $this->translations = [];
143 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
144 $this->translations[$row->lang_code] = new ilAdvancedMDRecordTranslation(
145 (int) $row->record_id,
146 (string) $row->title,
147 (string) $row->description,
148 (string) $row->lang_code
149 );
150 }
151
152 $this->record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
153 $this->default_language = $this->record->getDefaultLanguage();
154 }
155
156 public function addTranslationEntry(string $language_code, bool $default = false)
157 {
158 $this->translations[$language_code] = new ilAdvancedMDRecordTranslation(
159 $this->record_id,
160 '',
161 '',
162 $language_code,
163 $default
164 );
165 $this->translations[$language_code]->insert();
166 }
167
171 public function updateDefault(string $default)
172 {
173 foreach ($this->getTranslations() as $translation) {
174 if ($translation->getLangKey() != $default) {
175 $translation->setLangDefault(false);
176 $translation->update();
177 }
178 if ($translation->getLangKey() == $default) {
179 $translation->setLangDefault(true);
180 $translation->update();
181 }
182 }
183 }
184
188 public function getFormTranslationInfo(string $active_language) : string
189 {
190 if (count($this->translations) <= 1) {
191 return '';
192 }
193 $txt = '';
194 $txt = $this->lng->txt('md_adv_int_current'). ' ' . $this->lng->txt('meta_l_' . $active_language);
195 $txt .= ', ';
196 foreach ($this->translations as $translation) {
197 if ($translation->getLangKey() == $this->default_language) {
198 $txt .= ($this->lng->txt('md_adv_int_default') . ' ' . $this->lng->txt('meta_l_' . $translation->getLangKey()));
199 break;
200 }
201 }
202 return $txt;
203 }
204
209 public function modifyTranslationInfoForTitle(ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
210 {
211 if (count($this->translations) <= 1) {
212 return;
213 }
214 $default = $this->getDefaultTranslation();
215 if ($default->getLangKey() != $active_language) {
216 $title->setInfo($default->getLangKey() . ': ' . $default->getTitle());
217 }
218 if ($this->getTranslation($active_language) instanceof ilAdvancedMDRecordTranslation) {
219 $title->setValue($this->getTranslation($active_language)->getTitle());
220 }
221 }
222
227 public function modifyTranslationInfoForDescription(ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
228 {
229 if (count($this->translations) <= 1) {
230 return;
231 }
232 $default = $this->getDefaultTranslation();
233 if ($default->getLangKey() != $active_language) {
234 $description->setInfo($default->getLangKey() . ': ' . $default->getDescription());
235 }
236 if ($this->getTranslation($active_language) instanceof ilAdvancedMDRecordTranslation) {
237 $description->setValue($this->getTranslation($active_language)->getDescription());
238 }
239 }
240
246 public function updateTranslations(string $active_language, string $title, string $description)
247 {
248 $translation = $this->getTranslation($active_language);
249 if (!$translation instanceof ilAdvancedMDRecordTranslation) {
250 return false;
251 }
252 $translation->setTitle($title);
253 $translation->setDescription($description);
254 $translation->update();
255 }
256
261 public function getTitleForLanguage(string $language)
262 {
263 if ($this->getTranslation($language) && strlen($this->getTranslation($language)->getTitle())) {
264 return $this->getTranslation($language)->getTitle();
265 }
266 return $this->record->getTitle();
267 }
272 public function getDescriptionForLanguage(string $language)
273 {
274 if ($this->getTranslation($language) && strlen($this->getTranslation($language)->getDescription())) {
275 return $this->getTranslation($language)->getDescription();
276 }
277 return $this->record->getDescription();
278 }
279
284 public function toXML(ilXmlWriter $writer) : ilXmlWriter
285 {
286 if (!count($this->getTranslations())) {
287 return $writer;
288 }
289
290 $writer->xmlStartTag(
291 'RecordTranslations',
292 [
293 'defaultLanguage' => $this->getDefaultLanguage()
294 ]
295 );
296 foreach ($this->getTranslations() as $translation) {
297 $writer->xmlStartTag(
298 'RecordTranslation',
299 [
300 'language' => $translation->getLangKey()
301 ]
302 );
303 $writer->xmlElement('RecordTranslationTitle', [], $translation->getTitle());
304 $writer->xmlElement('RecordTranslationDescription', [], $translation->getDescription());
305 $writer->xmlEndTag('RecordTranslation');
306 }
307 $writer->xmlEndTag('RecordTranslations');
308 return $writer;
309 }
310}
An exception for terminatinating execution or to throw for unit testing.
Class ilAdvancedMDRecordTranslation.
Class ilAdvancedMDRecordTranslation.
addTranslationEntry(string $language_code, bool $default=false)
updateTranslations(string $active_language, string $title, string $description)
modifyTranslationInfoForDescription(ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
modifyTranslationInfoForTitle(ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
static _getInstanceByRecordId($a_record_id)
Get instance by record id.
setInfo($a_info)
Set Information Text.
This class represents a property form user interface.
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.
XML writer class.
xmlEndTag($tag)
Writes an endtag.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
$txt
Definition: error.php:13
global $DIC
Definition: goto.php:24
$query
foreach($_POST as $key=> $value) $res