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'),
72 list => t8('Greetings'),
73 add => t8('Add greeting'),
74 edit => t8('Edit greeting'),
79 # Make locales.pl happy: $self->render("simple_system_setting/_language_form")
82 list => t8('Languages'),
83 add => t8('Add language'),
84 edit => t8('Edit language'),
87 { method => 'description', title => t8('Description'), },
88 { method => 'template_code', title => t8('Template Code'), },
89 { method => 'article_code', title => t8('Article Code'), },
90 { title => t8('Number Format'), formatter => sub { $_[0]->output_numberformat || t8('use program settings') } },
91 { title => t8('Date Format'), formatter => sub { $_[0]->output_dateformat || t8('use program settings') } },
92 { title => t8('Long Dates'), formatter => sub { $_[0]->output_longdates ? t8('yes') : t8('no') } },
96 part_classification => {
97 # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
98 class => 'PartClassification',
100 list => t8('Part classifications'),
101 add => t8('Add part classification'),
102 edit => t8('Edit part classification'),
105 { title => t8('Description'), formatter => sub { t8($_[0]->description) } },
106 { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
107 { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
108 { title => t8('Used for Sale'), formatter => sub { $_[0]->used_for_sale ? t8('yes') : t8('no') } },
109 { title => t8('Report separately'), formatter => sub { $_[0]->report_separate ? t8('yes') : t8('no') } },
114 # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
115 class => 'PartsGroup',
117 list => t8('Partsgroups'),
118 add => t8('Add partsgroup'),
119 edit => t8('Edit partsgroup'),
122 { method => 'partsgroup', title => t8('Description') },
123 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
128 # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
129 class => 'PriceFactor',
131 list => t8('Price Factors'),
132 add => t8('Add Price Factor'),
133 edit => t8('Edit Price Factor'),
136 { method => 'description', title => t8('Description') },
137 { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
142 # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
143 class => 'Pricegroup',
145 list => t8('Pricegroups'),
146 add => t8('Add pricegroup'),
147 edit => t8('Edit pricegroup'),
150 { method => 'pricegroup', title => t8('Description') },
151 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
156 class => 'ProjectStatus',
158 list => t8('Project statuses'),
159 add => t8('Add project status'),
160 edit => t8('Edit project status'),
165 class => 'ProjectType',
167 list => t8('Project types'),
168 add => t8('Add project type'),
169 edit => t8('Edit project type'),
173 requirement_spec_acceptance_status => {
174 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_acceptance_status_form")
175 class => 'RequirementSpecAcceptanceStatus',
177 list => t8('Acceptance Statuses'),
178 add => t8('Add acceptance status'),
179 edit => t8('Edit acceptance status'),
182 { method => 'name', title => t8('Name') },
183 { method => 'description', title => t8('Description') },
187 requirement_spec_complexity => {
188 class => 'RequirementSpecComplexity',
190 list => t8('Complexities'),
191 add => t8('Add complexity'),
192 edit => t8('Edit complexity'),
196 requirement_spec_predefined_text => {
197 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_predefined_text_form")
198 class => 'RequirementSpecPredefinedText',
200 list => t8('Pre-defined Texts'),
201 add => t8('Add pre-defined text'),
202 edit => t8('Edit pre-defined text'),
205 { method => 'description', title => t8('Description') },
206 { method => 'title', title => t8('Title') },
207 { title => t8('Content'), formatter => sub { my $t = $_[0]->text_as_stripped_html; length($t) > 50 ? substr($t, 0, 50) . '…' : $t } },
208 { title => t8('Useable for text blocks'), formatter => sub { $_[0]->useable_for_text_blocks ? t8('yes') : t8('no') } },
209 { title => t8('Useable for sections'), formatter => sub { $_[0]->useable_for_sections ? t8('yes') : t8('no') } },
213 requirement_spec_risk => {
214 class => 'RequirementSpecRisk',
216 list => t8('Risk levels'),
217 add => t8('Add risk level'),
218 edit => t8('Edit risk level'),
222 requirement_spec_status => {
223 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_status_form")
224 class => 'RequirementSpecStatus',
226 list => t8('Requirement Spec Statuses'),
227 add => t8('Add requirement spec status'),
228 edit => t8('Edit requirement spec status'),
231 { method => 'name', title => t8('Name') },
232 { method => 'description', title => t8('Description') },
236 requirement_spec_type => {
237 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_type_form")
238 class => 'RequirementSpecType',
240 list => t8('Requirement Spec Types'),
241 add => t8('Add requirement spec type'),
242 edit => t8('Edit requirement spec type'),
245 { method => 'description', title => t8('Description') },
246 { method => 'section_number_format', title => t8('Section number format') },
247 { method => 'function_block_number_format', title => t8('Function block number format') },
253 my @default_list_attributes = (
254 { method => 'description', title => t8('Description') },
264 $self->setup_list_action_bar;
265 $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
271 $self->object($self->class->new);
272 $self->render_form(title => $self->config->{titles}->{add});
278 $self->render_form(title => $self->config->{titles}->{edit});
284 $self->object($self->class->new);
285 $self->create_or_update;
291 $self->create_or_update;
297 if ($self->object->can('orphaned') && !$self->object->orphaned) {
298 flash_later('error', t8('The object is in use and cannot be deleted.'));
300 } elsif ( eval { $self->object->delete; 1; } ) {
301 flash_later('info', t8('The object has been deleted.'));
304 flash_later('error', t8('The object is in use and cannot be deleted.'));
307 $self->redirect_to($self->list_url);
313 $self->class->reorder_list(@{ $::form->{object_id} || [] });
314 $self->render(\'', { type => 'json' });
321 sub check_type_and_auth {
324 $self->type($::form->{type});
325 $self->config($supported_types{$self->type}) || die "Unsupported type";
327 $::auth->assert($self->config->{auth} || 'config');
329 my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
332 my $setup = "setup_" . $self->type;
333 $self->$setup if $self->can($setup);
338 sub setup_javascript {
339 $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
342 sub init_class { "SL::DB::" . $_[0]->config->{class} }
343 sub init_manager_class { "SL::DB::Manager::" . $_[0]->config->{class} }
344 sub init_object { $_[0]->class->new(id => $::form->{id})->load }
345 sub init_all_objects { $_[0]->manager_class->get_all_sorted }
346 sub init_list_url { $_[0]->url_for(action => 'list', type => $_[0]->type) }
347 sub init_supports_reordering { $_[0]->class->new->can('reorder_list') }
348 sub init_defaults { SL::DB::Default->get }
350 sub init_list_attributes {
353 my $method = "list_attributes_" . $self->type;
355 return $self->$method if $self->can($method);
356 return $self->config->{list_attributes} // \@default_list_attributes;
363 sub create_or_update {
365 my $is_new = !$self->object->id;
367 my $params = delete($::form->{object}) || { };
369 $self->object->assign_attributes(%{ $params });
373 push @errors, $self->object->validate if $self->object->can('validate');
376 flash('error', @errors);
377 return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
382 flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
384 $self->redirect_to($self->list_url);
388 my ($self, %params) = @_;
390 my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
392 $self->setup_render_form_action_bar;
394 'simple_system_setting/form',
396 sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
401 # type-specific helper functions
404 sub setup_requirement_spec_acceptance_status {
408 $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
411 sub setup_requirement_spec_status {
415 $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
421 $self->{numberformats} = [ '1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00" ];
422 $self->{dateformats} = [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ];
429 sub setup_list_action_bar {
430 my ($self, %params) = @_;
432 for my $bar ($::request->layout->get('actionbar')) {
436 link => $self->url_for(action => 'new', type => $self->type),
442 sub setup_render_form_action_bar {
445 my $is_new = !$self->object->id;
446 my $can_be_deleted = !$is_new
447 && (!$self->object->can("orphaned") || $self->object->orphaned)
448 && (!$self->object->can("can_be_deleted") || $self->object->can_be_deleted);
450 for my $bar ($::request->layout->get('actionbar')) {
454 submit => [ '#form', { action => 'SimpleSystemSetting/' . ($is_new ? 'create' : 'update') } ],
455 checks => [ 'kivi.validate_form' ],
456 accesskey => 'enter',
461 submit => [ '#form', { action => 'SimpleSystemSetting/delete' } ],
462 confirm => t8('Do you really want to delete this object?'),
463 disabled => $is_new ? t8('This object has not been saved yet.')
464 : !$can_be_deleted ? t8('The object is in use and cannot be deleted.')
470 link => $self->list_url,
474 $::request->layout->add_javascripts('kivi.Validator.js');
485 SL::Controller::SimpleSystemSettings — a common CRUD controller for
486 various settings in the "System" menu
490 Moritz Bunkus <m.bunkus@linet-services.de>