Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / SL / Controller / Department.pm
1 package SL::Controller::Department;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::Department;
8 use SL::Helper::Flash;
9
10 use Rose::Object::MakeMethods::Generic
11 (
12  scalar => [ qw(department) ],
13 );
14
15 __PACKAGE__->run_before('check_auth');
16 __PACKAGE__->run_before('load_department', only => [ qw(edit update destroy) ]);
17
18 #
19 # actions
20 #
21
22 sub action_list {
23   my ($self) = @_;
24
25   $self->render('department/list',
26                 title       => $::locale->text('Departments'),
27                 DEPARTMENTS => SL::DB::Manager::Department->get_all_sorted);
28 }
29
30 sub action_new {
31   my ($self) = @_;
32
33   $self->render('department/form', title => $::locale->text('Create a new department'));
34 }
35
36 sub action_edit {
37   my ($self) = @_;
38   $self->render('department/form', title => $::locale->text('Edit department'));
39 }
40
41 sub action_create {
42   my ($self) = @_;
43
44   $self->{department} = SL::DB::Department->new;
45   $self->create_or_update;
46 }
47
48 sub action_update {
49   my ($self) = @_;
50   $self->create_or_update;
51 }
52
53 sub action_destroy {
54   my ($self) = @_;
55
56   if (eval { $self->{department}->delete; 1; }) {
57     flash_later('info',  $::locale->text('The department has been deleted.'));
58   } else {
59     flash_later('error', $::locale->text('The department is in use and cannot be deleted.'));
60   }
61
62   $self->redirect_to(action => 'list');
63 }
64
65 #
66 # filters
67 #
68
69 sub check_auth {
70   $::auth->assert('config');
71 }
72
73 #
74 # helpers
75 #
76
77 sub create_or_update {
78   my $self   = shift;
79   my $is_new = !$self->{department}->id;
80   my $params = delete($::form->{department}) || { };
81
82   $self->{department}->assign_attributes(%{ $params });
83
84   my @errors = $self->{department}->validate;
85
86   if (@errors) {
87     flash('error', @errors);
88     $self->render('department/form', title => $is_new ? $::locale->text('Create a new department') : $::locale->text('Edit department'));
89     return;
90   }
91   $self->{department}->save;
92
93   flash_later('info', $is_new ? $::locale->text('The department has been created.') : $::locale->text('The department has been saved.'));
94   $self->redirect_to(action => 'list');
95 }
96
97 sub load_department {
98   my ($self) = @_;
99   $self->{department} = SL::DB::Department->new(id => $::form->{id})->load;
100 }
101
102 1;