Zusammenfassung einzelner UPDATE-Queries auf defaults zu einem einzigen
[kivitendo-erp.git] / bin / mozilla / admin.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # setup module
31 # add/edit/delete users
32 #
33 #======================================================================
34
35 use DBI;
36 use Encode;
37 use English qw(-no_match_vars);
38 use Fcntl;
39 use File::Copy;
40 use File::Find;
41 use File::Spec;
42 use Cwd;
43 use IO::Dir;
44 use IO::File;
45 use POSIX qw(strftime);
46 use Sys::Hostname;
47
48 use SL::Auth;
49 use SL::Auth::PasswordPolicy;
50 use SL::DB::AuthClient;
51 use SL::DB::AuthUser;
52 use SL::Form;
53 use SL::Iconv;
54 use SL::Mailer;
55 use SL::User;
56 use SL::Common;
57 use SL::Inifile;
58 use SL::DBUpgrade2;
59 use SL::DBUtils;
60 use SL::Template;
61
62 require "bin/mozilla/common.pl";
63
64 use strict;
65
66 # parserhappy(R):
67
68 #  $locale->text('periodic')
69 #  $locale->text('income')
70 #  $locale->text('perpetual')
71 #  $locale->text('balance')
72
73 our $cgi;
74 our $form;
75 our $locale;
76 our $auth;
77
78 sub run {
79   $::lxdebug->enter_sub;
80   my $session_result = shift;
81
82   $form   = $::form;
83   $locale = $::locale;
84   $auth   = $::auth;
85
86   $::request->{layout} = SL::Layout::Dispatcher->new(style => 'admin');
87   $::request->{layout}->use_stylesheet("lx-office-erp.css");
88   $form->{favicon}    = "favicon.ico";
89
90   if ($form->{action}) {
91     if ($auth->authenticate_root($form->{'{AUTH}admin_password'}) != $auth->OK()) {
92       $auth->punish_wrong_login;
93       $form->{error} = $locale->text('Incorrect password!');
94       $auth->delete_session_value('admin_password');
95       adminlogin();
96     } else {
97       if ($auth->session_tables_present()) {
98         delete $::form->{'{AUTH}admin_password'};
99       }
100
101       call_sub($locale->findsub($form->{action}));
102     }
103   } else {
104     adminlogin();
105   }
106   $::lxdebug->leave_sub;
107 }
108
109 sub adminlogin {
110   print $::request->cgi->redirect('controller.pl?action=Admin/login');
111 }
112
113 sub pg_database_administration {
114   my $form = $main::form;
115   dbselect_source();
116 }
117
118 sub dbselect_source {
119   my $form           = $main::form;
120   my $locale         = $main::locale;
121
122   $form->{dbport}    = $::auth->{DB_config}->{port} || 5432;
123   $form->{dbuser}    = $::auth->{DB_config}->{user} || 'lxoffice';
124   $form->{dbdefault} = 'template1';
125   $form->{dbhost}    = $::auth->{DB_config}->{host} || 'localhost';
126
127   $form->{title}     = "kivitendo / " . $locale->text('Database Administration');
128
129   $form->header();
130   print $form->parse_html_template("admin/dbadmin");
131 }
132
133 sub continue {
134   call_sub($main::form->{"nextsub"});
135 }
136
137
138 sub create_dataset {
139   my $form           = $main::form;
140   my $locale         = $main::locale;
141
142   $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
143
144   $form->{CHARTS}    = [];
145
146   tie my %dir_h, 'IO::Dir', 'sql/';
147   foreach my $item (map { s/-chart\.sql$//; $_ } sort grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ } keys %dir_h) {
148     push @{ $form->{CHARTS} }, { name     => $item,
149                                  selected => $item eq "Germany-DATEV-SKR03EU" };
150   }
151
152   $form->{ACCOUNTING_METHODS}    = [ map { { name => $_, selected => $_ eq 'cash'     } } qw(accrual cash)       ];
153   $form->{INVENTORY_SYSTEMS}     = [ map { { name => $_, selected => $_ eq 'periodic' } } qw(perpetual periodic) ];
154   $form->{PROFIT_DETERMINATIONS} = [ map { { name => $_, selected => $_ eq 'income'   } } qw(balance income)     ];
155
156   my $default_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
157
158   my $cluster_encoding = User->dbclusterencoding($form);
159   if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
160     if ($::lx_office_conf{system}->{dbcharset} !~ m/^UTF-?8$/i) {
161       $form->show_generic_error($locale->text('The selected  PostgreSQL installation uses UTF-8 as its encoding. ' .
162                                               'Therefore you have to configure kivitendo to use UTF-8 as well.'),
163                                 'back_button' => 1);
164     }
165
166     $form->{FORCE_DBENCODING} = 'UNICODE';
167
168   } else {
169     $form->{DBENCODINGS} = [ map { { %{$_}, selected => $_->{charset} eq $default_charset } } @Common::db_encodings ];
170   }
171
172   $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
173
174   $form->header();
175   print $form->parse_html_template("admin/create_dataset");
176 }
177
178 sub dbcreate {
179   my $form   = $main::form;
180   my $locale = $main::locale;
181
182   $form->isblank("db", $locale->text('Dataset missing!'));
183   $form->isblank("defaultcurrency", $locale->text('Default currency missing!'));
184
185   User->dbcreate(\%$form);
186
187   $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
188
189   $form->header();
190   print $form->parse_html_template("admin/dbcreate");
191 }
192
193 sub delete_dataset {
194   my $form      = $main::form;
195   my $locale    = $main::locale;
196
197   my @dbsources = User->dbsources_unused($form);
198   $form->error($locale->text('Nothing to delete!')) unless @dbsources;
199
200   $form->{title}     = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
201   $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
202
203   $form->header();
204   print $form->parse_html_template("admin/delete_dataset");
205 }
206
207 sub dbdelete {
208   my $form   = $main::form;
209   my $locale = $main::locale;
210
211   if (!$form->{db}) {
212     $form->error($locale->text('No Dataset selected!'));
213   }
214
215   User->dbdelete(\%$form);
216
217   $form->{title} = "kivitendo " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
218   $form->header();
219   print $form->parse_html_template("admin/dbdelete");
220 }
221
222 1;