ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilMultilingualism.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 $languages = array();
34  protected $type = "";
35  static protected $instances = array();
36 
44  private function __construct($a_obj_id, $a_type)
45  {
46  global $ilDB;
47 
48  $this->db = $ilDB;
49 
50  $this->setObjId($a_obj_id);
51  $this->setType($a_type);
52 
53  if ($this->getObjId() <= 0)
54  {
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  static function getInstance($a_obj_id, $a_type)
69  {
70  if (!isset(self::$instances[$a_type][$a_obj_id]))
71  {
72  self::$instances[$a_type][$a_obj_id] = new self($a_obj_id, $a_type);
73  }
74 
75  return self::$instances[$a_type][$a_obj_id];
76  }
77 
78 
84  function setObjId($a_val)
85  {
86  $this->obj_id = $a_val;
87  }
88 
94  function getObjId()
95  {
96  return $this->obj_id;
97  }
98 
104  function setLanguages(array $a_val)
105  {
106  $this->languages = $a_val;
107  }
108 
114  function getLanguages()
115  {
116  return $this->languages;
117  }
118 
122  public function getType()
123  {
124  return $this->type;
125  }
126 
130  public function setType($type)
131  {
132  $this->type = $type;
133  }
134 
135  public function getDefaultLanguage()
136  {
137  global $lng;
138 
139  foreach($this->languages as $k => $v)
140  {
141  if($v["lang_default"])
142  {
143  return $k;
144  }
145  }
146 
147  return $lng->getDefaultLanguage();
148  }
149 
150 
159  function addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force = false)
160  {
161  if ($a_lang != "" && (!isset($this->languages[$a_lang]) || $a_force))
162  {
163  if ($a_default)
164  {
165  foreach ($this->languages as $k => $l)
166  {
167  $this->languages[$k]["lang_default"] = false;
168  }
169  }
170  $this->languages[$a_lang] = array("lang_code" => $a_lang, "lang_default" => $a_default,
171  "title" => $a_title, "description" => $a_description);
172  }
173  }
174 
180  function getDefaultTitle()
181  {
182  foreach ($this->languages as $l)
183  {
184  if ($l["lang_default"])
185  {
186  return $l["title"];
187  }
188  }
189  return "";
190  }
191 
197  function setDefaultTitle($a_title)
198  {
199  foreach ($this->languages as $k => $l)
200  {
201  if ($l["lang_default"])
202  {
203  $this->languages[$k]["title"] = $a_title;
204  }
205  }
206  }
207 
214  {
215  foreach ($this->languages as $l)
216  {
217  if ($l["lang_default"])
218  {
219  return $l["description"];
220  }
221  }
222  return "";
223  }
224 
230  function setDefaultDescription($a_description)
231  {
232  foreach ($this->languages as $k => $l)
233  {
234  if ($l["lang_default"])
235  {
236  $this->languages[$k]["description"] = $a_description;
237  }
238  }
239  }
240 
241 
247  function removeLanguage($a_lang)
248  {
249  if($a_lang != $this->getDefaultLanguage())
250  {
251  unset($this->languages[$a_lang]);
252  }
253  }
254 
258  function read()
259  {
260  $this->setLanguages(array());
261  $set = $this->db->query("SELECT * FROM il_translations ".
262  " WHERE id = ".$this->db->quote($this->getObjId(), "integer") .
263  " AND id_type = " . $this->db->quote($this->getType(), "text")
264  );
265  while ($rec = $this->db->fetchAssoc($set))
266  {
267  $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
268  }
269  }
270 
274  function delete()
275  {
276  $this->db->manipulate("DELETE FROM il_translations ".
277  " WHERE id = ".$this->db->quote($this->getObjId(), "integer").
278  " AND id_type = " . $this->db->quote($this->getType(), "text")
279  );
280  }
281 
285  function save()
286  {
287  $this->delete();
288 
289  foreach ($this->getLanguages() as $l => $trans)
290  {
291  $this->db->manipulate($t = "INSERT INTO il_translations ".
292  "(id, id_type, title, description, lang_code, lang_default) VALUES (".
293  $this->db->quote($this->getObjId(), "integer").",".
294  $this->db->quote($this->getType(), "text").",".
295  $this->db->quote($trans["title"], "text").",".
296  $this->db->quote($trans["description"], "text").",".
297  $this->db->quote($l, "text").",".
298  $this->db->quote($trans["lang_default"], "integer").
299  ")");
300  }
301  }
302 
310  function copy($a_obj_id)
311  {
312  $target_ml = new self($a_obj_id, $this->getType());
313  $target_ml->setLanguages($this->getLanguages());
314  $target_ml->save();
315  return $target_ml;
316  }
317 
318 
319 
325  public function toXml(ilXmlWriter $writer)
326  {
327  $writer->xmlStartTag('translations');
328 
329  foreach ($this->getLanguages() as $k => $v)
330  {
331  $writer->xmlStartTag('translation',array('language' => $k, 'default' => $v['lang_default'] ? 1 : 0));
332  $writer->xmlElement('title',array(),$v['title']);
333  $writer->xmlElement('description', array(), $v['description']);
334  $writer->xmlEndTag('translation');
335  }
336  $writer->xmlEndTag('translations');
337 
338  return $writer;
339  }
340 
347  public function fromXML(SimpleXMLElement $root)
348  {
349  if($root->translations)
350  {
351  $root = $root->translations;
352  }
353 
354  foreach($root->translation as $trans)
355  {
356  $this->addLanguage(
357  (string)trim($trans["language"]),
358  (string)trim($trans->title),
359  (string)trim($trans->description),
360  (int)$trans["default"] != 0?true:false
361  );
362  }
363  }
364 
365 }
366 
367 ?>
static getInstance($a_obj_id, $a_type)
Get instance.
toXml(ilXmlWriter $writer)
Export.
setLanguages(array $a_val)
Set languages.
fromXML(SimpleXMLElement $root)
xml import
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
Base exception class for object service.
XML writer class.
xmlElement($tag, $attrs=NULL, $data=Null, $encode=TRUE, $escape=TRUE)
Writes a basic element (no children, just textual content)
setDefaultDescription($a_description)
Set default description.
getDefaultTitle()
Get default title.
xmlEndTag($tag)
Writes an endtag.
setObjId($a_val)
Set object id.
$a_type
Definition: workflow.php:93
getDefaultDescription()
Get default description.
Create styles array
The data for the language used.
getLanguages()
Get languages.
removeLanguage($a_lang)
Remove language.
global $l
Definition: afr.php:30
setDefaultTitle($a_title)
Set default title.
global $lng
Definition: privfeed.php:17
global $ilDB
__construct($a_obj_id, $a_type)
Constructor.
copy($a_obj_id)
Copy multilinguality settings.
Class handles translation mode for an object.
addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force=false)
Add language.