ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjectTranslation.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
34{
35 protected $db;
36 protected $obj_id;
37 protected $master_lang;
38 protected $languages = array();
39 protected $content_activated = false;
40 protected static $instances = array();
41 protected $fallback_language = "";
42
49 private function __construct($a_obj_id)
50 {
51 global $DIC;
52
53 $ilDB = $DIC->database();
54
55 $this->db = $ilDB;
56
57 $this->setObjId($a_obj_id);
58
59 if ($this->getObjId() <= 0) {
60 include_once("./Services/Object/exceptions/class.ilObjectException.php");
61 throw new ilObjectException("ilObjectTranslation: No object ID passed.");
62 }
63
64 $this->read();
65 }
66
73 public static function getInstance($a_obj_id)
74 {
75 if (!isset(self::$instances[$a_obj_id])) {
76 self::$instances[$a_obj_id] = new ilObjectTranslation($a_obj_id);
77 }
78
79 return self::$instances[$a_obj_id];
80 }
81
82
88 public function setObjId($a_val)
89 {
90 $this->obj_id = $a_val;
91 }
92
98 public function getObjId()
99 {
100 return $this->obj_id;
101 }
102
108 public function setMasterLanguage($a_val)
109 {
110 $this->master_lang = $a_val;
111 }
112
118 public function getMasterLanguage()
119 {
120 return $this->master_lang;
121 }
122
128 public function setLanguages(array $a_val)
129 {
130 $this->languages = $a_val;
131 }
132
138 public function getLanguages()
139 {
140 return $this->languages;
141 }
142
147 public function setFallbackLanguage($a_val)
148 {
149 $this->fallback_language = $a_val;
150 }
151
156 public function getFallbackLanguage()
157 {
159 }
160
161
170 public function addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force = false)
171 {
172 if ($a_lang != "" && (!isset($this->languages[$a_lang]) || $a_force)) {
173 if ($a_default) {
174 foreach ($this->languages as $k => $l) {
175 $this->languages[$k]["lang_default"] = false;
176 }
177 }
178 $this->languages[$a_lang] = array("lang_code" => $a_lang, "lang_default" => $a_default,
179 "title" => $a_title, "description" => $a_description);
180 }
181 }
182
188 public function getDefaultTitle()
189 {
190 if ($this->getFallbackLanguage() != "") {
191 return $this->languages[$this->getFallbackLanguage()]["title"];
192 } else {
193 foreach ($this->languages as $l) {
194 if ($l["lang_default"]) {
195 return $l["title"];
196 }
197 }
198 }
199 if (count($this->languages) == 0) {
200 return ilObject::_lookupTitle($this->getObjId());
201 }
202 return "";
203 }
204
210 public function setDefaultTitle($a_title)
211 {
212 if ($this->getFallbackLanguage() != "") {
213 $this->languages[$this->getFallbackLanguage()]["title"] = $a_title;
214 } else {
215 foreach ($this->languages as $k => $l) {
216 if ($l["lang_default"]) {
217 $this->languages[$k]["title"] = $a_title;
218 }
219 }
220 }
221 }
222
228 public function getDefaultDescription()
229 {
230 if ($this->getFallbackLanguage() != "") {
231 return $this->languages[$this->getFallbackLanguage()]["description"];
232 }
233 foreach ($this->languages as $l) {
234 if ($l["lang_default"]) {
235 return $l["description"];
236 }
237 }
238 if (count($this->languages) == 0) {
239 return ilObject::_lookupDescription($this->getObjId());
240 }
241 return "";
242 }
243
249 public function setDefaultDescription($a_description)
250 {
251 if ($this->getFallbackLanguage() != "") {
252 $this->languages[$this->getFallbackLanguage()]["description"] = $a_description;
253 } else {
254 foreach ($this->languages as $k => $l) {
255 if ($l["lang_default"]) {
256 $this->languages[$k]["description"] = $a_description;
257 }
258 }
259 }
260 }
261
267 public function getDefaultLanguage()
268 {
269 if ($this->getFallbackLanguage() != "") {
270 return $this->getFallbackLanguage();
271 }
272 foreach ($this->languages as $l) {
273 if ($l["lang_default"]) {
274 return $l["lang_code"];
275 }
276 }
277 return "";
278 }
279
280
286 public function removeLanguage($a_lang)
287 {
288 if ($a_lang != $this->getMasterLanguage()) {
289 unset($this->languages[$a_lang]);
290 }
291 }
292
293
299 protected function setContentActivated($a_val)
300 {
301 $this->content_activated = $a_val;
302 }
303
309 public function getContentActivated()
310 {
312 }
313
317 public function read()
318 {
319 $set = $this->db->query(
320 "SELECT * FROM obj_content_master_lng " .
321 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
322 );
323 if ($rec = $this->db->fetchAssoc($set)) {
324 $this->setMasterLanguage($rec["master_lang"]);
325 $this->setFallbackLanguage($rec["fallback_lang"]);
326 $this->setContentActivated(true);
327 } else {
328 $this->setContentActivated(false);
329 }
330
331 $this->setLanguages(array());
332 $set = $this->db->query(
333 "SELECT * FROM object_translation " .
334 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
335 );
336 while ($rec = $this->db->fetchAssoc($set)) {
337 $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
338 }
339 }
340
344 public function delete()
345 {
346 $this->db->manipulate(
347 "DELETE FROM obj_content_master_lng " .
348 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
349 );
350 $this->db->manipulate(
351 "DELETE FROM object_translation " .
352 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
353 );
354 }
355
360 {
361 $this->db->manipulate(
362 "DELETE FROM obj_content_master_lng " .
363 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
364 );
365 }
366
370 public function save()
371 {
372 $this->delete();
373
374 if ($this->getMasterLanguage() != "") {
375 $this->db->manipulate("INSERT INTO obj_content_master_lng " .
376 "(obj_id, master_lang, fallback_lang) VALUES (" .
377 $this->db->quote($this->getObjId(), "integer") . "," .
378 $this->db->quote($this->getMasterLanguage(), "text") . "," .
379 $this->db->quote($this->getFallbackLanguage(), "text") .
380 ")");
381 // ensure that an entry for the master language exists and is the default
382 if (!isset($this->languages[$this->getMasterLanguage()])) {
383 $this->languages[$this->getMasterLanguage()] = array("title" => "",
384 "description" => "", "lang_code" => $this->getMasterLanguage(), "lang_default" => 1);
385 }
386 foreach ($this->languages as $l => $trans) {
387 if ($l == $this->getMasterLanguage()) {
388 $this->languages[$l]["lang_default"] = 1;
389 } else {
390 $this->languages[$l]["lang_default"] = 0;
391 }
392 }
393 }
394 foreach ($this->getLanguages() as $l => $trans) {
395 $this->db->manipulate($t = "INSERT INTO object_translation " .
396 "(obj_id, title, description, lang_code, lang_default) VALUES (" .
397 $this->db->quote($this->getObjId(), "integer") . "," .
398 $this->db->quote($trans["title"], "text") . "," .
399 $this->db->quote($trans["description"], "text") . "," .
400 $this->db->quote($l, "text") . "," .
401 $this->db->quote($trans["lang_default"], "integer") .
402 ")");
403 }
404 }
405
413 public function copy($a_obj_id)
414 {
415 $target_ml = new ilObjectTranslation($a_obj_id);
416 $target_ml->setMasterLanguage($this->getMasterLanguage());
417 $target_ml->setFallbackLanguage($this->getFallbackLanguage());
418 $target_ml->setLanguages($this->getLanguages());
419 $target_ml->save();
420 return $target_ml;
421 }
422
423
434 public function getEffectiveContentLang($a_lang, $a_parent_type)
435 {
436 $langs = $this->getLanguages();
437 $page_lang_key = ($a_lang == $this->getMasterLanguage())
438 ? "-"
439 : $a_lang;
440 if ($this->getContentActivated() &&
441 isset($langs[$a_lang]) &&
442 ilPageObject::_exists($a_parent_type, $this->getObjId(), $page_lang_key)) {
443 if ($a_lang == $this->getMasterLanguage()) {
444 return "-";
445 }
446 return $a_lang;
447 }
448 if ($this->getContentActivated() &&
449 isset($langs[$this->getFallbackLanguage()]) &&
450 ilPageObject::_exists($a_parent_type, $this->getObjId(), $this->getFallbackLanguage())) {
451 return $this->getFallbackLanguage();
452 }
453 return "-";
454 }
455}
An exception for terminatinating execution or to throw for unit testing.
Base exception class for object service.
Class handles translation mode for an object.
getDefaultTitle()
Get default title.
deactivateContentTranslation()
Deactivate content translation.
getDefaultDescription()
Get default description.
getEffectiveContentLang($a_lang, $a_parent_type)
Get effective language for given language.
copy($a_obj_id)
Copy multilinguality settings.
addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force=false)
Add language.
removeLanguage($a_lang)
Remove language.
setDefaultDescription($a_description)
Set default description.
getDefaultLanguage()
Get default language.
getFallbackLanguage()
Get fallback language.
setLanguages(array $a_val)
Set languages.
setObjId($a_val)
Set object id.
setFallbackLanguage($a_val)
Set fallback language.
setDefaultTitle($a_title)
Set default title.
setMasterLanguage($a_val)
Set master language.
__construct($a_obj_id)
Constructor.
static getInstance($a_obj_id)
Get instance.
getContentActivated()
Get activated for content.
setContentActivated($a_val)
Set activated for content.
getMasterLanguage()
Get master language.
static _lookupTitle($a_id)
lookup object title
static _lookupDescription($a_id)
lookup object description
static _exists($a_parent_type, $a_id, $a_lang="", $a_no_cache=false)
Checks whether page exists.
global $DIC
Definition: goto.php:24
global $ilDB