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