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 //---------------------------------------------------------------------------------------------
26 if (isset($_GET['selected_id']))
28 $selected_id = $_GET['selected_id'];
30 elseif (isset($_POST['selected_id']))
32 $selected_id = $_POST['selected_id'];
37 //---------------------------------------------------------------------------------------------
41 if ($_POST['name'] == "" || $_POST['path'] == "")
47 * @return Returns the array sorted as required
48 * @param $aryData Array containing data to sort
49 * @param $strIndex name of column to use as an index
50 * @param $strSortBy Column to sort the array by
51 * @param $strSortType String containing either asc or desc [default to asc]
52 * @desc Naturally sorts an array using by the column $strSortBy
54 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
56 // if the parameters are invalid
57 if (!is_array($aryData) || !$strIndex || !$strSortBy)
61 // create our temporary arrays
62 $arySort = $aryResult = array();
64 // loop through the array
65 foreach ($aryData as $aryRow)
66 // set up the value in the array
67 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
69 // apply the natural sort
72 // if the sort type is descending
73 if ($strSortType=="desc")
77 // loop through the sorted and original data
78 foreach ($arySort as $arySortKey => $arySorted)
79 foreach ($aryData as $aryOriginal)
81 if ($aryOriginal[$strIndex]==$arySortKey)
82 // add it to the output array
83 array_push($aryResult, $aryOriginal);
89 function write_modules()
91 global $path_to_root, $installed_modules;
93 $mods = array_natsort($installed_modules, 'tab', 'tab');
94 $installed_modules = $mods;
95 //reset($installed_languages);
96 $n = count($installed_modules);
99 $msg .= "/*****************************************************************\n";
100 $msg .= "External modules for FrontAccounting\n";
101 $msg .= "******************************************************************/\n";
104 $msg .= "\$installed_modules = array (\n";
107 for ($i = 0; $i < $n; $i++)
110 $msg .= "\t\tarray ";
113 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
114 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
115 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
116 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
121 $filename = $path_to_root . "/modules/installed_modules.php";
122 // Check if the file exists and is writable first.
123 if (file_exists($filename) && is_writable($filename))
125 if (!$zp = fopen($filename, 'w'))
127 display_error(_("Cannot open the modules file - ") . $filename);
132 if (!fwrite($zp, $msg))
134 display_error(_("Cannot write to the modules file - ") . $filename);
144 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
150 //---------------------------------------------------------------------------------------------
152 function handle_submit()
154 global $path_to_root, $installed_modules, $db_connections;
161 $installed_modules[$id]['tab'] = $_POST['tab'];
162 $installed_modules[$id]['name'] = $_POST['name'];
163 $installed_modules[$id]['path'] = $_POST['path'];
164 $directory = $path_to_root . "/modules/" . $_POST['path'];
165 if (!file_exists($directory))
169 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
171 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
172 $file1 = $_FILES['uploadfile']['tmp_name'];
173 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
174 if (file_exists($file2))
176 move_uploaded_file($file1, $file2);
179 $installed_modules[$id]['filename'] = $_POST['filename'];
180 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
182 $file1 = $_FILES['uploadfile2']['tmp_name'];
183 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
184 if (file_exists($file2))
186 move_uploaded_file($file1, $file2);
187 $db_name = $_SESSION["wa_current_user"]->company;
188 db_import($file2, $db_connections[$db_name]);
190 if (!write_modules())
195 //---------------------------------------------------------------------------------------------
197 function handle_delete()
199 global $path_to_root, $installed_modules;
203 $path = $installed_modules[$id]['path'];
204 $filename = "$path_to_root/modules/$path";
205 if ($h = opendir($filename))
207 while (($file = readdir($h)) !== false)
209 if (is_file("$filename/$file"))
210 unlink("$filename/$file");
216 unset($installed_modules[$id]);
217 $mods = array_values($installed_modules);
218 $installed_modules = $mods;
220 if (!write_modules())
222 meta_forward($_SERVER['PHP_SELF']);
225 //---------------------------------------------------------------------------------------------
227 function display_modules()
229 global $table_style, $installed_modules, $tabs;
232 <script language='javascript'>
233 function deleteModule(id, name) {
234 if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
236 document.location.replace('inst_module.php?c=df&id='+id)
239 start_table($table_style);
240 $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
244 $mods = $installed_modules;
246 for ($i = 0; $i < $n; $i++)
248 alt_table_row_color($k);
250 label_cell($tabs[$mods[$i]['tab']]);
251 label_cell($mods[$i]['name']);
252 label_cell($mods[$i]['path']);
253 label_cell($mods[$i]['filename']);
255 $delete = _("Delete");
256 if (user_graphic_links())
258 $edit = set_icon(ICON_EDIT, $edit);
259 $delete = set_icon(ICON_DELETE, $delete);
261 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
262 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
269 //---------------------------------------------------------------------------------------------
271 function display_module_edit($selected_id)
273 global $installed_modules, $table_style2;
275 if ($selected_id != -1)
278 $n = count($installed_modules);
283 <script language='javascript'>
284 function updateModule() {
285 document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
286 document.forms[0].submit()
290 start_table($table_style2);
292 if ($selected_id != -1)
294 $mod = $installed_modules[$selected_id];
295 $_POST['tab'] = $mod['tab'];
296 $_POST['name'] = $mod['name'];
297 $_POST['path'] = $mod['path'];
298 $_POST['filename'] = $mod['filename'];
299 hidden('selected_id', $selected_id);
300 hidden('filename', $_POST['filename']);
302 tab_list_row(_("Menu Tab"), 'tab', null);
303 text_row_ex(_("Name"), 'name', 30);
304 text_row_ex(_("Folder"), 'path', 20);
306 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
307 label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
310 display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
311 echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
318 //---------------------------------------------------------------------------------------------
320 if (isset($_GET['c']))
322 if ($_GET['c'] == 'df')
327 if ($_GET['c'] == 'u')
331 //meta_forward($_SERVER['PHP_SELF']);
336 //---------------------------------------------------------------------------------------------
340 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
342 display_module_edit($selected_id);
344 //---------------------------------------------------------------------------------------------