ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAdvancedMDRecordTranslations.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
11 {
15  private static $instances = null;
16 
17  private int $record_id;
19 
23  private $translations = [];
24 
25  private string $default_language = '';
26 
27  private ilDBInterface $db;
28  private ilLanguage $lng;
29 
30  private function __construct(int $record_id)
31  {
32  global $DIC;
33 
34  $this->db = $DIC->database();
35  $this->lng = $DIC->language();
36  $this->lng->loadLanguageModule('meta');
37 
38  $this->record_id = $record_id;
39  $this->read();
40  }
41 
42  public static function getInstanceByRecordId(int $record_id): ilAdvancedMDRecordTranslations
43  {
44  if (!isset(self::$instances[$record_id])) {
45  self::$instances[$record_id] = new self($record_id);
46  }
47  return self::$instances[$record_id];
48  }
49 
50  public function getDefaultLanguage(): string
51  {
53  }
54 
55  public function getRecordId(): int
56  {
57  return $this->record_id;
58  }
59 
60  public function isConfigured(string $lang_key): bool
61  {
62  return isset($this->translations[$lang_key]);
63  }
64 
65  public function getTranslation(string $lang_key): ?ilAdvancedMDRecordTranslation
66  {
67  if (!$this->isConfigured($lang_key)) {
68  return null;
69  }
70  return $this->translations[$lang_key];
71  }
72 
76  public function getTranslations(): array
77  {
78  return $this->translations;
79  }
80 
82  {
83  foreach ($this->getTranslations() as $translation) {
84  if ($translation->getLangKey() == $this->default_language) {
85  return $translation;
86  }
87  }
88  return null;
89  }
90 
91  public function cloneRecord(int $new_record_id): void
92  {
93  foreach ($this->getTranslations() as $recordTranslation) {
94  $recordTranslation->setRecordId($new_record_id);
95  $recordTranslation->insert();
96  }
97  }
98 
99  private function read(): void
100  {
101  $query = 'select * from ' . ilAdvancedMDRecordTranslation::TABLE_NAME . ' ' .
102  'where record_id = ' . $this->db->quote($this->getRecordId(), ilDBConstants::T_INTEGER);
103  $res = $this->db->query($query);
104 
105  $this->translations = [];
106  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
107  $this->translations[(string) $row->lang_code] = new ilAdvancedMDRecordTranslation(
108  (int) $row->record_id,
109  (string) $row->title,
110  (string) $row->description,
111  (string) $row->lang_code
112  );
113  }
114 
115  $this->record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
116  $this->default_language = $this->record->getDefaultLanguage();
117  }
118 
119  public function addTranslationEntry(string $language_code, bool $default = false): void
120  {
121  $this->translations[$language_code] = new ilAdvancedMDRecordTranslation(
122  $this->record_id,
123  '',
124  '',
125  $language_code,
126  $default
127  );
128  $this->translations[$language_code]->insert();
129  }
130 
131  public function updateDefault(string $default): void
132  {
133  foreach ($this->getTranslations() as $translation) {
134  if ($translation->getLangKey() != $default) {
135  $translation->setLangDefault(false);
136  $translation->update();
137  }
138  if ($translation->getLangKey() == $default) {
139  $translation->setLangDefault(true);
140  $translation->update();
141  }
142  }
143  }
144 
145  public function getFormTranslationInfo(string $active_language): string
146  {
147  if (count($this->translations) <= 1) {
148  return '';
149  }
150  $txt = '';
151  $txt = $this->lng->txt('md_adv_int_current') . ' ' . $this->lng->txt('meta_l_' . $active_language);
152  $txt .= ', ';
153  foreach ($this->translations as $translation) {
154  if ($translation->getLangKey() == $this->default_language) {
155  $txt .= ($this->lng->txt('md_adv_int_default') . ' ' . $this->lng->txt('meta_l_' . $translation->getLangKey()));
156  break;
157  }
158  }
159  return $txt;
160  }
161 
163  ilPropertyFormGUI $form,
164  ilTextInputGUI $title,
165  string $active_language
166  ): void {
167  if (count($this->translations) <= 1) {
168  return;
169  }
170  $default = $this->getDefaultTranslation();
171  if ($default->getLangKey() != $active_language) {
172  $title->setInfo($default->getLangKey() . ': ' . $default->getTitle());
173  }
174  if ($this->getTranslation($active_language) instanceof ilAdvancedMDRecordTranslation) {
175  $title->setValue($this->getTranslation($active_language)->getTitle());
176  }
177  }
178 
180  ilPropertyFormGUI $form,
181  ilTextAreaInputGUI $description,
182  string $active_language
183  ): void {
184  if (count($this->translations) <= 1) {
185  return;
186  }
187  $default = $this->getDefaultTranslation();
188  if ($default->getLangKey() != $active_language) {
189  $description->setInfo($default->getLangKey() . ': ' . $default->getDescription());
190  }
191  if ($this->getTranslation($active_language) instanceof ilAdvancedMDRecordTranslation) {
192  $description->setValue($this->getTranslation($active_language)->getDescription());
193  }
194  }
195 
196  public function updateTranslations(string $active_language, string $title, string $description): void
197  {
198  $translation = $this->getTranslation($active_language);
199  if (!$translation instanceof ilAdvancedMDRecordTranslation) {
200  return;
201  }
202  $translation->setTitle($title);
203  $translation->setDescription($description);
204  $translation->update();
205  }
206 
207  public function getTitleForLanguage(string $language): string
208  {
209  if ($this->getTranslation($language) && strlen($this->getTranslation($language)->getTitle())) {
210  return $this->getTranslation($language)->getTitle();
211  }
212  return $this->record->getTitle();
213  }
214 
215  public function getDescriptionForLanguage(string $language): string
216  {
217  if ($this->getTranslation($language) && strlen($this->getTranslation($language)->getDescription())) {
218  return $this->getTranslation($language)->getDescription();
219  }
220  return $this->record->getDescription();
221  }
222 
223  public function toXML(ilXmlWriter $writer): ilXmlWriter
224  {
225  if (!count($this->getTranslations())) {
226  return $writer;
227  }
228 
229  $writer->xmlStartTag(
230  'RecordTranslations',
231  [
232  'defaultLanguage' => $this->getDefaultLanguage()
233  ]
234  );
235  foreach ($this->getTranslations() as $translation) {
236  $writer->xmlStartTag(
237  'RecordTranslation',
238  [
239  'language' => $translation->getLangKey()
240  ]
241  );
242  $writer->xmlElement('RecordTranslationTitle', [], $translation->getTitle());
243  $writer->xmlElement('RecordTranslationDescription', [], $translation->getDescription());
244  $writer->xmlEndTag('RecordTranslation');
245  }
246  $writer->xmlEndTag('RecordTranslations');
247  return $writer;
248  }
249 }
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilAdvancedMDRecordTranslation.
modifyTranslationInfoForDescription(ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
getTranslations()
array<string, ilAdvancedMDRecordTranslation>
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
static _getInstanceByRecordId(int $a_record_id)
modifyTranslationInfoForTitle(ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
Class ilAdvancedMDRecordTranslation.
$query
$txt
Definition: error.php:13
addTranslationEntry(string $language_code, bool $default=false)
updateTranslations(string $active_language, string $title, string $description)
This class represents a text area property in a property form.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)