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