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 ***********************************************************************/
12 $page_security = 'SA_CREATEMODULES';
14 include_once($path_to_root . "/includes/session.inc");
16 page(_("Install/Activate extensions"));
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 . "/includes/ui.inc");
23 //---------------------------------------------------------------------------------------------
25 if (isset($_GET['selected_id']))
27 $selected_id = $_GET['selected_id'];
29 elseif (isset($_POST['selected_id']))
31 $selected_id = $_POST['selected_id'];
36 //---------------------------------------------------------------------------------------------
37 function get_company_extensions($id = -1) {
41 $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php';
42 $installed_extensions = array();
46 return $installed_extensions;
49 function check_data($id, $exts)
51 if ($_POST['name'] == "") {
52 display_error(_("Extension name cannot be empty."));
55 foreach($exts as $n =>$ext) {
56 if ($_POST['name'] == $ext['name'] && $id != $n) {
57 display_error(_("Extension name have to be unique."));
62 if ($_POST['title'] == "") {
63 display_error(_("Extension title cannot be empty."));
66 if ($_POST['path'] == "") {
67 display_error(_("Extension folder name cannot be empty."));
70 if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
71 display_error(_("You have to select plugin file to upload"));
77 //---------------------------------------------------------------------------------------------
79 function handle_submit()
81 global $path_to_root, $db_connections, $selected_id;
83 $extensions = get_company_extensions();
84 if (!check_data($selected_id, $extensions))
89 $extensions[$id]['tab'] = $_POST['tab'];
90 $extensions[$id]['name'] = $_POST['name'];
91 $extensions[$id]['path'] = $_POST['path'];
92 $extensions[$id]['title'] = $_POST['title'];
93 $extensions[$id]['active'] = $_POST['active'];
95 // Currently we support only plugin extensions here.
96 $extensions[$id]['type'] = 'plugin';
97 $directory = $path_to_root . "/modules/" . $_POST['path'];
98 if (!file_exists($directory))
102 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
104 $extensions[$id]['filename'] = $_FILES['uploadfile']['name'];
105 $file1 = $_FILES['uploadfile']['tmp_name'];
106 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
107 if (file_exists($file2))
109 move_uploaded_file($file1, $file2);
112 $extensions[$id]['filename'] = get_post('filename');
113 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
115 $file1 = $_FILES['uploadfile2']['tmp_name'];
116 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
117 if (file_exists($file2))
119 move_uploaded_file($file1, $file2);
120 $db_name = $_SESSION["wa_current_user"]->company;
121 db_import($file2, $db_connections[$db_name]);
124 if (is_uploaded_file($_FILES['uploadfile3']['tmp_name']))
126 $extensions[$id]['acc_file'] = $_FILES['uploadfile3']['name'];
127 $file1 = $_FILES['uploadfile3']['tmp_name'];
128 $file2 = $directory . "/".$_FILES['uploadfile3']['name'];
129 if (file_exists($file2))
131 move_uploaded_file($file1, $file2);
134 $extensions[$id]['acc_file'] = get_post('acc_file');
136 if (!write_extensions($extensions))
141 //---------------------------------------------------------------------------------------------
143 function handle_delete()
145 global $path_to_root, $db_connections;
147 $extensions = get_company_extensions();
151 $path = $extensions[$id]['path'];
153 if ($extensions[$id]['type'] != 'plugin') {
154 display_error(_('Module installation support is not implemented yet. You have to do it manually.'));
158 $filename = "$path_to_root/modules/$path";
159 if ($h = opendir($filename))
161 while (($file = readdir($h)) !== false)
163 if (is_file("$filename/$file"))
164 unlink("$filename/$file");
170 $ident = $extensions[$id]['name'];
171 unset($extensions[$id]);
172 $mods = array_values($extensions);
175 if (!write_extensions($extensions))
178 // update per company files
179 $cnt = count($db_connections);
180 for($i = 0; $i < $cnt; $i++)
182 $exts = get_company_extensions($i);
183 foreach($exts as $key => $ext) {
184 if ($ext['name'] == $ident) {
189 write_extensions($exts, $i);
191 meta_forward($_SERVER['PHP_SELF']);
194 //---------------------------------------------------------------------------------------------
196 function display_extensions()
198 global $table_style, $tabs;
201 <script language='javascript'>
202 function deleteExtension(id, name) {
203 if (!confirm('" . _("Are you sure you want to delete extension: ") . "'+name))
205 document.location.replace('inst_module.php?c=df&id='+id)
208 start_table($table_style);
209 $th = array(_("Name"),_("Tab"), _("Link text"), _("Folder"), _("Filename"),
210 _("Access extensions"),"", "");
214 $mods = get_company_extensions();
216 for ($i = 0; $i < $n; $i++)
218 $is_mod = $mods[$i]['type'] == 'module';
219 alt_table_row_color($k);
220 label_cell($mods[$i]['name']);
221 label_cell( $is_mod ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
222 $ttl = access_string($mods[$i]['title']);
224 label_cell($mods[$i]['path']);
225 label_cell($mods[$i]['filename']);
226 label_cell(@$mods[$i]['acc_file']);
228 $delete = _("Delete");
231 label_cell(''); // not implemented (yet)
236 if (user_graphic_links())
238 $edit = set_icon(ICON_EDIT, $edit);
239 $delete = set_icon(ICON_DELETE, $delete);
241 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
242 label_cell("<a href='javascript:deleteExtension(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
250 function company_extensions($id)
252 global $table_style, $tabs;
254 start_table($table_style);
256 $th = array(_("Name"),_("Tab"), _("Link text"), _("Active"));
258 // get all available extensions and display
259 // with current status stored in company directory.
261 $mods = get_company_extensions();
262 $exts = get_company_extensions($id);
263 foreach($mods as $key => $ins) {
264 foreach($exts as $ext)
265 if ($ext['name'] == $ins['name']) {
266 $mods[$key]['active'] = @$ext['active'];
274 for ($i = 0; $i < $n; $i++)
276 alt_table_row_color($k);
277 label_cell($mods[$i]['name']);
278 label_cell($mods[$i]['type'] == 'module' ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
279 $ttl = access_string($mods[$i]['title']);
281 check_cells(null, 'Active'.$i, @$mods[$i]['active'] ? 1:0,
282 false, false, "align='center'");
287 submit_center('Update', _('Update'), true, false, 'default');
290 //---------------------------------------------------------------------------------------------
292 function display_ext_edit($selected_id)
294 global $table_style2;
296 $extensions = get_company_extensions();
297 if ($selected_id != -1)
300 $n = count($extensions);
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 && $extensions[$selected_id]['type'] == 'plugin')
315 $mod = $extensions[$selected_id];
316 $_POST['tab'] = $mod['tab'];
317 $_POST['name'] = $mod['name'];
318 $_POST['title'] = $mod['title'];
319 $_POST['path'] = $mod['path'];
320 $_POST['filename'] = $mod['filename'];
321 $_POST['acc_file'] = @$mod['acc_file'];
322 hidden('selected_id', $selected_id);
323 hidden('filename', $_POST['filename']);
324 hidden('acc_file', $_POST['acc_file']);
326 text_row_ex(_("Name"), 'name', 30);
327 text_row_ex(_("Folder"), 'path', 20);
329 tab_list_row(_("Menu Tab"), 'tab', null);
330 text_row_ex(_("Menu Link Text"), 'title', 30);
331 record_status_list_row(_("Default status"), 'active');
333 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
334 label_row(_("Access Levels Extensions"), "<input name='uploadfile3' type='file'>");
335 label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
338 display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
340 echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
344 //---------------------------------------------------------------------------------------------
345 if (get_post('Update')) {
346 $exts = get_company_extensions();
347 for($i = 0; $i < count($exts); $i++) {
348 $exts[$i]['active'] = check_value('Active'.$i);
350 write_extensions($exts, get_post('extset'));
351 if (get_post('extset') == user_company())
352 $installed_extensions = $exts;
353 display_notification(_('Current active extensions set has been saved.'));
355 elseif (isset($_GET['c']))
357 if ($_GET['c'] == 'df')
362 if ($_GET['c'] == 'u')
366 if ($selected_id != -1)
367 display_notification(_("Extension data has been updated."));
369 display_notification(_("Extension has been installed."));
374 //---------------------------------------------------------------------------------------------
376 if (list_updated('extset'))
377 $Ajax->activate('_page_body');
379 echo "<center>" . _('Extensions:') . " ";
380 extset_list('extset', null, true);
381 echo "</center><br>";
383 $set = get_post('extset');
386 display_extensions();
388 hyperlink_no_params($_SERVER['PHP_SELF'], _("Add new extension"));
390 display_ext_edit($selected_id);
392 company_extensions($set);
394 //---------------------------------------------------------------------------------------------