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;
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 unset($extensions[$id]);
171 $mods = array_values($extensions);
174 if (!write_extensions($extensions))
177 // should we also delete module form per company extension files?
179 meta_forward($_SERVER['PHP_SELF']);
182 //---------------------------------------------------------------------------------------------
184 function display_extensions()
186 global $table_style, $tabs;
189 <script language='javascript'>
190 function deleteExtension(id, name) {
191 if (!confirm('" . _("Are you sure you want to delete extension: ") . "'+name))
193 document.location.replace('inst_module.php?c=df&id='+id)
196 start_table($table_style);
197 $th = array(_("Name"),_("Tab"), _("Link text"), _("Folder"), _("Filename"),
198 _("Access extensions"),"", "");
202 $mods = get_company_extensions();
204 for ($i = 0; $i < $n; $i++)
206 $is_mod = $mods[$i]['type'] == 'module';
207 alt_table_row_color($k);
208 label_cell($mods[$i]['name']);
209 label_cell( $is_mod ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
210 $ttl = access_string($mods[$i]['title']);
212 label_cell($mods[$i]['path']);
213 label_cell($mods[$i]['filename']);
214 label_cell(@$mods[$i]['acc_file']);
216 $delete = _("Delete");
219 label_cell(''); // not implemented (yet)
224 if (user_graphic_links())
226 $edit = set_icon(ICON_EDIT, $edit);
227 $delete = set_icon(ICON_DELETE, $delete);
229 label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
230 label_cell("<a href='javascript:deleteExtension(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
238 function company_extensions($id)
240 global $table_style, $tabs;
242 start_table($table_style);
244 $th = array(_("Name"),_("Tab"), _("Link text"), _("Active"));
246 // get all available extensions and display
247 // with current status stored in company directory.
249 $mods = get_company_extensions();
250 $exts = get_company_extensions($id);
251 foreach($mods as $key => $ins) {
252 foreach($exts as $ext)
253 if ($ext['name'] == $ins['name']) {
254 $mods[$key]['active'] = @$ext['active'];
262 for ($i = 0; $i < $n; $i++)
264 alt_table_row_color($k);
265 label_cell($mods[$i]['name']);
266 label_cell($mods[$i]['type'] == 'module' ? $mods[$i]['title'] : $tabs[$mods[$i]['tab']]);
267 $ttl = access_string($mods[$i]['title']);
269 check_cells(null, 'Active'.$i, @$mods[$i]['active'] ? 1:0,
270 false, false, "align='center'");
275 submit_center('Update', _('Update'), true, false, 'default');
278 //---------------------------------------------------------------------------------------------
280 function display_ext_edit($selected_id)
282 global $table_style2;
284 $extensions = get_company_extensions();
285 if ($selected_id != -1)
288 $n = count($extensions);
292 <script language='javascript'>
293 function updateModule() {
294 document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
295 document.forms[0].submit()
299 start_table($table_style2);
301 if ($selected_id != -1 && $extensions[$selected_id]['type'] == 'plugin')
303 $mod = $extensions[$selected_id];
304 $_POST['tab'] = $mod['tab'];
305 $_POST['name'] = $mod['name'];
306 $_POST['title'] = $mod['title'];
307 $_POST['path'] = $mod['path'];
308 $_POST['filename'] = $mod['filename'];
309 $_POST['acc_file'] = @$mod['acc_file'];
310 hidden('selected_id', $selected_id);
311 hidden('filename', $_POST['filename']);
312 hidden('acc_file', $_POST['acc_file']);
314 text_row_ex(_("Name"), 'name', 30);
315 text_row_ex(_("Folder"), 'path', 20);
317 tab_list_row(_("Menu Tab"), 'tab', null);
318 text_row_ex(_("Menu Link Text"), 'title', 30);
319 record_status_list_row(_("Default status"), 'active');
321 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
322 label_row(_("Access Levels Extensions"), "<input name='uploadfile3' type='file'>");
323 label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
326 display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
328 echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
332 //---------------------------------------------------------------------------------------------
333 if (get_post('Update')) {
334 $exts = get_company_extensions();
335 for($i = 0; $i < count($exts); $i++) {
336 $exts[$i]['active'] = check_value('Active'.$i);
338 write_extensions($exts, get_post('extset'));
339 if (get_post('extset') == user_company())
340 $installed_extensions = $exts;
341 display_notification(_('Current active extensions set has been saved.'));
343 elseif (isset($_GET['c']))
345 if ($_GET['c'] == 'df')
350 if ($_GET['c'] == 'u')
354 if ($selected_id != -1)
355 display_notification(_("Extension data has been updated."));
357 display_notification(_("Extension has been installed."));
362 //---------------------------------------------------------------------------------------------
364 if (list_updated('extset'))
365 $Ajax->activate('_page_body');
367 echo "<center>" . _('Extensions:') . " ";
368 extset_list('extset', null, true);
369 echo "</center><br>";
371 $set = get_post('extset');
374 display_extensions();
376 hyperlink_no_params($_SERVER['PHP_SELF'], _("Add new extension"));
378 display_ext_edit($selected_id);
380 company_extensions($set);
382 //---------------------------------------------------------------------------------------------