ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjLanguageFolder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
32 {
37  public string $lang_default;
38 
44  public string $lang_user;
45 
51  public string $lang_path;
52 
56  public string $separator;
57 
61  public array $languages;
62 
68  public function __construct(int $a_id, bool $a_call_by_reference = true)
69  {
70  $this->type = "lngf";
71  parent::__construct($a_id, $a_call_by_reference);
72 
73  $this->lang_path = $this->lng->lang_path;
74  $this->lang_default = $this->lng->lang_default;
75  $this->lang_user = $this->lng->lang_user;
76  $this->separator = $this->lng->separator;
77  }
78 
93  public function getLanguages(): array
94  {
95  $this->lng->loadLanguageModule("meta");
96 
97  // set path to directory where lang-files reside
98  $d = dir($this->lang_path);
99  $tmpPath = getcwd();
100  chdir($this->lang_path);
101 
102  $languages = [];
103 
104  // get available lang-files
105  while ($entry = $d->read()) {
106  if (is_file($entry) && (preg_match("~(^ilias_.{2}\.lang$)~", $entry))) {
107  $lang_key = substr($entry, 6, 2);
108  $languages[$lang_key] = ""; // long names will be set in class Out
109  }
110  }
111 
112  $language = array();
113  $tmp_array = array_keys($languages);
114  $lang_keys = array();
115 
116  // now get languages from database
117  if ($lang_db = ilObject::_getObjectsByType("lng")) {
118  foreach ($lang_db as $lang) {
119  // set values
120  $lang_key = $lang["title"];
121  $languages[$lang_key] = $lang;
122  $lang_keys[] = $lang_key;
123 
124  $languages[$lang_key]["info"] = "";
125  $languages[$lang_key]["status"] = "";
126 
127  // determine default language and language of current user
128  if ($lang_key === $this->lang_user) {
129  $languages[$lang_key]["status"] = "in_use";
130  }
131 
132  if ($lang_key === $this->lang_default) {
133  $languages[$lang_key]["status"] = "system_language";
134  }
135 
136  // check if files are missing
137  if (count($tmp_array) > 0 && !in_array($lang_key, $tmp_array, true)) {
138  $languages[$lang_key]["info"] = "file_not_found";
139  }
140  }
141  }
142 
143  // compute new languages
144  foreach ($languages as $lang_key => $lang_data) {
145  if (!in_array($lang_key, $lang_keys, true)) {
146  $languages[$lang_key] = array();
147  $languages[$lang_key]["info"] = "new_language";
148  //$languages[$lang_key]["desc"] = "not_installed";
149  }
150  }
151 
152  chdir($tmpPath);
153 
154  // Insert languages with files new found into table language
155  $languages = $this->addNewLanguages($languages);
156 
157  // Remove from array & db languages which are not installed and no lang-files
158  $languages = $this->removeLanguages($languages);
159 
160  // setting language's full names
161  foreach ($languages as $lang_key => $lang_data) {
162  $languages[$lang_key]["name"] = $this->lng->txt("meta_l_" . $lang_key);
163  }
164 
165  $this->languages = $languages;
166 
167  return $this->languages;
168  }
169 
176  public function addNewLanguages(array $a_languages): array
177  {
178  if (count($a_languages) > 0) {
179  foreach ($a_languages as $lang_key => $lang_data) {
180  if (isset($lang_data["info"]) && $lang_data["info"] === "new_language") {
181  include_once "./Services/Language/classes/class.ilObjLanguage.php";
182  $lngObj = new ilObjLanguage();
183  $lngObj->setTitle($lang_key);
184  $lngObj->setDescription("not_installed");
185  $lngObj->create();
186 
187  // must get OOP through the whole class some time
188  // (no arrays with db fields! this class doesn't know anything about table object!)
189  $a_languages[$lang_key] = array("obj_id" => $lngObj->getId(),
190  "type" => $lngObj->getType(),
191  "description" => $lngObj->getDescription(),
192  "desc" => $lngObj->getDescription(),
193  "owner" => $lngObj->getOwner(),
194  "create_date" => $lngObj->getCreateDate(),
195  "last_update" => $lngObj->getLastUpdateDate());
196 
197  $a_languages[$lang_key]["info"] = "new_language";
198  unset($lngObj); // better: the objects should be resident in an member array of this class
199  }
200  }
201  }
202 
203  return $a_languages;
204  }
205 
214  public function removeLanguages(array $a_languages): array
215  {
216  foreach ($a_languages as $lang_key => $lang_data) {
217  if ($lang_data["desc"] === "not_installed" && $lang_data["info"] === "file_not_found") {
218  // update languages array
219  unset($a_languages[$lang_key]);
220 
221  // update object_data table
222  $query = "DELETE FROM object_data " .
223  "WHERE type = " . $this->db->quote("lng", "text") . " " .
224  "AND title = " . $this->db->quote($lang_key, "text");
225  $this->db->manipulate($query);
226  }
227  }
228 
229  return $a_languages;
230  }
231 
240  public function checkAllLanguages(): string
241  {
242  // set path to directory where lang-files reside
243  $d = dir($this->lang_path);
244  $tmpPath = getcwd();
245  chdir($this->lang_path);
246 
247  // for giving a message when no lang-file was found
248  $found = false;
249 
250  $output = '';
251  // get available lang-files
252  while ($entry = $d->read()) {
253  if (is_file($entry) && (preg_match("~(^ilias_.{2}\.lang$)~", $entry))) {
254  // textmeldung, wenn langfile gefunden wurde
255  $output .= "<br/><br/>" . $this->lng->txt("langfile_found") . ": " . $entry;
256  $content = file($entry);
257  $lines_full = count($content);
258  $found = true;
259  $error_param = false;
260  $error_double = false;
261  $double_checker = [];
262 
263  if ($content = ilObjLanguage::cut_header($content)) {
264  $lines_cut = count($content);
265  foreach ($content as $key => $val) {
266  $separated = explode($this->separator, trim($val));
267  $num = count($separated);
268  $line = $key + $lines_full - $lines_cut + 1;
269 
270  if ($num !== 3) {
271  $error_param = true;
272 
273  $output .= "<br/><b/>" . $this->lng->txt("err_in_line") . " " . $line . " !</b>&nbsp;&nbsp;";
274 
275  switch ($num) {
276  case 1:
277  if (empty($separated[0])) {
278  $output .= "<br/>" . $this->lng->txt("err_no_param") . " " . $this->lng->txt("check_langfile");
279  } else {
280  $output .= $this->lng->txt("module") . ": " . $separated[0];
281  $output .= "<br/>" . $this->lng->txt("err_1_param") . " " . $this->lng->txt("check_langfile");
282  }
283  break;
284 
285  case 2:
286  $output .= $this->lng->txt("module") . ": " . $separated[0];
287  $output .= ", " . $this->lng->txt("identifier") . ": " . $separated[1];
288  $output .= "<br/>" . $this->lng->txt("err_2_param") . " " . $this->lng->txt("check_langfile");
289  break;
290 
291  default:
292  $output .= $this->lng->txt("module") . ": " . $separated[0];
293  $output .= ", " . $this->lng->txt("identifier") . ": " . $separated[1];
294  $output .= ", " . $this->lng->txt("value") . ": " . $separated[2];
295  $output .= "<br/>" . $this->lng->txt("err_over_3_param") . " " . $this->lng->txt("check_langfile");
296  break;
297  }
298  continue;
299  }
300  if ($double_checker[strtolower($separated[0])][strtolower($separated[1])] ?? false) {
301  $error_double = true;
302 
303  $output .= "<br/><b/>" . $this->lng->txt("err_in_line") . " " . $double_checker[strtolower($separated[0])][strtolower($separated[1])] . " " . $this->lng->txt("and") . " " . $line . " !</b>&nbsp;&nbsp;";
304  $output .= $this->lng->txt("module") . ": " . $separated[0];
305  $output .= ", " . $this->lng->txt("identifier") . ": " . $separated[1];
306  $output .= ", " . $this->lng->txt("value") . ": " . $separated[2];
307  }
308  $double_checker[strtolower($separated[0])][strtolower($separated[1])] = $line;
309  }
310 
311  if ($error_param || $error_double) {
312  $reason = "";
313  if ($error_param) {
314  $reason .= " " . $this->lng->txt("err_count_param");
315  }
316  if ($error_double) {
317  $reason .= " " . $this->lng->txt("err_double_entries");
318  }
319  $output .= "<br/>" . $this->lng->txt("file_not_valid") . $reason;
320  } else {
321  $output .= "<br/>" . $this->lng->txt("file_valid");
322  }
323  } else {
324  $output .= "<br/>" . $this->lng->txt("file_not_valid") . " " . $this->lng->txt("err_wrong_header");
325  }
326  }
327  }
328 
329  $d->close();
330 
331  if (!$found) {
332  $output .= "<br/>" . $this->lng->txt("err_no_langfile_found");
333  }
334 
335  chdir($tmpPath);
336  return $output;
337  }
338 } // END class.LanguageFolderObject
string $lang_default
indicator for the system language this language must not be deleted
Class ilObjLanguage.
static _getObjectsByType(string $obj_type="", int $owner=null)
string $lang_user
language that is in use by current user this language must not be deleted
__construct(int $a_id, bool $a_call_by_reference=true)
Constructor $a_id reference_id or object_id $a_call_by_reference treat the id as reference_id (true) ...
string $key
Consumer key/client ID value.
Definition: System.php:193
string $separator
separator value between module,identivier & value
getLanguages()
gather all information about available languages
$query
removeLanguages(array $a_languages)
remove languages which are not installed AND has no lang-file
$lang
Definition: xapiexit.php:26
array $languages
contians all informations about languages
Class ilObjLanguageFolder contains all function to manage language support for ILIAS3 install...
__construct(Container $dic, ilPlugin $plugin)
string $reason
Error message for last request processed.
Definition: System.php:102
string $lang_path
path to language files relative path is taken from ini file and added to absolute path of ilias ...
static cut_header(array $content)
remove lang-file haeder information from &#39;$content&#39; This function seeks for a special keyword where t...
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
addNewLanguages(array $a_languages)
add new languages
checkAllLanguages()
validate the logical structure of a lang-file