77fb8d091741dde20ff51731d46041662f697f20
[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(_($help_context = "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
49 function handle_submit()
50 {
51         global $path_to_root, $installed_languages, $dflt_lang;
52
53         if (!check_data())
54                 return false;
55
56         $id = $_GET['id'];
57
58         if ($_POST['dflt']) {
59                         $dflt_lang = $_POST['code'];
60         }
61         
62         $installed_languages[$id]['code'] = $_POST['code'];
63         $installed_languages[$id]['name'] = $_POST['name'];
64         $installed_languages[$id]['encoding'] = $_POST['encoding'];
65         $installed_languages[$id]['rtl'] = (bool)$_POST['rtl'];
66         if (!write_lang())
67                 return false;
68         $directory = $path_to_root . "/lang/" . $_POST['code'];
69         if (!file_exists($directory))
70         {
71                 mkdir($directory);
72                 mkdir($directory . "/LC_MESSAGES");
73         }
74         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
75         {
76                 $file1 = $_FILES['uploadfile']['tmp_name'];
77                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".po";
78                 if (file_exists($file2))
79                         unlink($file2);
80                 move_uploaded_file($file1, $file2);
81         }
82         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
83         {
84                 $file1 = $_FILES['uploadfile2']['tmp_name'];
85                 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".mo";
86                 if (file_exists($file2))
87                         unlink($file2);
88                 move_uploaded_file($file1, $file2);
89         }
90         return true;
91 }
92
93 //---------------------------------------------------------------------------------------------
94
95 function handle_delete()
96 {
97         global  $path_to_root, $installed_languages, $dflt_lang;
98
99         $id = $_GET['id'];
100
101         $lang = $installed_languages[$id]['code'];
102         $filename = "$path_to_root/lang/$lang/LC_MESSAGES";
103
104         if ($lang == $dflt_lang ) { 
105                 // on delete set default to current.
106                 $dflt_lang = $_SESSION['language']->code;
107         }
108         
109         unset($installed_languages[$id]);
110         $installed_languages = array_values($installed_languages);
111
112         if (!write_lang())
113                 return;
114
115         $filename = "$path_to_root/lang/$lang";
116         flush_dir($filename);
117         rmdir($filename);
118
119         meta_forward($_SERVER['PHP_SELF']);
120 }
121
122 //---------------------------------------------------------------------------------------------
123
124 function display_languages()
125 {
126         global $table_style, $installed_languages, $dflt_lang;
127
128         $lang = $_SESSION["language"]->code;
129
130         echo "
131                 <script language='javascript'>
132                 function deleteLanguage(id) {
133                         if (!confirm('" . _("Are you sure you want to delete language no. ") . "'+id))
134                                 return
135                         document.location.replace('inst_lang.php?c=df&id='+id)
136                 }
137                 </script>";
138         start_table($table_style);
139         $th = array(_("Language"), _("Name"), _("Encoding"), _("Right To Left"), _("Default"), "", "");
140         table_header($th);
141
142         $k = 0;
143         $conn = $installed_languages;
144         $n = count($conn);
145         for ($i = 0; $i < $n; $i++)
146         {
147                 if ($conn[$i]['code'] == $lang)
148                 start_row("class='stockmankobg'");
149         else
150                 alt_table_row_color($k);
151
152                 label_cell($conn[$i]['code']);
153                 label_cell($conn[$i]['name']);
154                 label_cell($conn[$i]['encoding']);
155                 if (isset($conn[$i]['rtl']) && $conn[$i]['rtl'])
156                         $rtl = _("Yes");
157                 else
158                         $rtl = _("No");
159                 label_cell($rtl);
160                 label_cell($dflt_lang == $conn[$i]['code'] ? _("Yes") :_("No"));
161                 $edit = _("Edit");
162                 $delete = _("Delete");
163                 if (user_graphic_links())
164                 {
165                         $edit = set_icon(ICON_EDIT, $edit);
166                         $delete = set_icon(ICON_DELETE, $delete);
167                 }
168         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
169                 label_cell($conn[$i]['code'] == $lang ? '' :
170                         "<a href='javascript:deleteLanguage(" . $i . ")'>$delete</a>");
171                 end_row();
172         }
173
174         end_table();
175     display_note(_("The marked language is the current language which cannot be deleted."), 0, 0, "class='currentfg'");
176 }
177
178 //---------------------------------------------------------------------------------------------
179
180 function display_language_edit($selected_id)
181 {
182         global $installed_languages, $table_style2, $dflt_lang;
183
184         if ($selected_id != -1)
185                 $n = $selected_id;
186         else
187                 $n = count($installed_languages);
188
189         start_form(true);
190
191         echo "
192                 <script language='javascript'>
193                 function updateLanguage() {
194                         document.forms[0].action='inst_lang.php?c=u&id=" . $n . "'
195                         document.forms[0].submit()
196                 }
197                 </script>";
198
199         start_table($table_style2);
200
201         if ($selected_id != -1)
202         {
203                 $conn = $installed_languages[$selected_id];
204                 $_POST['code'] = $conn['code'];
205                 $_POST['name']  = $conn['name'];
206                 $_POST['encoding']  = $conn['encoding'];
207                 if (isset($conn['rtl']))
208                         $_POST['rtl']  = $conn['rtl'];
209                 else
210                         $_POST['rtl'] = false;
211                 $_POST['dflt'] = $dflt_lang == $conn['code'];
212                 hidden('selected_id', $selected_id);
213         }
214         text_row_ex(_("Language Code"), 'code', 20);
215         text_row_ex(_("Language Name"), 'name', 20);
216         text_row_ex(_("Encoding"), 'encoding', 20);
217
218         yesno_list_row(_("Right To Left"), 'rtl', null, "", "", false);
219         yesno_list_row(_("Default Language"), 'dflt', null, "", "", false);
220
221         label_row(_("Language File") . " (PO)", "<input name='uploadfile' type='file'>");
222         label_row(_("Language File") . " (MO)", "<input name='uploadfile2' type='file'>");
223
224         end_table(0);
225         display_note(_("Select your language files from your local harddisk."), 0, 1);
226         echo "<center><input onclick='javascript:updateLanguage()' type='button' style='width:150px' value='". _("Save"). "'></center>";
227
228
229         end_form();
230 }
231
232
233 //---------------------------------------------------------------------------------------------
234
235 if (isset($_GET['c']))
236 {
237         if ($_GET['c'] == 'df')
238         {
239                 handle_delete();
240         }
241
242         if ($_GET['c'] == 'u')
243         {
244                 if (handle_submit())
245                 {
246                         //meta_forward($_SERVER['PHP_SELF']);
247                 }
248         }
249 }
250
251 //---------------------------------------------------------------------------------------------
252
253 display_languages();
254
255 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new language"));
256
257 display_language_edit($selected_id);
258
259 //---------------------------------------------------------------------------------------------
260
261 end_page();
262
263 ?>