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