ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilADTLocalizedTextDBBridge.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  public function getTable(): ?string
28  {
29  return 'adv_md_values_ltext';
30  }
31 
35  protected function isValidADT(ilADT $adt): bool
36  {
37  return $adt instanceof ilADTLocalizedText;
38  }
39 
43  public function readRecord(array $a_row): void
44  {
45  $active_languages = $this->getADT()->getCopyOfDefinition()->getActiveLanguages();
46  $default_language = $this->getADT()->getCopyOfDefinition()->getDefaultLanguage();
47  $language = $a_row[$this->getElementId() . '_language'];
48 
49  if (strcmp($language, $default_language) === 0) {
50  $this->getADT()->setText($a_row[$this->getElementId() . '_translation']);
51  } elseif (!strlen($default_language)) {
52  $this->getADT()->setText($a_row[$this->getElementId() . '_translation']);
53  }
54  if (in_array($language, $active_languages)) {
55  $this->getADT()->setTranslation(
56  $language,
57  (string) $a_row[$this->getElementId() . '_translation']
58  );
59  }
60  }
61 
65  public function prepareInsert(array &$a_fields): void
66  {
67  $a_fields[$this->getElementId()] = [ilDBConstants::T_TEXT, $this->getADT()->getText()];
68  }
69 
73  public function afterInsert(): void
74  {
75  $this->afterUpdate();
76  }
77 
78  public function getAdditionalPrimaryFields(): array
79  {
80  return [
81  'value_index' => [ilDBConstants::T_TEXT, '']
82  ];
83  }
84 
88  public function afterUpdate(): void
89  {
90  if (!$this->getADT()->getCopyOfDefinition()->supportsTranslations()) {
91  return;
92  }
93  $this->deleteTranslations();
94  $this->insertTranslations();
95  }
96 
100  protected function deleteTranslations(): void
101  {
102  $this->db->manipulate(
103  $q =
104  'delete from ' . $this->getTable() . ' ' .
105  'where ' . $this->buildPrimaryWhere() . ' ' .
106  'and value_index != ' . $this->db->quote('', ilDBConstants::T_TEXT)
107  );
108  }
109 
115  protected function insertTranslations(): void
116  {
117  foreach ($this->getADT()->getTranslations() as $language => $value) {
118  $fields = $this->getPrimary();
119  $fields['value_index'] = [ilDBConstants::T_TEXT, $language];
120  $fields['value'] = [ilDBConstants::T_TEXT, $value];
121  $this->db->insert($this->getTable(), $fields);
122  }
123  }
124 }
buildPrimaryWhere()
Convert primary keys array to sql string.
getPrimary()
Get primary fields.
getElementId()
Get element id.
ADT base class.
Definition: class.ilADT.php:11
Class ilADTLocalizedText.
Class ilADTLocalizedTextDBBridge.
ADT DB bridge base class.
insertTranslations()
Save all translations TODO: Translations are always persisted for all active languages, even if the translation is an empty string.