ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAdvancedMDFieldTranslations.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 array $instances = [];
16 
17  private int $record_id;
19 
23  private array $definitions;
24 
28  private array $translations = [];
29 
31  private string $default_language = '';
32 
36  private ilDBInterface $db;
37 
41  private ilLanguage $lng;
42 
43  private function __construct(int $record_id)
44  {
45  global $DIC;
46 
47  $this->db = $DIC->database();
48  $this->lng = $DIC->language();
49  $this->lng->loadLanguageModule('meta');
50 
51  $this->record_id = $record_id;
52  $this->record_translations = ilAdvancedMDRecordTranslations::getInstanceByRecordId($this->record_id);
53  $this->read();
54  }
55 
56  public static function getInstanceByRecordId(int $record_id): ilAdvancedMDFieldTranslations
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 getRecordId(): int
65  {
66  return $this->record_id;
67  }
68 
69  public function getDefaultLanguage(): string
70  {
72  }
73 
79  public function getActivatedLanguages(int $field_id, bool $with_default = true): array
80  {
81  $activated = [];
82  foreach ($this->getTranslations($field_id) as $language => $translation) {
83  if ($language == self::getDefaultLanguage() && !$with_default) {
84  continue;
85  }
86  $activated[] = $language;
87  }
88  return $activated;
89  }
90 
91  public function isConfigured(int $field_id, string $lang_key): bool
92  {
93  if (!$this->record_translations->isConfigured($lang_key)) {
94  return false;
95  }
96  return isset($this->translations[$field_id][$lang_key]);
97  }
98 
99  public function getTranslation(int $field_id, string $lang_key): ?ilAdvancedMDFieldTranslation
100  {
101  if (!$this->isConfigured($field_id, $lang_key)) {
102  return null;
103  }
104  return $this->translations[$field_id][$lang_key];
105  }
106 
110  public function getTranslations(int $field_id): array
111  {
112  if (isset($this->translations[$field_id])) {
113  return $this->translations[$field_id];
114  }
115  return [];
116  }
117 
121  public function getDefaultTranslation(int $field_id): ?ilAdvancedMDFieldTranslation
122  {
123  foreach ($this->getTranslations($field_id) as $translation) {
124  if ($translation->getLangKey() == $this->default_language) {
125  return $translation;
126  }
127  }
128  return null;
129  }
130 
131  public function read(): void
132  {
133  $query = 'select fi.field_id tfield, de.field_id ofield, fi.title, fi.description, ri.lang_code ' .
134  'from adv_md_record_int ri join adv_mdf_definition de on ri.record_id = de.record_id ' .
135  'left join adv_md_field_int fi on (ri.lang_code = fi.lang_code and de.field_id = fi.field_id) ' .
136  'where ri.record_id = ' . $this->db->quote($this->getRecordId(), ilDBConstants::T_INTEGER);
137  $res = $this->db->query($query);
138 
139  $this->record = ilAdvancedMDRecord::_getInstanceByRecordId($this->record_id);
140  $this->default_language = $this->record->getDefaultLanguage();
141 
142  $this->translations = [];
143  $this->definitions = ilAdvancedMDFieldDefinition::getInstancesByRecordId($this->record_id, false);
144  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
145  $this->translations[$row->ofield][$row->lang_code] = new ilAdvancedMDFieldTranslation(
146  (int) $row->ofield,
147  (string) $row->title,
148  (string) $row->description,
149  (string) $row->lang_code
150  );
151  if ((string) $row->lang_code == $this->default_language && $row->tfield == null) {
152  $this->translations[(int) $row->ofield][(string) $row->lang_code]->setTitle($this->definitions[(int) $row->ofield]->getTitle());
153  $this->translations[(int) $row->ofield][(string) $row->lang_code]->setDescription(
154  $this->definitions[(int) $row->ofield]->getDescription()
155  );
156  }
157  }
158  }
159 
160  public function getFormTranslationInfo(int $field_id, string $active_language): string
161  {
162  if (count($this->getTranslations($field_id)) <= 1) {
163  return '';
164  }
165 
166  $txt = '';
167  $txt = $this->lng->txt('md_adv_int_current') . ' ' . $this->lng->txt('meta_l_' . $active_language);
168  $txt .= ', ';
169  foreach ($this->getTranslations($field_id) as $translation) {
170  if ($translation->getLangKey() == $this->getDefaultLanguage()) {
171  $txt .= ($this->lng->txt('md_adv_int_default') . ' ' . $this->lng->txt('meta_l_' . $translation->getLangKey()));
172  break;
173  }
174  }
175  return $txt;
176  }
177 
179  int $field_id,
180  ilPropertyFormGUI $form,
181  ilTextInputGUI $title,
182  string $active_language
183  ): void {
184  if (!strlen($active_language)) {
185  return;
186  }
187  $show_info = ($active_language !== $this->getDefaultLanguage() && $this->getDefaultLanguage());
188  $default = $this->getDefaultTranslation($field_id);
189  if ($default instanceof ilAdvancedMDFieldTranslation && $show_info) {
190  $title->setInfo($default->getLangKey() . ': ' . $default->getTitle());
191  }
192  if ($this->getTranslation($field_id, $active_language) instanceof ilAdvancedMDFieldTranslation) {
193  $title->setValue($this->getTranslation($field_id, $active_language)->getTitle());
194  }
195  }
196 
198  int $field_id,
199  ilPropertyFormGUI $form,
200  ilTextAreaInputGUI $description,
201  string $active_language
202  ): void {
203  if (!strlen($active_language)) {
204  return;
205  }
206  $show_info = ($active_language !== $this->getDefaultLanguage() && $this->getDefaultLanguage());
207  $default = $this->getDefaultTranslation($field_id);
208  if ($default instanceof ilAdvancedMDFieldTranslation && $show_info) {
209  $description->setInfo($default->getLangKey() . ': ' . $default->getDescription());
210  }
211  if ($this->getTranslation($field_id, $active_language) instanceof ilAdvancedMDFieldTranslation) {
212  $description->setValue($this->getTranslation($field_id, $active_language)->getDescription());
213  }
214  }
215 
216  public function updateFromForm(int $field_id, string $active_language, ilPropertyFormGUI $form): void
217  {
218  $translation = $this->getTranslation($field_id, $active_language);
219  if (!$translation instanceof ilAdvancedMDFieldTranslation) {
220  return;
221  }
222  $translation->setTitle($form->getInput('title'));
223  $translation->setDescription($form->getInput('description'));
224  $translation->update();
225  }
226 
227  public function getTitleForLanguage(int $field_id, string $language): string
228  {
229  if ($this->getTranslation($field_id, $language) && strlen($this->getTranslation(
230  $field_id,
231  $language
232  )->getTitle())) {
233  return $this->getTranslation($field_id, $language)->getTitle();
234  }
235  if (
236  $this->getTranslation($field_id, $this->getDefaultLanguage()) &&
237  strlen(($this->getTranslation($field_id, $this->getDefaultLanguage())->getTitle()))
238  ) {
239  return $this->getTranslation($field_id, $this->getDefaultLanguage())->getTitle();
240  }
241  if ($this->definitions[$field_id] instanceof ilAdvancedMDFieldDefinition) {
242  return $this->definitions[$field_id]->getTitle();
243  }
244  return '';
245  }
246 
247  public function getDescriptionForLanguage(int $field_id, string $language): string
248  {
249  if ($this->getTranslation($field_id, $language) && strlen($this->getTranslation(
250  $field_id,
251  $language
252  )->getDescription())) {
253  return $this->getTranslation($field_id, $language)->getDescription();
254  }
255  if ($this->getTranslation($field_id, $this->getDefaultLanguage()) &&
256  strlen($this->getTranslation($field_id, $this->getDefaultLanguage())->getDescription())
257  ) {
258  return $this->getTranslation($field_id, $this->getDefaultLanguage())->getDescription();
259  }
260  if ($this->definitions[$field_id] instanceof ilAdvancedMDFieldDefinition) {
261  return $this->definitions[$field_id]->getDescription();
262  }
263  return '';
264  }
265 }
$res
Definition: ltiservices.php:69
Class ilAdvancedMDFieldTranslations.
modifyTranslationInfoForDescription(int $field_id, ilPropertyFormGUI $form, ilTextAreaInputGUI $description, string $active_language)
modifyTranslationInfoForTitle(int $field_id, ilPropertyFormGUI $form, ilTextInputGUI $title, string $active_language)
getActivatedLanguages(int $field_id, bool $with_default=true)
Class ilAdvancedMDRecordTranslation.
static getInstancesByRecordId( $a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
getDescriptionForLanguage(int $field_id, string $language)
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
global $DIC
Definition: feed.php:28
static _getInstanceByRecordId(int $a_record_id)
getTranslation(int $field_id, string $lang_key)
isConfigured(int $field_id, string $lang_key)
$query
getTitleForLanguage(int $field_id, string $language)
ilAdvancedMDRecordTranslations $record_translations
$txt
Definition: error.php:13
This class represents a text area property in a property form.
getFormTranslationInfo(int $field_id, string $active_language)
Class ilAdvancedMDFieldTranslation.
updateFromForm(int $field_id, string $active_language, ilPropertyFormGUI $form)