1 package SL::Controller::SimpleSystemSetting;
6 use parent qw(SL::Controller::Base);
9 use SL::Locale::String;
11 use SL::System::Process;
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) ],
18 __PACKAGE__->run_before('check_type_and_auth');
19 __PACKAGE__->run_before('setup_javascript', only => [ qw(add create edit update delete) ]);
21 # Make locales.pl happy: $self->render("simple_system_setting/_default_form")
23 my %supported_types = (
25 # Make locales.pl happy: $self->render("simple_system_setting/_bank_account_form")
26 class => 'BankAccount',
28 list => t8('Bank accounts'),
29 add => t8('Add bank account'),
30 edit => t8('Edit bank account'),
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' },
44 class => 'Department',
46 list => t8('Departments'),
47 add => t8('Add department'),
48 edit => t8('Edit department'),
52 part_classification => {
53 # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
54 class => 'PartClassification',
56 list => t8('Part classifications'),
57 add => t8('Add part classification'),
58 edit => t8('Edit part classification'),
61 { title => t8('Description'), formatter => sub { t8($_[0]->description) } },
62 { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
63 { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
64 { title => t8('Used for Sale'), formatter => sub { $_[0]->used_for_sale ? t8('yes') : t8('no') } },
65 { title => t8('Report separately'), formatter => sub { $_[0]->report_separate ? t8('yes') : t8('no') } },
70 # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
71 class => 'PartsGroup',
73 list => t8('Partsgroups'),
74 add => t8('Add partsgroup'),
75 edit => t8('Edit partsgroup'),
78 { method => 'partsgroup', title => t8('Description') },
79 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
84 # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
85 class => 'PriceFactor',
87 list => t8('Price Factors'),
88 add => t8('Add Price Factor'),
89 edit => t8('Edit Price Factor'),
92 { method => 'description', title => t8('Description') },
93 { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
98 # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
99 class => 'Pricegroup',
101 list => t8('Pricegroups'),
102 add => t8('Add pricegroup'),
103 edit => t8('Edit pricegroup'),
106 { method => 'pricegroup', title => t8('Description') },
107 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
112 my @default_list_attributes = (
113 { method => 'description', title => t8('Description') },
123 $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
129 $self->object($self->class->new);
130 $self->render_form(title => $self->config->{titles}->{add});
136 $self->render_form(title => $self->config->{titles}->{edit});
142 $self->object($self->class->new);
143 $self->create_or_update;
149 $self->create_or_update;
155 if ($self->object->can('orphaned') && !$self->object->orphaned) {
156 flash_later('error', t8('The object is in use and cannot be deleted.'));
158 } elsif ( eval { $self->object->delete; 1; } ) {
159 flash_later('info', t8('The object has been deleted.'));
162 flash_later('error', t8('The object is in use and cannot be deleted.'));
165 $self->redirect_to($self->list_url);
171 $self->class->reorder_list(@{ $::form->{object_id} || [] });
172 $self->render(\'', { type => 'json' });
179 sub check_type_and_auth {
182 $self->type($::form->{type});
183 $self->config($supported_types{$self->type}) || die "Unsupported type";
185 $::auth->assert($self->config->{auth} || 'config');
187 my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
190 my $setup = "setup_" . $self->type;
191 $self->$setup if $self->can($setup);
196 sub setup_javascript {
197 $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
200 sub init_class { "SL::DB::" . $_[0]->config->{class} }
201 sub init_manager_class { "SL::DB::Manager::" . $_[0]->config->{class} }
202 sub init_object { $_[0]->class->new(id => $::form->{id})->load }
203 sub init_all_objects { $_[0]->manager_class->get_all_sorted }
204 sub init_list_url { $_[0]->url_for(action => 'list', type => $_[0]->type) }
205 sub init_supports_reordering { $_[0]->class->new->can('reorder_list') }
206 sub init_defaults { SL::DB::Default->get }
208 sub init_list_attributes {
211 my $method = "list_attributes_" . $self->type;
213 return $self->$method if $self->can($method);
214 return $self->config->{list_attributes} // \@default_list_attributes;
221 sub create_or_update {
223 my $is_new = !$self->object->id;
225 my $params = delete($::form->{object}) || { };
227 $self->object->assign_attributes(%{ $params });
231 push @errors, $self->object->validate if $self->object->can('validate');
234 flash('error', @errors);
235 return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
240 flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
242 $self->redirect_to($self->list_url);
246 my ($self, %params) = @_;
248 my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
251 'simple_system_setting/form',
253 sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
258 # type-specific helper functions
269 SL::Controller::SimpleSystemSettings — a common CRUD controller for
270 various settings in the "System" menu
274 Moritz Bunkus <m.bunkus@linet-services.de>