Rewritten third-party module installation according to extended hooks system.
[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         @include_once($path_to_root.'/modules/'.$id.'/hooks.php');
46         $hooks_class = 'hooks_'.$id;
47         if (class_exists($hooks_class)) {
48                 $hooks = new $hooks_class;
49                 $hooks->install_extension(false);
50         }
51         
52         $Ajax->activate('ext_tbl'); // refresh settings display
53         if (!update_extensions($exts))
54                 return false;
55         return true;
56 }
57
58 function handle_delete($id)
59 {
60         global $path_to_root;
61         
62         $extensions = get_company_extensions();
63         $ext = $extensions[$id];
64         if ($ext['version'] != '-') {
65                 if (!uninstall_package($ext['package']))
66                         return false;
67         } else {
68                 @include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
69                 $hooks_class = 'hooks_'.$ext['package'];
70                 if (class_exists($hooks_class)) {
71                         $hooks = new $hooks_class;
72                         $hooks->uninstall_extension(false);
73                 }
74         }
75         unset($extensions[$id]);
76         if (update_extensions($extensions)) {
77                 display_notification(_("Selected extension has been successfully deleted"));
78         }
79         return true;
80 }
81 //
82 // Helper for formating menu tabs/entries to be displayed in extension table
83 //
84 function fmt_titles($defs)
85 {
86                 if (!$defs) return '';
87                 foreach($defs as $def) {
88                         $str[] = access_string($def['title'], true);
89                 }
90                 return implode('<br>', array_values($str));
91 }
92 //---------------------------------------------------------------------------------------------
93 //
94 // Display list of all extensions - installed and available from repository
95 //
96 function display_extensions()
97 {
98         global $installed_extensions;
99         
100         div_start('ext_tbl');
101         start_table(TABLESTYLE);
102
103         $th = array(_("Extension"),_("Modules provided"), _("Options provided"),
104                  _("Installed"), _("Available"),  "", "");
105         table_header($th);
106
107         $k = 0;
108         $mods = get_extensions_list('extension');
109
110         foreach($mods as $pkg_name => $ext)
111         {
112                 $available = @$ext['available'];
113                 $installed = @$ext['version'];
114                 $id = @$ext['local_id'];
115                 $is_mod = $ext['type'] == 'module';
116
117                 $entries = fmt_titles(@$ext['entries']);
118                 $tabs = fmt_titles(@$ext['tabs']);
119
120                 alt_table_row_color($k);
121 //              label_cell(is_array($ext['Descr']) ? $ext['Descr'][0] : $ext['Descr']);
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         submit_center_last('Add', _("Add third-party extension"), '', false);
157
158         div_end();
159 }
160 //---------------------------------------------------------------------------------
161 //
162 // Get all installed extensions and display
163 // with current status stored in company directory.
164 //
165 function company_extensions($id)
166 {
167         start_table(TABLESTYLE);
168         
169         $th = array(_("Extension"),_("Modules provided"), _("Options provided"), _("Active"));
170         
171         $mods = get_company_extensions();
172         $exts = get_company_extensions($id);
173         foreach($mods as $key => $ins) {
174                 foreach($exts as $ext)
175                         if ($ext['name'] == $ins['name']) {
176                                 $mods[$key]['active'] = @$ext['active'];
177                                 continue 2;
178                         }
179         }
180         $mods = array_natsort($mods, null, 'name');
181         table_header($th);
182         $k = 0;
183         foreach($mods as $i => $mod)
184         {
185                 if ($mod['type'] != 'extension') continue;
186                 alt_table_row_color($k);
187                 label_cell($mod['name']);
188                 $entries = fmt_titles(@$mod['entries']);
189                 $tabs = fmt_titles(@$mod['tabs']);
190                 label_cell($tabs);
191                 label_cell($entries);
192
193                 check_cells(null, 'Active'.$i, @$mod['active'] ? 1:0, 
194                         false, false, "align='center'");
195                 end_row();
196         }
197
198         end_table(1);
199         submit_center('Refresh', _('Update'), true, false, 'default');
200 }
201
202 //---------------------------------------------------------------------------------------------
203 if ($Mode == 'Delete')
204 {
205         handle_delete($selected_id);
206         $Mode = 'RESET';
207 }
208
209 if (get_post('Refresh')) {
210         $comp = get_post('extset');
211         $exts = get_company_extensions($comp);
212
213         $result = true;
214         foreach($exts as $i => $ext) {
215                 if ($ext['package'] && ($ext['active'] ^ check_value('Active'.$i))) {
216                         if (!$ext['active'])
217                                 $activated = activate_hooks($ext['package'], $comp);
218                         else
219                                 $activated = hook_invoke($ext['package'], check_value('Active'.$i) ?
220                                  'activate_extension':'deactivate_extension', $comp, false);
221                         if ($activated !== null)
222                                 $result &= $activated;
223                         if ($activated || ($activated === null))
224                                 $exts[$i]['active'] = check_value('Active'.$i);
225                 }
226         }
227         write_extensions($exts, get_post('extset'));
228         if (get_post('extset') == user_company())
229                 $installed_extensions = $exts;
230         
231         if(!$result) {
232                 display_error(_('Status change for some extensions failed.'));
233                 $Ajax->activate('ext_tbl'); // refresh settings display
234         }else
235                 display_notification(_('Current active extensions set has been saved.'));
236 }
237
238 if ($id = find_submit('Update', false))
239         install_extension($id);
240
241 if ($id = find_submit('Local', false))
242         local_extension($id);
243
244 if ($Mode == 'RESET')
245 {
246         $selected_id = -1;
247         unset($_POST);
248 }
249
250 //---------------------------------------------------------------------------------------------
251 start_form(true);
252 if (list_updated('extset'))
253         $Ajax->activate('_page_body');
254
255 $set = get_post('extset', -1);
256
257 echo "<center>" . _('Extensions:') . "&nbsp;&nbsp;";
258 echo extset_list('extset', null, true);
259 echo "</center><br>";
260
261 if ($set == -1) 
262         display_extensions();
263 else 
264         company_extensions($set);
265
266 //---------------------------------------------------------------------------------------------
267 end_form();
268
269 end_page();
270 ?>