Auftrags-Controller: Projektnummer in zweiter Zeile
[kivitendo-erp.git] / SL / Controller / RequirementSpecPredefinedText.pm
1 package SL::Controller::RequirementSpecPredefinedText;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::MoreUtils qw(none);
8
9 use SL::DB::RequirementSpecPredefinedText;
10 use SL::Helper::Flash;
11 use SL::Locale::String;
12
13 use Rose::Object::MakeMethods::Generic
14 (
15  scalar => [ qw(requirement_spec_predefined_text) ],
16 );
17
18 __PACKAGE__->run_before('check_auth');
19 __PACKAGE__->run_before('setup');
20 __PACKAGE__->run_before('load_requirement_spec_predefined_text', only => [ qw(edit update destroy) ]);
21
22 #
23 # actions
24 #
25
26 sub action_list {
27   my ($self) = @_;
28
29   $self->render('requirement_spec_predefined_text/list',
30                 title                             => t8('Pre-defined Texts'),
31                 REQUIREMENT_SPEC_PREDEFINED_TEXTS => SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted);
32 }
33
34 sub action_new {
35   my ($self) = @_;
36
37   $self->{requirement_spec_predefined_text} = SL::DB::RequirementSpecPredefinedText->new(useable_for_text_blocks => 1);
38   $self->render('requirement_spec_predefined_text/form', title => t8('Create a new predefined text'));
39 }
40
41 sub action_edit {
42   my ($self) = @_;
43   $self->render('requirement_spec_predefined_text/form', title => t8('Edit predefined text'));
44 }
45
46 sub action_create {
47   my ($self) = @_;
48
49   $self->{requirement_spec_predefined_text} = SL::DB::RequirementSpecPredefinedText->new;
50   $self->create_or_update;
51 }
52
53 sub action_update {
54   my ($self) = @_;
55   $self->create_or_update;
56 }
57
58 sub action_destroy {
59   my ($self) = @_;
60
61   if (eval { $self->{requirement_spec_predefined_text}->delete; 1; }) {
62     flash_later('info',  t8('The predefined text has been deleted.'));
63   } else {
64     flash_later('error', t8('The predefined text is in use and cannot be deleted.'));
65   }
66
67   $self->redirect_to(action => 'list');
68 }
69
70 sub action_reorder {
71   my ($self) = @_;
72
73   SL::DB::RequirementSpecPredefinedText->reorder_list(@{ $::form->{requirement_spec_predefined_text_id} || [] });
74
75   $self->render(\'', { type => 'json' });
76 }
77
78 #
79 # filters
80 #
81
82 sub check_auth {
83   $::auth->assert('config');
84 }
85
86 sub setup {
87   $::request->layout->use_javascript("${_}.js")  for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
88 }
89
90 #
91 # helpers
92 #
93
94 sub create_or_update {
95   my $self   = shift;
96   my $is_new = !$self->{requirement_spec_predefined_text}->id;
97   my $params = delete($::form->{requirement_spec_predefined_text}) || { };
98   my $title  = $is_new ? t8('Create a new predefined text') : t8('Edit predefined text');
99
100   # Force presence of booleans for the useable_* flags.
101   my @useable_flags = qw(text_blocks sections);
102   $params->{"useable_for_${_}"} = !!$params->{"useable_for_${_}"} for @useable_flags;
103
104   # Force usage for text blocks if none of the check boxes are marked.
105   $params->{useable_for_text_blocks} = 1 if none { $params->{"useable_for_${_}"} } @useable_flags;
106
107   $self->{requirement_spec_predefined_text}->assign_attributes(%{ $params });
108
109   my @errors = $self->{requirement_spec_predefined_text}->validate;
110
111   if (@errors) {
112     flash('error', @errors);
113     $self->render('requirement_spec_predefined_text/form', title => $title);
114     return;
115   }
116
117   $self->{requirement_spec_predefined_text}->save;
118
119   flash_later('info', $is_new ? t8('The predefined text has been created.') : t8('The predefined text has been saved.'));
120   $self->redirect_to(action => 'list');
121 }
122
123 sub load_requirement_spec_predefined_text {
124   my ($self) = @_;
125   $self->{requirement_spec_predefined_text} = SL::DB::RequirementSpecPredefinedText->new(id => $::form->{id})->load;
126 }
127
128 1;