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 => 'bank_account_id', title => t8('Bank Account Id Number (Swiss)'), },
38 { method => 'bic', title => t8('BIC'), },
39 { title => t8('Use for Factur-X/ZUGFeRD'), formatter => sub { $_[0]->use_for_zugferd ? t8('yes') : t8('no') } },
40 { title => t8('Use for Swiss QR-Bill'), formatter => sub { $_[0]->use_for_qrbill ? t8('yes') : t8('no') } },
41 { method => 'reconciliation_starting_date_as_date', title => t8('Date'), align => 'right' },
42 { method => 'reconciliation_starting_balance_as_number', title => t8('Balance'), align => 'right' },
47 # Make locales.pl happy: $self->render("simple_system_setting/_business_form")
50 list => t8('Businesses'),
51 add => t8('Add business'),
52 edit => t8('Edit business'),
55 { method => 'description', title => t8('Description'), },
56 { title => t8('Discount'), formatter => sub { $_[0]->discount_as_percent . ' %' }, align => 'right' },
57 { method => 'customernumberinit', title => t8('Customernumberinit'), },
61 contact_department => {
62 class => 'ContactDepartment',
65 list => t8('Contact Departments'),
66 add => t8('Add department'),
67 edit => t8('Edit department'),
72 class => 'ContactTitle',
75 list => t8('Contact Titles'),
76 add => t8('Add title'),
77 edit => t8('Edit title'),
82 class => 'Department',
84 list => t8('Departments'),
85 add => t8('Add department'),
86 edit => t8('Edit department'),
94 list => t8('Greetings'),
95 add => t8('Add greeting'),
96 edit => t8('Edit greeting'),
101 # Make locales.pl happy: $self->render("simple_system_setting/_language_form")
104 list => t8('Languages'),
105 add => t8('Add language'),
106 edit => t8('Edit language'),
109 { method => 'description', title => t8('Description'), },
110 { method => 'template_code', title => t8('Template Code'), },
111 { method => 'article_code', title => t8('Article Code'), },
112 { title => t8('Number Format'), formatter => sub { $_[0]->output_numberformat || t8('use program settings') } },
113 { title => t8('Date Format'), formatter => sub { $_[0]->output_dateformat || t8('use program settings') } },
114 { title => t8('Long Dates'), formatter => sub { $_[0]->output_longdates ? t8('yes') : t8('no') } },
118 part_classification => {
119 # Make locales.pl happy: $self->render("simple_system_setting/_part_classification_form")
120 class => 'PartClassification',
122 list => t8('Part classifications'),
123 add => t8('Add part classification'),
124 edit => t8('Edit part classification'),
127 { title => t8('Description'), formatter => sub { t8($_[0]->description) } },
128 { title => t8('Type abbreviation'), formatter => sub { t8($_[0]->abbreviation) } },
129 { title => t8('Used for Purchase'), formatter => sub { $_[0]->used_for_purchase ? t8('yes') : t8('no') } },
130 { title => t8('Used for Sale'), formatter => sub { $_[0]->used_for_sale ? t8('yes') : t8('no') } },
131 { title => t8('Report separately'), formatter => sub { $_[0]->report_separate ? t8('yes') : t8('no') } },
136 # Make locales.pl happy: $self->render("simple_system_setting/_parts_group_form")
137 class => 'PartsGroup',
139 list => t8('Partsgroups'),
140 add => t8('Add partsgroup'),
141 edit => t8('Edit partsgroup'),
144 { method => 'partsgroup', title => t8('Description') },
145 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
150 # Make locales.pl happy: $self->render("simple_system_setting/_price_factor_form")
151 class => 'PriceFactor',
153 list => t8('Price Factors'),
154 add => t8('Add Price Factor'),
155 edit => t8('Edit Price Factor'),
158 { method => 'description', title => t8('Description') },
159 { method => 'factor_as_number', title => t8('Factor'), align => 'right' },
164 # Make locales.pl happy: $self->render("simple_system_setting/_pricegroup_form")
165 class => 'Pricegroup',
167 list => t8('Pricegroups'),
168 add => t8('Add pricegroup'),
169 edit => t8('Edit pricegroup'),
172 { method => 'pricegroup', title => t8('Description') },
173 { method => 'obsolete', title => t8('Obsolete'), formatter => sub { $_[0]->obsolete ? t8('yes') : t8('no') } },
178 class => 'ProjectStatus',
180 list => t8('Project statuses'),
181 add => t8('Add project status'),
182 edit => t8('Edit project status'),
187 class => 'ProjectType',
189 list => t8('Project types'),
190 add => t8('Add project type'),
191 edit => t8('Edit project type'),
195 requirement_spec_acceptance_status => {
196 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_acceptance_status_form")
197 class => 'RequirementSpecAcceptanceStatus',
199 list => t8('Acceptance Statuses'),
200 add => t8('Add acceptance status'),
201 edit => t8('Edit acceptance status'),
204 { method => 'name', title => t8('Name') },
205 { method => 'description', title => t8('Description') },
209 requirement_spec_complexity => {
210 class => 'RequirementSpecComplexity',
212 list => t8('Complexities'),
213 add => t8('Add complexity'),
214 edit => t8('Edit complexity'),
218 requirement_spec_predefined_text => {
219 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_predefined_text_form")
220 class => 'RequirementSpecPredefinedText',
222 list => t8('Pre-defined Texts'),
223 add => t8('Add pre-defined text'),
224 edit => t8('Edit pre-defined text'),
227 { method => 'description', title => t8('Description') },
228 { method => 'title', title => t8('Title') },
229 { title => t8('Content'), formatter => sub { my $t = $_[0]->text_as_stripped_html; length($t) > 50 ? substr($t, 0, 50) . '…' : $t } },
230 { title => t8('Useable for text blocks'), formatter => sub { $_[0]->useable_for_text_blocks ? t8('yes') : t8('no') } },
231 { title => t8('Useable for sections'), formatter => sub { $_[0]->useable_for_sections ? t8('yes') : t8('no') } },
235 requirement_spec_risk => {
236 class => 'RequirementSpecRisk',
238 list => t8('Risk levels'),
239 add => t8('Add risk level'),
240 edit => t8('Edit risk level'),
244 requirement_spec_status => {
245 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_status_form")
246 class => 'RequirementSpecStatus',
248 list => t8('Requirement Spec Statuses'),
249 add => t8('Add requirement spec status'),
250 edit => t8('Edit requirement spec status'),
253 { method => 'name', title => t8('Name') },
254 { method => 'description', title => t8('Description') },
258 requirement_spec_type => {
259 # Make locales.pl happy: $self->render("simple_system_setting/_requirement_spec_type_form")
260 class => 'RequirementSpecType',
262 list => t8('Requirement Spec Types'),
263 add => t8('Add requirement spec type'),
264 edit => t8('Edit requirement spec type'),
267 { method => 'description', title => t8('Description') },
268 { method => 'section_number_format', title => t8('Section number format') },
269 { method => 'function_block_number_format', title => t8('Function block number format') },
273 time_recording_article => {
274 # Make locales.pl happy: $self->render("simple_system_setting/_time_recording_article_form")
275 class => 'TimeRecordingArticle',
278 list => t8('Time Recording Articles'),
279 add => t8('Add time recording article'),
280 edit => t8('Edit time recording article'),
283 { title => t8('Article'), formatter => sub { $_[0]->part->displayable_name } },
289 my @default_list_attributes = (
290 { method => 'description', title => t8('Description') },
300 $self->setup_list_action_bar;
301 $self->render('simple_system_setting/list', title => $self->config->{titles}->{list});
307 $self->object($self->class->new);
308 $self->render_form(title => $self->config->{titles}->{add});
314 $self->render_form(title => $self->config->{titles}->{edit});
320 $self->object($self->class->new);
321 $self->create_or_update;
327 $self->create_or_update;
333 if ($self->object->can('orphaned') && !$self->object->orphaned) {
334 flash_later('error', t8('The object is in use and cannot be deleted.'));
336 } elsif ( eval { $self->object->delete; 1; } ) {
337 flash_later('info', t8('The object has been deleted.'));
340 flash_later('error', t8('The object is in use and cannot be deleted.'));
343 $self->redirect_to($self->list_url);
349 $self->class->reorder_list(@{ $::form->{object_id} || [] });
350 $self->render(\'', { type => 'json' });
357 sub check_type_and_auth {
360 $self->type($::form->{type});
361 $self->config($supported_types{$self->type}) || die "Unsupported type";
363 $::auth->assert($self->config->{auth} || 'config');
365 my $pm = (map { s{::}{/}g; "${_}.pm" } $self->class)[0];
368 my $setup = "setup_" . $self->type;
369 $self->$setup if $self->can($setup);
374 sub setup_javascript {
375 $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
378 sub init_class { "SL::DB::" . $_[0]->config->{class} }
379 sub init_manager_class { "SL::DB::Manager::" . $_[0]->config->{class} }
380 sub init_object { $_[0]->class->new(id => $::form->{id})->load }
381 sub init_all_objects { $_[0]->manager_class->get_all_sorted }
382 sub init_list_url { $_[0]->url_for(action => 'list', type => $_[0]->type) }
383 sub init_supports_reordering { $_[0]->class->new->can('reorder_list') }
384 sub init_defaults { SL::DB::Default->get }
386 sub init_list_attributes {
389 my $method = "list_attributes_" . $self->type;
391 return $self->$method if $self->can($method);
392 return $self->config->{list_attributes} // \@default_list_attributes;
399 sub create_or_update {
401 my $is_new = !$self->object->id;
403 my $params = delete($::form->{object}) || { };
405 $self->object->assign_attributes(%{ $params });
409 push @errors, $self->object->validate if $self->object->can('validate');
412 flash('error', @errors);
413 return $self->render_form(title => $self->config->{titles}->{$is_new ? 'add' : 'edit'});
418 flash_later('info', $is_new ? t8('The object has been created.') : t8('The object has been saved.'));
420 $self->redirect_to($self->list_url);
424 my ($self, %params) = @_;
426 my $sub_form_template = SL::System::Process->exe_dir . '/templates/webpages/simple_system_setting/_' . $self->type . '_form.html';
428 $self->setup_render_form_action_bar;
430 'simple_system_setting/form',
432 sub_form_template => (-f $sub_form_template ? $self->type : 'default'),
437 # type-specific helper functions
440 sub setup_requirement_spec_acceptance_status {
444 $self->{valid_names} = \@SL::DB::RequirementSpecAcceptanceStatus::valid_names;
447 sub setup_requirement_spec_status {
451 $self->{valid_names} = \@SL::DB::RequirementSpecStatus::valid_names;
457 $self->{numberformats} = [ '1,000.00', '1000.00', '1.000,00', '1000,00', "1'000.00" ];
458 $self->{dateformats} = [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ];
465 sub setup_list_action_bar {
466 my ($self, %params) = @_;
468 for my $bar ($::request->layout->get('actionbar')) {
472 link => $self->url_for(action => 'new', type => $self->type),
478 sub setup_render_form_action_bar {
481 my $is_new = !$self->object->id;
482 my $can_be_deleted = !$is_new
483 && (!$self->object->can("orphaned") || $self->object->orphaned)
484 && (!$self->object->can("can_be_deleted") || $self->object->can_be_deleted);
486 for my $bar ($::request->layout->get('actionbar')) {
490 submit => [ '#form', { action => 'SimpleSystemSetting/' . ($is_new ? 'create' : 'update') } ],
491 checks => [ 'kivi.validate_form' ],
492 accesskey => 'enter',
497 submit => [ '#form', { action => 'SimpleSystemSetting/delete' } ],
498 confirm => t8('Do you really want to delete this object?'),
499 disabled => $is_new ? t8('This object has not been saved yet.')
500 : !$can_be_deleted ? t8('The object is in use and cannot be deleted.')
506 link => $self->list_url,
510 $::request->layout->add_javascripts('kivi.Validator.js');
521 SL::Controller::SimpleSystemSettings — a common CRUD controller for
522 various settings in the "System" menu
526 Moritz Bunkus <m.bunkus@linet-services.de>