ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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{
34 protected $lng;
35
36 protected $db;
37 protected $obj_id;
38 protected $languages = array();
39 protected $type = "";
40 protected static $instances = array();
41
49 private function __construct($a_obj_id, $a_type)
50 {
51 global $DIC;
52
53 $this->lng = $DIC->language();
54 $ilDB = $DIC->database();
55
56 $this->db = $ilDB;
57
58 $this->setObjId($a_obj_id);
59 $this->setType($a_type);
60
61 if ($this->getObjId() <= 0) {
62 include_once("./Services/Object/exceptions/class.ilObjectException.php");
63 throw new ilObjectException("ilObjectTranslation: No object ID passed.");
64 }
65
66 $this->read();
67 }
68
75 public static function getInstance($a_obj_id, $a_type)
76 {
77 if (!isset(self::$instances[$a_type][$a_obj_id])) {
78 self::$instances[$a_type][$a_obj_id] = new self($a_obj_id, $a_type);
79 }
80
81 return self::$instances[$a_type][$a_obj_id];
82 }
83
84
90 public function setObjId($a_val)
91 {
92 $this->obj_id = $a_val;
93 }
94
100 public function getObjId()
101 {
102 return $this->obj_id;
103 }
104
110 public function setLanguages(array $a_val)
111 {
112 $this->languages = $a_val;
113 }
114
120 public function getLanguages()
121 {
122 return $this->languages;
123 }
124
128 public function getType()
129 {
130 return $this->type;
131 }
132
136 public function setType($type)
137 {
138 $this->type = $type;
139 }
140
141 public function getDefaultLanguage()
142 {
144
145 foreach ($this->languages as $k => $v) {
146 if ($v["lang_default"]) {
147 return $k;
148 }
149 }
150
151 return $lng->getDefaultLanguage();
152 }
153
154
163 public function addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force = false)
164 {
165 if ($a_lang != "" && (!isset($this->languages[$a_lang]) || $a_force)) {
166 if ($a_default) {
167 foreach ($this->languages as $k => $l) {
168 $this->languages[$k]["lang_default"] = false;
169 }
170 }
171 $this->languages[$a_lang] = array("lang_code" => $a_lang, "lang_default" => $a_default,
172 "title" => $a_title, "description" => $a_description);
173 }
174 }
175
181 public function getDefaultTitle()
182 {
183 foreach ($this->languages as $l) {
184 if ($l["lang_default"]) {
185 return $l["title"];
186 }
187 }
188 return "";
189 }
190
196 public function setDefaultTitle($a_title)
197 {
198 foreach ($this->languages as $k => $l) {
199 if ($l["lang_default"]) {
200 $this->languages[$k]["title"] = $a_title;
201 }
202 }
203 }
204
210 public function getDefaultDescription()
211 {
212 foreach ($this->languages as $l) {
213 if ($l["lang_default"]) {
214 return $l["description"];
215 }
216 }
217 return "";
218 }
219
225 public function setDefaultDescription($a_description)
226 {
227 foreach ($this->languages as $k => $l) {
228 if ($l["lang_default"]) {
229 $this->languages[$k]["description"] = $a_description;
230 }
231 }
232 }
233
234
240 public function removeLanguage($a_lang)
241 {
242 if ($a_lang != $this->getDefaultLanguage()) {
243 unset($this->languages[$a_lang]);
244 }
245 }
246
250 public function read()
251 {
252 $this->setLanguages(array());
253 $set = $this->db->query(
254 "SELECT * FROM il_translations " .
255 " WHERE id = " . $this->db->quote($this->getObjId(), "integer") .
256 " AND id_type = " . $this->db->quote($this->getType(), "text")
257 );
258 while ($rec = $this->db->fetchAssoc($set)) {
259 $this->addLanguage($rec["lang_code"], $rec["title"], $rec["description"], $rec["lang_default"]);
260 }
261 }
262
266 public function delete()
267 {
268 $this->db->manipulate(
269 "DELETE FROM il_translations " .
270 " WHERE id = " . $this->db->quote($this->getObjId(), "integer") .
271 " AND id_type = " . $this->db->quote($this->getType(), "text")
272 );
273 }
274
278 public function save()
279 {
280 $this->delete();
281
282 foreach ($this->getLanguages() as $l => $trans) {
283 $this->db->manipulate($t = "INSERT INTO il_translations " .
284 "(id, id_type, title, description, lang_code, lang_default) VALUES (" .
285 $this->db->quote($this->getObjId(), "integer") . "," .
286 $this->db->quote($this->getType(), "text") . "," .
287 $this->db->quote($trans["title"], "text") . "," .
288 $this->db->quote($trans["description"], "text") . "," .
289 $this->db->quote($l, "text") . "," .
290 $this->db->quote($trans["lang_default"], "integer") .
291 ")");
292 }
293 }
294
302 public function copy($a_obj_id)
303 {
304 $target_ml = new self($a_obj_id, $this->getType());
305 $target_ml->setLanguages($this->getLanguages());
306 $target_ml->save();
307 return $target_ml;
308 }
309
310
311
317 public function toXml(ilXmlWriter $writer)
318 {
319 $writer->xmlStartTag('translations');
320
321 foreach ($this->getLanguages() as $k => $v) {
322 $writer->xmlStartTag('translation', array('language' => $k, 'default' => $v['lang_default'] ? 1 : 0));
323 $writer->xmlElement('title', array(), $v['title']);
324 $writer->xmlElement('description', array(), $v['description']);
325 $writer->xmlEndTag('translation');
326 }
327 $writer->xmlEndTag('translations');
328
329 return $writer;
330 }
331
338 public function fromXML(SimpleXMLElement $root)
339 {
340 if ($root->translations) {
341 $root = $root->translations;
342 }
343
344 foreach ($root->translation as $trans) {
345 $this->addLanguage(
346 (string) trim($trans["language"]),
347 (string) trim($trans->title),
348 (string) trim($trans->description),
349 (int) $trans["default"] != 0?true:false
350 );
351 }
352 }
353}
global $l
Definition: afr.php:30
An exception for terminatinating execution or to throw for unit testing.
Class handles translation mode for an object.
toXml(ilXmlWriter $writer)
Export.
setLanguages(array $a_val)
Set languages.
setObjId($a_val)
Set object id.
copy($a_obj_id)
Copy multilinguality settings.
addLanguage($a_lang, $a_title, $a_description, $a_default, $a_force=false)
Add language.
__construct($a_obj_id, $a_type)
Constructor.
removeLanguage($a_lang)
Remove language.
static getInstance($a_obj_id, $a_type)
Get instance.
getDefaultTitle()
Get default title.
getDefaultDescription()
Get default description.
fromXML(SimpleXMLElement $root)
xml import
setDefaultDescription($a_description)
Set default description.
setDefaultTitle($a_title)
Set default title.
Base exception class for object service.
XML writer class.
xmlEndTag($tag)
Writes an endtag.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
global $DIC
Definition: saml.php:7
global $ilDB
$a_type
Definition: workflow.php:92