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'), },
59 contact_department => {
60 class => 'ContactDepartment',
63 list => t8('Contact Departments'),
64 add => t8('Add department'),
65 edit => t8('Edit department'),
70 class => 'ContactTitle',
73 list => t8('Contact Titles'),
74 add => t8('Add title'),
75 edit => t8('Edit title'),
80 class => 'Department',
82 list => t8('Departments'),
83 add => t8('Add department'),
84 edit => t8('Edit department'),
92 list => t8('Greetings'),
93 add => t8('Add greeting'),
94 edit => t8('Edit greeting'),
99 # Make locales.pl happy: $self->render("simple_system_setting/_language_form")
102 list => t8('Languages'),
103 add => t8('Add language'),
104 edit => t8('Edit language'),
107 { method => 'description', title => t8('Description'), },
108 { method => 'template_code', title => t8('Template Code'), },
109 { method => 'article_code', title => t8('Article Code'), },
110 { title => t8('Number Format'), formatter => sub { $_[0]->output_numberformat || t8('use program settings') } },
111 { title => t8('Date Format'), formatter => sub { $_[0]->output_dateformat || t8('use program settings') } },
112 { title => t8('Long Dates'), formatter => sub { $_[0]->output_longdates ? t8('yes') : t8('no') } },
116 part_classification => {
117 # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
118 class => 'PartClassification',
120 list => t8('Part classifications'),
121 add => t8('Add part classification'),
122 edit => t8('Edit part classification'),
125 { title => t8('Description'), formatter => sub { t8($_[0]->description) } },
126 { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
127 { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
128 { title => t8('Used for Sale'), formatter => sub { $_[0]->used_for_sale ? t8('yes') : t8('no') } },
129 { title => t8('Report separately'), formatter => sub { $_[0]->report_separate ? t8('yes') : t8('no') } },
134 # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
135 class => 'PartsGroup',
137 list => t8('Partsgroups'),
138 add => t8('Add partsgroup'),
139 edit => t8('Edit partsgroup'),
142 { method => 'partsgroup', title => t8('Description') },
143 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
148 # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
149 class => 'PriceFactor',
151 list => t8('Price Factors'),
152 add => t8('Add Price Factor'),
153 edit => t8('Edit Price Factor'),
156 { method => 'description', title => t8('Description') },
157 { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
162 # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
163 class => 'Pricegroup',
165 list => t8('Pricegroups'),
166 add => t8('Add pricegroup'),
167 edit => t8('Edit pricegroup'),
170 { method => 'pricegroup', title => t8('Description') },
171 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
176 class => 'ProjectStatus',
178 list => t8('Project statuses'),
179 add => t8('Add project status'),
180 edit => t8('Edit project status'),
185 class => 'ProjectType',
187 list => t8('Project types'),
188 add => t8('Add project type'),
189 edit => t8('Edit project type'),
193 requirement_spec_acceptance_status => {
194 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_acceptance_status_form")
195 class => 'RequirementSpecAcceptanceStatus',
197 list => t8('Acceptance Statuses'),
198 add => t8('Add acceptance status'),
199 edit => t8('Edit acceptance status'),
202 { method => 'name', title => t8('Name') },
203 { method => 'description', title => t8('Description') },
207 requirement_spec_complexity => {
208 class => 'RequirementSpecComplexity',
210 list => t8('Complexities'),
211 add => t8('Add complexity'),
212 edit => t8('Edit complexity'),
216 requirement_spec_predefined_text => {
217 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_predefined_text_form")
218 class => 'RequirementSpecPredefinedText',
220 list => t8('Pre-defined Texts'),
221 add => t8('Add pre-defined text'),
222 edit => t8('Edit pre-defined text'),
225 { method => 'description', title => t8('Description') },
226 { method => 'title', title => t8('Title') },
227 { title => t8('Content'), formatter => sub { my $t = $_[0]->text_as_stripped_html; length($t) > 50 ? substr($t, 0, 50) . '…' : $t } },
228 { title => t8('Useable for text blocks'), formatter => sub { $_[0]->useable_for_text_blocks ? t8('yes') : t8('no') } },
229 { title => t8('Useable for sections'), formatter => sub { $_[0]->useable_for_sections ? t8('yes') : t8('no') } },
233 requirement_spec_risk => {
234 class => 'RequirementSpecRisk',
236 list => t8('Risk levels'),
237 add => t8('Add risk level'),
238 edit => t8('Edit risk level'),
242 requirement_spec_status => {
243 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_status_form")
244 class => 'RequirementSpecStatus',
246 list => t8('Requirement Spec Statuses'),
247 add => t8('Add requirement spec status'),
248 edit => t8('Edit requirement spec status'),
251 { method => 'name', title => t8('Name') },
252 { method => 'description', title => t8('Description') },
256 requirement_spec_type => {
257 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_type_form")
258 class => 'RequirementSpecType',
260 list => t8('Requirement Spec Types'),
261 add => t8('Add requirement spec type'),
262 edit => t8('Edit requirement spec type'),
265 { method => 'description', title => t8('Description') },
266 { method => 'section_number_format', title => t8('Section number format') },
267 { method => 'function_block_number_format', title => t8('Function block number format') },
273 my @default_list_attributes = (
274 { method => 'description', title => t8('Description') },
284 $self->setup_list_action_bar;
285 $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
291 $self->object($self->class->new);
292 $self->render_form(title => $self->config->{titles}->{add});
298 $self->render_form(title => $self->config->{titles}->{edit});
304 $self->object($self->class->new);
305 $self->create_or_update;
311 $self->create_or_update;
317 if ($self->object->can('orphaned') && !$self->object->orphaned) {
318 flash_later('error', t8('The object is in use and cannot be deleted.'));
320 } elsif ( eval { $self->object->delete; 1; } ) {
321 flash_later('info', t8('The object has been deleted.'));
324 flash_later('error', t8('The object is in use and cannot be deleted.'));
327 $self->redirect_to($self->list_url);
333 $self->class->reorder_list(@{ $::form->{object_id} || [] });
334 $self->render(\'', { type => 'json' });
341 sub check_type_and_auth {
344 $self->type($::form->{type});
345 $self->config($supported_types{$self->type}) || die "Unsupported type";
347 $::auth->assert($self->config->{auth} || 'config');
349 my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
352 my $setup = "setup_" . $self->type;
353 $self->$setup if $self->can($setup);
358 sub setup_javascript {
359 $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
362 sub init_class { "SL::DB::" . $_[0]->config->{class} }
363 sub init_manager_class { "SL::DB::Manager::" . $_[0]->config->{class} }
364 sub init_object { $_[0]->class->new(id => $::form->{id})->load }
365 sub init_all_objects { $_[0]->manager_class->get_all_sorted }
366 sub init_list_url { $_[0]->url_for(action => 'list', type => $_[0]->type) }
367 sub init_supports_reordering { $_[0]->class->new->can('reorder_list') }
368 sub init_defaults { SL::DB::Default->get }
370 sub init_list_attributes {
373 my $method = "list_attributes_" . $self->type;
375 return $self->$method if $self->can($method);
376 return $self->config->{list_attributes} // \@default_list_attributes;
383 sub create_or_update {
385 my $is_new = !$self->object->id;
387 my $params = delete($::form->{object}) || { };
389 $self->object->assign_attributes(%{ $params });
393 push @errors, $self->object->validate if $self->object->can('validate');
396 flash('error', @errors);
397 return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
402 flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
404 $self->redirect_to($self->list_url);
408 my ($self, %params) = @_;
410 my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
412 $self->setup_render_form_action_bar;
414 'simple_system_setting/form',
416 sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
421 # type-specific helper functions
424 sub setup_requirement_spec_acceptance_status {
428 $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
431 sub setup_requirement_spec_status {
435 $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
441 $self->{numberformats} = [ '1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00" ];
442 $self->{dateformats} = [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ];
449 sub setup_list_action_bar {
450 my ($self, %params) = @_;
452 for my $bar ($::request->layout->get('actionbar')) {
456 link => $self->url_for(action => 'new', type => $self->type),
462 sub setup_render_form_action_bar {
465 my $is_new = !$self->object->id;
466 my $can_be_deleted = !$is_new
467 && (!$self->object->can("orphaned") || $self->object->orphaned)
468 && (!$self->object->can("can_be_deleted") || $self->object->can_be_deleted);
470 for my $bar ($::request->layout->get('actionbar')) {
474 submit => [ '#form', { action => 'SimpleSystemSetting/' . ($is_new ? 'create' : 'update') } ],
475 checks => [ 'kivi.validate_form' ],
476 accesskey => 'enter',
481 submit => [ '#form', { action => 'SimpleSystemSetting/delete' } ],
482 confirm => t8('Do you really want to delete this object?'),
483 disabled => $is_new ? t8('This object has not been saved yet.')
484 : !$can_be_deleted ? t8('The object is in use and cannot be deleted.')
490 link => $self->list_url,
494 $::request->layout->add_javascripts('kivi.Validator.js');
505 SL::Controller::SimpleSystemSettings — a common CRUD controller for
506 various settings in the "System" menu
510 Moritz Bunkus <m.bunkus@linet-services.de>