JavaScript-Tests: Liste der Testdateien sortieren
[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
11 use Rose::Object::MakeMethods::Generic
12 (
13  scalar => [ qw(delivery_term languages) ],
14 );
15
16 __PACKAGE__->run_before('check_auth');
17 __PACKAGE__->run_before('load_delivery_term', only => [ qw(edit update destroy) ]);
18 __PACKAGE__->run_before('load_languages',     only => [ qw(new list edit create update) ]);
19
20
21 #
22 # actions
23 #
24
25 sub action_list {
26   my ($self) = @_;
27
28   $self->render('delivery_term/list',
29                 title          => $::locale->text('Delivery terms'),
30                 DELIVERY_TERMS => SL::DB::Manager::DeliveryTerm->get_all_sorted);
31 }
32
33 sub action_new {
34   my ($self) = @_;
35
36   $self->{delivery_term} = SL::DB::DeliveryTerm->new;
37   $self->render('delivery_term/form', title => $::locale->text('Create a new delivery term'));
38 }
39
40 sub action_edit {
41   my ($self) = @_;
42   $self->render('delivery_term/form', title => $::locale->text('Edit delivery term'));
43 }
44
45 sub action_create {
46   my ($self) = @_;
47
48   $self->{delivery_term} = SL::DB::DeliveryTerm->new;
49   $self->create_or_update;
50 }
51
52 sub action_update {
53   my ($self) = @_;
54   $self->create_or_update;
55 }
56
57 sub action_destroy {
58   my ($self) = @_;
59
60   if (eval { $self->{delivery_term}->delete; 1; }) {
61     flash_later('info',  $::locale->text('The delivery term has been deleted.'));
62   } else {
63     flash_later('error', $::locale->text('The delivery term is in use and cannot be deleted.'));
64   }
65
66   $self->redirect_to(action => 'list');
67 }
68
69 sub action_reorder {
70   my ($self) = @_;
71
72   SL::DB::DeliveryTerm->reorder_list(@{ $::form->{delivery_term_id} || [] });
73
74   $self->render(\'', { type => 'json' });     # ' make Emacs happy
75 }
76
77 #
78 # filters
79 #
80
81 sub check_auth {
82   $::auth->assert('config');
83 }
84
85 #
86 # helpers
87 #
88
89 sub create_or_update {
90   my $self   = shift;
91   my $is_new = !$self->{delivery_term}->id;
92   my $params = delete($::form->{delivery_term}) || { };
93
94   $self->{delivery_term}->assign_attributes(%{ $params });
95
96   my @errors = $self->{delivery_term}->validate;
97
98   if (@errors) {
99     flash('error', @errors);
100     $self->render('delivery_term/form', title => $is_new ? $::locale->text('Create a new delivery term') : $::locale->text('Edit delivery term'));
101     return;
102   }
103
104   $self->{delivery_term}->save;
105   foreach my $language (@{ $self->{languages} }) {
106     $self->{delivery_term}->save_attribute_translation('description_long', $language, $::form->{"translation_" . $language->id});
107   }
108
109   flash_later('info', $is_new ? $::locale->text('The delivery term has been created.') : $::locale->text('The delivery term has been saved.'));
110   $self->redirect_to(action => 'list');
111 }
112
113 sub load_delivery_term {
114   my ($self) = @_;
115   $self->{delivery_term} = SL::DB::DeliveryTerm->new(id => $::form->{id})->load;
116 }
117
118 sub load_languages {
119   my ($self) = @_;
120   $self->{languages} = SL::DB::Manager::Language->get_all_sorted;
121 }
122
123 1;