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 { title => t8('Use for ZUGFeRD'), formatter => sub { $_[0]->use_for_zugferd ? t8('yes') : t8('no') } },
39 { method => 'reconciliation_starting_date_as_date', title => t8('Date'), align => 'right' },
40 { method => 'reconciliation_starting_balance_as_number', title => t8('Balance'), align => 'right' },
45 # Make locales.pl happy: $self->render("simple_system_setting/_business_form")
48 list => t8('Businesses'),
49 add => t8('Add business'),
50 edit => t8('Edit business'),
53 { method => 'description', title => t8('Description'), },
54 { title => t8('Discount'), formatter => sub { $_[0]->discount_as_percent . ' %' }, align => 'right' },
55 { method => 'customernumberinit', title => t8('Customernumberinit'), },
60 class => 'Department',
62 list => t8('Departments'),
63 add => t8('Add department'),
64 edit => t8('Edit department'),
69 # Make locales.pl happy: $self->render("simple_system_setting/_language_form")
72 list => t8('Languages'),
73 add => t8('Add language'),
74 edit => t8('Edit language'),
77 { method => 'description', title => t8('Description'), },
78 { method => 'template_code', title => t8('Template Code'), },
79 { method => 'article_code', title => t8('Article Code'), },
80 { title => t8('Number Format'), formatter => sub { $_[0]->output_numberformat || t8('use program settings') } },
81 { title => t8('Date Format'), formatter => sub { $_[0]->output_dateformat || t8('use program settings') } },
82 { title => t8('Long Dates'), formatter => sub { $_[0]->output_longdates ? t8('yes') : t8('no') } },
86 part_classification => {
87 # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
88 class => 'PartClassification',
90 list => t8('Part classifications'),
91 add => t8('Add part classification'),
92 edit => t8('Edit part classification'),
95 { title => t8('Description'), formatter => sub { t8($_[0]->description) } },
96 { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
97 { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
98 { title => t8('Used for Sale'), formatter => sub { $_[0]->used_for_sale ? t8('yes') : t8('no') } },
99 { title => t8('Report separately'), formatter => sub { $_[0]->report_separate ? t8('yes') : t8('no') } },
104 # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
105 class => 'PartsGroup',
107 list => t8('Partsgroups'),
108 add => t8('Add partsgroup'),
109 edit => t8('Edit partsgroup'),
112 { method => 'partsgroup', title => t8('Description') },
113 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
118 # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
119 class => 'PriceFactor',
121 list => t8('Price Factors'),
122 add => t8('Add Price Factor'),
123 edit => t8('Edit Price Factor'),
126 { method => 'description', title => t8('Description') },
127 { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
132 # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
133 class => 'Pricegroup',
135 list => t8('Pricegroups'),
136 add => t8('Add pricegroup'),
137 edit => t8('Edit pricegroup'),
140 { method => 'pricegroup', title => t8('Description') },
141 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
146 class => 'ProjectStatus',
148 list => t8('Project statuses'),
149 add => t8('Add project status'),
150 edit => t8('Edit project status'),
155 class => 'ProjectType',
157 list => t8('Project types'),
158 add => t8('Add project type'),
159 edit => t8('Edit project type'),
163 requirement_spec_acceptance_status => {
164 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_acceptance_status_form")
165 class => 'RequirementSpecAcceptanceStatus',
167 list => t8('Acceptance Statuses'),
168 add => t8('Add acceptance status'),
169 edit => t8('Edit acceptance status'),
172 { method => 'name', title => t8('Name') },
173 { method => 'description', title => t8('Description') },
177 requirement_spec_complexity => {
178 class => 'RequirementSpecComplexity',
180 list => t8('Complexities'),
181 add => t8('Add complexity'),
182 edit => t8('Edit complexity'),
186 requirement_spec_predefined_text => {
187 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_predefined_text_form")
188 class => 'RequirementSpecPredefinedText',
190 list => t8('Pre-defined Texts'),
191 add => t8('Add pre-defined text'),
192 edit => t8('Edit pre-defined text'),
195 { method => 'description', title => t8('Description') },
196 { method => 'title', title => t8('Title') },
197 { title => t8('Content'), formatter => sub { my $t = $_[0]->text_as_stripped_html; length($t) > 50 ? substr($t, 0, 50) . '…' : $t } },
198 { title => t8('Useable for text blocks'), formatter => sub { $_[0]->useable_for_text_blocks ? t8('yes') : t8('no') } },
199 { title => t8('Useable for sections'), formatter => sub { $_[0]->useable_for_sections ? t8('yes') : t8('no') } },
203 requirement_spec_risk => {
204 class => 'RequirementSpecRisk',
206 list => t8('Risk levels'),
207 add => t8('Add risk level'),
208 edit => t8('Edit risk level'),
212 requirement_spec_status => {
213 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_status_form")
214 class => 'RequirementSpecStatus',
216 list => t8('Requirement Spec Statuses'),
217 add => t8('Add requirement spec status'),
218 edit => t8('Edit requirement spec status'),
221 { method => 'name', title => t8('Name') },
222 { method => 'description', title => t8('Description') },
226 requirement_spec_type => {
227 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_type_form")
228 class => 'RequirementSpecType',
230 list => t8('Requirement Spec Types'),
231 add => t8('Add requirement spec type'),
232 edit => t8('Edit requirement spec type'),
235 { method => 'description', title => t8('Description') },
236 { method => 'section_number_format', title => t8('Section number format') },
237 { method => 'function_block_number_format', title => t8('Function block number format') },
243 my @default_list_attributes = (
244 { method => 'description', title => t8('Description') },
254 $self->setup_list_action_bar;
255 $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
261 $self->object($self->class->new);
262 $self->render_form(title => $self->config->{titles}->{add});
268 $self->render_form(title => $self->config->{titles}->{edit});
274 $self->object($self->class->new);
275 $self->create_or_update;
281 $self->create_or_update;
287 if ($self->object->can('orphaned') && !$self->object->orphaned) {
288 flash_later('error', t8('The object is in use and cannot be deleted.'));
290 } elsif ( eval { $self->object->delete; 1; } ) {
291 flash_later('info', t8('The object has been deleted.'));
294 flash_later('error', t8('The object is in use and cannot be deleted.'));
297 $self->redirect_to($self->list_url);
303 $self->class->reorder_list(@{ $::form->{object_id} || [] });
304 $self->render(\'', { type => 'json' });
311 sub check_type_and_auth {
314 $self->type($::form->{type});
315 $self->config($supported_types{$self->type}) || die "Unsupported type";
317 $::auth->assert($self->config->{auth} || 'config');
319 my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
322 my $setup = "setup_" . $self->type;
323 $self->$setup if $self->can($setup);
328 sub setup_javascript {
329 $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
332 sub init_class { "SL::DB::" . $_[0]->config->{class} }
333 sub init_manager_class { "SL::DB::Manager::" . $_[0]->config->{class} }
334 sub init_object { $_[0]->class->new(id => $::form->{id})->load }
335 sub init_all_objects { $_[0]->manager_class->get_all_sorted }
336 sub init_list_url { $_[0]->url_for(action => 'list', type => $_[0]->type) }
337 sub init_supports_reordering { $_[0]->class->new->can('reorder_list') }
338 sub init_defaults { SL::DB::Default->get }
340 sub init_list_attributes {
343 my $method = "list_attributes_" . $self->type;
345 return $self->$method if $self->can($method);
346 return $self->config->{list_attributes} // \@default_list_attributes;
353 sub create_or_update {
355 my $is_new = !$self->object->id;
357 my $params = delete($::form->{object}) || { };
359 $self->object->assign_attributes(%{ $params });
363 push @errors, $self->object->validate if $self->object->can('validate');
366 flash('error', @errors);
367 return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
372 flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
374 $self->redirect_to($self->list_url);
378 my ($self, %params) = @_;
380 my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
382 $self->setup_render_form_action_bar;
384 'simple_system_setting/form',
386 sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
391 # type-specific helper functions
394 sub setup_requirement_spec_acceptance_status {
398 $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
401 sub setup_requirement_spec_status {
405 $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
411 $self->{numberformats} = [ '1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00" ];
412 $self->{dateformats} = [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ];
419 sub setup_list_action_bar {
420 my ($self, %params) = @_;
422 for my $bar ($::request->layout->get('actionbar')) {
426 link => $self->url_for(action => 'new', type => $self->type),
432 sub setup_render_form_action_bar {
435 my $is_new = !$self->object->id;
436 my $can_be_deleted = !$is_new
437 && (!$self->object->can("orphaned") || $self->object->orphaned)
438 && (!$self->object->can("can_be_deleted") || $self->object->can_be_deleted);
440 for my $bar ($::request->layout->get('actionbar')) {
444 submit => [ '#form', { action => 'SimpleSystemSetting/' . ($is_new ? 'create' : 'update') } ],
445 checks => [ 'kivi.validate_form' ],
446 accesskey => 'enter',
451 submit => [ '#form', { action => 'SimpleSystemSetting/delete' } ],
452 confirm => t8('Do you really want to delete this object?'),
453 disabled => $is_new ? t8('This object has not been saved yet.')
454 : !$can_be_deleted ? t8('The object is in use and cannot be deleted.')
460 link => $self->list_url,
464 $::request->layout->add_javascripts('kivi.Validator.js');
475 SL::Controller::SimpleSystemSettings — a common CRUD controller for
476 various settings in the "System" menu
480 Moritz Bunkus <m.bunkus@linet-services.de>