Lokalisierung für Fehlermeldungen und Doku ergänzt
[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   SL::DB->client->with_transaction(sub {
39     1;
40
41     $self->{employee}->save;
42
43     if ($self->{employee}->deleted) {
44       my $auth_user = SL::DB::Manager::AuthUser->get_first(login => $self->{employee}->login);
45       if ($auth_user) {
46         SL::DB::Manager::AuthClientUser->delete_all(
47           where => [
48             client_id => $::auth->client->{id},
49             user_id   => $auth_user->id,
50           ]);
51       }
52     }
53
54     1;
55   });
56
57   flash('info', $::locale->text('Employee #1 saved!', $self->{employee}->safe_name));
58
59   $self->redirect_to(action => 'edit', 'employee.id' => $self->{employee}->id);
60 }
61
62 #################### private stuff ##########################
63
64 sub check_auth {
65   $::auth->assert('admin');
66 }
67
68 sub load_all {
69   $_[0]{employees} = SL::DB::Manager::Employee->get_all;
70 }
71
72 sub load_from_form {
73   $_[0]{employee} = SL::DB::Manager::Employee->find_by(id => delete $::form->{employee}{id});
74 }
75
76 sub assign_from_form {
77   my %data = %{ $::form->{employee} || {} };
78
79   return 1 unless keys %data;
80
81   $_[0]{employee}->assign_attributes(map { $_ => $data{$_} } @updatable_columns);
82   return 1;
83 }
84
85 sub setup_edit_action_bar {
86   my ($self) = @_;
87
88   for my $bar ($::request->layout->get('actionbar')) {
89     $bar->add(
90       action => [
91         t8('Save'),
92         submit    => [ '#form', { action => 'Employee/save' } ],
93         accesskey => 'enter',
94       ],
95
96       'separator',
97
98       link => [
99         t8('Abort'),
100         link => $self->url_for(action => 'list'),
101       ],
102     );
103   }
104 }
105
106 ######################## behaviour ##########################
107
108 sub delay_flash_on_redirect { 1 }
109
110 1;