Added COA encoding display.
[fa-stable.git] / install / index.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_OPEN';
13 $path_to_root="..";
14
15 if (file_exists($path_to_root.'/config_db.php'))
16         header("Location: $path_to_root/index.php");
17
18 include($path_to_root . "/install/isession.inc");
19
20 page(_($help_context = "FrontAccouting ERP Installation Wizard"), true, false, "", '', false,
21         'stylesheet.css');
22
23 include($path_to_root . "/includes/ui.inc");
24 include($path_to_root . "/includes/system_tests.inc");
25 include($path_to_root . "/admin/db/maintenance_db.inc");
26 include($path_to_root . "/includes/packages.inc");
27 include($path_to_root . "/installed_extensions.php");
28 //-------------------------------------------------------------------------------------------------
29
30 function subpage_title($txt) 
31 {
32         global $path_to_root;
33         
34         echo '<center><img src="'.$path_to_root.'/themes/default/images/logo_frontaccounting.png" width="250" height="50" alt="Logo" />
35                 </center>';
36         $page = @$_POST['Page'] ? $_POST['Page'] : 1;
37
38         display_heading(
39                 $page==5 ? $txt :
40                         _("FrontAccouting ERP Installation Wizard").'<br>'
41                         . sprintf(_('Step %d: %s'),  $page , $txt));
42         br();
43 }
44
45 function display_coas()
46 {
47         start_table(TABLESTYLE);
48         $th = array(_("Chart of accounts"), _("Description"), _("Encoding"), _("Install"));
49         table_header($th);
50
51         $k = 0;
52         $charts = get_charts_list();
53
54         foreach($charts as $pkg_name => $coa)
55         {
56                 $available = @$coa['available'];
57                 $installed = @$coa['version'];
58                 $id = @$coa['local_id'];
59
60                 alt_table_row_color($k);
61                 label_cell($coa['name']);
62                 label_cell(is_array($coa['Descr']) ? implode('<br>', $coa['Descr']) :  $coa['Descr']);
63                 label_cell($coa['encoding']);
64                 if ($installed)
65                         label_cell(_("Installed"));
66                 else
67                         check_cells(null, 'coas['.$coa['package'].']');
68
69                 end_row();
70         }
71         end_table(1);
72 }
73
74 function install_connect_db() {
75         global $db;
76
77         $conn = $_SESSION['inst_set'];
78         
79         $db = mysql_connect($conn["host"] , $conn["dbuser"], $conn["dbpassword"]);
80         if(!$db) {
81                 display_error('Cannot connect to database server. Host name, username and/or password incorrect.');
82                 return false;
83         }
84         if (!defined('TB_PREF'))
85                 define('TB_PREF', $conn["tbpref"]);
86
87         if (!mysql_select_db($conn["dbname"], $db)) {
88                 $sql = "CREATE DATABASE " . $conn["dbname"];
89                 if (!mysql_query($sql)) {
90                         display_error('Cannot create database. Check your permissions to database creation or selct already created database.');
91                         return false;
92                 }
93                 return mysql_select_db($conn["dbname"], $db);
94         }
95         return true;
96 }
97
98 function do_install() {
99         global $path_to_root, $db_connections, $def_coy, $installed_extensions;
100
101         $coa = $_SESSION['inst_set']['coa'];
102         if (install_connect_db() && db_import($path_to_root.'/sql/'.$coa, $_SESSION['inst_set'])) {
103                 $con = $_SESSION['inst_set'];
104                 $table_prefix = $con['tbpref'];
105                 update_admin_password($con, md5($con['pass']));
106                 update_company_prefs(array('coy_name'=>$con['name']));
107
108                 $def_coy = 0;
109                 $tb_pref_counter = 0;
110                 $db_connections = array (0=> array (
111                  'name' => $con['name'],
112                  'host' => $con['host'],
113                  'dbuser' => $con['dbuser'],
114                  'dbpassword' => $con['dbpassword'],
115                  'dbname' => $con['dbname'],
116                  'tbpref' => $table_prefix
117                 ));
118                 $err = write_config_db($table_prefix != "");
119
120                 if ($err == -1) {
121                         display_error(_("Cannot open the config_db.php configuration file:"));
122                         return false;
123                 } else if ($err == -2) {
124                         display_error(_("Cannot write to the config_db.php configuration file"));
125                         return false;
126                 } else if ($err == -3) {
127                         display_error(_("The configuration file config_db.php is not writable. Change its permissions so it is, then re-run step 5."));
128                         return false;
129                 }
130                 if (!copy($path_to_root. "/config.default.php", $path_to_root. "/config.php")) {
131                         display_error(_("Cannot save system configuration file config.php"));
132                         return false;
133                 }
134                 if (count($installed_extensions))
135                  if (!update_extensions($installed_extensions)) { // update company 0 extensions (charts)
136                         display_error(_("Can't update extensions configuration."));
137                         return false;
138                  }
139                 return true;
140         }
141         return false;
142 }
143
144 if (!isset($_SESSION['inst_set']))  // default settings
145         $_SESSION['inst_set'] = array(
146                 'host'=>'localhost', 
147                 'dbuser' => 'root',
148                 'dbpassword' => '',
149                 'username' => 'admin',
150                 'tbpref' => '0_',
151                 'admin' => 'admin',
152                 'coa_type' => 0
153         );
154
155 if (!@$_POST['Tests'])
156         $_POST['Page'] = 1; // set to start page
157
158 if (isset($_POST['back']) && (@$_POST['Page']>1)) {
159         $_POST['Page']--;
160         if ($_POST['Page'] == 3)
161                 $_POST['Page'] = 2;
162 }
163 elseif (isset($_POST['continue'])) {
164         $_POST['Page'] = 2;
165 }
166 elseif (isset($_POST['db_test'])) {
167         if (get_post('host')=='') {
168                 display_error(_('Host name cannot be empty'));
169                 set_focus('host');
170         }
171         elseif ($_POST['dbuser']=='') {
172                 display_error(_('Database user name cannot be empty'));
173                 set_focus('dbuser');
174         }
175         elseif ($_POST['dbname']=='') {
176                 display_error(_('Database name cannot be empty'));
177                 set_focus('dbname');
178         }
179         else {
180                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
181                         'host' => $_POST['host'],
182                         'dbuser' => $_POST['dbuser'],
183                         'dbpassword' => $_POST['dbpassword'],
184                         'dbname' => $_POST['dbname'],
185                         'tbpref' => $_POST['tbpref'] ? '0_' : '',
186                 ));
187                 if (install_connect_db()) {
188                         $_POST['Page'] = check_value('sel_coas') ? 3 : 4;
189                 }
190         }
191 }
192 elseif(get_post('install_coas')) 
193 {
194         $ret = true;
195         if (isset($_POST['coas']))
196                 foreach($_POST['coas'] as $package => $ok) {
197                         $ret &= install_extension($package);
198                 }
199         if ($ret) {
200                 $_POST['Page'] = 4;
201         }
202 }
203 elseif (isset($_POST['set_admin'])) {
204         // check company settings
205         if (get_post('name')=='') {
206                 display_error(_('Company name cannot be empty.'));
207                 set_focus('name');
208         }
209         elseif (get_post('admin')=='') {
210                 display_error(_('Company admin name cannot be empty.'));
211                 set_focus('admin');
212         }
213         elseif (get_post('pass')=='') {
214                 display_error(_('Company admin password cannot be empty.'));
215                 set_focus('pass');
216         }
217         elseif (get_post('pass')!=get_post('repass')) {
218                 display_error(_('Company admin passwords differ.'));
219                 unset($_POST['pass'],$_POST['repass']);
220                 set_focus('pass');
221         }
222         else {
223
224                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
225                         'coa' => $_POST['coa'],
226                         'pass' => $_POST['pass'],
227                         'name' => $_POST['name'],
228                         'admin' => $_POST['admin'],
229                 ));
230                 if (do_install()) {
231                         $_POST['Page'] = 5;
232                 }
233         }
234 }
235
236 start_form();
237         switch(@$_POST['Page']) {
238                 default:
239 //                      include ('../install.html');
240 //                      submit_center('continue', _('Continue >>'));
241 //                      break;
242                 case '1':
243                         subpage_title(_('System Diagnostics'));
244                         $_POST['Tests'] = display_system_tests(true);
245                         br();
246                         if (@$_POST['Tests']) {
247                                 display_notification(_('All application preliminary requirements seems to be correct. Please press Continue button below.'));
248                                 submit_center('continue', _('Continue >>'));
249                         } else {
250                                 display_error(_('Application cannot be installed. Please fix problems listed below in red, and press Refresh button.'));
251                                 submit_center('refresh', _('Refresh'));
252                         }
253                         break;
254
255                 case '2':
256                         if (!isset($_POST['host'])) {
257                                 foreach($_SESSION['inst_set'] as $name => $val)
258                                         $_POST[$name] = $val;
259                         }
260                         subpage_title(_('Database Server Settings'));
261                         start_table(TABLESTYLE);
262                         text_row_ex(_("Server Host"), 'host', 30);
263                         text_row_ex(_("Database User"), 'dbuser', 30);
264                         text_row_ex(_("Database Password"), 'dbpassword', 30);
265                         text_row_ex(_("Database Name"), 'dbname', 30);
266                         yesno_list_row(_("Use '0_' Table Prefix"), 'tbpref', 1, _('Yes'), _('No'), false);
267                         check_row(_("Install additional COAs form FA repository"), 'sel_coas');
268                         end_table(1);
269                         display_note(_('Use table prefix if you share selected database with another application, or you want to use it for more than one FA company.'));
270                         submit_center_first('back', _('<< Back'));
271                         submit_center_last('db_test', _('Continue >>'));
272                         break;
273
274                 case '3': // select COA
275                         subpage_title(_('Charts of accounts selection'));
276                         display_coas();
277                         submit_center_first('back', _('<< Back'));
278                         submit_center_last('install_coas', _('Continue >>'));
279                         break;
280
281                 case '4':
282                         if (!isset($_POST['name'])) {
283                                 foreach($_SESSION['inst_set'] as $name => $val)
284                                         $_POST[$name] = $val;
285                                 set_focus('name');
286                         }
287                         subpage_title(_('Company Settings'));
288                         start_table(TABLESTYLE);
289                         text_row_ex(_("Company Name"), 'name', 30);
290                         text_row_ex(_("Admin Login"), 'admin', 30);
291                         password_row(_("Admin Password"), 'pass', @$_POST['pass']);
292                         password_row(_("Reenter Password"), 'repass', @$_POST['repass']);
293                         coa_list_row(_("Select Chart of Accounts"), 'coa');
294                         end_table(1);
295                         submit_center_first('back', _('<< Back'));
296                         submit_center_last('set_admin', _('Continue >>'));
297                         break;
298
299                 case '5': // final screen
300                         subpage_title(_('FrontAccounting ERP has been installed successsfully.'));
301                         display_note(_('Please remove install wizard folder.'));
302                         $install_done = true;
303                         hyperlink_no_params($path_to_root.'/index.php', _('Click here to start.'));
304                         break;
305
306         }
307
308         hidden('Tests');
309         hidden('Page');
310 end_form(1);
311
312 end_page(false, false, true);
313
314 ?>