ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  static protected $instances = array();
37 
44  private function __construct($a_obj_id)
45  {
46  global $ilDB;
47 
48  $this->db = $ilDB;
49 
50  $this->setObjId($a_obj_id);
51 
52  if ($this->getObjId() <= 0)
53  {
54  include_once("./Services/Object/exceptions/class.ilObjectException.php");
55  throw new ilObjectException("ilObjectTranslation: No object ID passed.");
56  }
57 
58  $this->read();
59  }
60 
67  static function getInstance($a_obj_id)
68  {
69  if (!isset(self::$instances[$a_obj_id]))
70  {
71  self::$instances[$a_obj_id] = new ilObjectTranslation($a_obj_id);
72  }
73 
74  return self::$instances[$a_obj_id];
75  }
76 
77 
83  function setObjId($a_val)
84  {
85  $this->obj_id = $a_val;
86  }
87 
93  function getObjId()
94  {
95  return $this->obj_id;
96  }
97 
103  function setMasterLanguage($a_val)
104  {
105  $this->master_lang = $a_val;
106  }
107 
113  function getMasterLanguage()
114  {
115  return $this->master_lang;
116  }
117 
123  function setLanguages(array $a_val)
124  {
125  $this->languages = $a_val;
126  }
127 
133  function getLanguages()
134  {
135  return $this->languages;
136  }
137 
146  function addLanguage($a_lang, $a_title, $a_description, $a_default)
147  {
148  if ($a_lang != "" && !isset($this->languages[$a_lang]))
149  {
150  if ($a_default)
151  {
152  foreach ($this->languages as $k => $l)
153  {
154  $this->languages[$k]["lang_default"] = false;
155  }
156  }
157  $this->languages[$a_lang] = array("lang_code" => $a_lang, "lang_default" => $a_default,
158  "title" => $a_title, "description" => $a_description);
159  }
160  }
161 
167  function getDefaultTitle()
168  {
169  foreach ($this->languages as $l)
170  {
171  if ($l["lang_default"])
172  {
173  return $l["title"];
174  }
175  }
176  return "";
177  }
178 
184  function setDefaultTitle($a_title)
185  {
186  foreach ($this->languages as $k => $l)
187  {
188  if ($l["lang_default"])
189  {
190  $this->languages[$k]["title"] = $a_title;
191  }
192  }
193  }
194 
201  {
202  foreach ($this->languages as $l)
203  {
204  if ($l["lang_default"])
205  {
206  return $l["description"];
207  }
208  }
209  return "";
210  }
211 
217  function setDefaultDescription($a_description)
218  {
219  foreach ($this->languages as $k => $l)
220  {
221  if ($l["lang_default"])
222  {
223  $this->languages[$k]["description"] = $a_description;
224  }
225  }
226  }
227 
228 
234  function removeLanguage($a_lang)
235  {
236  if ($a_lang != $this->getMasterLanguage())
237  {
238  unset($this->languages[$a_lang]);
239  }
240  }
241 
242 
248  protected function setContentActivated($a_val)
249  {
250  $this->content_activated = $a_val;
251  }
252 
259  {
261  }
262 
266  function read()
267  {
268  $set = $this->db->query("SELECT * FROM obj_content_master_lng ".
269  " WHERE obj_id = ".$this->db->quote($this->getObjId(), "integer")
270  );
271  if ($rec = $this->db->fetchAssoc($set))
272  {
273  $this->setMasterLanguage($rec["master_lang"]);
274  $this->setContentActivated(true);
275  }
276  else
277  {
278  $this->setContentActivated(false);
279  }
280 
281  $this->setLanguages(array());
282  $set = $this->db->query("SELECT * FROM object_translation ".
283  " WHERE obj_id = ".$this->db->quote($this->getObjId(), "integer")
284  );
285  while ($rec = $this->db->fetchAssoc($set))
286  {
287  $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
288  }
289  }
290 
294  function delete()
295  {
296  $this->db->manipulate("DELETE FROM obj_content_master_lng ".
297  " WHERE obj_id = ".$this->db->quote($this->getObjId(), "integer")
298  );
299  $this->db->manipulate("DELETE FROM object_translation ".
300  " WHERE obj_id = ".$this->db->quote($this->getObjId(), "integer")
301  );
302  }
303 
308  {
309  $this->db->manipulate("DELETE FROM obj_content_master_lng ".
310  " WHERE obj_id = ".$this->db->quote($this->getObjId(), "integer")
311  );
312  }
313 
317  function save()
318  {
319  $this->delete();
320 
321  if ($this->getMasterLanguage() != "")
322  {
323  $this->db->manipulate("INSERT INTO obj_content_master_lng ".
324  "(obj_id, master_lang) VALUES (".
325  $this->db->quote($this->getObjId(), "integer").",".
326  $this->db->quote($this->getMasterLanguage(), "text").
327  ")");
328 
329  // ensure that an entry for the master language exists and is the default
330  if (!isset($this->languages[$this->getMasterLanguage()]))
331  {
332  $this->languages[$this->getMasterLanguage()] = array("title" => "",
333  "description" => "", "lang_code" => $this->getMasterLanguage(), "lang_default" => 1);
334  }
335  foreach ($this->languages as $l => $trans)
336  {
337  if ($l == $this->getMasterLanguage())
338  {
339  $this->languages[$l]["lang_default"] = 1;
340  }
341  else
342  {
343  $this->languages[$l]["lang_default"] = 0;
344  }
345  }
346  }
347 
348  foreach ($this->getLanguages() as $l => $trans)
349  {
350  $this->db->manipulate("INSERT INTO object_translation ".
351  "(obj_id, title, description, lang_code, lang_default) VALUES (".
352  $this->db->quote($this->getObjId(), "integer").",".
353  $this->db->quote($trans["title"], "text").",".
354  $this->db->quote($trans["description"], "text").",".
355  $this->db->quote($l, "text").",".
356  $this->db->quote($trans["lang_default"], "integer").
357  ")");
358  }
359  }
360 
368  function copy($a_obj_id)
369  {
370  $target_ml = new ilObjectTranslation($a_obj_id);
371  $target_ml->setMasterLanguage($this->getMasterLanguage());
372  $target_ml->setLanguages($this->getLanguages());
373  $target_ml->save();
374  return $target_ml;
375  }
376 
377 
388  function getEffectiveContentLang($a_lang, $a_parent_type)
389  {
390  $langs = $this->getLanguages();
391  if ($this->getContentActivated() &&
392  isset($langs[$a_lang]) &&
393  ilPageObject::_exists($a_parent_type, $this->getObjId(), $a_lang))
394  {
395  return $a_lang;
396  }
397  return "-";
398  }
399 
400 
401 
402 }
403 
404 ?>