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