marei: new koma-names + fallback for outdated versions
[kivitendo-erp.git] / SL / Controller / Employee.pm
1 package SL::Controller::Employee;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use SL::DB::Employee;
7 use SL::Helper::Flash;
8 use SL::Locale::String qw(t8);
9
10 __PACKAGE__->run_before('check_auth');
11 __PACKAGE__->run_before('load_all');
12 __PACKAGE__->run_before('load_from_form');
13 __PACKAGE__->run_before('assign_from_form');
14
15 our @updatable_columns = qw(deleted);
16
17 sub action_list {
18   my ($self, %params) = @_;
19
20   $self->render('employee/list', title => $::locale->text('Employees'));
21 }
22
23 sub action_edit {
24   my ($self, %params) = @_;
25
26   if ($self->{employee}) {
27     $self->setup_edit_action_bar;
28     $self->render('employee/edit', title => $::locale->text('Edit Employee #1', $self->{employee}->safe_name));
29   } else {
30     flash_later('error', $::locale->text('Could not load employee'));
31     $self->redirect_to(action => 'list');
32   }
33 }
34
35 sub action_save {
36   my ($self, %params) = @_;
37
38   $self->{employee}->save;
39
40   flash('info', $::locale->text('Employee #1 saved!', $self->{employee}->safe_name));
41
42   $self->redirect_to(action => 'edit', 'employee.id' => $self->{employee}->id);
43 }
44
45 #################### private stuff ##########################
46
47 sub check_auth {
48   $::auth->assert('admin');
49 }
50
51 sub load_all {
52   $_[0]{employees} = SL::DB::Manager::Employee->get_all;
53 }
54
55 sub load_from_form {
56   $_[0]{employee} = SL::DB::Manager::Employee->find_by(id => delete $::form->{employee}{id});
57 }
58
59 sub assign_from_form {
60   my %data = %{ $::form->{employee} || {} };
61
62   return 1 unless keys %data;
63
64   $_[0]{employee}->assign_attributes(map { $_ => $data{$_} } @updatable_columns);
65   return 1;
66 }
67
68 sub setup_edit_action_bar {
69   my ($self) = @_;
70
71   for my $bar ($::request->layout->get('actionbar')) {
72     $bar->add(
73       action => [
74         t8('Save'),
75         submit    => [ '#form', { action => 'Employee/save' } ],
76         accesskey => 'enter',
77       ],
78
79       'separator',
80
81       link => [
82         t8('Abort'),
83         link => $self->url_for(action => 'list'),
84       ],
85     );
86   }
87 }
88
89 ######################## behaviour ##########################
90
91 sub delay_flash_on_redirect { 1 }
92
93 1;