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