Release 1.1. Enabled module addons and all the below bugfixes. No changes in database...
[fa-stable.git] / admin / inst_module.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 Modules"));
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 . "/modules/installed_modules.php");
12 include_once($path_to_root . "/includes/ui.inc");
13
14 $tabs = array('orders', 'AP', 'stock', 'manuf', 'proj', 'GL', 'system');
15 $names = array(_("Sales"), _("Purchases"), _("Items and Inventory"), _("Manufacturing"), 
16         _("Dimensions"), _("Banking and General Ledger"), _("Setup"));
17         
18 //---------------------------------------------------------------------------------------------
19
20 if (isset($_GET['selected_id']))
21 {
22         $selected_id = $_GET['selected_id'];
23
24 elseif (isset($_POST['selected_id']))
25 {
26         $selected_id = $_POST['selected_id'];
27 }
28 else
29         $selected_id = -1;
30
31 //---------------------------------------------------------------------------------------------
32
33 function get_tab_title($tab)
34 {
35         global $tabs, $names;
36         for ($i = 0; $i < count($tabs); $i++)
37         {
38                 if ($tabs[$i] == $tab)
39                         return $names[$i];
40         }
41         return "";
42 }
43
44 function tab_list_row($label, $name, $selected)
45 {
46         global $tabs, $names;
47         echo "<tr>\n";
48         if ($label != null)
49                 echo "<td>$label</td>\n";
50         if ($selected == null)
51                 $selected = (!isset($_POST[$name]) ? "orders" : $_POST[$name]);
52         echo "<td><select name='$name'>";
53         for ($i = 0; $i < count($tabs); $i++)
54         {
55                 if ($selected == $tabs[$i])
56                         echo "<option selected value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
57                 else    
58                         echo "<option value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
59         }               
60         echo "</select></td>\n";
61         echo "</tr>\n";
62 }
63
64 //---------------------------------------------------------------------------------------------
65
66 function check_data()
67 {
68         return true;
69 }
70
71 /**
72  * @return Returns the array sorted as required
73  * @param $aryData Array containing data to sort
74  * @param $strIndex name of column to use as an index
75  * @param $strSortBy Column to sort the array by
76  * @param $strSortType String containing either asc or desc [default to asc]
77  * @desc Naturally sorts an array using by the column $strSortBy
78  */
79 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
80 {
81    //    if the parameters are invalid
82    if (!is_array($aryData) || !$strIndex || !$strSortBy)
83        //    return the array
84        return $aryData;
85        
86    //    create our temporary arrays
87    $arySort = $aryResult = array();
88    
89    //    loop through the array
90    foreach ($aryData as $aryRow)
91        //    set up the value in the array
92        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
93        
94    //    apply the natural sort
95    natsort($arySort);
96
97    //    if the sort type is descending
98    if ($strSortType=="desc")
99        //    reverse the array
100        arsort($arySort);
101        
102    //    loop through the sorted and original data
103    foreach ($arySort as $arySortKey => $arySorted)
104        foreach ($aryData as $aryOriginal)
105            //    if the key matches
106            if ($aryOriginal[$strIndex]==$arySortKey)
107                //    add it to the output array
108                array_push($aryResult, $aryOriginal);
109
110    //    return the return
111    return $aryResult;
112
113
114 function write_modules()
115 {
116         global $path_to_root, $installed_modules;
117
118         $mods = array_natsort($installed_modules, 'tab', 'tab');
119         $installed_modules = $mods;
120         //reset($installed_languages);
121         $n = count($installed_modules); 
122         $msg = "<?php\n\n";
123         
124         $msg .= "/*****************************************************************\n";
125         $msg .= "External modules for FrontAccounting\n";
126         $msg .= "******************************************************************/\n";
127         $msg .= "\n\n";
128
129         $msg .= "\$installed_modules = array (\n";
130         $msg .= "\t0 => ";
131         for ($i = 0; $i < $n; $i++)
132         {
133                 if ($i > 0)
134                         $msg .= "\t\tarray ";
135                 else
136                         $msg .= "array ";
137                 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
138                 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
139                 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
140                 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
141                 $msg .= "),\n";
142         }
143         $msg .= "\t);\n?>";
144
145         $filename = $path_to_root . "/modules/installed_modules.php";
146         // Check if the file exists and is writable first.
147         if (file_exists($filename) && is_writable($filename)) 
148         {
149                 if (!$zp = fopen($filename, 'w')) 
150                 {
151                         display_error(_("Cannot open the modules file - ") . $filename);
152                         return false;
153                 } 
154                 else 
155                 {
156                         if (!fwrite($zp, $msg)) 
157                         {
158                                 display_error(_("Cannot write to the modules file - ") . $filename);
159                                 fclose($zp);
160                                 return false;
161                         }
162                         // Close file
163                         fclose($zp);
164                 }
165         } 
166         else 
167         {
168                 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
169                 return false;
170         }
171         return true;
172 }
173
174 //---------------------------------------------------------------------------------------------
175
176 function handle_submit()
177 {
178         global $path_to_root, $installed_modules;
179
180         if (!check_data())
181                 return false;
182
183         $id = $_GET['id'];
184
185         $installed_modules[$id]['tab'] = $_POST['tab'];
186         $installed_modules[$id]['name'] = $_POST['name'];
187         $installed_modules[$id]['path'] = $_POST['path'];
188         $directory = $path_to_root . "/modules/" . $_POST['path'];      
189         if (!file_exists($directory))
190         {
191                 mkdir($directory);
192         }       
193         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
194         {
195                 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
196                 $file1 = $_FILES['uploadfile']['tmp_name'];
197                 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
198                 if (file_exists($file2))
199                         unlink($file2);
200                 move_uploaded_file($file1, $file2);
201         }
202         else
203                 $installed_modules[$id]['filename'] = $_POST['filename'];
204         if (!write_modules())
205                 return false;
206         return true;
207 }
208
209 //---------------------------------------------------------------------------------------------
210
211 function handle_delete()
212 {
213         global  $path_to_root, $installed_modules;
214
215         $id = $_GET['id'];      
216
217         $path = $installed_modules[$id]['path'];
218         $filename = "$path_to_root/modules/$path";      
219         if ($h = opendir($filename)) 
220         {
221                 while (($file = readdir($h)) !== false) 
222                 {
223                         if (is_file("$filename/$file"))
224                         unlink("$filename/$file");
225                 }
226                 closedir($h);
227         }
228         rmdir($filename);
229
230         unset($installed_modules[$id]);
231         $mods = array_values($installed_modules);
232         $installed_modules = $mods;
233
234         if (!write_modules())
235                 return;
236         meta_forward($_SERVER['PHP_SELF']);
237 }
238
239 //---------------------------------------------------------------------------------------------
240
241 function display_modules()
242 {
243         global $table_style, $installed_modules;
244
245         echo "
246                 <script language='javascript'>
247                 function deleteModule(id, name) {
248                         if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
249                                 return
250                         document.location.replace('inst_module.php?c=df&id='+id)
251                 }
252                 </script>";
253         start_table($table_style);
254         $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
255         table_header($th);
256
257         $k = 0;
258         $mods = $installed_modules;
259         $n = count($mods);
260         for ($i = 0; $i < $n; $i++)
261         {
262                 alt_table_row_color($k);
263
264                 label_cell(get_tab_title($mods[$i]['tab']));
265                 label_cell($mods[$i]['name']);
266                 label_cell($mods[$i]['path']);
267                 label_cell($mods[$i]['filename']);
268                 edit_link_cell("selected_id=" . $i);
269                 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>" . _("Delete") . "</a>");
270                 end_row();
271         }
272
273         end_table();
274 }
275
276 //---------------------------------------------------------------------------------------------
277
278 function display_module_edit($selected_id)
279 {
280         global $installed_modules, $table_style2;
281
282         if ($selected_id != -1)
283                 $n = $selected_id;
284         else
285                 $n = count($installed_modules);
286         
287         start_form(true, true);
288
289         echo "
290                 <script language='javascript'>
291                 function updateModule() {
292                         document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
293                         document.forms[0].submit()
294                 }
295                 </script>";
296         
297         start_table($table_style2);
298
299         if ($selected_id != -1) 
300         {
301                 $mod = $installed_modules[$selected_id];
302                 $_POST['tab']  = $mod['tab'];
303                 $_POST['name'] = $mod['name'];
304                 $_POST['path'] = $mod['path'];
305                 $_POST['filename'] = $mod['filename'];
306                 hidden('selected_id', $selected_id);
307                 hidden('filename', $_POST['filename']);
308         } 
309         tab_list_row(_("Menu Tab"), 'tab', null);
310         text_row_ex(_("Name"), 'name', 30);
311         text_row_ex(_("Folder"), 'path', 20);
312
313         label_row(_("Module File"), "<input name='uploadfile' type='file'>");
314
315         end_table(0);
316         display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
317         echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150' value='". _("Save"). "'>";
318
319
320         end_form();
321 }
322
323
324 //---------------------------------------------------------------------------------------------
325
326 if (isset($_GET['c']))
327 {
328         if ($_GET['c'] == 'df') 
329         {
330                 handle_delete();
331         }
332
333         if ($_GET['c'] == 'u') 
334         {
335                 if (handle_submit()) 
336                 {
337                         //meta_forward($_SERVER['PHP_SELF']);
338                 }
339         }       
340 }
341
342 //---------------------------------------------------------------------------------------------
343
344 display_modules();
345
346 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
347
348 display_module_edit($selected_id);
349
350 //---------------------------------------------------------------------------------------------
351
352 end_page();
353
354 ?>