check_table helper moved.
[fa-stable.git] / admin / inst_upgrade.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_SOFTWAREUPGRADE';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Software Upgrade"));
17
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");
22
23 //
24 //      Creates table of installer objects sorted by version.
25 //
26 function get_installers()
27 {
28         global $path_to_root;
29
30         $patchdir = $path_to_root."/sql/";
31         $upgrades = array();    
32         $datadir = @opendir($patchdir);
33
34         if ($datadir)
35         {
36                 while(false !== ($fname = readdir($datadir)))
37                 { // check all php files but index.php
38                         if (!is_dir($patchdir . $fname) && ($fname != 'index.php')
39                                 && stristr($fname, '.php') != false && $fname[0] != '.')
40                         {
41                                 unset($install);
42                                 include_once($patchdir . $fname);
43                                 if (isset($install)) // add installer if found
44                                         $upgrades[$install->version] =  $install;
45                         }
46                 }
47                 ksort($upgrades); // sort by file name
48                 $upgrades = array_values($upgrades);
49         }
50         return $upgrades;
51 }
52 //
53 //      Apply one differential data set.
54 //
55 function upgrade_step($index, $conn) 
56 {
57         global $path_to_root, $installers;
58
59         $inst = $installers[$index];
60         $pref = $conn['tbpref'];
61         $ret = true;
62
63         $force = get_post('force_'.$index);
64         if ($force || get_post('install_'.$index)) 
65         {
66                 $state = $inst->installed($pref);
67                 if (!$state || $force) 
68                 {
69                         if (!$inst->pre_check($pref, $force)) return false;
70                         $sql = $inst->sql;
71
72                         error_log(sprintf(_("Database upgrade for company '%s' (%s:%s*) started..."),
73                                 $conn['name'], $conn['dbname'], $conn['tbpref']));
74                                 
75                         if ($sql != '')
76                                 $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
77
78                         $ret &= $inst->install($pref, $force);
79
80                         error_log(_("Database upgrade finished."));
81
82                 } else
83                         if ($state!==true) {
84                                 display_error(_("Upgrade cannot be done because database has been already partially upgraded. Please downgrade database to clean previous version or try forced upgrade."));
85                                 $ret = false;
86                         }
87         }
88         return $ret;
89 }
90
91 $installers = get_installers();
92
93 if (get_post('Upgrade')) 
94 {
95
96         $ret = true;
97         foreach ($db_connections as $comp => $conn) 
98         {
99         // connect to database
100                 if (!(set_global_connection($comp))) 
101                 {
102                         display_error(_("Cannot connect to database for company")
103                                 ." '".$conn['name']."'");
104                         continue;
105                 }
106         // create security backup       
107                 db_backup($conn, 'no', 'Security backup before upgrade', $conn['tbpref']);
108         // apply all upgrade data
109                 foreach ($installers as $i => $inst) 
110                 {
111                         $ret = upgrade_step($i, $conn);
112                         if (!$ret)
113                                 display_error(
114                                 sprintf(_("Database upgrade to version %s failed for company '%s'."),
115                                         $inst->version, $conn['name'])
116                                         .'<br>'
117                                         ._('You should restore company database from latest backup file'));
118                 }
119 //              db_close($conn); ?
120                 if (!$ret) break;
121         }
122         set_global_connection();
123         if($ret)
124         {       // re-read the prefs
125                 global $path_to_root;
126                 include_once($path_to_root . "/admin/db/users_db.inc");
127                 $user = get_user_by_login($_SESSION["wa_current_user"]->username);
128                 $_SESSION["wa_current_user"]->prefs = new user_prefs($user);
129                 display_notification(_('All companies data has been successfully updated'));
130         }       
131         unset($_SESSION['SysPrefs']); // re-read system setup
132         $_SESSION['SysPrefs'] = new sys_prefs();
133         $Ajax->activate('_page_body');
134 }
135
136 start_form();
137 start_table(TABLESTYLE);
138 $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
139         _("Force upgrade"));
140 table_header($th);
141
142 $k = 0; //row colour counter
143 $partial = 0;
144 foreach($installers as $i => $inst)
145 {
146         alt_table_row_color($k);
147         start_row();
148         label_cell($inst->version);
149         label_cell($inst->description);
150         label_cell($inst->sql ? $inst->sql : '<i>'._('None').'</i>', 'align=center');
151 // this is checked only for first (site admin) company, 
152 // but in fact we should always upgrade all data sets after
153 // source upgrade.
154         $check = $inst->installed(TB_PREF);
155         if ($check === true)
156                 label_cell(_("Installed"));
157         else 
158                 if (!$check)
159                         check_cells(null,'install_'.$i, 0);
160                 else {
161                         label_cell("<span class=redfg>"
162                                 . sprintf(_("Partially installed (%s)"), $check) . "</span>");
163                         $partial++;
164                 }
165
166         check_cells(null,'force_'.$i, 0);
167         end_row();
168 }
169 end_table(1);
170 if ($partial!=0)        {
171         display_note(_("Database upgrades marked as partially installed cannot be installed automatically.
172 You have to clean database manually to enable them, or try to perform forced upgrade."));
173         br();
174 }
175 submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');
176 end_form();
177
178 end_page();
179
180 ?>