Install/Activate Extensions: displays more sensible information for modules available...
[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 //
94 // Display list of all extensions - installed and available from repository
95 //
96 function display_extensions($mods)
97 {
98         global $installed_extensions;
99         
100         div_start('ext_tbl');
101         start_table(TABLESTYLE);
102
103         $th = array(_("Extension"), _("Installed"), _("Available"),  "", "");
104         table_header($th);
105
106         $k = 0;
107
108         foreach($mods as $pkg_name => $ext)
109         {
110                 $available = @$ext['available'];
111                 $installed = @$ext['version'];
112                 $id = @$ext['local_id'];
113
114                 alt_table_row_color($k);
115
116                 label_cell($available ? get_package_view_str($pkg_name, $ext['name']) : $ext['name']);
117
118                 label_cell($id === null ? _("None") :
119                         (($installed && ($installed != '-' || $installed != '')) ? $installed : _("Unknown")));
120                 label_cell($available ? $available : _("Unknown"));
121
122                 if (!$available && $ext['type'] == 'extension') {// third-party plugin
123                         if (!$installed)
124                                 button_cell('Local'.$ext['package'], _("Install"), _('Install third-party extension.'), 
125                                         ICON_DOWN);
126                         else
127                                 label_cell('');
128                 } elseif (check_pkg_upgrade($installed, $available)) // outdated or not installed extension in repo
129                         button_cell('Update'.$pkg_name, $installed ? _("Update") : _("Install"),
130                                 _('Upload and install latest extension package'), ICON_DOWN);
131                 else
132                         label_cell('');
133
134                 if ($id !== null) {
135                         delete_button_cell('Delete'.$id, _('Delete'));
136                         submit_js_confirm('Delete'.$id, 
137                                 sprintf(_("You are about to remove package \'%s\'.\nDo you want to continue ?"), 
138                                         $ext['name']));
139                 } else
140                         label_cell('');
141
142                 end_row();
143         }
144
145         end_table(1);
146
147         submit_center_first('Refresh', _("Update"), '', null);
148
149         div_end();
150 }
151 //---------------------------------------------------------------------------------
152 //
153 // Get all installed extensions and display
154 // with current status stored in company directory.
155 //
156 function company_extensions($id)
157 {
158         start_table(TABLESTYLE);
159         
160         $th = array(_("Extension"), _("Version"), _("Path"), _("Active"));
161         
162         $mods = get_company_extensions();
163         $exts = get_company_extensions($id);
164         foreach($mods as $key => $ins) {
165                 foreach($exts as $ext)
166                         if ($ext['name'] == $ins['name']) {
167                                 $mods[$key]['active'] = @$ext['active'];
168                                 continue 2;
169                         }
170         }
171         $mods = array_natsort($mods, null, 'name');
172         table_header($th);
173         $k = 0;
174         foreach($mods as $i => $mod)
175         {
176                 if ($mod['type'] != 'extension') continue;
177                 alt_table_row_color($k);
178                 label_cell($mod['name']);
179                 label_cell($mod['version']);
180                 label_cell($mod['path']);
181
182                 check_cells(null, 'Active'.$i, @$mod['active'] ? 1:0, 
183                         false, false, "align='center'");
184
185                 end_row();
186         }
187
188         end_table(1);
189         submit_center('Refresh', _('Update'), true, false, 'default');
190 }
191
192 //---------------------------------------------------------------------------------------------
193 if ($Mode == 'Delete')
194 {
195         handle_delete($selected_id);
196         $Mode = 'RESET';
197 }
198
199 if (get_post('Refresh')) {
200         $comp = get_post('extset');
201         $exts = get_company_extensions($comp);
202
203         $result = true;
204         foreach($exts as $i => $ext) {
205                 if ($ext['package'] && ($ext['active'] ^ check_value('Active'.$i))) 
206                 {
207                         if (check_value('Active'.$i) && !check_src_ext_version($ext['version']))
208                         {
209                                 display_warning(sprintf(_("Package '%s' is incompatible with current application version and cannot be activated.\n")
210                                         . _("Check Install/Activate page for newer package version."), $ext['name']));
211                                 continue;
212                         }
213                         $activated = activate_hooks($ext['package'], $comp, !$ext['active']);   // change active state
214
215                         if ($activated !== null)
216                                 $result &= $activated;
217                         if ($activated || ($activated === null))
218                                 $exts[$i]['active'] = check_value('Active'.$i);
219                 }
220         }
221         write_extensions($exts, get_post('extset'));
222         if (get_post('extset') == user_company())
223                 $installed_extensions = $exts;
224         
225         if(!$result) {
226                 display_error(_('Status change for some extensions failed.'));
227                 $Ajax->activate('ext_tbl'); // refresh settings display
228         }else
229                 display_notification(_('Current active extensions set has been saved.'));
230 }
231
232 if ($id = find_submit('Update', false))
233         install_extension($id);
234
235 if ($id = find_submit('Local', false))
236         local_extension($id);
237
238 if ($Mode == 'RESET')
239 {
240         $selected_id = -1;
241         unset($_POST);
242 }
243
244 //---------------------------------------------------------------------------------------------
245 start_form(true);
246 if (list_updated('extset'))
247         $Ajax->activate('_page_body');
248
249 $set = get_post('extset', -1);
250
251 echo "<center>" . _('Extensions:') . "&nbsp;&nbsp;";
252 echo extset_list('extset', null, true);
253 echo "</center><br>";
254
255 if ($set == -1) 
256 {
257         $mods = get_extensions_list('extension');
258         if (!$mods)
259                 display_note(_("No optional extension module is currently available."));
260         else
261                 display_extensions($mods);
262 } else 
263         company_extensions($set);
264
265 //---------------------------------------------------------------------------------------------
266 end_form();
267
268 end_page();