de0feb073d7b8b3694cc90e49822fc485285082a
[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   business => {
44     # Make locales.pl happy: $self->render("simple_system_setting/_business_form")
45     class  => 'Business',
46     titles => {
47       list => t8('Businesses'),
48       add  => t8('Add business'),
49       edit => t8('Edit business'),
50     },
51     list_attributes => [
52       { method => 'description',         title => t8('Description'), },
53       {                                  title => t8('Discount'), formatter => sub { $_[0]->discount_as_percent . ' %' }, align => 'right' },
54       { method => 'customernumberinit',  title => t8('Customernumberinit'), },
55     ],
56   },
57
58   department => {
59     class  => 'Department',
60     titles => {
61       list => t8('Departments'),
62       add  => t8('Add department'),
63       edit => t8('Edit department'),
64     },
65   },
66
67   part_classification => {
68     # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
69     class  => 'PartClassification',
70     titles => {
71       list => t8('Part classifications'),
72       add  => t8('Add part classification'),
73       edit => t8('Edit part classification'),
74     },
75     list_attributes => [
76       { title => t8('Description'),       formatter => sub { t8($_[0]->description) } },
77       { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
78       { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
79       { title => t8('Used for Sale'),     formatter => sub { $_[0]->used_for_sale     ? t8('yes') : t8('no') } },
80       { title => t8('Report separately'), formatter => sub { $_[0]->report_separate   ? t8('yes') : t8('no') } },
81     ],
82   },
83
84   parts_group => {
85     # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
86     class  => 'PartsGroup',
87     titles => {
88       list => t8('Partsgroups'),
89       add  => t8('Add partsgroup'),
90       edit => t8('Edit partsgroup'),
91     },
92     list_attributes => [
93       { method => 'partsgroup', title => t8('Description') },
94       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
95     ],
96   },
97
98   price_factor => {
99     # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
100     class  => 'PriceFactor',
101     titles => {
102       list => t8('Price Factors'),
103       add  => t8('Add Price Factor'),
104       edit => t8('Edit Price Factor'),
105     },
106     list_attributes => [
107       { method => 'description',      title => t8('Description') },
108       { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
109     ],
110   },
111
112   pricegroup => {
113     # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
114     class  => 'Pricegroup',
115     titles => {
116       list => t8('Pricegroups'),
117       add  => t8('Add pricegroup'),
118       edit => t8('Edit pricegroup'),
119     },
120     list_attributes => [
121       { method => 'pricegroup', title => t8('Description') },
122       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
123     ],
124   },
125
126   project_status => {
127     class  => 'ProjectStatus',
128     titles => {
129       list => t8('Project statuses'),
130       add  => t8('Add project status'),
131       edit => t8('Edit project status'),
132     },
133   },
134
135   project_type => {
136     class  => 'ProjectType',
137     titles => {
138       list => t8('Project types'),
139       add  => t8('Add project type'),
140       edit => t8('Edit project type'),
141     },
142   },
143
144   requirement_spec_acceptance_status => {
145     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_acceptance_status_form")
146     class  => 'RequirementSpecAcceptanceStatus',
147     titles => {
148       list => t8('Acceptance Statuses'),
149       add  => t8('Add acceptance status'),
150       edit => t8('Edit acceptance status'),
151     },
152     list_attributes => [
153       { method => 'name',        title => t8('Name') },
154       { method => 'description', title => t8('Description') },
155     ],
156   },
157
158   requirement_spec_complexity => {
159     class  => 'RequirementSpecComplexity',
160     titles => {
161       list => t8('Complexities'),
162       add  => t8('Add complexity'),
163       edit => t8('Edit complexity'),
164     },
165   },
166
167   requirement_spec_predefined_text => {
168     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_predefined_text_form")
169     class  => 'RequirementSpecPredefinedText',
170     titles => {
171       list => t8('Pre-defined Texts'),
172       add  => t8('Add pre-defined text'),
173       edit => t8('Edit pre-defined text'),
174     },
175     list_attributes => [
176       { method => 'description', title => t8('Description') },
177       { method => 'title',       title => t8('Title') },
178       {                          title => t8('Content'),                 formatter => sub { my $t = $_[0]->text_as_stripped_html; length($t) > 50 ? substr($t, 0, 50) . '…' : $t } },
179       {                          title => t8('Useable for text blocks'), formatter => sub { $_[0]->useable_for_text_blocks ? t8('yes') : t8('no') } },
180       {                          title => t8('Useable for sections'),    formatter => sub { $_[0]->useable_for_sections    ? t8('yes') : t8('no') } },
181     ],
182   },
183
184   requirement_spec_risk => {
185     class  => 'RequirementSpecRisk',
186     titles => {
187       list => t8('Risk levels'),
188       add  => t8('Add risk level'),
189       edit => t8('Edit risk level'),
190     },
191   },
192
193   requirement_spec_status => {
194     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_status_form")
195     class  => 'RequirementSpecStatus',
196     titles => {
197       list => t8('Requirement Spec Statuses'),
198       add  => t8('Add requirement spec status'),
199       edit => t8('Edit requirement spec status'),
200     },
201     list_attributes => [
202       { method => 'name',        title => t8('Name') },
203       { method => 'description', title => t8('Description') },
204     ],
205   },
206
207   requirement_spec_type => {
208     # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_type_form")
209     class  => 'RequirementSpecType',
210     titles => {
211       list => t8('Requirement Spec Types'),
212       add  => t8('Add requirement spec type'),
213       edit => t8('Edit requirement spec type'),
214     },
215     list_attributes => [
216       { method => 'description',                  title => t8('Description') },
217       { method => 'section_number_format',        title => t8('Section number format') },
218       { method => 'function_block_number_format', title => t8('Function block number format') },
219     ],
220   },
221
222 );
223
224 my @default_list_attributes = (
225   { method => 'description', title => t8('Description') },
226 );
227
228 #
229 # actions
230 #
231
232 sub action_list {
233   my ($self) = @_;
234
235   $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
236 }
237
238 sub action_new {
239   my ($self) = @_;
240
241   $self->object($self->class->new);
242   $self->render_form(title => $self->config->{titles}->{add});
243 }
244
245 sub action_edit {
246   my ($self) = @_;
247
248   $self->render_form(title => $self->config->{titles}->{edit});
249 }
250
251 sub action_create {
252   my ($self) = @_;
253
254   $self->object($self->class->new);
255   $self->create_or_update;
256 }
257
258 sub action_update {
259   my ($self) = @_;
260
261   $self->create_or_update;
262 }
263
264 sub action_delete {
265   my ($self) = @_;
266
267   if ($self->object->can('orphaned') && !$self->object->orphaned) {
268     flash_later('error', t8('The object is in use and cannot be deleted.'));
269
270   } elsif ( eval { $self->object->delete; 1; } ) {
271     flash_later('info',  t8('The object has been deleted.'));
272
273   } else {
274     flash_later('error', t8('The object is in use and cannot be deleted.'));
275   }
276
277   $self->redirect_to($self->list_url);
278 }
279
280 sub action_reorder {
281   my ($self) = @_;
282
283   $self->class->reorder_list(@{ $::form->{object_id} || [] });
284   $self->render(\'', { type => 'json' });
285 }
286
287 #
288 # filters
289 #
290
291 sub check_type_and_auth {
292   my ($self) = @_;
293
294   $self->type($::form->{type});
295   $self->config($supported_types{$self->type}) || die "Unsupported type";
296
297   $::auth->assert($self->config->{auth} || 'config');
298
299   my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
300   require $pm;
301
302   my $setup = "setup_" . $self->type;
303   $self->$setup if $self->can($setup);
304
305   1;
306 }
307
308 sub setup_javascript {
309   $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
310 }
311
312 sub init_class               { "SL::DB::"          . $_[0]->config->{class}                  }
313 sub init_manager_class       { "SL::DB::Manager::" . $_[0]->config->{class}                  }
314 sub init_object              { $_[0]->class->new(id => $::form->{id})->load                  }
315 sub init_all_objects         { $_[0]->manager_class->get_all_sorted                          }
316 sub init_list_url            { $_[0]->url_for(action => 'list', type => $_[0]->type)         }
317 sub init_supports_reordering { $_[0]->class->new->can('reorder_list')                        }
318 sub init_defaults            { SL::DB::Default->get                                          }
319
320 sub init_list_attributes {
321   my ($self) = @_;
322
323   my $method = "list_attributes_" . $self->type;
324
325   return $self->$method if $self->can($method);
326   return $self->config->{list_attributes} // \@default_list_attributes;
327 }
328
329 #
330 # helpers
331 #
332
333 sub create_or_update {
334   my ($self) = @_;
335   my $is_new = !$self->object->id;
336
337   my $params = delete($::form->{object}) || { };
338
339   $self->object->assign_attributes(%{ $params });
340
341   my @errors;
342
343   push @errors, $self->object->validate if $self->object->can('validate');
344
345   if (@errors) {
346     flash('error', @errors);
347     return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
348   }
349
350   $self->object->save;
351
352   flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
353
354   $self->redirect_to($self->list_url);
355 }
356
357 sub render_form {
358   my ($self, %params) = @_;
359
360   my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
361
362   $self->render(
363     'simple_system_setting/form',
364     %params,
365     sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
366   );
367 }
368
369 #
370 # type-specific helper functions
371 #
372
373 sub setup_requirement_spec_acceptance_status {
374   my ($self) = @_;
375
376   no warnings 'once';
377   $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
378 }
379
380 sub setup_requirement_spec_status {
381   my ($self) = @_;
382
383   no warnings 'once';
384   $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
385 }
386
387 1;
388
389 __END__
390
391 =encoding utf-8
392
393 =head1 NAME
394
395 SL::Controller::SimpleSystemSettings — a common CRUD controller for
396 various settings in the "System" menu
397
398 =head1 AUTHOR
399
400 Moritz Bunkus <m.bunkus@linet-services.de>
401
402 =cut