test action
[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::DB::CustomVariableValidity;
11 use SL::DB::PartsGroup;
12 use SL::Helper::Flash;
13 use SL::Locale::String;
14 use Data::Dumper;
15
16 use Rose::Object::MakeMethods::Generic (
17   scalar                  => [ qw(config module module_description flags) ],
18   'scalar --get_set_init' => [ qw(translated_types modules) ],
19 );
20
21 __PACKAGE__->run_before('check_auth');
22 __PACKAGE__->run_before('check_module');
23 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
24
25 our %translations = (
26   text      => t8('Free-form text'),
27   textfield => t8('Text field'),
28   number    => t8('Number'),
29   date      => t8('Date'),
30   timestamp => t8('Timestamp'),
31   bool      => t8('Yes/No (Checkbox)'),
32   select    => t8('Selection'),
33   customer  => t8('Customer'),
34   vendor    => t8('Vendor'),
35   part      => t8('Part'),
36 );
37
38 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
39
40 #
41 # actions
42 #
43
44 sub action_list {
45   my ($self) = @_;
46
47   my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
48
49   $self->setup_list_action_bar;
50   $::form->header;
51   $self->render('custom_variable_config/list',
52                 title   => t8('List of custom variables'),
53                 CONFIGS => $configs);
54 }
55
56 sub action_new {
57   my ($self) = @_;
58
59   $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
60   $self->show_form(title => t8('Add custom variable'));
61 }
62
63 sub show_form {
64   my ($self, %params) = @_;
65
66   $self->flags({
67     map { split m/=/, $_, 2 }
68     split m/:/, ($self->config->flags || '')
69   });
70
71   $params{all_partsgroups} = SL::DB::Manager::PartsGroup->get_all();
72
73   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
74   $self->setup_form_action_bar;
75   $self->render('custom_variable_config/form', %params);
76 }
77
78 sub action_edit {
79   my ($self) = @_;
80
81   $self->show_form(title => t8('Edit custom variable'));
82 }
83
84 sub action_create {
85   my ($self) = @_;
86
87   $self->config(SL::DB::CustomVariableConfig->new);
88   $self->create_or_update;
89 }
90
91 sub action_update {
92   my ($self) = @_;
93   $self->create_or_update;
94 }
95
96 sub action_destroy {
97   my ($self) = @_;
98
99   # delete relationship to partsgroups (for filter) before cvar can be deleted
100   $self->config->update_attributes(partsgroups => []);
101
102   if (eval { $self->config->delete; 1; }) {
103     flash_later('info',  t8('The custom variable has been deleted.'));
104   } else {
105     flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
106   }
107
108   $self->redirect_to(action => 'list', module => $self->module);
109 }
110
111 sub action_reorder {
112   my ($self) = @_;
113
114   SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
115
116   $self->render(\'', { type => 'json' }); # ' make emacs happy
117 }
118
119 #
120 # filters
121 #
122
123 sub check_auth {
124   $::auth->assert('config');
125 }
126
127 sub check_module {
128   my ($self)          = @_;
129
130   $::form->{module} ||= 'CT';
131   my $mod_desc        = first { $_->{module} eq $::form->{module} } @{ $self->modules };
132   die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
133
134   $self->module($mod_desc->{module});
135   $self->module_description($mod_desc->{description});
136 }
137
138 sub load_config {
139   my ($self) = @_;
140
141   $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
142 }
143
144 #
145 # helpers
146 #
147
148 sub get_translation {
149   my ($self, $type) = @_;
150
151   return $translations{$type};
152 }
153
154 sub init_translated_types {
155   my ($self) = @_;
156
157   return [ map { { type => $_, translation => $translations{$_} } } @types ];
158 }
159
160 sub init_modules {
161   my ($self, %params) = @_;
162
163   return [ sort { $a->{description}->translated cmp $b->{description}->translated } (
164     { module => 'CT',               description => t8('Customers and vendors')          },
165     { module => 'Contacts',         description => t8('Contact persons')                },
166     { module => 'IC',               description => t8('Parts, services and assemblies') },
167     { module => 'Projects',         description => t8('Projects')                       },
168     { module => 'RequirementSpecs', description => t8('Requirement Specs')              },
169     { module => 'ShipTo',           description => t8('Shipping Address')               },
170   )];
171 }
172
173 sub create_or_update {
174   my ($self) = @_;
175   my $is_new = !$self->config->id;
176
177   my $params = delete($::form->{config}) || { };
178   delete $params->{id};
179
180   if ($self->module eq 'IC') {
181     $params->{partsgroups} = [] if !$params->{flag_partsgroup_filter};
182   } else {
183     delete $params->{flag_partsgroup_filter};
184     $params->{partsgroups} = [];
185   }
186
187   $params->{partsgroups}       ||= []; # The list is empty, if control is not send by the browser.
188   $params->{default_value}       = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
189   $params->{included_by_default} = 0                                                             if !$params->{includeable};
190   $params->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
191
192   $self->config->assign_attributes(%{ $params }, module => $self->module);
193
194   my @errors = $self->config->validate;
195
196   if (@errors) {
197     flash('error', @errors);
198     $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
199     return;
200   }
201
202   SL::DB->client->with_transaction(sub {
203     my $dbh = SL::DB->client->dbh;
204
205     $self->config->save;
206     $self->_set_cvar_validity() if $is_new;
207     1;
208   }) or do { die SL::DB->client->error };
209
210   flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
211   $self->redirect_to(action => 'list', module => $self->module);
212 }
213
214 sub _set_cvar_validity {
215   my ($self) = @_;
216
217   my $flags = {
218     map { split m/=/, $_, 2 }
219     split m/:/, ($self->config->flags || '')
220   };
221
222   # nothing to do to set valid
223   return if !$flags->{defaults_to_invalid};
224
225   my $all_parts  = SL::DB::Manager::Part->get_all(where => [ or => [ obsolete => 0, obsolete => undef ] ]);
226   foreach my $part (@{ $all_parts }) {
227     SL::DB::CustomVariableValidity->new(config_id => $self->config->id, trans_id => $part->id)->save;
228   }
229 }
230
231 sub setup_list_action_bar {
232   my ($self) = @_;
233
234   for my $bar ($::request->layout->get('actionbar')) {
235     $bar->add(
236       action => [
237         t8('Add'),
238         link => $self->url_for(action => 'new', module => $self->module),
239       ],
240     );
241   }
242 }
243
244 sub setup_form_action_bar {
245   my ($self) = @_;
246
247   my $is_new = !$self->config->id;
248
249   for my $bar ($::request->layout->get('actionbar')) {
250     $bar->add(
251       combobox => [
252         action => [
253           t8('Save'),
254           submit    => [ '#form', { action => 'CustomVariableConfig/' . ($is_new ? 'create' : 'update') } ],
255           checks    => [ 'check_prerequisites' ],
256           accesskey => 'enter',
257         ],
258
259         action => [
260           t8('Save as new'),
261           submit => [ '#form', { action => 'CustomVariableConfig/create'} ],
262           checks => [ 'check_prerequisites' ],
263           not_if => $is_new,
264         ],
265       ], # end of combobox "Save"
266
267       action => [
268         t8('Delete'),
269         submit   => [ '#form', { action => 'CustomVariableConfig/destroy' } ],
270         confirm  => t8('Do you really want to delete this object?'),
271         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
272       ],
273
274       'separator',
275
276       link => [
277         t8('Abort'),
278         link => $self->url_for(action => 'list', module => $self->module),
279       ],
280     );
281   }
282 }
283
284 1;