ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
30{
31 protected $db;
32 protected $obj_id;
33 protected $master_lang;
34 protected $languages = array();
35 protected $content_activated = false;
36 protected static $instances = array();
37
44 private function __construct($a_obj_id)
45 {
46 global $DIC;
47
48 $ilDB = $DIC->database();
49
50 $this->db = $ilDB;
51
52 $this->setObjId($a_obj_id);
53
54 if ($this->getObjId() <= 0) {
55 include_once("./Services/Object/exceptions/class.ilObjectException.php");
56 throw new ilObjectException("ilObjectTranslation: No object ID passed.");
57 }
58
59 $this->read();
60 }
61
68 public static function getInstance($a_obj_id)
69 {
70 if (!isset(self::$instances[$a_obj_id])) {
71 self::$instances[$a_obj_id] = new ilObjectTranslation($a_obj_id);
72 }
73
74 return self::$instances[$a_obj_id];
75 }
76
77
83 public function setObjId($a_val)
84 {
85 $this->obj_id = $a_val;
86 }
87
93 public function getObjId()
94 {
95 return $this->obj_id;
96 }
97
103 public function setMasterLanguage($a_val)
104 {
105 $this->master_lang = $a_val;
106 }
107
113 public function getMasterLanguage()
114 {
115 return $this->master_lang;
116 }
117
123 public function setLanguages(array $a_val)
124 {
125 $this->languages = $a_val;
126 }
127
133 public function getLanguages()
134 {
135 return $this->languages;
136 }
137
146 public function addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force = false)
147 {
148 if ($a_lang != "" && (!isset($this->languages[$a_lang]) || $a_force)) {
149 if ($a_default) {
150 foreach ($this->languages as $k => $l) {
151 $this->languages[$k]["lang_default"] = false;
152 }
153 }
154 $this->languages[$a_lang] = array("lang_code" => $a_lang, "lang_default" => $a_default,
155 "title" => $a_title, "description" => $a_description);
156 }
157 }
158
164 public function getDefaultTitle()
165 {
166 foreach ($this->languages as $l) {
167 if ($l["lang_default"]) {
168 return $l["title"];
169 }
170 }
171 if (count($this->languages) == 0) {
172 return ilObject::_lookupTitle($this->getObjId());
173 }
174 return "";
175 }
176
182 public function setDefaultTitle($a_title)
183 {
184 foreach ($this->languages as $k => $l) {
185 if ($l["lang_default"]) {
186 $this->languages[$k]["title"] = $a_title;
187 }
188 }
189 }
190
196 public function getDefaultDescription()
197 {
198 foreach ($this->languages as $l) {
199 if ($l["lang_default"]) {
200 return $l["description"];
201 }
202 }
203 if (count($this->languages) == 0) {
204 return ilObject::_lookupDescription($this->getObjId());
205 }
206 return "";
207 }
208
214 public function setDefaultDescription($a_description)
215 {
216 foreach ($this->languages as $k => $l) {
217 if ($l["lang_default"]) {
218 $this->languages[$k]["description"] = $a_description;
219 }
220 }
221 }
222
228 public function getDefaultLanguage()
229 {
230 foreach ($this->languages as $l) {
231 if ($l["lang_default"]) {
232 return $l["lang_code"];
233 }
234 }
235 return "";
236 }
237
238
244 public function removeLanguage($a_lang)
245 {
246 if ($a_lang != $this->getMasterLanguage()) {
247 unset($this->languages[$a_lang]);
248 }
249 }
250
251
257 protected function setContentActivated($a_val)
258 {
259 $this->content_activated = $a_val;
260 }
261
267 public function getContentActivated()
268 {
270 }
271
275 public function read()
276 {
277 $set = $this->db->query(
278 "SELECT * FROM obj_content_master_lng " .
279 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
280 );
281 if ($rec = $this->db->fetchAssoc($set)) {
282 $this->setMasterLanguage($rec["master_lang"]);
283 $this->setContentActivated(true);
284 } else {
285 $this->setContentActivated(false);
286 }
287
288 $this->setLanguages(array());
289 $set = $this->db->query(
290 "SELECT * FROM object_translation " .
291 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
292 );
293 while ($rec = $this->db->fetchAssoc($set)) {
294 $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
295 }
296 }
297
301 public function delete()
302 {
303 $this->db->manipulate(
304 "DELETE FROM obj_content_master_lng " .
305 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
306 );
307 $this->db->manipulate(
308 "DELETE FROM object_translation " .
309 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
310 );
311 }
312
317 {
318 $this->db->manipulate(
319 "DELETE FROM obj_content_master_lng " .
320 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
321 );
322 }
323
327 public function save()
328 {
329 $this->delete();
330
331 if ($this->getMasterLanguage() != "") {
332 $this->db->manipulate("INSERT INTO obj_content_master_lng " .
333 "(obj_id, master_lang) VALUES (" .
334 $this->db->quote($this->getObjId(), "integer") . "," .
335 $this->db->quote($this->getMasterLanguage(), "text") .
336 ")");
337
338 // ensure that an entry for the master language exists and is the default
339 if (!isset($this->languages[$this->getMasterLanguage()])) {
340 $this->languages[$this->getMasterLanguage()] = array("title" => "",
341 "description" => "", "lang_code" => $this->getMasterLanguage(), "lang_default" => 1);
342 }
343 foreach ($this->languages as $l => $trans) {
344 if ($l == $this->getMasterLanguage()) {
345 $this->languages[$l]["lang_default"] = 1;
346 } else {
347 $this->languages[$l]["lang_default"] = 0;
348 }
349 }
350 }
351
352 foreach ($this->getLanguages() as $l => $trans) {
353 $this->db->manipulate($t = "INSERT INTO object_translation " .
354 "(obj_id, title, description, lang_code, lang_default) VALUES (" .
355 $this->db->quote($this->getObjId(), "integer") . "," .
356 $this->db->quote($trans["title"], "text") . "," .
357 $this->db->quote($trans["description"], "text") . "," .
358 $this->db->quote($l, "text") . "," .
359 $this->db->quote($trans["lang_default"], "integer") .
360 ")");
361 }
362 }
363
371 public function copy($a_obj_id)
372 {
373 $target_ml = new ilObjectTranslation($a_obj_id);
374 $target_ml->setMasterLanguage($this->getMasterLanguage());
375 $target_ml->setLanguages($this->getLanguages());
376 $target_ml->save();
377 return $target_ml;
378 }
379
380
391 public function getEffectiveContentLang($a_lang, $a_parent_type)
392 {
393 $langs = $this->getLanguages();
394 if ($this->getContentActivated() &&
395 isset($langs[$a_lang]) &&
396 ilPageObject::_exists($a_parent_type, $this->getObjId(), $a_lang)) {
397 if ($a_lang == $this->getMasterLanguage()) {
398 return "-";
399 }
400 return $a_lang;
401 }
402 return "-";
403 }
404}
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.
setLanguages(array $a_val)
Set languages.
setObjId($a_val)
Set object id.
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 $ilDB
$DIC
Definition: xapitoken.php:46