2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
14 include_once($path_to_root . "/includes/session.inc");
16 page(_("Install/Update Modules"));
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/company_db.inc");
20 include_once($path_to_root . "/admin/db/maintenance_db.inc");
21 include_once($path_to_root . "/modules/installed_modules.php");
22 include_once($path_to_root . "/includes/ui.inc");
24 $tabs = array('orders', 'AP', 'stock', 'manuf', 'proj', 'GL', 'system');
25 $names = array(_("Sales"), _("Purchases"), _("Items and Inventory"), _("Manufacturing"),
26 _("Dimensions"), _("Banking and General Ledger"), _("Setup"));
28 //---------------------------------------------------------------------------------------------
30 if (isset($_GET['selected_id']))
32 $selected_id = $_GET['selected_id'];
34 elseif (isset($_POST['selected_id']))
36 $selected_id = $_POST['selected_id'];
41 //---------------------------------------------------------------------------------------------
43 function get_tab_title($tab)
46 for ($i = 0; $i < count($tabs); $i++)
48 if ($tabs[$i] == $tab)
54 function tab_list_row($label, $name, $selected)
59 echo "<td>$label</td>\n";
60 if ($selected == null)
61 $selected = (!isset($_POST[$name]) ? "orders" : $_POST[$name]);
62 echo "<td><select name='$name'>";
63 for ($i = 0; $i < count($tabs); $i++)
65 if ($selected == $tabs[$i])
66 echo "<option selected value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
68 echo "<option value='".$tabs[$i]."'>" . $names[$i]. "</option>\n";
70 echo "</select></td>\n";
74 //---------------------------------------------------------------------------------------------
78 if ($_POST['name'] == "" || $_POST['path'] == "")
84 * @return Returns the array sorted as required
85 * @param $aryData Array containing data to sort
86 * @param $strIndex name of column to use as an index
87 * @param $strSortBy Column to sort the array by
88 * @param $strSortType String containing either asc or desc [default to asc]
89 * @desc Naturally sorts an array using by the column $strSortBy
91 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
93 // if the parameters are invalid
94 if (!is_array($aryData) || !$strIndex || !$strSortBy)
98 // create our temporary arrays
99 $arySort = $aryResult = array();
101 // loop through the array
102 foreach ($aryData as $aryRow)
103 // set up the value in the array
104 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
106 // apply the natural sort
109 // if the sort type is descending
110 if ($strSortType=="desc")
114 // loop through the sorted and original data
115 foreach ($arySort as $arySortKey => $arySorted)
116 foreach ($aryData as $aryOriginal)
117 // if the key matches
118 if ($aryOriginal[$strIndex]==$arySortKey)
119 // add it to the output array
120 array_push($aryResult, $aryOriginal);
126 function write_modules()
128 global $path_to_root, $installed_modules;
130 $mods = array_natsort($installed_modules, 'tab', 'tab');
131 $installed_modules = $mods;
132 //reset($installed_languages);
133 $n = count($installed_modules);
136 $msg .= "/*****************************************************************\n";
137 $msg .= "External modules for FrontAccounting\n";
138 $msg .= "******************************************************************/\n";
141 $msg .= "\$installed_modules = array (\n";
144 for ($i = 0; $i < $n; $i++)
147 $msg .= "\t\tarray ";
150 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
151 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
152 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
153 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
158 $filename = $path_to_root . "/modules/installed_modules.php";
159 // Check if the file exists and is writable first.
160 if (file_exists($filename) && is_writable($filename))
162 if (!$zp = fopen($filename, 'w'))
164 display_error(_("Cannot open the modules file - ") . $filename);
169 if (!fwrite($zp, $msg))
171 display_error(_("Cannot write to the modules file - ") . $filename);
181 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
187 //---------------------------------------------------------------------------------------------
189 function handle_submit()
191 global $path_to_root, $installed_modules, $db_connections;
198 $installed_modules[$id]['tab'] = $_POST['tab'];
199 $installed_modules[$id]['name'] = $_POST['name'];
200 $installed_modules[$id]['path'] = $_POST['path'];
201 $directory = $path_to_root . "/modules/" . $_POST['path'];
202 if (!file_exists($directory))
206 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
208 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
209 $file1 = $_FILES['uploadfile']['tmp_name'];
210 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
211 if (file_exists($file2))
213 move_uploaded_file($file1, $file2);
216 $installed_modules[$id]['filename'] = $_POST['filename'];
217 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
219 $file1 = $_FILES['uploadfile2']['tmp_name'];
220 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
221 if (file_exists($file2))
223 move_uploaded_file($file1, $file2);
224 $db_name = $_SESSION["wa_current_user"]->company;
225 db_import($file2, $db_connections[$db_name]);
227 if (!write_modules())
232 //---------------------------------------------------------------------------------------------
234 function handle_delete()
236 global $path_to_root, $installed_modules;
240 $path = $installed_modules[$id]['path'];
241 $filename = "$path_to_root/modules/$path";
242 if ($h = opendir($filename))
244 while (($file = readdir($h)) !== false)
246 if (is_file("$filename/$file"))
247 unlink("$filename/$file");
253 unset($installed_modules[$id]);
254 $mods = array_values($installed_modules);
255 $installed_modules = $mods;
257 if (!write_modules())
259 meta_forward($_SERVER['PHP_SELF']);
262 //---------------------------------------------------------------------------------------------
264 function display_modules()
266 global $table_style, $installed_modules;
269 <script language='javascript'>
270 function deleteModule(id, name) {
271 if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
273 document.location.replace('inst_module.php?c=df&id='+id)
276 start_table($table_style);
277 $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
281 $mods = $installed_modules;
283 for ($i = 0; $i < $n; $i++)
285 alt_table_row_color($k);
287 label_cell(get_tab_title($mods[$i]['tab']));
288 label_cell($mods[$i]['name']);
289 label_cell($mods[$i]['path']);
290 label_cell($mods[$i]['filename']);
292 $delete = _("Delete");
293 if (user_graphic_links())
295 $edit = set_icon(ICON_EDIT, $edit);
296 $delete = set_icon(ICON_DELETE, $delete);
298 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
299 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
306 //---------------------------------------------------------------------------------------------
308 function display_module_edit($selected_id)
310 global $installed_modules, $table_style2;
312 if ($selected_id != -1)
315 $n = count($installed_modules);
320 <script language='javascript'>
321 function updateModule() {
322 document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
323 document.forms[0].submit()
327 start_table($table_style2);
329 if ($selected_id != -1)
331 $mod = $installed_modules[$selected_id];
332 $_POST['tab'] = $mod['tab'];
333 $_POST['name'] = $mod['name'];
334 $_POST['path'] = $mod['path'];
335 $_POST['filename'] = $mod['filename'];
336 hidden('selected_id', $selected_id);
337 hidden('filename', $_POST['filename']);
339 tab_list_row(_("Menu Tab"), 'tab', null);
340 text_row_ex(_("Name"), 'name', 30);
341 text_row_ex(_("Folder"), 'path', 20);
343 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
344 label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
347 display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
348 echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
355 //---------------------------------------------------------------------------------------------
357 if (isset($_GET['c']))
359 if ($_GET['c'] == 'df')
364 if ($_GET['c'] == 'u')
368 //meta_forward($_SERVER['PHP_SELF']);
373 //---------------------------------------------------------------------------------------------
377 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
379 display_module_edit($selected_id);
381 //---------------------------------------------------------------------------------------------