Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / admin / inst_lang.php
1 <?php
2
3 $page_security = 20;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 page(_("Install/Update Languages"));
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/admin/db/company_db.inc");
11 include_once($path_to_root . "/admin/db/maintenance_db.inc");
12 include_once($path_to_root . "/includes/ui.inc");
13
14 //---------------------------------------------------------------------------------------------
15
16 if (isset($_GET['selected_id']))
17 {
18         $selected_id = $_GET['selected_id'];
19 }
20 elseif (isset($_POST['selected_id']))
21 {
22         $selected_id = $_POST['selected_id'];
23 }
24 else
25         $selected_id = -1;
26
27 //---------------------------------------------------------------------------------------------
28
29 function check_data()
30 {
31         if ($_POST['code'] == "" || $_POST['name'] == "" || $_POST['encoding'] == "")
32                 return false;
33         return true;
34 }
35
36 /**
37  * @return Returns the array sorted as required
38  * @param $aryData Array containing data to sort
39  * @param $strIndex name of column to use as an index
40  * @param $strSortBy Column to sort the array by
41  * @param $strSortType String containing either asc or desc [default to asc]
42  * @desc Naturally sorts an array using by the column $strSortBy
43  */
44 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
45 {
46    //    if the parameters are invalid
47    if (!is_array($aryData) || !$strIndex || !$strSortBy)
48        //    return the array
49        return $aryData;
50
51    //    create our temporary arrays
52    $arySort = $aryResult = array();
53
54    //    loop through the array
55    foreach ($aryData as $aryRow)
56        //    set up the value in the array
57        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
58
59    //    apply the natural sort
60    natsort($arySort);
61
62    //    if the sort type is descending
63    if ($strSortType=="desc")
64        //    reverse the array
65        arsort($arySort);
66
67    //    loop through the sorted and original data
68    foreach ($arySort as $arySortKey => $arySorted)
69        foreach ($aryData as $aryOriginal)
70            //    if the key matches
71            if ($aryOriginal[$strIndex]==$arySortKey)
72                //    add it to the output array
73                array_push($aryResult, $aryOriginal);
74
75    //    return the return
76    return $aryResult;
77 }
78
79 function write_lang()
80 {
81         global $path_to_root, $installed_languages;
82         include_once($path_to_root . "/lang/installed_languages.inc");
83
84         $conn = array_natsort($installed_languages, 'code', 'code');
85         $installed_languages = $conn;
86         //reset($installed_languages);
87         $n = count($installed_languages);
88         $msg = "<?php\n\n";
89
90         $msg .= "/* How to make new entries here\n\n";
91         $msg .= "-- if adding languages at the beginning of the list, make sure it's index is set to 0 (it has ' 0 => ')\n";
92         $msg .= "-- 'code' should match the name of the directory for the language under \\lang\n";
93         $msg .= "-- 'name' is the name that will be displayed in the language selection list (in Users and Display Setup)\n";
94         $msg .= "-- 'rtl' only needs to be set for right-to-left languages like Arabic and Hebrew\n\n";
95         $msg .= "*/\n\n\n";
96
97         $msg .= "\$installed_languages = array (\n";
98         if ($n > 0)
99             $msg .= "\t0 => ";
100         for ($i = 0; $i < $n; $i++)
101         {
102                 if ($i > 0)
103                         $msg .= "\t\tarray ";
104                 else
105                         $msg .= "array ";
106                 $msg .= "('code' => '" . $installed_languages[$i]['code'] . "', ";
107                 $msg .= "'name' => '" . $installed_languages[$i]['name'] . "', ";
108                 $msg .= "'encoding' => '" . $installed_languages[$i]['encoding'] . "'";
109                 if (isset($installed_languages[$i]['rtl']) && $installed_languages[$i]['rtl'])
110                         $msg .= ", 'rtl' => true),\n";
111                 else
112                         $msg .= "),\n";
113         }
114         $msg .= "\t);\n?>";
115
116         $filename = $path_to_root . "/lang/installed_languages.inc";
117         // Check if the file exists and is writable first.
118         if (file_exists($filename) && is_writable($filename))
119         {
120                 if (!$zp = fopen($filename, 'w'))
121                 {
122                         display_error(_("Cannot open the languages file - ") . $filename);
123                         return false;
124                 }
125                 else
126                 {
127                         if (!fwrite($zp, $msg))
128                         {
129                                 display_error(_("Cannot write to the language file - ") . $filename);
130                                 fclose($zp);
131                                 return false;
132                         }
133                         // Close file
134                         fclose($zp);
135                 }
136         }
137         else
138         {
139                 display_error(_("The language file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
140                 return false;
141         }
142         return true;
143 }
144
145 //---------------------------------------------------------------------------------------------
146
147 function handle_submit()
148 {
149         global $path_to_root, $installed_languages;
150
151         if (!check_data())
152                 return false;
153
154         $id = $_GET['id'];
155
156         $installed_languages[$id]['code'] = $_POST['code'];
157         $installed_languages[$id]['name'] = $_POST['name'];
158         $installed_languages[$id]['encoding'] = $_POST['encoding'];
159         $installed_languages[$id]['rtl'] = (bool)$_POST['rtl'];
160         if (!write_lang())
161                 return false;
162         $directory = $path_to_root . "/lang/" . $_POST['code'];
163         if (!file_exists($directory))
164         {
165                 mkdir($directory);
166                 mkdir($directory . "/LC_MESSAGES");
167         }
168         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
169         {
170                 $file1 = $_FILES['uploadfile']['tmp_name'];
171                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".po";
172                 if (file_exists($file2))
173                         unlink($file2);
174                 move_uploaded_file($file1, $file2);
175         }
176         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
177         {
178                 $file1 = $_FILES['uploadfile2']['tmp_name'];
179                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".mo";
180                 if (file_exists($file2))
181                         unlink($file2);
182                 move_uploaded_file($file1, $file2);
183         }
184         return true;
185 }
186
187 //---------------------------------------------------------------------------------------------
188
189 function handle_delete()
190 {
191         global  $path_to_root, $installed_languages;
192
193         $id = $_GET['id'];
194
195         $lang = $installed_languages[$id]['code'];
196         $filename = "$path_to_root/lang/$lang/LC_MESSAGES";
197         if ($h = opendir($filename))
198         {
199                 while (($file = readdir($h)) !== false)
200                 {
201                         if (is_file("$filename/$file"))
202                         unlink("$filename/$file");
203                 }
204                 closedir($h);
205         }
206         rmdir($filename);
207         $filename = "$path_to_root/lang/$lang";
208         if ($h = opendir($filename))
209         {
210                 while (($file = readdir($h)) !== false)
211                 {
212                         if (is_file("$filename/$file"))
213                         unlink("$filename/$file");
214                 }
215                 closedir($h);
216         }
217         rmdir($filename);
218
219         unset($installed_languages[$id]);
220         $conn = array_values($installed_languages);
221         $installed_languages = $conn;
222
223         //$$db_connections = array_values($db_connections);
224
225         if (!write_lang())
226                 return;
227         meta_forward($_SERVER['PHP_SELF']);
228 }
229
230 //---------------------------------------------------------------------------------------------
231
232 function display_languages()
233 {
234         global $table_style, $installed_languages;
235
236         $lang = $_SESSION["language"]->code;
237
238         echo "
239                 <script language='javascript'>
240                 function deleteLanguage(id) {
241                         if (!confirm('" . _("Are you sure you want to delete language no. ") . "'+id))
242                                 return
243                         document.location.replace('inst_lang.php?c=df&id='+id)
244                 }
245                 </script>";
246         start_table($table_style);
247         $th = array(_("Language"), _("Name"), _("Encoding"), _("Right To Left"), "", "");
248         table_header($th);
249
250         $k = 0;
251         $conn = $installed_languages;
252         $n = count($conn);
253         for ($i = 0; $i < $n; $i++)
254         {
255                 if ($conn[$i]['code'] == $lang)
256                 start_row("class='stockmankobg'");
257         else
258                 alt_table_row_color($k);
259
260                 label_cell($conn[$i]['code']);
261                 label_cell($conn[$i]['name']);
262                 label_cell($conn[$i]['encoding']);
263                 if (isset($conn[$i]['rtl']) && $conn[$i]['rtl'])
264                         $rtl = _("Yes");
265                 else
266                         $rtl = _("No");
267                 label_cell($rtl);
268                 edit_link_cell("selected_id=" . $i);
269                 
270                 label_cell($conn[$i]['code'] == $lang ? '' :
271                         "<a href='javascript:deleteLanguage(" . $i . ")'>" . _("Delete") . "</a>");
272                 end_row();
273         }
274
275         end_table();
276     display_note(_("The marked language is the current language which cannot be deleted."), 0, 0, "class='currentfg'");
277 }
278
279 //---------------------------------------------------------------------------------------------
280
281 function display_language_edit($selected_id)
282 {
283         global $installed_languages, $table_style2;
284
285         if ($selected_id != -1)
286                 $n = $selected_id;
287         else
288                 $n = count($installed_languages);
289
290         start_form(true, true);
291
292         echo "
293                 <script language='javascript'>
294                 function updateLanguage() {
295                         document.forms[0].action='inst_lang.php?c=u&id=" . $n . "'
296                         document.forms[0].submit()
297                 }
298                 </script>";
299
300         start_table($table_style2);
301
302         if ($selected_id != -1)
303         {
304                 $conn = $installed_languages[$selected_id];
305                 $_POST['code'] = $conn['code'];
306                 $_POST['name']  = $conn['name'];
307                 $_POST['encoding']  = $conn['encoding'];
308                 if (isset($conn['rtl']))
309                         $_POST['rtl']  = $conn['rtl'];
310                 else
311                         $_POST['rtl'] = false;
312                 hidden('selected_id', $selected_id);
313         }
314         text_row_ex(_("Language"), 'code', 20);
315         text_row_ex(_("Name"), 'name', 20);
316         text_row_ex(_("Encoding"), 'encoding', 20);
317
318         yesno_list_row(_("Right To Left"), 'rtl', null, "", "", false);
319
320         label_row(_("Language File") . " (PO)", "<input name='uploadfile' type='file'>");
321         label_row(_("Language File") . " (MO)", "<input name='uploadfile2' type='file'>");
322
323         end_table(0);
324         display_note(_("Select your language files from your local harddisk."), 0, 1);
325         echo "<center><input onclick='javascript:updateLanguage()' type='button' style='width:150px' value='". _("Save"). "'></center>";
326
327
328         end_form();
329 }
330
331
332 //---------------------------------------------------------------------------------------------
333
334 if (isset($_GET['c']))
335 {
336         if ($_GET['c'] == 'df')
337         {
338                 handle_delete();
339         }
340
341         if ($_GET['c'] == 'u')
342         {
343                 if (handle_submit())
344                 {
345                         //meta_forward($_SERVER['PHP_SELF']);
346                 }
347         }
348 }
349
350 //---------------------------------------------------------------------------------------------
351
352 display_languages();
353
354 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new language"));
355
356 display_language_edit($selected_id);
357
358 //---------------------------------------------------------------------------------------------
359
360 end_page();
361
362 ?>