ILIAS  trunk Revision v11.0_alpha-1851-ga8564da6fed
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAdvancedMDRecordTranslations.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
25 {
29  private static $instances = null;
30 
31  private int $record_id;
33 
37  private $translations = [];
38 
39  private string $default_language = '';
40 
41  private ilDBInterface $db;
42  private ilLanguage $lng;
43 
44  private function __construct(int $record_id)
45  {
46  global $DIC;
47 
48  $this->db = $DIC->database();
49  $this->lng = $DIC->language();
50  $this->lng->loadLanguageModule('meta');
51 
52  $this->record_id = $record_id;
53  $this->read();
54  }
55 
56  public static function getInstanceByRecordId(int $record_id): ilAdvancedMDRecordTranslations
57  {
58  if (!isset(self::$instances[$record_id])) {
59  self::$instances[$record_id] = new self($record_id);
60  }
61  return self::$instances[$record_id];
62  }
63 
64  public function getDefaultLanguage(): string
65  {
67  }
68 
69  public function getRecordId(): int
70  {
71  return $this->record_id;
72  }
73 
74  public function isConfigured(string $lang_key): bool
75  {
76  return isset($this->translations[$lang_key]);
77  }
78 
79  public function getTranslation(string $lang_key): ?ilAdvancedMDRecordTranslation
80  {
81  if (!$this->isConfigured($lang_key)) {
82  return null;
83  }
84  return $this->translations[$lang_key];
85  }
86 
90  public function getTranslations(): array
91  {
92  return $this->translations;
93  }
94 
96  {
97  foreach ($this->getTranslations() as $translation) {
98  if ($translation->getLangKey() == $this->default_language) {
99  return $translation;
100  }
101  }
102  return null;
103  }
104 
105  public function cloneRecord(int $new_record_id): void
106  {
107  foreach ($this->getTranslations() as $recordTranslation) {
108  $recordTranslation->setRecordId($new_record_id);
109  $recordTranslation->insert();
110  }
111  }
112 
113  private function read(): void
114  {
115  $query = 'select * from ' . ilAdvancedMDRecordTranslation::TABLE_NAME . ' ' .
116  'where record_id = ' . $this->db->quote($this->getRecordId(), ilDBConstants::T_INTEGER);
117  $res = $this->db->query($query);
118 
119  $this->translations = [];
120  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
121  $this->translations[(string) $row->lang_code] = new ilAdvancedMDRecordTranslation(
122  (int) $row->record_id,
123  (string) $row->title,
124  (string) $row->description,
125  (string) $row->lang_code
126  );
127  }
128 
129  $this->record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
130  $this->default_language = $this->record->getDefaultLanguage();
131  }
132 
133  public function addTranslationEntry(string $language_code, bool $default = false): void
134  {
135  $this->translations[$language_code] = new ilAdvancedMDRecordTranslation(
136  $this->record_id,
137  '',
138  '',
139  $language_code,
140  $default
141  );
142  $this->translations[$language_code]->insert();
143  }
144 
145  public function updateDefault(string $default): void
146  {
147  foreach ($this->getTranslations() as $translation) {
148  if ($translation->getLangKey() != $default) {
149  $translation->setLangDefault(false);
150  $translation->update();
151  }
152  if ($translation->getLangKey() == $default) {
153  $translation->setLangDefault(true);
154  $translation->update();
155  }
156  }
157  }
158 
159  public function getFormTranslationInfo(string $active_language): string
160  {
161  if (count($this->translations) <= 1) {
162  return '';
163  }
164  $txt = '';
165  $txt = $this->lng->txt('md_adv_int_current') . ' ' . $this->lng->txt('meta_l_' . $active_language);
166  $txt .= ', ';
167  foreach ($this->translations as $translation) {
168  if ($translation->getLangKey() == $this->default_language) {
169  $txt .= ($this->lng->txt('md_adv_int_default') . ' ' . $this->lng->txt('meta_l_' . $translation->getLangKey()));
170  break;
171  }
172  }
173  return $txt;
174  }
175 
177  ilPropertyFormGUI $form,
178  ilTextInputGUI $title,
179  string $active_language
180  ): void {
181  if (count($this->translations) <= 1) {
182  return;
183  }
184  $default = $this->getDefaultTranslation();
185  if ($default->getLangKey() != $active_language) {
186  $title->setInfo($default->getLangKey() . ': ' . $default->getTitle());
187  }
188  if ($this->getTranslation($active_language) instanceof ilAdvancedMDRecordTranslation) {
189  $title->setValue($this->getTranslation($active_language)->getTitle());
190  }
191  }
192 
194  ilPropertyFormGUI $form,
195  ilTextAreaInputGUI $description,
196  string $active_language
197  ): void {
198  if (count($this->translations) <= 1) {
199  return;
200  }
201  $default = $this->getDefaultTranslation();
202  if ($default->getLangKey() != $active_language) {
203  $description->setInfo($default->getLangKey() . ': ' . $default->getDescription());
204  }
205  if ($this->getTranslation($active_language) instanceof ilAdvancedMDRecordTranslation) {
206  $description->setValue($this->getTranslation($active_language)->getDescription());
207  }
208  }
209 
210  public function updateTranslations(string $active_language, string $title, string $description): void
211  {
212  $translation = $this->getTranslation($active_language);
213  if (!$translation instanceof ilAdvancedMDRecordTranslation) {
214  return;
215  }
216  $translation->setTitle($title);
217  $translation->setDescription($description);
218  $translation->update();
219  }
220 
221  public function getTitleForLanguage(string $language): string
222  {
223  if ($this->getTranslation($language) && strlen($this->getTranslation($language)->getTitle())) {
224  return $this->getTranslation($language)->getTitle();
225  }
226  return $this->record->getTitle();
227  }
228 
229  public function getDescriptionForLanguage(string $language): string
230  {
231  if ($this->getTranslation($language) && strlen($this->getTranslation($language)->getDescription())) {
232  return $this->getTranslation($language)->getDescription();
233  }
234  return $this->record->getDescription();
235  }
236 
237  public function toXML(ilXmlWriter $writer): ilXmlWriter
238  {
239  if (!count($this->getTranslations())) {
240  return $writer;
241  }
242 
243  $writer->xmlStartTag(
244  'RecordTranslations',
245  [
246  'defaultLanguage' => $this->getDefaultLanguage()
247  ]
248  );
249  foreach ($this->getTranslations() as $translation) {
250  $writer->xmlStartTag(
251  'RecordTranslation',
252  [
253  'language' => $translation->getLangKey()
254  ]
255  );
256  $writer->xmlElement('RecordTranslationTitle', [], $translation->getTitle());
257  $writer->xmlElement('RecordTranslationDescription', [], $translation->getDescription());
258  $writer->xmlEndTag('RecordTranslation');
259  }
260  $writer->xmlEndTag('RecordTranslations');
261  return $writer;
262  }
263 }
$res
Definition: ltiservices.php:66
Class ilAdvancedMDRecordTranslation.
modifyTranslationInfoForDescription(ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
getTranslations()
array<string, ilAdvancedMDRecordTranslation>
xmlEndTag(string $tag)
Writes an endtag.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _getInstanceByRecordId(int $a_record_id)
modifyTranslationInfoForTitle(ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
global $DIC
Definition: shib_login.php:22
Class ilAdvancedMDRecordTranslation.
$txt
Definition: error.php:31
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)