1 package SL::Controller::DeliveryTerm;
5 use parent qw(SL::Controller::Base);
7 use SL::DB::DeliveryTerm;
10 use SL::Locale::String qw(t8);
12 use Rose::Object::MakeMethods::Generic
14 scalar => [ qw(delivery_term languages) ],
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) ]);
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);
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'));
45 $self->setup_form_action_bar;
46 $self->render('delivery_term/form', title => $::locale->text('Edit delivery term'));
52 $self->{delivery_term} = SL::DB::DeliveryTerm->new;
53 $self->create_or_update;
58 $self->create_or_update;
64 if (eval { $self->{delivery_term}->delete; 1; }) {
65 flash_later('info', $::locale->text('The delivery term has been deleted.'));
67 flash_later('error', $::locale->text('The delivery term is in use and cannot be deleted.'));
70 $self->redirect_to(action => 'list');
76 SL::DB::DeliveryTerm->reorder_list(@{ $::form->{delivery_term_id} || [] });
78 $self->render(\'', { type => 'json' }); # ' make Emacs happy
86 $::auth->assert('config');
93 sub create_or_update {
95 my $is_new = !$self->{delivery_term}->id;
96 my $params = delete($::form->{delivery_term}) || { };
98 $self->{delivery_term}->assign_attributes(%{ $params });
100 my @errors = $self->{delivery_term}->validate;
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'));
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});
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');
117 sub load_delivery_term {
119 $self->{delivery_term} = SL::DB::DeliveryTerm->new(id => $::form->{id})->load;
124 $self->{languages} = SL::DB::Manager::Language->get_all_sorted;
127 sub setup_list_action_bar {
130 for my $bar ($::request->layout->get('actionbar')) {
134 link => $self->url_for(action => 'new'),
140 sub setup_form_action_bar {
143 my $is_new = !$self->delivery_term->id;
145 for my $bar ($::request->layout->get('actionbar')) {
149 submit => [ '#form', { action => 'DeliveryTerm/' . ($is_new ? 'create' : 'update') } ],
150 checks => [ 'kivi.validate_form' ],
151 accesskey => 'enter',
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,
165 link => $self->url_for(action => 'list'),
169 $::request->layout->add_javascripts('kivi.Validator.js');