Small table view fix.
[fa-stable.git] / admin / inst_lang.php
1 <?php
2
3 $page_security = 15;
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         $msg .= "\t0 => ";
99         for ($i = 0; $i < $n; $i++)
100         {
101                 if ($i > 0)
102                         $msg .= "\t\tarray ";
103                 else
104                         $msg .= "array ";
105                 $msg .= "('code' => '" . $installed_languages[$i]['code'] . "', ";
106                 $msg .= "'name' => '" . $installed_languages[$i]['name'] . "', ";
107                 $msg .= "'encoding' => '" . $installed_languages[$i]['encoding'] . "'";
108                 if (isset($installed_languages[$i]['rtl']) && $installed_languages[$i]['rtl'])
109                         $msg .= ", 'rtl' => true),\n";
110                 else
111                         $msg .= "),\n";
112         }
113         $msg .= "\t);\n?>";
114
115         $filename = $path_to_root . "/lang/installed_languages.inc";
116         // Check if the file exists and is writable first.
117         if (file_exists($filename) && is_writable($filename))
118         {
119                 if (!$zp = fopen($filename, 'w'))
120                 {
121                         display_error(_("Cannot open the languages file - ") . $filename);
122                         return false;
123                 }
124                 else
125                 {
126                         if (!fwrite($zp, $msg))
127                         {
128                                 display_error(_("Cannot write to the language file - ") . $filename);
129                                 fclose($zp);
130                                 return false;
131                         }
132                         // Close file
133                         fclose($zp);
134                 }
135         }
136         else
137         {
138                 display_error(_("The language file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
139                 return false;
140         }
141         return true;
142 }
143
144 //---------------------------------------------------------------------------------------------
145
146 function handle_submit()
147 {
148         global $path_to_root, $installed_languages;
149
150         if (!check_data())
151                 return false;
152
153         $id = $_GET['id'];
154
155         $installed_languages[$id]['code'] = $_POST['code'];
156         $installed_languages[$id]['name'] = $_POST['name'];
157         $installed_languages[$id]['encoding'] = $_POST['encoding'];
158         $installed_languages[$id]['rtl'] = (bool)$_POST['rtl'];
159         if (!write_lang())
160                 return false;
161         $directory = $path_to_root . "/lang/" . $_POST['code'];
162         if (!file_exists($directory))
163         {
164                 mkdir($directory);
165                 mkdir($directory . "/LC_MESSAGES");
166         }
167         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
168         {
169                 $file1 = $_FILES['uploadfile']['tmp_name'];
170                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".po";
171                 if (file_exists($file2))
172                         unlink($file2);
173                 move_uploaded_file($file1, $file2);
174         }
175         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
176         {
177                 $file1 = $_FILES['uploadfile2']['tmp_name'];
178                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".mo";
179                 if (file_exists($file2))
180                         unlink($file2);
181                 move_uploaded_file($file1, $file2);
182         }
183         return true;
184 }
185
186 //---------------------------------------------------------------------------------------------
187
188 function handle_delete()
189 {
190         global  $path_to_root, $installed_languages;
191
192         $id = $_GET['id'];
193
194         $lang = $installed_languages[$id]['code'];
195         $filename = "$path_to_root/lang/$lang/LC_MESSAGES";
196         if ($h = opendir($filename))
197         {
198                 while (($file = readdir($h)) !== false)
199                 {
200                         if (is_file("$filename/$file"))
201                         unlink("$filename/$file");
202                 }
203                 closedir($h);
204         }
205         rmdir($filename);
206         $filename = "$path_to_root/lang/$lang";
207         if ($h = opendir($filename))
208         {
209                 while (($file = readdir($h)) !== false)
210                 {
211                         if (is_file("$filename/$file"))
212                         unlink("$filename/$file");
213                 }
214                 closedir($h);
215         }
216         rmdir($filename);
217
218         unset($installed_languages[$id]);
219         $conn = array_values($installed_languages);
220         $installed_languages = $conn;
221
222         //$$db_connections = array_values($db_connections);
223
224         if (!write_lang())
225                 return;
226         meta_forward($_SERVER['PHP_SELF']);
227 }
228
229 //---------------------------------------------------------------------------------------------
230
231 function display_languages()
232 {
233         global $table_style, $installed_languages;
234
235         $lang = $_SESSION["language"]->code;
236
237         echo "
238                 <script language='javascript'>
239                 function deleteLanguage(id) {
240                         if (!confirm('" . _("Are you sure you want to delete language no. ") . "'+id))
241                                 return
242                         document.location.replace('inst_lang.php?c=df&id='+id)
243                 }
244                 </script>";
245         start_table($table_style);
246         $th = array(_("Language"), _("Name"), _("Encoding"), _("Right To Left"), "", "");
247         table_header($th);
248
249         $k = 0;
250         $conn = $installed_languages;
251         $n = count($conn);
252         for ($i = 0; $i < $n; $i++)
253         {
254                 if ($conn[$i]['code'] == $lang)
255                 start_row("class='stockmankobg'");
256         else
257                 alt_table_row_color($k);
258
259                 label_cell($conn[$i]['code']);
260                 label_cell($conn[$i]['name']);
261                 label_cell($conn[$i]['encoding']);
262                 if (isset($conn[$i]['rtl']) && $conn[$i]['rtl'])
263                         $rtl = _("Yes");
264                 else
265                         $rtl = _("No");
266                 label_cell($rtl);
267                 edit_link_cell("selected_id=" . $i);
268                 
269                 label_cell($conn[$i]['code'] == $lang ? '' :
270                         "<a href='javascript:deleteLanguage(" . $i . ")'>" . _("Delete") . "</a>");
271                 end_row();
272         }
273
274         end_table();
275     display_note(_("The marked language is the current language which cannot be deleted."), 0, 0, "class='currentfg'");
276 }
277
278 //---------------------------------------------------------------------------------------------
279
280 function display_language_edit($selected_id)
281 {
282         global $installed_languages, $table_style2;
283
284         if ($selected_id != -1)
285                 $n = $selected_id;
286         else
287                 $n = count($installed_languages);
288
289         start_form(true, true);
290
291         echo "
292                 <script language='javascript'>
293                 function updateLanguage() {
294                         document.forms[0].action='inst_lang.php?c=u&id=" . $n . "'
295                         document.forms[0].submit()
296                 }
297                 </script>";
298
299         start_table($table_style2);
300
301         if ($selected_id != -1)
302         {
303                 $conn = $installed_languages[$selected_id];
304                 $_POST['code'] = $conn['code'];
305                 $_POST['name']  = $conn['name'];
306                 $_POST['encoding']  = $conn['encoding'];
307                 if (isset($conn['rtl']))
308                         $_POST['rtl']  = $conn['rtl'];
309                 else
310                         $_POST['rtl'] = false;
311                 hidden('selected_id', $selected_id);
312         }
313         text_row_ex(_("Language"), 'code', 20);
314         text_row_ex(_("Name"), 'name', 20);
315         text_row_ex(_("Encoding"), 'encoding', 20);
316
317         yesno_list_row(_("Right To Left"), 'rtl', null, "", "", false);
318
319         label_row(_("Language File") . " (PO)", "<input name='uploadfile' type='file'>");
320         label_row(_("Language File") . " (MO)", "<input name='uploadfile2' type='file'>");
321
322         end_table(0);
323         display_note(_("Select your language files from your local harddisk."), 0, 1);
324         echo "<center><input onclick='javascript:updateLanguage()' type='button' style='width:150px' value='". _("Save"). "'></center>";
325
326
327         end_form();
328 }
329
330
331 //---------------------------------------------------------------------------------------------
332
333 if (isset($_GET['c']))
334 {
335         if ($_GET['c'] == 'df')
336         {
337                 handle_delete();
338         }
339
340         if ($_GET['c'] == 'u')
341         {
342                 if (handle_submit())
343                 {
344                         //meta_forward($_SERVER['PHP_SELF']);
345                 }
346         }
347 }
348
349 //---------------------------------------------------------------------------------------------
350
351 display_languages();
352
353 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new language"));
354
355 display_language_edit($selected_id);
356
357 //---------------------------------------------------------------------------------------------
358
359 end_page();
360
361 ?>