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