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