Added languages selection.
[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 == 6 ? $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"), _("Encoding"), _("Description"), _("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($coa['encoding']);
63                 label_cell(is_array($coa['Descr']) ? implode('<br>', $coa['Descr']) :  $coa['Descr']);
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 display_langs()
75 {
76         start_table(TABLESTYLE);
77         $th = array(_("Language"), _("Encoding"), _("Description"), _("Install"));
78         table_header($th);
79
80         $k = 0;
81         $langs = get_languages_list();
82
83         foreach($langs as $pkg_name => $lang)
84         {
85                 $available = @$lang['available'];
86                 $installed = @$lang['version'];
87                 $id = @$lang['local_id'];
88                 if (!$available) continue;
89
90                 alt_table_row_color($k);
91                 label_cell($lang['name']);
92                 label_cell($lang['encoding']);
93                 label_cell(is_array($lang['Descr']) ? implode('<br>', $lang['Descr']) :  $lang['Descr']);
94                 if ($installed)
95                         label_cell(_("Installed"));
96                 else
97                         check_cells(null, 'langs['.$lang['package'].']');
98
99                 end_row();
100         }
101         end_table(1);
102 }
103
104 function install_connect_db() {
105
106         global $db;
107
108         $conn = $_SESSION['inst_set'];
109         
110         $db = mysql_connect($conn["host"] , $conn["dbuser"], $conn["dbpassword"]);
111         if(!$db) {
112                 display_error('Cannot connect to database server. Host name, username and/or password incorrect.');
113                 return false;
114         }
115         if (!defined('TB_PREF'))
116                 define('TB_PREF', $conn["tbpref"]);
117
118         if (!mysql_select_db($conn["dbname"], $db)) {
119                 $sql = "CREATE DATABASE " . $conn["dbname"];
120                 if (!mysql_query($sql)) {
121                         display_error('Cannot create database. Check your permissions to database creation or selct already created database.');
122                         return false;
123                 }
124                 return mysql_select_db($conn["dbname"], $db);
125         }
126         return true;
127 }
128
129 function do_install() {
130
131         global $path_to_root, $db_connections, $def_coy, $installed_extensions, 
132                 $dflt_lang, $installed_languages;
133
134         $coa = $_SESSION['inst_set']['coa'];
135         if (install_connect_db() && db_import($path_to_root.'/sql/'.$coa, $_SESSION['inst_set'])) {
136                 $con = $_SESSION['inst_set'];
137                 $table_prefix = $con['tbpref'];
138                 update_company_prefs(array('coy_name'=>$con['name']));
139                 $admin = get_user_by_login('admin');
140 //              update_admin_password($con, md5($con['pass']));
141                 update_user_prefs($admin['id'], array('language' => $_POST['lang'], 
142                         'password' => md5($con['pass'])));
143
144                 if (!copy($path_to_root. "/config.default.php", $path_to_root. "/config.php")) {
145                         display_error(_("Cannot save system configuration file config.php"));
146                         return false;
147                 }
148
149                 $def_coy = 0;
150                 $tb_pref_counter = 0;
151                 $db_connections = array (0=> array (
152                  'name' => $con['name'],
153                  'host' => $con['host'],
154                  'dbuser' => $con['dbuser'],
155                  'dbpassword' => $con['dbpassword'],
156                  'dbname' => $con['dbname'],
157                  'tbpref' => $table_prefix
158                 ));
159                 $err = write_config_db($table_prefix != "");
160
161                 if ($err == -1) {
162                         display_error(_("Cannot open the config_db.php configuration file:"));
163                         return false;
164                 } else if ($err == -2) {
165                         display_error(_("Cannot write to the config_db.php configuration file"));
166                         return false;
167                 } else if ($err == -3) {
168                         display_error(_("The configuration file config_db.php is not writable. Change its permissions so it is, then re-run step 5."));
169                         return false;
170                 }
171                 // update default language
172                 include_once($path_to_root . "/lang/installed_languages.inc");
173                 $dflt_lang = $_POST['lang'];
174                 write_lang();
175
176                 return true;
177         }
178         return false;
179 }
180
181 if (!isset($_SESSION['inst_set']))  // default settings
182         $_SESSION['inst_set'] = array(
183                 'host'=>'localhost', 
184                 'dbuser' => 'root',
185                 'dbpassword' => '',
186                 'username' => 'admin',
187                 'tbpref' => '0_',
188                 'admin' => 'admin',
189         );
190
191 if (!@$_POST['Tests'])
192         $_POST['Page'] = 1; // set to start page
193
194 if (isset($_POST['back']) && (@$_POST['Page']>1)) {
195         if ($_POST['Page'] == 5)
196                 $_POST['Page'] = 2;
197         else
198                 $_POST['Page']--;
199 }
200 elseif (isset($_POST['continue'])) {
201         $_POST['Page'] = 2;
202 }
203 elseif (isset($_POST['db_test'])) {
204         if (get_post('host')=='') {
205                 display_error(_('Host name cannot be empty'));
206                 set_focus('host');
207         }
208         elseif ($_POST['dbuser']=='') {
209                 display_error(_('Database user name cannot be empty'));
210                 set_focus('dbuser');
211         }
212         elseif ($_POST['dbname']=='') {
213                 display_error(_('Database name cannot be empty'));
214                 set_focus('dbname');
215         }
216         else {
217                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
218                         'host' => $_POST['host'],
219                         'dbuser' => $_POST['dbuser'],
220                         'dbpassword' => $_POST['dbpassword'],
221                         'dbname' => $_POST['dbname'],
222                         'tbpref' => $_POST['tbpref'] ? '0_' : '',
223                         'sel_langs' => check_value('sel_langs'),
224                         'sel_coas' => check_value('sel_coas'),
225                 ));
226                 if (install_connect_db()) {
227                         $_POST['Page'] = check_value('sel_langs') ? 3 :
228                                 (check_value('sel_coas') ? 4 : 5);
229                 }
230         }
231         if (!file_exists($path_to_root . "/lang/installed_languages.inc")) {
232                 $installed_languages = array (
233                         0 => array ('code' => 'C', 'name' => 'English', 'encoding' => 'iso-8859-1'));
234                         $dflt_lang = 'C';
235                         write_lang();
236         }
237 }
238 elseif(get_post('install_langs')) 
239 {
240         $ret = true;
241         if (isset($_POST['langs']))
242                 foreach($_POST['langs'] as $package => $ok) {
243                         $ret &= install_language($package);
244                 }
245         if ($ret) {
246                 $_POST['Page'] = $_SESSION['inst_set']['sel_coas'] ? 4 : 5;
247         }
248 }
249 elseif(get_post('install_coas')) 
250 {
251         $ret = true;
252
253         if (isset($_POST['coas']))
254                 foreach($_POST['coas'] as $package => $ok) {
255                         $ret &= install_extension($package);
256                 }
257         if ($ret) {
258                 include($path_to_root.'/installed_extensions.php');
259                 $_POST['Page'] = 5;
260         }
261 }
262 elseif (isset($_POST['set_admin'])) {
263         // check company settings
264         if (get_post('name')=='') {
265                 display_error(_('Company name cannot be empty.'));
266                 set_focus('name');
267         }
268         elseif (get_post('admin')=='') {
269                 display_error(_('Company admin name cannot be empty.'));
270                 set_focus('admin');
271         }
272         elseif (get_post('pass')=='') {
273                 display_error(_('Company admin password cannot be empty.'));
274                 set_focus('pass');
275         }
276         elseif (get_post('pass')!=get_post('repass')) {
277                 display_error(_('Company admin passwords differ.'));
278                 unset($_POST['pass'],$_POST['repass']);
279                 set_focus('pass');
280         }
281         else {
282
283                 $_SESSION['inst_set'] = array_merge($_SESSION['inst_set'], array(
284                         'coa' => $_POST['coa'],
285                         'pass' => $_POST['pass'],
286                         'name' => $_POST['name'],
287                         'admin' => $_POST['admin'],
288                 ));
289                 if (do_install()) {
290                         $_POST['Page'] = 6;
291                 }
292         }
293 }
294
295 start_form();
296         switch(@$_POST['Page']) {
297                 default:
298 //                      include ('../install.html');
299 //                      submit_center('continue', _('Continue >>'));
300 //                      break;
301                 case '1':
302                         subpage_title(_('System Diagnostics'));
303                         $_POST['Tests'] = display_system_tests(true);
304                         br();
305                         if (@$_POST['Tests']) {
306                                 display_notification(_('All application preliminary requirements seems to be correct. Please press Continue button below.'));
307                                 submit_center('continue', _('Continue >>'));
308                         } else {
309                                 display_error(_('Application cannot be installed. Please fix problems listed below in red, and press Refresh button.'));
310                                 submit_center('refresh', _('Refresh'));
311                         }
312                         break;
313
314                 case '2':
315                         if (!isset($_POST['host'])) {
316                                 foreach($_SESSION['inst_set'] as $name => $val)
317                                         $_POST[$name] = $val;
318                         }
319                         subpage_title(_('Database Server Settings'));
320                         start_table(TABLESTYLE);
321                         text_row_ex(_("Server Host"), 'host', 30);
322                         text_row_ex(_("Database User"), 'dbuser', 30);
323                         text_row_ex(_("Database Password"), 'dbpassword', 30);
324                         text_row_ex(_("Database Name"), 'dbname', 30);
325                         yesno_list_row(_("Use '0_' Table Prefix"), 'tbpref', 1, _('Yes'), _('No'), false);
326                         check_row(_("Install additional language packs from FA repository"), 'sel_langs');
327                         check_row(_("Install additional COAs from FA repository"), 'sel_coas');
328                         end_table(1);
329                         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.'));
330                         display_note(_("Do not select additional langs nor COAs if you have no internet connection right now. You can install them later."));
331                         submit_center_first('back', _('<< Back'));
332                         submit_center_last('db_test', _('Continue >>'));
333                         break;
334
335                 case '3': // select langauges
336                         subpage_title(_('Language packs selection'));
337                         display_langs();
338                         submit_center_first('back', _('<< Back'));
339                         submit_center_last('install_langs', _('Continue >>'));
340                         break;
341
342                 case '4': // select COA
343                         subpage_title(_('Charts of accounts selection'));
344                         display_coas();
345                         submit_center_first('back', _('<< Back'));
346                         submit_center_last('install_coas', _('Continue >>'));
347                         break;
348
349                 case '5':
350                         if (!isset($_POST['name'])) {
351                                 foreach($_SESSION['inst_set'] as $name => $val)
352                                         $_POST[$name] = $val;
353                                 set_focus('name');
354                         }
355                         subpage_title(_('Company Settings'));
356                         start_table(TABLESTYLE);
357                         text_row_ex(_("Company Name"), 'name', 30);
358                         text_row_ex(_("Admin Login"), 'admin', 30);
359                         password_row(_("Admin Password"), 'pass', @$_POST['pass']);
360                         password_row(_("Reenter Password"), 'repass', @$_POST['repass']);
361                         coa_list_row(_("Select Chart of Accounts"), 'coa');
362                         languages_list_row(_("Select default language"), 'lang');
363                         end_table(1);
364                         submit_center_first('back', _('<< Back'));
365                         submit_center_last('set_admin', _('Continue >>'));
366                         break;
367
368                 case '6': // final screen
369                         subpage_title(_('FrontAccounting ERP has been installed successsfully.'));
370                         display_note(_('Please remove install wizard folder.'));
371                         $install_done = true;
372                         hyperlink_no_params($path_to_root.'/index.php', _('Click here to start.'));
373                         break;
374
375         }
376
377         hidden('Tests');
378         hidden('Page');
379 end_form(1);
380
381 end_page(false, false, true);
382
383 ?>