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