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 ***********************************************************************/
14 include_once($path_to_root . "/includes/session.inc");
16 page(_("Install/Update Languages"));
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");
23 //---------------------------------------------------------------------------------------------
25 if (isset($_GET['selected_id']))
27 $selected_id = $_GET['selected_id'];
29 elseif (isset($_POST['selected_id']))
31 $selected_id = $_POST['selected_id'];
36 //---------------------------------------------------------------------------------------------
40 if ($_POST['code'] == "" || $_POST['name'] == "" || $_POST['encoding'] == "")
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
53 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
55 // if the parameters are invalid
56 if (!is_array($aryData) || !$strIndex || !$strSortBy)
60 // create our temporary arrays
61 $arySort = $aryResult = array();
63 // loop through the array
64 foreach ($aryData as $aryRow)
65 // set up the value in the array
66 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
68 // apply the natural sort
71 // if the sort type is descending
72 if ($strSortType=="desc")
76 // loop through the sorted and original data
77 foreach ($arySort as $arySortKey => $arySorted)
78 foreach ($aryData as $aryOriginal)
80 if ($aryOriginal[$strIndex]==$arySortKey)
81 // add it to the output array
82 array_push($aryResult, $aryOriginal);
90 global $path_to_root, $installed_languages;
91 include_once($path_to_root . "/lang/installed_languages.inc");
93 $conn = array_natsort($installed_languages, 'code', 'code');
94 $installed_languages = $conn;
95 //reset($installed_languages);
96 $n = count($installed_languages);
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";
106 $msg .= "\$installed_languages = array (\n";
109 for ($i = 0; $i < $n; $i++)
112 $msg .= "\t\tarray ";
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";
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))
129 if (!$zp = fopen($filename, 'w'))
131 display_error(_("Cannot open the languages file - ") . $filename);
136 if (!fwrite($zp, $msg))
138 display_error(_("Cannot write to the language file - ") . $filename);
148 display_error(_("The language file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
154 //---------------------------------------------------------------------------------------------
156 function handle_submit()
158 global $path_to_root, $installed_languages;
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'];
171 $directory = $path_to_root . "/lang/" . $_POST['code'];
172 if (!file_exists($directory))
175 mkdir($directory . "/LC_MESSAGES");
177 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
179 $file1 = $_FILES['uploadfile']['tmp_name'];
180 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".po";
181 if (file_exists($file2))
183 move_uploaded_file($file1, $file2);
185 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
187 $file1 = $_FILES['uploadfile2']['tmp_name'];
188 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".mo";
189 if (file_exists($file2))
191 move_uploaded_file($file1, $file2);
196 //---------------------------------------------------------------------------------------------
198 function handle_delete()
200 global $path_to_root, $installed_languages;
204 $lang = $installed_languages[$id]['code'];
205 $filename = "$path_to_root/lang/$lang/LC_MESSAGES";
206 if ($h = opendir($filename))
208 while (($file = readdir($h)) !== false)
210 if (is_file("$filename/$file"))
211 unlink("$filename/$file");
216 $filename = "$path_to_root/lang/$lang";
217 if ($h = opendir($filename))
219 while (($file = readdir($h)) !== false)
221 if (is_file("$filename/$file"))
222 unlink("$filename/$file");
228 unset($installed_languages[$id]);
229 $conn = array_values($installed_languages);
230 $installed_languages = $conn;
232 //$$db_connections = array_values($db_connections);
236 meta_forward($_SERVER['PHP_SELF']);
239 //---------------------------------------------------------------------------------------------
241 function display_languages()
243 global $table_style, $installed_languages;
245 $lang = $_SESSION["language"]->code;
248 <script language='javascript'>
249 function deleteLanguage(id) {
250 if (!confirm('" . _("Are you sure you want to delete language no. ") . "'+id))
252 document.location.replace('inst_lang.php?c=df&id='+id)
255 start_table($table_style);
256 $th = array(_("Language"), _("Name"), _("Encoding"), _("Right To Left"), "", "");
260 $conn = $installed_languages;
262 for ($i = 0; $i < $n; $i++)
264 if ($conn[$i]['code'] == $lang)
265 start_row("class='stockmankobg'");
267 alt_table_row_color($k);
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'])
278 $delete = _("Delete");
279 if (user_graphic_links())
281 $edit = set_icon(ICON_EDIT, $edit);
282 $delete = set_icon(ICON_DELETE, $delete);
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>");
291 display_note(_("The marked language is the current language which cannot be deleted."), 0, 0, "class='currentfg'");
294 //---------------------------------------------------------------------------------------------
296 function display_language_edit($selected_id)
298 global $installed_languages, $table_style2;
300 if ($selected_id != -1)
303 $n = count($installed_languages);
305 start_form(true, true);
308 <script language='javascript'>
309 function updateLanguage() {
310 document.forms[0].action='inst_lang.php?c=u&id=" . $n . "'
311 document.forms[0].submit()
315 start_table($table_style2);
317 if ($selected_id != -1)
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'];
326 $_POST['rtl'] = false;
327 hidden('selected_id', $selected_id);
329 text_row_ex(_("Language"), 'code', 20);
330 text_row_ex(_("Name"), 'name', 20);
331 text_row_ex(_("Encoding"), 'encoding', 20);
333 yesno_list_row(_("Right To Left"), 'rtl', null, "", "", false);
335 label_row(_("Language File") . " (PO)", "<input name='uploadfile' type='file'>");
336 label_row(_("Language File") . " (MO)", "<input name='uploadfile2' type='file'>");
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>";
347 //---------------------------------------------------------------------------------------------
349 if (isset($_GET['c']))
351 if ($_GET['c'] == 'df')
356 if ($_GET['c'] == 'u')
360 //meta_forward($_SERVER['PHP_SELF']);
365 //---------------------------------------------------------------------------------------------
369 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new language"));
371 display_language_edit($selected_id);
373 //---------------------------------------------------------------------------------------------