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