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