5 include_once($path_to_root . "/includes/session.inc");
7 page(_("Install/Update Modules"));
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");
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"));
19 //---------------------------------------------------------------------------------------------
21 if (isset($_GET['selected_id']))
23 $selected_id = $_GET['selected_id'];
25 elseif (isset($_POST['selected_id']))
27 $selected_id = $_POST['selected_id'];
32 //---------------------------------------------------------------------------------------------
34 function get_tab_title($tab)
37 for ($i = 0; $i < count($tabs); $i++)
39 if ($tabs[$i] == $tab)
45 function tab_list_row($label, $name, $selected)
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++)
56 if ($selected == $tabs[$i])
57 echo "<option selected value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
59 echo "<option value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
61 echo "</select></td>\n";
65 //---------------------------------------------------------------------------------------------
69 if ($_POST['name'] == "" || $_POST['path'] == "")
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
82 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
84 // if the parameters are invalid
85 if (!is_array($aryData) || !$strIndex || !$strSortBy)
89 // create our temporary arrays
90 $arySort = $aryResult = array();
92 // loop through the array
93 foreach ($aryData as $aryRow)
94 // set up the value in the array
95 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
97 // apply the natural sort
100 // if the sort type is descending
101 if ($strSortType=="desc")
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);
117 function write_modules()
119 global $path_to_root, $installed_modules;
121 $mods = array_natsort($installed_modules, 'tab', 'tab');
122 $installed_modules = $mods;
123 //reset($installed_languages);
124 $n = count($installed_modules);
127 $msg .= "/*****************************************************************\n";
128 $msg .= "External modules for FrontAccounting\n";
129 $msg .= "******************************************************************/\n";
132 $msg .= "\$installed_modules = array (\n";
135 for ($i = 0; $i < $n; $i++)
138 $msg .= "\t\tarray ";
141 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
142 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
143 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
144 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
149 $filename = $path_to_root . "/modules/installed_modules.php";
150 // Check if the file exists and is writable first.
151 if (file_exists($filename) && is_writable($filename))
153 if (!$zp = fopen($filename, 'w'))
155 display_error(_("Cannot open the modules file - ") . $filename);
160 if (!fwrite($zp, $msg))
162 display_error(_("Cannot write to the modules file - ") . $filename);
172 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
178 //---------------------------------------------------------------------------------------------
180 function handle_submit()
182 global $path_to_root, $installed_modules, $db_connections;
189 $installed_modules[$id]['tab'] = $_POST['tab'];
190 $installed_modules[$id]['name'] = $_POST['name'];
191 $installed_modules[$id]['path'] = $_POST['path'];
192 $directory = $path_to_root . "/modules/" . $_POST['path'];
193 if (!file_exists($directory))
197 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
199 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
200 $file1 = $_FILES['uploadfile']['tmp_name'];
201 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
202 if (file_exists($file2))
204 move_uploaded_file($file1, $file2);
207 $installed_modules[$id]['filename'] = $_POST['filename'];
208 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
210 $file1 = $_FILES['uploadfile2']['tmp_name'];
211 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
212 if (file_exists($file2))
214 move_uploaded_file($file1, $file2);
215 $db_name = $_SESSION["wa_current_user"]->company;
216 db_import($file2, $db_connections[$db_name]);
218 if (!write_modules())
223 //---------------------------------------------------------------------------------------------
225 function handle_delete()
227 global $path_to_root, $installed_modules;
231 $path = $installed_modules[$id]['path'];
232 $filename = "$path_to_root/modules/$path";
233 if ($h = opendir($filename))
235 while (($file = readdir($h)) !== false)
237 if (is_file("$filename/$file"))
238 unlink("$filename/$file");
244 unset($installed_modules[$id]);
245 $mods = array_values($installed_modules);
246 $installed_modules = $mods;
248 if (!write_modules())
250 meta_forward($_SERVER['PHP_SELF']);
253 //---------------------------------------------------------------------------------------------
255 function display_modules()
257 global $table_style, $installed_modules;
260 <script language='javascript'>
261 function deleteModule(id, name) {
262 if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
264 document.location.replace('inst_module.php?c=df&id='+id)
267 start_table($table_style);
268 $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
272 $mods = $installed_modules;
274 for ($i = 0; $i < $n; $i++)
276 alt_table_row_color($k);
278 label_cell(get_tab_title($mods[$i]['tab']));
279 label_cell($mods[$i]['name']);
280 label_cell($mods[$i]['path']);
281 label_cell($mods[$i]['filename']);
282 edit_link_cell("selected_id=" . $i);
283 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>" . _("Delete") . "</a>");
290 //---------------------------------------------------------------------------------------------
292 function display_module_edit($selected_id)
294 global $installed_modules, $table_style2;
296 if ($selected_id != -1)
299 $n = count($installed_modules);
301 start_form(true, true);
304 <script language='javascript'>
305 function updateModule() {
306 document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
307 document.forms[0].submit()
311 start_table($table_style2);
313 if ($selected_id != -1)
315 $mod = $installed_modules[$selected_id];
316 $_POST['tab'] = $mod['tab'];
317 $_POST['name'] = $mod['name'];
318 $_POST['path'] = $mod['path'];
319 $_POST['filename'] = $mod['filename'];
320 hidden('selected_id', $selected_id);
321 hidden('filename', $_POST['filename']);
323 tab_list_row(_("Menu Tab"), 'tab', null);
324 text_row_ex(_("Name"), 'name', 30);
325 text_row_ex(_("Folder"), 'path', 20);
327 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
328 label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
331 display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
332 echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
339 //---------------------------------------------------------------------------------------------
341 if (isset($_GET['c']))
343 if ($_GET['c'] == 'df')
348 if ($_GET['c'] == 'u')
352 //meta_forward($_SERVER['PHP_SELF']);
357 //---------------------------------------------------------------------------------------------
361 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
363 display_module_edit($selected_id);
365 //---------------------------------------------------------------------------------------------