9c8ea4b555cbe1c565ae026864b1f2a3d0081968
[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 = 'SA_CREATELANGUAGE';
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 function write_lang()
48 {
49         global $path_to_root, $installed_languages, $dflt_lang;
50         include_once($path_to_root . "/lang/installed_languages.inc");
51
52         $conn = array_natsort($installed_languages, 'code', 'code');
53         $installed_languages = $conn;
54         //reset($installed_languages);
55         $n = count($installed_languages);
56         $msg = "<?php\n\n";
57
58         $msg .= "/* How to make new entries here\n\n";
59         $msg .= "-- if adding languages at the beginning of the list, make sure it's index is set to 0 (it has ' 0 => ')\n";
60         $msg .= "-- 'code' should match the name of the directory for the language under \\lang\n";
61         $msg .= "-- 'name' is the name that will be displayed in the language selection list (in Users and Display Setup)\n";
62         $msg .= "-- 'rtl' only needs to be set for right-to-left languages like Arabic and Hebrew\n\n";
63         $msg .= "*/\n\n\n";
64
65         $msg .= "\$installed_languages = array (\n";
66         if ($n > 0)
67             $msg .= "\t0 => ";
68         for ($i = 0; $i < $n; $i++)
69         {
70                 if ($i > 0)
71                         $msg .= "\t\tarray ";
72                 else
73                         $msg .= "array ";
74                 $msg .= "('code' => '" . $installed_languages[$i]['code'] . "', ";
75                 $msg .= "'name' => '" . $installed_languages[$i]['name'] . "', ";
76                 $msg .= "'encoding' => '" . $installed_languages[$i]['encoding'] . "'";
77                 if (isset($installed_languages[$i]['rtl']) && $installed_languages[$i]['rtl'])
78                         $msg .= ", 'rtl' => true),\n";
79                 else
80                         $msg .= "),\n";
81         }
82
83         $msg .= "\t);\n";
84         $msg .= "\n\$dflt_lang = '$dflt_lang';\n?>\n";
85
86         $filename = $path_to_root . "/lang/installed_languages.inc";
87         // Check if the file exists and is writable first.
88         if (file_exists($filename) && is_writable($filename))
89         {
90                 if (!$zp = fopen($filename, 'w'))
91                 {
92                         display_error(_("Cannot open the languages file - ") . $filename);
93                         return false;
94                 }
95                 else
96                 {
97                         if (!fwrite($zp, $msg))
98                         {
99                                 display_error(_("Cannot write to the language file - ") . $filename);
100                                 fclose($zp);
101                                 return false;
102                         }
103                         // Close file
104                         fclose($zp);
105                 }
106         }
107         else
108         {
109                 display_error(_("The language file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
110                 return false;
111         }
112         return true;
113 }
114
115 //---------------------------------------------------------------------------------------------
116
117 function handle_submit()
118 {
119         global $path_to_root, $installed_languages, $dflt_lang;
120
121         if (!check_data())
122                 return false;
123
124         $id = $_GET['id'];
125
126         if ($_POST['dflt']) {
127                         $dflt_lang = $_POST['code'];
128         }
129         
130         $installed_languages[$id]['code'] = $_POST['code'];
131         $installed_languages[$id]['name'] = $_POST['name'];
132         $installed_languages[$id]['encoding'] = $_POST['encoding'];
133         $installed_languages[$id]['rtl'] = (bool)$_POST['rtl'];
134         if (!write_lang())
135                 return false;
136         $directory = $path_to_root . "/lang/" . $_POST['code'];
137         if (!file_exists($directory))
138         {
139                 mkdir($directory);
140                 mkdir($directory . "/LC_MESSAGES");
141         }
142         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
143         {
144                 $file1 = $_FILES['uploadfile']['tmp_name'];
145                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".po";
146                 if (file_exists($file2))
147                         unlink($file2);
148                 move_uploaded_file($file1, $file2);
149         }
150         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
151         {
152                 $file1 = $_FILES['uploadfile2']['tmp_name'];
153                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".mo";
154                 if (file_exists($file2))
155                         unlink($file2);
156                 move_uploaded_file($file1, $file2);
157         }
158         return true;
159 }
160
161 //---------------------------------------------------------------------------------------------
162
163 function handle_delete()
164 {
165         global  $path_to_root, $installed_languages, $dflt_lang;
166
167         $id = $_GET['id'];
168
169         $lang = $installed_languages[$id]['code'];
170         $filename = "$path_to_root/lang/$lang/LC_MESSAGES";
171
172         if ($lang == $dflt_lang ) { 
173                 // on delete set default to current.
174                 $dflt_lang = $_SESSION['language']->code;
175         }
176         
177         unset($installed_languages[$id]);
178         $installed_languages = array_values($installed_languages);
179
180         if (!write_lang())
181                 return;
182
183         $filename = "$path_to_root/lang/$lang";
184         flush_dir($filename);
185         rmdir($filename);
186
187         meta_forward($_SERVER['PHP_SELF']);
188 }
189
190 //---------------------------------------------------------------------------------------------
191
192 function display_languages()
193 {
194         global $table_style, $installed_languages, $dflt_lang;
195
196         $lang = $_SESSION["language"]->code;
197
198         echo "
199                 <script language='javascript'>
200                 function deleteLanguage(id) {
201                         if (!confirm('" . _("Are you sure you want to delete language no. ") . "'+id))
202                                 return
203                         document.location.replace('inst_lang.php?c=df&id='+id)
204                 }
205                 </script>";
206         start_table($table_style);
207         $th = array(_("Language"), _("Name"), _("Encoding"), _("Right To Left"), _("Default"), "", "");
208         table_header($th);
209
210         $k = 0;
211         $conn = $installed_languages;
212         $n = count($conn);
213         for ($i = 0; $i < $n; $i++)
214         {
215                 if ($conn[$i]['code'] == $lang)
216                 start_row("class='stockmankobg'");
217         else
218                 alt_table_row_color($k);
219
220                 label_cell($conn[$i]['code']);
221                 label_cell($conn[$i]['name']);
222                 label_cell($conn[$i]['encoding']);
223                 if (isset($conn[$i]['rtl']) && $conn[$i]['rtl'])
224                         $rtl = _("Yes");
225                 else
226                         $rtl = _("No");
227                 label_cell($rtl);
228                 label_cell($dflt_lang == $conn[$i]['code'] ? _("Yes") :_("No"));
229                 $edit = _("Edit");
230                 $delete = _("Delete");
231                 if (user_graphic_links())
232                 {
233                         $edit = set_icon(ICON_EDIT, $edit);
234                         $delete = set_icon(ICON_DELETE, $delete);
235                 }
236         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
237                 label_cell($conn[$i]['code'] == $lang ? '' :
238                         "<a href='javascript:deleteLanguage(" . $i . ")'>$delete</a>");
239                 end_row();
240         }
241
242         end_table();
243     display_note(_("The marked language is the current language which cannot be deleted."), 0, 0, "class='currentfg'");
244 }
245
246 //---------------------------------------------------------------------------------------------
247
248 function display_language_edit($selected_id)
249 {
250         global $installed_languages, $table_style2, $dflt_lang;
251
252         if ($selected_id != -1)
253                 $n = $selected_id;
254         else
255                 $n = count($installed_languages);
256
257         start_form(true);
258
259         echo "
260                 <script language='javascript'>
261                 function updateLanguage() {
262                         document.forms[0].action='inst_lang.php?c=u&id=" . $n . "'
263                         document.forms[0].submit()
264                 }
265                 </script>";
266
267         start_table($table_style2);
268
269         if ($selected_id != -1)
270         {
271                 $conn = $installed_languages[$selected_id];
272                 $_POST['code'] = $conn['code'];
273                 $_POST['name']  = $conn['name'];
274                 $_POST['encoding']  = $conn['encoding'];
275                 if (isset($conn['rtl']))
276                         $_POST['rtl']  = $conn['rtl'];
277                 else
278                         $_POST['rtl'] = false;
279                 $_POST['dflt'] = $dflt_lang == $conn['code'];
280                 hidden('selected_id', $selected_id);
281         }
282         text_row_ex(_("Language Code"), 'code', 20);
283         text_row_ex(_("Language Name"), 'name', 20);
284         text_row_ex(_("Encoding"), 'encoding', 20);
285
286         yesno_list_row(_("Right To Left"), 'rtl', null, "", "", false);
287         yesno_list_row(_("Default Language"), 'dflt', null, "", "", false);
288
289         label_row(_("Language File") . " (PO)", "<input name='uploadfile' type='file'>");
290         label_row(_("Language File") . " (MO)", "<input name='uploadfile2' type='file'>");
291
292         end_table(0);
293         display_note(_("Select your language files from your local harddisk."), 0, 1);
294         echo "<center><input onclick='javascript:updateLanguage()' type='button' style='width:150px' value='". _("Save"). "'></center>";
295
296
297         end_form();
298 }
299
300
301 //---------------------------------------------------------------------------------------------
302
303 if (isset($_GET['c']))
304 {
305         if ($_GET['c'] == 'df')
306         {
307                 handle_delete();
308         }
309
310         if ($_GET['c'] == 'u')
311         {
312                 if (handle_submit())
313                 {
314                         //meta_forward($_SERVER['PHP_SELF']);
315                 }
316         }
317 }
318
319 //---------------------------------------------------------------------------------------------
320
321 display_languages();
322
323 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new language"));
324
325 display_language_edit($selected_id);
326
327 //---------------------------------------------------------------------------------------------
328
329 end_page();
330
331 ?>