POD-Dokumentation aktualisiert
[kivitendo-erp.git] / SL / InstanceConfiguration.pm
1 package SL::InstanceConfiguration;
2
3 use strict;
4
5 use Carp;
6 use SL::DBUtils ();
7 use SL::System::Process;
8
9 use parent qw(Rose::Object);
10 use Rose::Object::MakeMethods::Generic (
11   'scalar --get_set_init' => [ qw(data currencies default_currency _table_currencies_exists crm_installed) ],
12 );
13
14 sub init_data {
15   return {} if !$::auth->client;
16   return SL::DBUtils::selectfirst_hashref_query($::form, $::form->get_standard_dbh, qq|SELECT * FROM defaults|);
17 }
18
19 sub init__table_currencies_exists {
20   return 0 if !$::auth->client;
21   return !!(SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, qq|SELECT tablename FROM pg_tables WHERE (schemaname = 'public') AND (tablename = 'currencies')|))[0];
22 }
23
24 sub init_currencies {
25   my ($self) = @_;
26
27   return [] if !$self->_table_currencies_exists;
28   return [ map { $_->{name} } SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, qq|SELECT name FROM currencies ORDER BY id ASC|) ];
29 }
30
31 sub init_default_currency {
32   my ($self) = @_;
33
34   return undef if !$self->_table_currencies_exists || !$self->data->{currency_id};
35   return (SL::DBUtils::selectfirst_array_query($::form, $::form->get_standard_dbh, qq|SELECT name FROM currencies WHERE id = ?|, $self->data->{currency_id}))[0];
36 }
37
38 sub init_crm_installed {
39   return -f (SL::System::Process->exe_dir . '/crm/Changelog');
40 }
41
42 sub reload {
43   my ($self) = @_;
44
45   delete @{ $self }{qw(data currencies default_currency)};
46
47   return $self;
48 }
49
50 sub get_currencies {
51   my ($self) = @_;
52   return @{ $self->currencies };
53 }
54
55 sub AUTOLOAD {
56   our $AUTOLOAD;
57
58   my $self   =  shift;
59   my $method =  $AUTOLOAD;
60   $method    =~ s/.*:://;
61
62   return if $method eq 'DESTROY';
63
64   if ($method =~ m/^get_/) {
65     $method = substr $method, 4;
66     return $self->data->{$method} if exists $self->data->{$method};
67     croak "Invalid method 'get_${method}'";
68   }
69
70   croak "Invalid method '${method}'" if !$self->can($method);
71   return $self->$method(@_);
72 }
73
74 1;
75
76 __END__
77
78 =pod
79
80 =encoding utf8
81
82 =head1 NAME
83
84 SL::InstanceConfiguration - Provide instance-specific configuration data
85
86 =head1 SYNOPSIS
87
88 kivitendo has two configuration levels: installation specific
89 (provided by the global variable C<%::lx_office_conf>) and instance
90 specific. The latter is provided by a global instance of this class,
91 C<$::instance_conf>.
92
93 =head1 FUNCTIONS
94
95 =over 4
96
97 =item C<get_currencies>
98
99 Returns an array of configured currencies.
100
101 =back
102
103 =head1 BUGS
104
105 Updates to the I<defaults> table require that the instance
106 configuration is re-read. This has not been implemented yet.
107
108 =head1 AUTHOR
109
110 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
111
112 =cut