ILIAS  release_8 Revision v8.24
class.ilObjectTranslation.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
48{
49 protected static array $instances = [];
50
51 protected ilDBInterface $db;
52 protected int $obj_id;
53
54 protected string $master_lang = "";
58 protected array $languages = [];
59 protected bool $content_activated = false;
60 protected string $fallback_language = "";
61
62 private function __construct(int $obj_id)
63 {
64 global $DIC;
65 $this->db = $DIC->database();
66
67 $this->setObjId($obj_id);
68
69 if ($obj_id <= 0) {
70 throw new ilObjectException("ilObjectTranslation: No object ID passed.");
71 }
72
73 $this->read();
74 }
75
76 public static function getInstance(int $obj_id): ilObjectTranslation
77 {
78 if (!isset(self::$instances[$obj_id])) {
79 self::$instances[$obj_id] = new ilObjectTranslation($obj_id);
80 }
81
82 return self::$instances[$obj_id];
83 }
84
85 public function setObjId(int $val): void
86 {
87 $this->obj_id = $val;
88 }
89
90 public function getObjId(): int
91 {
92 return $this->obj_id;
93 }
94
95 public function setMasterLanguage(string $val): void
96 {
97 $this->master_lang = $val;
98 }
99
100 public function getMasterLanguage(): string
101 {
102 return $this->master_lang;
103 }
104
108 public function setLanguages(array $val): void
109 {
110 $this->languages = $val;
111 }
112
116 public function getLanguages(): array
117 {
118 return $this->languages;
119 }
120
121 public function setFallbackLanguage(string $val): void
122 {
123 $this->fallback_language = $val;
124 }
125
126 public function getFallbackLanguage(): string
127 {
129 }
130
131 public function addLanguage(
132 string $lang,
133 string $title,
134 string $description,
135 bool $default,
136 bool $force = false
137 ): void {
138 if ($lang !== "" && (!isset($this->languages[$lang]) || $force)) {
139 if ($default) {
140 foreach ($this->languages as $l) {
141 $l->setDefault(false);
142 }
143 }
144 $this->languages[$lang] = new ilObjectTranslationLanguage(
145 $lang,
146 $title,
147 $description,
148 $default
149 );
150 }
151 }
152
153 public function getDefaultTitle(): string
154 {
155 if ($this->fallback_language !== ""
156 && array_key_exists($this->fallback_language, $this->languages)) {
157 return $this->languages[$this->fallback_language]->getTitle();
158 } else {
159 foreach ($this->languages as $l) {
160 if ($l->isDefault()) {
161 return $l->getTitle();
162 }
163 }
164 }
165 if (count($this->languages) == 0) {
166 return ilObject::_lookupTitle($this->getObjId());
167 }
168 return '';
169 }
170
171 public function setDefaultTitle(string $title): void
172 {
173 if ($this->getFallbackLanguage() !== ''
174 && isset($this->languages[$this->getFallbackLanguage()])) {
175 $this->languages[$this->getFallbackLanguage()]->setTitle($title);
176 return;
177 }
178
179 foreach ($this->languages as $l) {
180 if ($l->isDefault()) {
181 $l->setTitle($title);
182 return;
183 }
184 }
185 }
186
187 public function getDefaultDescription(): string
188 {
189 if ($this->getFallbackLanguage() !== ''
190 && array_key_exists($this->fallback_language, $this->languages)) {
191 return $this->languages[$this->getFallbackLanguage()]->getDescription();
192 }
193 foreach ($this->languages as $l) {
194 if ($l->isDefault()) {
195 return $l->getDescription();
196 }
197 }
198 if (count($this->languages) == 0) {
199 return ilObject::_lookupDescription($this->getObjId());
200 }
201 return '';
202 }
203
204 public function setDefaultDescription(string $description): void
205 {
206 if ($this->getFallbackLanguage() !== ''
207 && isset($this->languages[$this->getFallbackLanguage()])) {
208 $this->languages[$this->getFallbackLanguage()]
209 ->setDescription($description);
210 return;
211 }
212
213 foreach ($this->languages as $l) {
214 if ($l->isDefault()) {
215 $l->setDescription($description);
216 return;
217 }
218 }
219 }
220
221 public function getDefaultLanguage(): string
222 {
223 if ($this->getFallbackLanguage() !== "") {
224 return $this->getFallbackLanguage();
225 }
226 foreach ($this->languages as $l) {
227 if ($l->isDefault()) {
228 return $l->getLanguageCode();
229 }
230 }
231 return "";
232 }
233
234 public function removeLanguage(string $lang): void
235 {
236 if ($lang != $this->getMasterLanguage()) {
237 unset($this->languages[$lang]);
238 }
239 }
240
241 protected function setContentActivated(bool $val): void
242 {
243 $this->content_activated = $val;
244 }
245
246 public function getContentActivated(): bool
247 {
248 return $this->content_activated;
249 }
250
251 public function read(): void
252 {
253 $sql =
254 "SELECT obj_id, master_lang, fallback_lang" . PHP_EOL
255 . "FROM obj_content_master_lng" . PHP_EOL
256 . "WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer") . PHP_EOL
257 ;
258 $result = $this->db->query($sql);
259 if ($row = $this->db->fetchAssoc($result)) {
260 $this->setMasterLanguage($row["master_lang"]);
261 $this->setFallbackLanguage($row["fallback_lang"] ?? '');
262 $this->setContentActivated(true);
263 } else {
264 $this->setContentActivated(false);
265 }
266
267 $this->setLanguages([]);
268
269 $sql =
270 "SELECT title, description, lang_code, lang_default" . PHP_EOL
271 . "FROM object_translation" . PHP_EOL
272 . "WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer") . PHP_EOL
273 ;
274 $result = $this->db->query($sql);
275 while ($row = $this->db->fetchAssoc($result)) {
276 $this->addLanguage($row["lang_code"], (string) $row["title"], (string) $row["description"], (bool) $row["lang_default"]);
277 }
278 }
279
280 public function delete(): void
281 {
282 $this->db->manipulate(
283 "DELETE FROM obj_content_master_lng " .
284 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
285 );
286 $this->db->manipulate(
287 "DELETE FROM object_translation " .
288 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
289 );
290 }
291
292 public function deactivateContentTranslation(): void
293 {
294 $this->db->manipulate(
295 "DELETE FROM obj_content_master_lng " .
296 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
297 );
298 }
299
300 public function save(): void
301 {
302 $this->delete();
303
304 if ($this->getMasterLanguage() != "") {
305 $values = [
306 "obj_id" => ["integer", $this->getObjId()],
307 "master_lang" => ["text", $this->getMasterLanguage()],
308 "fallback_lang" => ["text", $this->getFallbackLanguage()]
309 ];
310
311 $this->db->insert("obj_content_master_lng", $values);
312 // ensure that an entry for the master language exists and is the default
313 if (!isset($this->languages[$this->getMasterLanguage()])) {
314 $this->languages[$this->getMasterLanguage()] = new ilObjectTranslationLanguage(
315 "",
316 "",
317 $this->getMasterLanguage(),
318 true
319 );
320 }
321 foreach ($this->languages as $trans) {
322 if ($trans->getLanguageCode() === $this->getMasterLanguage()) {
323 $trans->setDefault(true);
324 } else {
325 $trans->setDefault(false);
326 }
327 }
328 }
329
330 foreach ($this->getLanguages() as $trans) {
331 $values = [
332 "obj_id" => ["integer", $this->getObjId()],
333 "title" => ["text", $trans->getTitle()],
334 "description" => ["text", $trans->getDescription()],
335 "lang_code" => ["text", $trans->getLanguageCode()],
336 "lang_default" => ["integer", $trans->isDefault()],
337 ];
338 $this->db->insert("object_translation", $values);
339 }
340 }
341
345 public function copy(int $obj_id): ilObjectTranslation
346 {
347 $target_ml = new ilObjectTranslation($obj_id);
348 $target_ml->setMasterLanguage($this->getMasterLanguage());
349 $target_ml->setFallbackLanguage($this->getFallbackLanguage());
350 $target_ml->setLanguages($this->getLanguages());
351 $target_ml->save();
352 return $target_ml;
353 }
354
355
366 public function getEffectiveContentLang(string $lang, string $parent_type): string
367 {
368 $langs = $this->getLanguages();
369 $page_lang_key = ($lang == $this->getMasterLanguage())
370 ? "-"
371 : $lang;
372 if ($this->getContentActivated() &&
373 isset($langs[$lang]) &&
374 ilPageObject::_exists($parent_type, $this->getObjId(), $page_lang_key)) {
375 if ($lang == $this->getMasterLanguage()) {
376 return "-";
377 }
378 return $lang;
379 }
380 if ($this->getContentActivated() &&
381 isset($langs[$this->getFallbackLanguage()]) &&
382 ilPageObject::_exists($parent_type, $this->getObjId(), $this->getFallbackLanguage())) {
383 return $this->getFallbackLanguage();
384 }
385 return "-";
386 }
387}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addLanguage(string $lang, string $title, string $description, bool $default, bool $force=false)
copy(int $obj_id)
Copy multilingual settings.
setDefaultDescription(string $description)
static getInstance(int $obj_id)
getEffectiveContentLang(string $lang, string $parent_type)
Get effective language for given language.
static _lookupTitle(int $obj_id)
static _lookupDescription(int $obj_id)
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$lang
Definition: xapiexit.php:26