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 . "/modules/installed_modules.php");
12 include_once($path_to_root . "/includes/ui.inc");
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"));
18 //---------------------------------------------------------------------------------------------
20 if (isset($_GET['selected_id']))
22 $selected_id = $_GET['selected_id'];
24 elseif (isset($_POST['selected_id']))
26 $selected_id = $_POST['selected_id'];
31 //---------------------------------------------------------------------------------------------
33 function get_tab_title($tab)
36 for ($i = 0; $i < count($tabs); $i++)
38 if ($tabs[$i] == $tab)
44 function tab_list_row($label, $name, $selected)
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++)
55 if ($selected == $tabs[$i])
56 echo "<option selected value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
58 echo "<option value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
60 echo "</select></td>\n";
64 //---------------------------------------------------------------------------------------------
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
79 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
81 // if the parameters are invalid
82 if (!is_array($aryData) || !$strIndex || !$strSortBy)
86 // create our temporary arrays
87 $arySort = $aryResult = array();
89 // loop through the array
90 foreach ($aryData as $aryRow)
91 // set up the value in the array
92 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
94 // apply the natural sort
97 // if the sort type is descending
98 if ($strSortType=="desc")
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);
114 function write_modules()
116 global $path_to_root, $installed_modules;
118 $mods = array_natsort($installed_modules, 'tab', 'tab');
119 $installed_modules = $mods;
120 //reset($installed_languages);
121 $n = count($installed_modules);
124 $msg .= "/*****************************************************************\n";
125 $msg .= "External modules for FrontAccounting\n";
126 $msg .= "******************************************************************/\n";
129 $msg .= "\$installed_modules = array (\n";
131 for ($i = 0; $i < $n; $i++)
134 $msg .= "\t\tarray ";
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'] . "'";
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))
149 if (!$zp = fopen($filename, 'w'))
151 display_error(_("Cannot open the modules file - ") . $filename);
156 if (!fwrite($zp, $msg))
158 display_error(_("Cannot write to the modules file - ") . $filename);
168 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
174 //---------------------------------------------------------------------------------------------
176 function handle_submit()
178 global $path_to_root, $installed_modules;
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))
193 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
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))
200 move_uploaded_file($file1, $file2);
203 $installed_modules[$id]['filename'] = $_POST['filename'];
204 if (!write_modules())
209 //---------------------------------------------------------------------------------------------
211 function handle_delete()
213 global $path_to_root, $installed_modules;
217 $path = $installed_modules[$id]['path'];
218 $filename = "$path_to_root/modules/$path";
219 if ($h = opendir($filename))
221 while (($file = readdir($h)) !== false)
223 if (is_file("$filename/$file"))
224 unlink("$filename/$file");
230 unset($installed_modules[$id]);
231 $mods = array_values($installed_modules);
232 $installed_modules = $mods;
234 if (!write_modules())
236 meta_forward($_SERVER['PHP_SELF']);
239 //---------------------------------------------------------------------------------------------
241 function display_modules()
243 global $table_style, $installed_modules;
246 <script language='javascript'>
247 function deleteModule(id, name) {
248 if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
250 document.location.replace('inst_module.php?c=df&id='+id)
253 start_table($table_style);
254 $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
258 $mods = $installed_modules;
260 for ($i = 0; $i < $n; $i++)
262 alt_table_row_color($k);
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>");
276 //---------------------------------------------------------------------------------------------
278 function display_module_edit($selected_id)
280 global $installed_modules, $table_style2;
282 if ($selected_id != -1)
285 $n = count($installed_modules);
287 start_form(true, true);
290 <script language='javascript'>
291 function updateModule() {
292 document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
293 document.forms[0].submit()
297 start_table($table_style2);
299 if ($selected_id != -1)
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']);
309 tab_list_row(_("Menu Tab"), 'tab', null);
310 text_row_ex(_("Name"), 'name', 30);
311 text_row_ex(_("Folder"), 'path', 20);
313 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
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"). "'>";
324 //---------------------------------------------------------------------------------------------
326 if (isset($_GET['c']))
328 if ($_GET['c'] == 'df')
333 if ($_GET['c'] == 'u')
337 //meta_forward($_SERVER['PHP_SELF']);
342 //---------------------------------------------------------------------------------------------
346 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
348 display_module_edit($selected_id);
350 //---------------------------------------------------------------------------------------------