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'] == "") {
41 display_error(_("Language name, code nor encoding cannot be empty"));
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
55 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
57 // if the parameters are invalid
58 if (!is_array($aryData) || !$strIndex || !$strSortBy)
62 // create our temporary arrays
63 $arySort = $aryResult = array();
65 // loop through the array
66 foreach ($aryData as $aryRow)
67 // set up the value in the array
68 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
70 // apply the natural sort
73 // if the sort type is descending
74 if ($strSortType=="desc")
78 // loop through the sorted and original data
79 foreach ($arySort as $arySortKey => $arySorted)
80 foreach ($aryData as $aryOriginal)
82 if ($aryOriginal[$strIndex]==$arySortKey)
83 // add it to the output array
84 array_push($aryResult, $aryOriginal);
92 global $path_to_root, $installed_languages, $dflt_lang;
93 include_once($path_to_root . "/lang/installed_languages.inc");
95 $conn = array_natsort($installed_languages, 'code', 'code');
96 $installed_languages = $conn;
97 //reset($installed_languages);
98 $n = count($installed_languages);
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";
108 $msg .= "\$installed_languages = array (\n";
111 for ($i = 0; $i < $n; $i++)
114 $msg .= "\t\tarray ";
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";
127 $msg .= "\n\$dflt_lang = '$dflt_lang';\n?>\n";
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))
133 if (!$zp = fopen($filename, 'w'))
135 display_error(_("Cannot open the languages file - ") . $filename);
140 if (!fwrite($zp, $msg))
142 display_error(_("Cannot write to the language file - ") . $filename);
152 display_error(_("The language file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
158 //---------------------------------------------------------------------------------------------
160 function handle_submit()
162 global $path_to_root, $installed_languages, $dflt_lang;
169 if ($_POST['dflt']) {
170 $dflt_lang = $_POST['code'];
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'];
179 $directory = $path_to_root . "/lang/" . $_POST['code'];
180 if (!file_exists($directory))
183 mkdir($directory . "/LC_MESSAGES");
185 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
187 $file1 = $_FILES['uploadfile']['tmp_name'];
188 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".po";
189 if (file_exists($file2))
191 move_uploaded_file($file1, $file2);
193 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
195 $file1 = $_FILES['uploadfile2']['tmp_name'];
196 $file2 = $directory . "/LC_MESSAGES/".$_POST['code'].".mo";
197 if (file_exists($file2))
199 move_uploaded_file($file1, $file2);
204 //---------------------------------------------------------------------------------------------
206 function handle_delete()
208 global $path_to_root, $installed_languages, $dflt_lang;
212 $lang = $installed_languages[$id]['code'];
213 $filename = "$path_to_root/lang/$lang/LC_MESSAGES";
215 if ($lang == $dflt_lang ) {
216 // on delete set default to current.
217 $dflt_lang = $_SESSION['language']->code;
220 unset($installed_languages[$id]);
221 $installed_languages = array_values($installed_languages);
226 $filename = "$path_to_root/lang/$lang";
227 flush_dir($filename);
230 meta_forward($_SERVER['PHP_SELF']);
233 //---------------------------------------------------------------------------------------------
235 function display_languages()
237 global $table_style, $installed_languages, $dflt_lang;
239 $lang = $_SESSION["language"]->code;
242 <script language='javascript'>
243 function deleteLanguage(id) {
244 if (!confirm('" . _("Are you sure you want to delete language no. ") . "'+id))
246 document.location.replace('inst_lang.php?c=df&id='+id)
249 start_table($table_style);
250 $th = array(_("Language"), _("Name"), _("Encoding"), _("Right To Left"), _("Default"), "", "");
254 $conn = $installed_languages;
256 for ($i = 0; $i < $n; $i++)
258 if ($conn[$i]['code'] == $lang)
259 start_row("class='stockmankobg'");
261 alt_table_row_color($k);
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'])
271 label_cell($dflt_lang == $conn[$i]['code'] ? _("Yes") :_("No"));
273 $delete = _("Delete");
274 if (user_graphic_links())
276 $edit = set_icon(ICON_EDIT, $edit);
277 $delete = set_icon(ICON_DELETE, $delete);
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>");
286 display_note(_("The marked language is the current language which cannot be deleted."), 0, 0, "class='currentfg'");
289 //---------------------------------------------------------------------------------------------
291 function display_language_edit($selected_id)
293 global $installed_languages, $table_style2, $dflt_lang;
295 if ($selected_id != -1)
298 $n = count($installed_languages);
300 start_form(true, true);
303 <script language='javascript'>
304 function updateLanguage() {
305 document.forms[0].action='inst_lang.php?c=u&id=" . $n . "'
306 document.forms[0].submit()
310 start_table($table_style2);
312 if ($selected_id != -1)
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'];
321 $_POST['rtl'] = false;
322 $_POST['dflt'] = $dflt_lang == $conn['code'];
323 hidden('selected_id', $selected_id);
325 text_row_ex(_("Language Code"), 'code', 20);
326 text_row_ex(_("Language Name"), 'name', 20);
327 text_row_ex(_("Encoding"), 'encoding', 20);
329 yesno_list_row(_("Right To Left"), 'rtl', null, "", "", false);
330 yesno_list_row(_("Default Language"), 'dflt', null, "", "", false);
332 label_row(_("Language File") . " (PO)", "<input name='uploadfile' type='file'>");
333 label_row(_("Language File") . " (MO)", "<input name='uploadfile2' type='file'>");
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>";
344 //---------------------------------------------------------------------------------------------
346 if (isset($_GET['c']))
348 if ($_GET['c'] == 'df')
353 if ($_GET['c'] == 'u')
357 //meta_forward($_SERVER['PHP_SELF']);
362 //---------------------------------------------------------------------------------------------
366 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new language"));
368 display_language_edit($selected_id);
370 //---------------------------------------------------------------------------------------------