ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMultilingualism.php
Go to the documentation of this file.
1<?php
2
25{
26 protected ilLanguage $lng;
27 protected ilDBInterface $db;
28 protected int $obj_id;
30 protected array $languages = [];
31 protected string $type = "";
33 protected static array $instances = [];
34
38 private function __construct(
39 int $a_obj_id,
40 string $a_type
41 ) {
42 global $DIC;
43
44 $this->lng = $DIC->language();
45 $ilDB = $DIC->database();
46
47 $this->db = $ilDB;
48
49 $this->setObjId($a_obj_id);
50 $this->setType($a_type);
51
52 if ($this->getObjId() <= 0) {
53 throw new ilObjectException("Translation: No object ID passed.");
54 }
55
56 $this->read();
57 }
58
62 public static function getInstance(int $a_obj_id, string $a_type): self
63 {
64 if (!isset(self::$instances[$a_type][$a_obj_id])) {
65 self::$instances[$a_type][$a_obj_id] = new self($a_obj_id, $a_type);
66 }
67
68 return self::$instances[$a_type][$a_obj_id];
69 }
70
71 public function setObjId(int $a_val): void
72 {
73 $this->obj_id = $a_val;
74 }
75
76 public function getObjId(): int
77 {
78 return $this->obj_id;
79 }
80
85 public function setLanguages(array $a_val): void
86 {
87 $this->languages = $a_val;
88 }
89
93 public function getLanguages(): array
94 {
95 return $this->languages;
96 }
97
98 public function getType(): string
99 {
100 return $this->type;
101 }
102
103 public function setType(string $type): void
104 {
105 $this->type = $type;
106 }
107
108 public function getDefaultLanguage(): string
109 {
111
112 foreach ($this->languages as $k => $v) {
113 if ($v["lang_default"]) {
114 return $k;
115 }
116 }
117
118 return $lng->getDefaultLanguage();
119 }
120
121
131 public function addLanguage(
132 string $a_lang,
133 string $a_title,
134 string $a_description,
135 bool $a_default,
136 bool $a_force = false
137 ): void {
138 if ($a_lang !== "" && (!isset($this->languages[$a_lang]) || $a_force)) {
139 if ($a_default) {
140 foreach ($this->languages as $k => $l) {
141 $this->languages[$k]["lang_default"] = false;
142 }
143 }
144 $this->languages[$a_lang] = [
145 "lang_code" => $a_lang,
146 "lang_default" => $a_default,
147 "title" => $a_title,
148 "description" => $a_description
149 ];
150 }
151 }
152
157 public function getDefaultTitle(): string
158 {
159 foreach ($this->languages as $l) {
160 if ($l["lang_default"]) {
161 return $l["title"];
162 }
163 }
164 return "";
165 }
166
170 public function setDefaultTitle(string $a_title): void
171 {
172 foreach ($this->languages as $k => $l) {
173 if ($l["lang_default"]) {
174 $this->languages[$k]["title"] = $a_title;
175 }
176 }
177 }
178
182 public function getDefaultDescription(): string
183 {
184 foreach ($this->languages as $l) {
185 if ($l["lang_default"]) {
186 return $l["description"];
187 }
188 }
189 return "";
190 }
191
195 public function setDefaultDescription(string $a_description): void
196 {
197 foreach ($this->languages as $k => $l) {
198 if ($l["lang_default"]) {
199 $this->languages[$k]["description"] = $a_description;
200 }
201 }
202 }
203
204
208 public function removeLanguage(string $a_lang): void
209 {
210 if ($a_lang !== $this->getDefaultLanguage()) {
211 unset($this->languages[$a_lang]);
212 }
213 }
214
215 public function read(): void
216 {
217 $this->setLanguages(array());
218 $set = $this->db->query(
219 "SELECT * FROM il_dt_translations " .
220 " WHERE id = " . $this->db->quote($this->getObjId(), "integer") .
221 " AND id_type = " . $this->db->quote($this->getType(), "text")
222 );
223 while ($rec = $this->db->fetchAssoc($set)) {
224 $this->addLanguage(
225 $rec["lang_code"],
226 (string) $rec["title"],
227 (string) $rec["description"],
228 (bool) $rec["lang_default"]
229 );
230 }
231 }
232
233 public function delete(): void
234 {
235 $this->db->manipulate(
236 "DELETE FROM il_dt_translations " .
237 " WHERE id = " . $this->db->quote($this->getObjId(), "integer") .
238 " AND id_type = " . $this->db->quote($this->getType(), "text")
239 );
240 }
241
242 public function save(): void
243 {
244 $this->delete();
245
246 foreach ($this->getLanguages() as $l => $trans) {
247 $this->db->manipulate($t = "INSERT INTO il_dt_translations " .
248 "(id, id_type, title, description, lang_code, lang_default) VALUES (" .
249 $this->db->quote($this->getObjId(), "integer") . "," .
250 $this->db->quote($this->getType(), "text") . "," .
251 $this->db->quote($trans["title"], "text") . "," .
252 $this->db->quote($trans["description"], "text") . "," .
253 $this->db->quote($l, "text") . "," .
254 $this->db->quote($trans["lang_default"], "integer") .
255 ")");
256 }
257 }
258
263 public function copy(int $a_obj_id): self
264 {
265 $target_ml = new self($a_obj_id, $this->getType());
266 $target_ml->setLanguages($this->getLanguages());
267 $target_ml->save();
268 return $target_ml;
269 }
270
271
272
276 public function toXml(
277 ilXmlWriter $writer
278 ): ilXmlWriter {
279 $writer->xmlStartTag('translations');
280
281 foreach ($this->getLanguages() as $k => $v) {
282 $writer->xmlStartTag('translation', array('language' => $k, 'default' => $v['lang_default'] ? 1 : 0));
283 $writer->xmlElement('title', array(), $v['title']);
284 $writer->xmlElement('description', array(), $v['description']);
285 $writer->xmlEndTag('translation');
286 }
287 $writer->xmlEndTag('translations');
288
289 return $writer;
290 }
291
296 public function fromXML(SimpleXMLElement $root): void
297 {
298 if ($root->translations) {
299 $root = $root->translations;
300 }
301
302 foreach ($root->translation as $trans) {
303 $this->addLanguage(
304 trim($trans["language"]),
305 trim($trans->title),
306 trim($trans->description),
307 (int) $trans["default"] !== 0
308 );
309 }
310 }
311}
language handling
getDefaultLanguage()
Return default language.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $a_obj_id, string $a_type)
removeLanguage(string $a_lang)
toXml(ilXmlWriter $writer)
Export to xml.
copy(int $a_obj_id)
Copy multilinguality settings.
setDefaultTitle(string $a_title)
Set title for default language.
getDefaultTitle()
Get default title.
static getInstance(int $a_obj_id, string $a_type)
setDefaultDescription(string $a_description)
Set default description.
fromXML(SimpleXMLElement $root)
xml import
addLanguage(string $a_lang, string $a_title, string $a_description, bool $a_default, bool $a_force=false)
Add language.
Base exception class for object service.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
Interface ilDBInterface.
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26