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