ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 return "";
172 }
173
179 public function setDefaultTitle($a_title)
180 {
181 foreach ($this->languages as $k => $l) {
182 if ($l["lang_default"]) {
183 $this->languages[$k]["title"] = $a_title;
184 }
185 }
186 }
187
193 public function getDefaultDescription()
194 {
195 foreach ($this->languages as $l) {
196 if ($l["lang_default"]) {
197 return $l["description"];
198 }
199 }
200 return "";
201 }
202
208 public function setDefaultDescription($a_description)
209 {
210 foreach ($this->languages as $k => $l) {
211 if ($l["lang_default"]) {
212 $this->languages[$k]["description"] = $a_description;
213 }
214 }
215 }
216
217
223 public function removeLanguage($a_lang)
224 {
225 if ($a_lang != $this->getMasterLanguage()) {
226 unset($this->languages[$a_lang]);
227 }
228 }
229
230
236 protected function setContentActivated($a_val)
237 {
238 $this->content_activated = $a_val;
239 }
240
246 public function getContentActivated()
247 {
249 }
250
254 public function read()
255 {
256 $set = $this->db->query(
257 "SELECT * FROM obj_content_master_lng " .
258 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
259 );
260 if ($rec = $this->db->fetchAssoc($set)) {
261 $this->setMasterLanguage($rec["master_lang"]);
262 $this->setContentActivated(true);
263 } else {
264 $this->setContentActivated(false);
265 }
266
267 $this->setLanguages(array());
268 $set = $this->db->query(
269 "SELECT * FROM object_translation " .
270 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
271 );
272 while ($rec = $this->db->fetchAssoc($set)) {
273 $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
274 }
275 }
276
280 public function delete()
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
296 {
297 $this->db->manipulate(
298 "DELETE FROM obj_content_master_lng " .
299 " WHERE obj_id = " . $this->db->quote($this->getObjId(), "integer")
300 );
301 }
302
306 public function save()
307 {
308 $this->delete();
309
310 if ($this->getMasterLanguage() != "") {
311 $this->db->manipulate("INSERT INTO obj_content_master_lng " .
312 "(obj_id, master_lang) VALUES (" .
313 $this->db->quote($this->getObjId(), "integer") . "," .
314 $this->db->quote($this->getMasterLanguage(), "text") .
315 ")");
316
317 // ensure that an entry for the master language exists and is the default
318 if (!isset($this->languages[$this->getMasterLanguage()])) {
319 $this->languages[$this->getMasterLanguage()] = array("title" => "",
320 "description" => "", "lang_code" => $this->getMasterLanguage(), "lang_default" => 1);
321 }
322 foreach ($this->languages as $l => $trans) {
323 if ($l == $this->getMasterLanguage()) {
324 $this->languages[$l]["lang_default"] = 1;
325 } else {
326 $this->languages[$l]["lang_default"] = 0;
327 }
328 }
329 }
330
331 foreach ($this->getLanguages() as $l => $trans) {
332 $this->db->manipulate($t = "INSERT INTO object_translation " .
333 "(obj_id, title, description, lang_code, lang_default) VALUES (" .
334 $this->db->quote($this->getObjId(), "integer") . "," .
335 $this->db->quote($trans["title"], "text") . "," .
336 $this->db->quote($trans["description"], "text") . "," .
337 $this->db->quote($l, "text") . "," .
338 $this->db->quote($trans["lang_default"], "integer") .
339 ")");
340 }
341 }
342
350 public function copy($a_obj_id)
351 {
352 $target_ml = new ilObjectTranslation($a_obj_id);
353 $target_ml->setMasterLanguage($this->getMasterLanguage());
354 $target_ml->setLanguages($this->getLanguages());
355 $target_ml->save();
356 return $target_ml;
357 }
358
359
370 public function getEffectiveContentLang($a_lang, $a_parent_type)
371 {
372 $langs = $this->getLanguages();
373 if ($this->getContentActivated() &&
374 isset($langs[$a_lang]) &&
375 ilPageObject::_exists($a_parent_type, $this->getObjId(), $a_lang)) {
376 return $a_lang;
377 }
378 return "-";
379 }
380}
global $l
Definition: afr.php:30
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.
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 _exists($a_parent_type, $a_id, $a_lang="", $a_no_cache=false)
Checks whether page exists.
global $DIC
Definition: saml.php:7
global $ilDB