Steuerzonen und Buchungsgruppen bearbeiten
[kivitendo-erp.git] / SL / Controller / CustomVariableConfig.pm
1 package SL::Controller::CustomVariableConfig;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::Util qw(first);
8
9 use SL::DB::CustomVariableConfig;
10 use SL::Helper::Flash;
11 use SL::Locale::String;
12 use Data::Dumper;
13
14 use Rose::Object::MakeMethods::Generic (
15   scalar                  => [ qw(config module module_description flags) ],
16   'scalar --get_set_init' => [ qw(translated_types modules) ],
17 );
18
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('check_module');
21 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
22
23 our %translations = (
24   text      => t8('Free-form text'),
25   textfield => t8('Text field'),
26   number    => t8('Number'),
27   date      => t8('Date'),
28   timestamp => t8('Timestamp'),
29   bool      => t8('Yes/No (Checkbox)'),
30   select    => t8('Selection'),
31   customer  => t8('Customer'),
32   vendor    => t8('Vendor'),
33   part      => t8('Part'),
34 );
35
36 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
37
38 #
39 # actions
40 #
41
42 sub action_list {
43   my ($self) = @_;
44
45   my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
46
47   $::form->header;
48   $self->render('custom_variable_config/list',
49                 title   => t8('List of custom variables'),
50                 CONFIGS => $configs);
51 }
52
53 sub action_new {
54   my ($self) = @_;
55
56   $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
57   $self->show_form(title => t8('Add custom variable'));
58 }
59
60 sub show_form {
61   my ($self, %params) = @_;
62
63   $self->flags({
64     map { split m/=/, $_, 2 }
65     split m/:/, ($self->config->flags || '')
66   });
67
68   $self->render('custom_variable_config/form', %params);
69 }
70
71 sub action_edit {
72   my ($self) = @_;
73
74   $self->show_form(title => t8('Edit custom variable'));
75 }
76
77 sub action_create {
78   my ($self) = @_;
79
80   $self->config(SL::DB::CustomVariableConfig->new);
81   $self->create_or_update;
82 }
83
84 sub action_update {
85   my ($self) = @_;
86   $self->create_or_update;
87 }
88
89 sub action_destroy {
90   my ($self) = @_;
91
92   if (eval { $self->config->delete; 1; }) {
93     flash_later('info',  t8('The custom variable has been deleted.'));
94   } else {
95     flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
96   }
97
98   $self->redirect_to(action => 'list');
99 }
100
101 sub action_reorder {
102   my ($self) = @_;
103
104   SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
105
106   $self->render(\'', { type => 'json' });
107 }
108
109 #
110 # filters
111 #
112
113 sub check_auth {
114   $::auth->assert('config');
115 }
116
117 sub check_module {
118   my ($self)          = @_;
119
120   $::form->{module} ||= 'CT';
121   my $mod_desc        = first { $_->{module} eq $::form->{module} } @{ $self->modules };
122   die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
123
124   $self->module($mod_desc->{module});
125   $self->module_description($mod_desc->{description});
126 }
127
128 sub load_config {
129   my ($self) = @_;
130
131   $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
132 }
133
134 #
135 # helpers
136 #
137
138 sub get_translation {
139   my ($self, $type) = @_;
140
141   return $translations{$type};
142 }
143
144 sub init_translated_types {
145   my ($self) = @_;
146
147   return [ map { { type => $_, translation => $translations{$_} } } @types ];
148 }
149
150 sub init_modules {
151   my ($self, %params) = @_;
152
153   return [
154     { module => 'CT',       description => t8('Customers and vendors')          },
155     { module => 'Contacts', description => t8('Contact persons')                },
156     { module => 'IC',       description => t8('Parts, services and assemblies') },
157     { module => 'Projects', description => t8('Projects')                       },
158   ];
159 }
160
161 sub create_or_update {
162   my ($self) = @_;
163   my $is_new = !$self->config->id;
164
165   my $params = delete($::form->{config}) || { };
166   delete $params->{id};
167
168   $params->{default_value}       = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
169   $params->{included_by_default} = 0                                                             if !$params->{includeable};
170   $params->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
171
172   $self->config->assign_attributes(%{ $params }, module => $self->module);
173
174   my @errors = $self->config->validate;
175
176   if (@errors) {
177     flash('error', @errors);
178     $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
179     return;
180   }
181
182   $self->config->save;
183
184   flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
185   $self->redirect_to(action => 'list', module => $self->module);
186 }
187
188 1;