Textblöcke bearbeiten, verschieben: verschiedene Fixes
[kivitendo-erp.git] / SL / Controller / RequirementSpec.pm
1 package SL::Controller::RequirementSpec;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Controller::Helper::GetModels;
8 use SL::Controller::Helper::Paginated;
9 use SL::Controller::Helper::Sorted;
10 use SL::Controller::Helper::ParseFilter;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::DB::Customer;
13 use SL::DB::Project;
14 use SL::DB::RequirementSpecStatus;
15 use SL::DB::RequirementSpecType;
16 use SL::DB::RequirementSpec;
17 use SL::Helper::Flash;
18 use SL::Locale::String;
19
20 use Rose::Object::MakeMethods::Generic
21 (
22  scalar => [ qw(requirement_spec requirement_spec_item customers projects types statuses db_args flat_filter is_template) ],
23 );
24
25 __PACKAGE__->run_before('setup');
26 __PACKAGE__->run_before('load_requirement_spec',      only => [ qw(    edit        update show destroy) ]);
27 __PACKAGE__->run_before('load_select_options',        only => [ qw(new edit create update list) ]);
28 __PACKAGE__->run_before('load_search_select_options', only => [ qw(                       list) ]);
29
30 __PACKAGE__->get_models_url_params('flat_filter');
31 __PACKAGE__->make_paginated(
32   MODEL         => 'RequirementSpec',
33   PAGINATE_ARGS => 'db_args',
34   ONLY          => [ qw(list) ],
35 );
36
37 __PACKAGE__->make_sorted(
38   MODEL         => 'RequirementSpec',
39   ONLY          => [ qw(list) ],
40
41   DEFAULT_BY    => 'customer',
42   DEFAULT_DIR   => 1,
43
44   customer      => t8('Customer'),
45   title         => t8('Title'),
46   type          => t8('Requirement Spec Type'),
47   status        => t8('Requirement Spec Status'),
48   projectnumber => t8('Project Number'),
49 );
50
51 #
52 # actions
53 #
54
55 sub action_list {
56   my ($self) = @_;
57
58   $self->setup_db_args_from_filter;
59   $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
60
61   $self->prepare_report;
62
63   my $requirement_specs = $self->get_models(%{ $self->db_args });
64
65   $self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
66 }
67
68 sub action_new {
69   my ($self) = @_;
70
71   $self->{requirement_spec} = SL::DB::RequirementSpec->new;
72   $self->render('requirement_spec/form', title => t8('Create a new requirement spec'));
73 }
74
75 sub action_edit {
76   my ($self) = @_;
77   $self->render('requirement_spec/form', title => t8('Edit requirement spec'));
78 }
79
80 sub action_show {
81   my ($self) = @_;
82
83   my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections }[0];
84   $self->requirement_spec_item($item);
85
86   $self->render('requirement_spec/show', title => t8('Show requirement spec'));
87 }
88
89 sub action_create {
90   my ($self) = @_;
91
92   $self->{requirement_spec} = SL::DB::RequirementSpec->new;
93   $self->create_or_update;
94 }
95
96 sub action_update {
97   my ($self) = @_;
98   $self->create_or_update;
99 }
100
101 sub action_destroy {
102   my ($self) = @_;
103
104   if (eval { $self->{requirement_spec}->delete; 1; }) {
105     flash_later('info',  t8('The requirement spec has been deleted.'));
106   } else {
107     flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
108   }
109
110   $self->redirect_to(action => 'list');
111 }
112
113 sub action_reorder {
114   my ($self) = @_;
115
116   SL::DB::RequirementSpec->reorder_list(@{ $::form->{requirement_spec_id} || [] });
117
118   $self->render('1;', { type => 'js', inline => 1 });
119 }
120
121 #
122 # filters
123 #
124
125 sub setup {
126   my ($self) = @_;
127
128   $::auth->assert('config');
129   $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
130   $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
131   $self->is_template($::form->{is_template} ? 1 : 0);
132
133   return 1;
134 }
135
136 sub load_requirement_spec {
137   my ($self) = @_;
138   $self->{requirement_spec} = SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec";
139 }
140
141 sub load_select_options {
142   my ($self) = @_;
143
144   my @filter = ('!obsolete' => 1);
145   if ($self->requirement_spec && $self->requirement_spec->customer_id) {
146     @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] );
147   }
148
149   $self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
150   $self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
151   $self->types(    SL::DB::Manager::RequirementSpecType->get_all_sorted);
152 }
153
154 sub load_search_select_options {
155   my ($self) = @_;
156
157   $self->projects(SL::DB::Manager::Project->get_all_sorted);
158 }
159
160 #
161 # helpers
162 #
163
164 sub create_or_update {
165   my $self   = shift;
166   my $is_new = !$self->{requirement_spec}->id;
167   my $params = delete($::form->{requirement_spec}) || { };
168   my $title  = $is_new ? t8('Create a new requirement spec') : t8('Edit requirement spec');
169
170   $self->{requirement_spec}->assign_attributes(%{ $params });
171
172   my @errors = $self->{requirement_spec}->validate;
173
174   if (@errors) {
175     flash('error', @errors);
176     $self->render('requirement_spec/form', title => $title);
177     return;
178   }
179
180   $self->{requirement_spec}->save;
181
182   flash_later('info', $is_new ? t8('The requirement spec has been created.') : t8('The requirement spec has been saved.'));
183   $self->redirect_to(action => 'list');
184 }
185
186 sub setup_db_args_from_filter {
187   my ($self) = @_;
188
189   $self->{filter} = {};
190   my %args = parse_filter(
191     $::form->{filter},
192     with_objects => [ 'customer', 'type', 'status', 'project' ],
193     launder_to   => $self->{filter},
194   );
195
196   $args{where} = [
197     and => [
198       @{ $args{where} || [] },
199       is_template => $self->is_template
200     ]];
201
202   $self->db_args(\%args);
203 }
204
205 sub prepare_report {
206   my ($self)      = @_;
207
208   my $callback    = $self->get_callback;
209
210   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
211   $self->{report} = $report;
212
213   my @columns     = qw(title customer status type projectnumber);
214   my @sortable    = qw(title customer status type projectnumber);
215
216   my %column_defs = (
217     title         => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
218     customer      => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
219                        sub      => sub { $_[0]->customer->name } },
220     projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
221                        sub      => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
222     status        => { sub      => sub { $_[0]->status->description } },
223     type          => { sub      => sub { $_[0]->type->description } },
224   );
225
226   map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
227
228   $report->set_options(
229     std_column_visibility => 1,
230     controller_class      => 'RequirementSpec',
231     output_format         => 'HTML',
232     raw_top_info_text     => $self->render('requirement_spec/report_top',    { output => 0 }),
233     raw_bottom_info_text  => $self->render('requirement_spec/report_bottom', { output => 0 }),
234     title                 => $::locale->text('Requirement Specs'),
235     allow_pdf_export      => 1,
236     allow_csv_export      => 1,
237   );
238   $report->set_columns(%column_defs);
239   $report->set_column_order(@columns);
240   $report->set_export_options(qw(list filter));
241   $report->set_options_from_form;
242   $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
243
244   $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
245 }
246
247 1;