SimpleSystemSetting: Controller für die ganzen trivialen CRUD-Masken im System-Menü
[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   pricegroup => {
25     # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
26     class  => 'Pricegroup',
27     titles => {
28       list => t8('Pricegroups'),
29       add  => t8('Add pricegroup'),
30       edit => t8('Edit pricegroup'),
31     },
32     list_attributes => [
33       { method => 'pricegroup', title => t8('Description') },
34       { method => 'obsolete',   title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
35     ],
36   },
37 );
38
39 my @default_list_attributes = (
40   { method => 'description', title => t8('Description') },
41 );
42
43 #
44 # actions
45 #
46
47 sub action_list {
48   my ($self) = @_;
49
50   $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
51 }
52
53 sub action_new {
54   my ($self) = @_;
55
56   $self->object($self->class->new);
57   $self->render_form(title => $self->config->{titles}->{add});
58 }
59
60 sub action_edit {
61   my ($self) = @_;
62
63   $self->render_form(title => $self->config->{titles}->{edit});
64 }
65
66 sub action_create {
67   my ($self) = @_;
68
69   $self->object($self->class->new);
70   $self->create_or_update;
71 }
72
73 sub action_update {
74   my ($self) = @_;
75
76   $self->create_or_update;
77 }
78
79 sub action_delete {
80   my ($self) = @_;
81
82   if ($self->object->can('orphaned') && !$self->object->orphaned) {
83     flash_later('error', t8('The object is in use and cannot be deleted.'));
84
85   } elsif ( eval { $self->object->delete; 1; } ) {
86     flash_later('info',  t8('The object has been deleted.'));
87
88   } else {
89     flash_later('error', t8('The object is in use and cannot be deleted.'));
90   }
91
92   $self->redirect_to($self->list_url);
93 }
94
95 sub action_reorder {
96   my ($self) = @_;
97
98   $self->class->reorder_list(@{ $::form->{object_id} || [] });
99   $self->render(\'', { type => 'json' });
100 }
101
102 #
103 # filters
104 #
105
106 sub check_type_and_auth {
107   my ($self) = @_;
108
109   $self->type($::form->{type});
110   $self->config($supported_types{$self->type}) || die "Unsupported type";
111
112   $::auth->assert($self->config->{auth} || 'config');
113
114   my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
115   require $pm;
116
117   my $setup = "setup_" . $self->type;
118   $self->$setup if $self->can($setup);
119
120   1;
121 }
122
123 sub setup_javascript {
124   $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
125 }
126
127 sub init_class               { "SL::DB::"          . $_[0]->config->{class}                  }
128 sub init_manager_class       { "SL::DB::Manager::" . $_[0]->config->{class}                  }
129 sub init_object              { $_[0]->class->new(id => $::form->{id})->load                  }
130 sub init_all_objects         { $_[0]->manager_class->get_all_sorted                          }
131 sub init_list_url            { $_[0]->url_for(action => 'list', type => $_[0]->type)         }
132 sub init_supports_reordering { $_[0]->class->new->can('reorder_list')                        }
133 sub init_defaults            { SL::DB::Default->get                                          }
134
135 sub init_list_attributes {
136   my ($self) = @_;
137
138   my $method = "list_attributes_" . $self->type;
139
140   return $self->$method if $self->can($method);
141   return $self->config->{list_attributes} // \@default_list_attributes;
142 }
143
144 #
145 # helpers
146 #
147
148 sub create_or_update {
149   my ($self) = @_;
150   my $is_new = !$self->object->id;
151
152   my $params = delete($::form->{object}) || { };
153
154   $self->object->assign_attributes(%{ $params });
155
156   my @errors;
157
158   push @errors, $self->object->validate if $self->object->can('validate');
159
160   if (@errors) {
161     flash('error', @errors);
162     return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
163   }
164
165   $self->object->save;
166
167   flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
168
169   $self->redirect_to($self->list_url);
170 }
171
172 sub render_form {
173   my ($self, %params) = @_;
174
175   my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
176
177   $self->render(
178     'simple_system_setting/form',
179     %params,
180     sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
181   );
182 }
183
184 #
185 # type-specific helper functions
186 #
187
188 1;
189
190 __END__
191
192 =encoding utf-8
193
194 =head1 NAME
195
196 SL::Controller::SimpleSystemSettings — a common CRUD controller for
197 various settings in the "System" menu
198
199 =head1 AUTHOR
200
201 Moritz Bunkus <m.bunkus@linet-services.de>
202
203 =cut