marei: new koma-names + fallback for outdated versions
[kivitendo-erp.git] / SL / Controller / DeliveryTerm.pm
1 package SL::Controller::DeliveryTerm;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::DeliveryTerm;
8 use SL::DB::Language;
9 use SL::Helper::Flash;
10 use SL::Locale::String qw(t8);
11
12 use Rose::Object::MakeMethods::Generic
13 (
14  scalar => [ qw(delivery_term languages) ],
15 );
16
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_delivery_term', only => [ qw(edit update destroy) ]);
19 __PACKAGE__->run_before('load_languages',     only => [ qw(new list edit create update) ]);
20
21
22 #
23 # actions
24 #
25
26 sub action_list {
27   my ($self) = @_;
28
29   $self->setup_list_action_bar;
30   $self->render('delivery_term/list',
31                 title          => $::locale->text('Delivery terms'),
32                 DELIVERY_TERMS => SL::DB::Manager::DeliveryTerm->get_all_sorted);
33 }
34
35 sub action_new {
36   my ($self) = @_;
37
38   $self->{delivery_term} = SL::DB::DeliveryTerm->new;
39   $self->setup_form_action_bar;
40   $self->render('delivery_term/form', title => $::locale->text('Create a new delivery term'));
41 }
42
43 sub action_edit {
44   my ($self) = @_;
45   $self->setup_form_action_bar;
46   $self->render('delivery_term/form', title => $::locale->text('Edit delivery term'));
47 }
48
49 sub action_create {
50   my ($self) = @_;
51
52   $self->{delivery_term} = SL::DB::DeliveryTerm->new;
53   $self->create_or_update;
54 }
55
56 sub action_update {
57   my ($self) = @_;
58   $self->create_or_update;
59 }
60
61 sub action_destroy {
62   my ($self) = @_;
63
64   if (eval { $self->{delivery_term}->delete; 1; }) {
65     flash_later('info',  $::locale->text('The delivery term has been deleted.'));
66   } else {
67     flash_later('error', $::locale->text('The delivery term is in use and cannot be deleted.'));
68   }
69
70   $self->redirect_to(action => 'list');
71 }
72
73 sub action_reorder {
74   my ($self) = @_;
75
76   SL::DB::DeliveryTerm->reorder_list(@{ $::form->{delivery_term_id} || [] });
77
78   $self->render(\'', { type => 'json' });     # ' make Emacs happy
79 }
80
81 #
82 # filters
83 #
84
85 sub check_auth {
86   $::auth->assert('config');
87 }
88
89 #
90 # helpers
91 #
92
93 sub create_or_update {
94   my $self   = shift;
95   my $is_new = !$self->{delivery_term}->id;
96   my $params = delete($::form->{delivery_term}) || { };
97
98   $self->{delivery_term}->assign_attributes(%{ $params });
99
100   my @errors = $self->{delivery_term}->validate;
101
102   if (@errors) {
103     flash('error', @errors);
104     $self->render('delivery_term/form', title => $is_new ? $::locale->text('Create a new delivery term') : $::locale->text('Edit delivery term'));
105     return;
106   }
107
108   $self->{delivery_term}->save;
109   foreach my $language (@{ $self->{languages} }) {
110     $self->{delivery_term}->save_attribute_translation('description_long', $language, $::form->{"translation_" . $language->id});
111   }
112
113   flash_later('info', $is_new ? $::locale->text('The delivery term has been created.') : $::locale->text('The delivery term has been saved.'));
114   $self->redirect_to(action => 'list');
115 }
116
117 sub load_delivery_term {
118   my ($self) = @_;
119   $self->{delivery_term} = SL::DB::DeliveryTerm->new(id => $::form->{id})->load;
120 }
121
122 sub load_languages {
123   my ($self) = @_;
124   $self->{languages} = SL::DB::Manager::Language->get_all_sorted;
125 }
126
127 sub setup_list_action_bar {
128   my ($self) = @_;
129
130   for my $bar ($::request->layout->get('actionbar')) {
131     $bar->add(
132       link => [
133         t8('Add'),
134         link => $self->url_for(action => 'new'),
135       ],
136     );
137   }
138 }
139
140 sub setup_form_action_bar {
141   my ($self) = @_;
142
143   my $is_new = !$self->delivery_term->id;
144
145   for my $bar ($::request->layout->get('actionbar')) {
146     $bar->add(
147       action => [
148         t8('Save'),
149         submit    => [ '#form', { action => 'DeliveryTerm/' . ($is_new ? 'create' : 'update') } ],
150         checks    => [ 'kivi.validate_form' ],
151         accesskey => 'enter',
152       ],
153
154       action => [
155         t8('Delete'),
156         submit   => [ '#form', { action => 'DeliveryTerm/destroy' } ],
157         confirm  => t8('Do you really want to delete this object?'),
158         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
159       ],
160
161       'separator',
162
163       link => [
164         t8('Abort'),
165         link => $self->url_for(action => 'list'),
166       ],
167     );
168   }
169   $::request->layout->add_javascripts('kivi.Validator.js');
170 }
171
172 1;