[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[fa-stable.git] / admin / inst_module.php
1 <?php
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';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15 include_once($path_to_root."/includes/packages.inc");
16
17 if ($SysPrefs->use_popup_windows) {
18         $js = get_js_open_window(900, 500);
19 }
20 page(_($help_context = "Install/Activate extensions"), false, false, "", $js);
21
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/admin/db/company_db.inc");
24 include_once($path_to_root . "/admin/db/maintenance_db.inc");
25 include_once($path_to_root . "/includes/ui.inc");
26
27 simple_page_mode(true);
28
29 //---------------------------------------------------------------------------------------------
30 function local_extension($id)
31 {
32         global $next_extension_id, $Ajax, $path_to_root;
33
34         $exts = get_company_extensions();
35         $exts[$next_extension_id++] = array(
36                         'package' => $id,
37                         'name' => $id,
38                         'version' => '-',
39                         'available' => '',
40                         'type' => 'extension',
41                         'path' => 'modules/'.$id,
42                         'active' => false
43         );
44
45         $local_module_path = $path_to_root.'/modules/'.clean_file_name($id);
46         $local_config_file = $local_module_path.'/_init/config';
47         $local_hook_file = $local_module_path.'/hooks.php';
48
49         if (file_exists($local_config_file)) {
50                 $ctrl = get_control_file($local_config_file);
51                 if (key_exists('Name', $ctrl)) $exts[$next_extension_id-1]['name'] = $ctrl['Name'];
52                 if (key_exists('Version', $ctrl)) $exts[$next_extension_id-1]['version'] = $ctrl['Version'];
53         }
54         if (file_exists($local_hook_file)) {
55                 include_once($local_hook_file);
56
57         }
58         $hooks_class = 'hooks_'.$id;
59         if (class_exists($hooks_class, false)) {
60                 $hooks = new $hooks_class;
61                 $hooks->install_extension(false);
62         }
63         $Ajax->activate('ext_tbl'); // refresh settings display
64         if (!update_extensions($exts))
65                 return false;
66         return true;
67 }
68
69 function handle_delete($id)
70 {
71         global $path_to_root;
72         
73         $extensions = get_company_extensions();
74         $ext = $extensions[$id];
75         if ($ext['version'] != '-') {
76                 if (!uninstall_package($ext['package']))
77                         return false;
78         } else {
79                 @include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
80                 $hooks_class = 'hooks_'.$ext['package'];
81                 if (class_exists($hooks_class)) {
82                         $hooks = new $hooks_class;
83                         $hooks->uninstall_extension(false);
84                 }
85         }
86         unset($extensions[$id]);
87         if (update_extensions($extensions)) {
88                 display_notification(_("Selected extension has been successfully deleted"));
89         }
90         return true;
91 }
92 //
93 // Helper for formating menu tabs/entries to be displayed in extension table
94 //
95 function fmt_titles($defs)
96 {
97                 if (!$defs) return '';
98                 foreach($defs as $def) {
99                         $str[] = access_string($def['title'], true);
100                 }
101                 return implode('<br>', array_values($str));
102 }
103 //---------------------------------------------------------------------------------------------
104 //
105 // Display list of all extensions - installed and available from repository
106 //
107 function display_extensions($mods)
108 {
109         global $installed_extensions;
110         
111         div_start('ext_tbl');
112         start_table(TABLESTYLE);
113
114         $th = array(_("Extension"),_("Modules provided"), _("Options provided"),
115                  _("Installed"), _("Available"),  "", "");
116         table_header($th);
117
118         $k = 0;
119
120         foreach($mods as $pkg_name => $ext)
121         {
122                 $available = @$ext['available'];
123                 $installed = @$ext['version'];
124                 $id = @$ext['local_id'];
125
126                 $entries = fmt_titles(@$ext['entries']);
127                 $tabs = fmt_titles(@$ext['tabs']);
128
129                 alt_table_row_color($k);
130
131                 label_cell($available ? get_package_view_str($pkg_name, $ext['name']) : $ext['name']);
132                 label_cell($tabs);
133                 label_cell($entries);
134
135                 label_cell($id === null ? _("None") :
136                         (($installed && ($installed != '-' || $installed != '')) ? $installed : _("Unknown")));
137                 label_cell($available ? $available : _("Unknown"));
138
139                 if (!$available && $ext['type'] == 'extension') {// third-party plugin
140                         if (!$installed)
141                                 button_cell('Local'.$ext['package'], _("Install"), _('Install third-party extension.'), 
142                                         ICON_DOWN);
143                         else
144                                 label_cell('');
145                 } elseif (check_pkg_upgrade($installed, $available)) // outdated or not installed extension in repo
146                         button_cell('Update'.$pkg_name, $installed ? _("Update") : _("Install"),
147                                 _('Upload and install latest extension package'), ICON_DOWN);
148                 else
149                         label_cell('');
150
151                 if ($id !== null) {
152                         delete_button_cell('Delete'.$id, _('Delete'));
153                         submit_js_confirm('Delete'.$id, 
154                                 sprintf(_("You are about to remove package \'%s\'.\nDo you want to continue ?"), 
155                                         $ext['name']));
156                 } else
157                         label_cell('');
158
159                 end_row();
160         }
161
162         end_table(1);
163
164         submit_center_first('Refresh', _("Update"), '', null);
165
166         div_end();
167 }
168 //---------------------------------------------------------------------------------
169 //
170 // Get all installed extensions and display
171 // with current status stored in company directory.
172 //
173 function company_extensions($id)
174 {
175         start_table(TABLESTYLE);
176         
177         $th = array(_("Extension"),_("Modules provided"), _("Options provided"), _("Active"));
178         
179         $mods = get_company_extensions();
180         $exts = get_company_extensions($id);
181         foreach($mods as $key => $ins) {
182                 foreach($exts as $ext)
183                         if ($ext['name'] == $ins['name']) {
184                                 $mods[$key]['active'] = @$ext['active'];
185                                 continue 2;
186                         }
187         }
188         $mods = array_natsort($mods, null, 'name');
189         table_header($th);
190         $k = 0;
191         foreach($mods as $i => $mod)
192         {
193                 if ($mod['type'] != 'extension') continue;
194                 alt_table_row_color($k);
195                 label_cell($mod['name']);
196                 $entries = fmt_titles(@$mod['entries']);
197                 $tabs = fmt_titles(@$mod['tabs']);
198                 label_cell($tabs);
199                 label_cell($entries);
200
201                 check_cells(null, 'Active'.$i, @$mod['active'] ? 1:0, 
202                         false, false, "align='center'");
203                 end_row();
204         }
205
206         end_table(1);
207         submit_center('Refresh', _('Update'), true, false, 'default');
208 }
209
210 //---------------------------------------------------------------------------------------------
211 if ($Mode == 'Delete')
212 {
213         handle_delete($selected_id);
214         $Mode = 'RESET';
215 }
216
217 if (get_post('Refresh')) {
218         $comp = get_post('extset');
219         $exts = get_company_extensions($comp);
220
221         $result = true;
222         foreach($exts as $i => $ext) {
223                 if ($ext['package'] && ($ext['active'] ^ check_value('Active'.$i))) 
224                 {
225                         if (check_value('Active'.$i) && !check_src_ext_version($ext['version']))
226                         {
227                                 display_warning(sprintf(_("Package '%s' is incompatible with current application version and cannot be activated.\n")
228                                         . _("Check Install/Activate page for newer package version."), $ext['name']));
229                                 continue;
230                         }
231                         $activated = activate_hooks($ext['package'], $comp, !$ext['active']);   // change active state
232
233                         if ($activated !== null)
234                                 $result &= $activated;
235                         if ($activated || ($activated === null))
236                                 $exts[$i]['active'] = check_value('Active'.$i);
237                 }
238         }
239         write_extensions($exts, get_post('extset'));
240         if (get_post('extset') == user_company())
241                 $installed_extensions = $exts;
242         
243         if(!$result) {
244                 display_error(_('Status change for some extensions failed.'));
245                 $Ajax->activate('ext_tbl'); // refresh settings display
246         }else
247                 display_notification(_('Current active extensions set has been saved.'));
248 }
249
250 if ($id = find_submit('Update', false))
251         install_extension($id);
252
253 if ($id = find_submit('Local', false))
254         local_extension($id);
255
256 if ($Mode == 'RESET')
257 {
258         $selected_id = -1;
259         unset($_POST);
260 }
261
262 //---------------------------------------------------------------------------------------------
263 start_form(true);
264 if (list_updated('extset'))
265         $Ajax->activate('_page_body');
266
267 $set = get_post('extset', -1);
268
269 echo "<center>" . _('Extensions:') . "&nbsp;&nbsp;";
270 echo extset_list('extset', null, true);
271 echo "</center><br>";
272
273 if ($set == -1) 
274 {
275         $mods = get_extensions_list('extension');
276         if (!$mods)
277                 display_note(_("No optional extension module is currently available."));
278         else
279                 display_extensions($mods);
280 } else 
281         company_extensions($set);
282
283 //---------------------------------------------------------------------------------------------
284 end_form();
285
286 end_page();