SimpleSystemSetting: Umstellung von »Warengruppen«
[kivitendo-erp.git] / SL / Controller / SimpleSystemSetting.pm
1 package SL::Controller::SimpleSystemSetting;
2
3 use strict;
4 use utf8;
5
6 use parent qw(SL::Controller::Base);
7
8 use SL::Helper::Flash;
9 use SL::Locale::String;
10 use SL::DB::Default;
11 use SL::System::Process;
12
13 use Rose::Object::MakeMethods::Generic (
14   scalar                  => [ qw(type config) ],
15   'scalar --get_set_init' => [ qw(defaults object all_objects class manager_class list_attributes list_url supports_reordering) ],
16 );
17
18 __PACKAGE__->run_before('check_type_and_auth');
19 __PACKAGE__->run_before('setup_javascript', only => [ qw(add create edit update delete) ]);
20
21 # Make locales.pl happy: $self->render("simple_system_setting/_default_form")
22
23 my %supported_types = (
24   bank_account => {
25     # Make locales.pl happy: $self->render("simple_system_setting/_bank_account_form")
26     class  => 'BankAccount',
27     titles => {
28       list => t8('Bank accounts'),
29       add  => t8('Add bank account'),
30       edit => t8('Edit bank account'),
31     },
32     list_attributes => [
33       { method => 'name',                                      title => t8('Name'), },
34       { method => 'iban',                                      title => t8('IBAN'), },
35       { method => 'bank',                                      title => t8('Bank'), },
36       { method => 'bank_code',                                 title => t8('Bank code'), },
37       { method => 'bic',                                       title => t8('BIC'), },
38       { method => 'reconciliation_starting_date_as_date',      title => t8('Date'),    align => 'right' },
39       { method => 'reconciliation_starting_balance_as_number', title => t8('Balance'), align => 'right' },
40     ],
41   },
42
43   parts_group => {
44     # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
45     class  => 'PartsGroup',
46     titles => {
47       list => t8('Partsgroups'),
48       add  => t8('Add partsgroup'),
49       edit => t8('Edit partsgroup'),
50     },
51     list_attributes => [
52       { method => 'partsgroup', title => t8('Description') },
53       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
54     ],
55   },
56
57   pricegroup => {
58     # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
59     class  => 'Pricegroup',
60     titles => {
61       list => t8('Pricegroups'),
62       add  => t8('Add pricegroup'),
63       edit => t8('Edit pricegroup'),
64     },
65     list_attributes => [
66       { method => 'pricegroup', title => t8('Description') },
67       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
68     ],
69   },
70 );
71
72 my @default_list_attributes = (
73   { method => 'description', title => t8('Description') },
74 );
75
76 #
77 # actions
78 #
79
80 sub action_list {
81   my ($self) = @_;
82
83   $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
84 }
85
86 sub action_new {
87   my ($self) = @_;
88
89   $self->object($self->class->new);
90   $self->render_form(title => $self->config->{titles}->{add});
91 }
92
93 sub action_edit {
94   my ($self) = @_;
95
96   $self->render_form(title => $self->config->{titles}->{edit});
97 }
98
99 sub action_create {
100   my ($self) = @_;
101
102   $self->object($self->class->new);
103   $self->create_or_update;
104 }
105
106 sub action_update {
107   my ($self) = @_;
108
109   $self->create_or_update;
110 }
111
112 sub action_delete {
113   my ($self) = @_;
114
115   if ($self->object->can('orphaned') && !$self->object->orphaned) {
116     flash_later('error', t8('The object is in use and cannot be deleted.'));
117
118   } elsif ( eval { $self->object->delete; 1; } ) {
119     flash_later('info',  t8('The object has been deleted.'));
120
121   } else {
122     flash_later('error', t8('The object is in use and cannot be deleted.'));
123   }
124
125   $self->redirect_to($self->list_url);
126 }
127
128 sub action_reorder {
129   my ($self) = @_;
130
131   $self->class->reorder_list(@{ $::form->{object_id} || [] });
132   $self->render(\'', { type => 'json' });
133 }
134
135 #
136 # filters
137 #
138
139 sub check_type_and_auth {
140   my ($self) = @_;
141
142   $self->type($::form->{type});
143   $self->config($supported_types{$self->type}) || die "Unsupported type";
144
145   $::auth->assert($self->config->{auth} || 'config');
146
147   my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
148   require $pm;
149
150   my $setup = "setup_" . $self->type;
151   $self->$setup if $self->can($setup);
152
153   1;
154 }
155
156 sub setup_javascript {
157   $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
158 }
159
160 sub init_class               { "SL::DB::"          . $_[0]->config->{class}                  }
161 sub init_manager_class       { "SL::DB::Manager::" . $_[0]->config->{class}                  }
162 sub init_object              { $_[0]->class->new(id => $::form->{id})->load                  }
163 sub init_all_objects         { $_[0]->manager_class->get_all_sorted                          }
164 sub init_list_url            { $_[0]->url_for(action => 'list', type => $_[0]->type)         }
165 sub init_supports_reordering { $_[0]->class->new->can('reorder_list')                        }
166 sub init_defaults            { SL::DB::Default->get                                          }
167
168 sub init_list_attributes {
169   my ($self) = @_;
170
171   my $method = "list_attributes_" . $self->type;
172
173   return $self->$method if $self->can($method);
174   return $self->config->{list_attributes} // \@default_list_attributes;
175 }
176
177 #
178 # helpers
179 #
180
181 sub create_or_update {
182   my ($self) = @_;
183   my $is_new = !$self->object->id;
184
185   my $params = delete($::form->{object}) || { };
186
187   $self->object->assign_attributes(%{ $params });
188
189   my @errors;
190
191   push @errors, $self->object->validate if $self->object->can('validate');
192
193   if (@errors) {
194     flash('error', @errors);
195     return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
196   }
197
198   $self->object->save;
199
200   flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
201
202   $self->redirect_to($self->list_url);
203 }
204
205 sub render_form {
206   my ($self, %params) = @_;
207
208   my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
209
210   $self->render(
211     'simple_system_setting/form',
212     %params,
213     sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
214   );
215 }
216
217 #
218 # type-specific helper functions
219 #
220
221 1;
222
223 __END__
224
225 =encoding utf-8
226
227 =head1 NAME
228
229 SL::Controller::SimpleSystemSettings — a common CRUD controller for
230 various settings in the "System" menu
231
232 =head1 AUTHOR
233
234 Moritz Bunkus <m.bunkus@linet-services.de>
235
236 =cut