ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
13 {
17  protected $lng;
18 
19  protected $db;
20  protected $obj_id;
21  protected $languages = array();
22  protected $type = "";
23  protected static $instances = array();
24 
32  private function __construct($a_obj_id, $a_type)
33  {
34  global $DIC;
35 
36  $this->lng = $DIC->language();
37  $ilDB = $DIC->database();
38 
39  $this->db = $ilDB;
40 
41  $this->setObjId($a_obj_id);
42  $this->setType($a_type);
43 
44  if ($this->getObjId() <= 0) {
45  include_once("./Services/Object/exceptions/class.ilObjectException.php");
46  throw new ilObjectException("ilObjectTranslation: No object ID passed.");
47  }
48 
49  $this->read();
50  }
51 
58  public static function getInstance($a_obj_id, $a_type)
59  {
60  if (!isset(self::$instances[$a_type][$a_obj_id])) {
61  self::$instances[$a_type][$a_obj_id] = new self($a_obj_id, $a_type);
62  }
63 
64  return self::$instances[$a_type][$a_obj_id];
65  }
66 
67 
73  public function setObjId($a_val)
74  {
75  $this->obj_id = $a_val;
76  }
77 
83  public function getObjId()
84  {
85  return $this->obj_id;
86  }
87 
93  public function setLanguages(array $a_val)
94  {
95  $this->languages = $a_val;
96  }
97 
103  public function getLanguages()
104  {
105  return $this->languages;
106  }
107 
111  public function getType()
112  {
113  return $this->type;
114  }
115 
119  public function setType($type)
120  {
121  $this->type = $type;
122  }
123 
124  public function getDefaultLanguage()
125  {
126  $lng = $this->lng;
127 
128  foreach ($this->languages as $k => $v) {
129  if ($v["lang_default"]) {
130  return $k;
131  }
132  }
133 
134  return $lng->getDefaultLanguage();
135  }
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->getDefaultLanguage()) {
226  unset($this->languages[$a_lang]);
227  }
228  }
229 
233  public function read()
234  {
235  $this->setLanguages(array());
236  $set = $this->db->query(
237  "SELECT * FROM il_translations " .
238  " WHERE id = " . $this->db->quote($this->getObjId(), "integer") .
239  " AND id_type = " . $this->db->quote($this->getType(), "text")
240  );
241  while ($rec = $this->db->fetchAssoc($set)) {
242  $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
243  }
244  }
245 
249  public function delete()
250  {
251  $this->db->manipulate(
252  "DELETE FROM il_translations " .
253  " WHERE id = " . $this->db->quote($this->getObjId(), "integer") .
254  " AND id_type = " . $this->db->quote($this->getType(), "text")
255  );
256  }
257 
261  public function save()
262  {
263  $this->delete();
264 
265  foreach ($this->getLanguages() as $l => $trans) {
266  $this->db->manipulate($t = "INSERT INTO il_translations " .
267  "(id, id_type, title, description, lang_code, lang_default) VALUES (" .
268  $this->db->quote($this->getObjId(), "integer") . "," .
269  $this->db->quote($this->getType(), "text") . "," .
270  $this->db->quote($trans["title"], "text") . "," .
271  $this->db->quote($trans["description"], "text") . "," .
272  $this->db->quote($l, "text") . "," .
273  $this->db->quote($trans["lang_default"], "integer") .
274  ")");
275  }
276  }
277 
285  public function copy($a_obj_id)
286  {
287  $target_ml = new self($a_obj_id, $this->getType());
288  $target_ml->setLanguages($this->getLanguages());
289  $target_ml->save();
290  return $target_ml;
291  }
292 
293 
294 
300  public function toXml(ilXmlWriter $writer)
301  {
302  $writer->xmlStartTag('translations');
303 
304  foreach ($this->getLanguages() as $k => $v) {
305  $writer->xmlStartTag('translation', array('language' => $k, 'default' => $v['lang_default'] ? 1 : 0));
306  $writer->xmlElement('title', array(), $v['title']);
307  $writer->xmlElement('description', array(), $v['description']);
308  $writer->xmlEndTag('translation');
309  }
310  $writer->xmlEndTag('translations');
311 
312  return $writer;
313  }
314 
321  public function fromXML(SimpleXMLElement $root)
322  {
323  if ($root->translations) {
324  $root = $root->translations;
325  }
326 
327  foreach ($root->translation as $trans) {
328  $this->addLanguage(
329  (string) trim($trans["language"]),
330  (string) trim($trans->title),
331  (string) trim($trans->description),
332  (int) $trans["default"] != 0?true:false
333  );
334  }
335  }
336 }
static getInstance($a_obj_id, $a_type)
Get instance.
toXml(ilXmlWriter $writer)
Export.
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
setLanguages(array $a_val)
Set languages.
fromXML(SimpleXMLElement $root)
xml import
Base exception class for object service.
XML writer class.
setDefaultDescription($a_description)
Set default description.
getDefaultTitle()
Get default title.
xmlEndTag($tag)
Writes an endtag.
setObjId($a_val)
Set object id.
global $DIC
Definition: goto.php:24
getDefaultDescription()
Get default description.
getLanguages()
Get languages.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
removeLanguage($a_lang)
Remove language.
setDefaultTitle($a_title)
Set default title.
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.