1 package SL::Controller::RequirementSpec;
5 use parent qw(SL::Controller::Base);
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;
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;
20 use Rose::Object::MakeMethods::Generic
22 scalar => [ qw(requirement_spec requirement_spec_item customers projects types statuses db_args flat_filter is_template) ],
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) ]);
30 __PACKAGE__->get_models_url_params('flat_filter');
31 __PACKAGE__->make_paginated(
32 MODEL => 'RequirementSpec',
33 PAGINATE_ARGS => 'db_args',
37 __PACKAGE__->make_sorted(
38 MODEL => 'RequirementSpec',
41 DEFAULT_BY => 'customer',
44 customer => t8('Customer'),
46 type => t8('Requirement Spec Type'),
47 status => t8('Requirement Spec Status'),
48 projectnumber => t8('Project Number'),
58 $self->setup_db_args_from_filter;
59 $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
61 $self->prepare_report;
63 my $requirement_specs = $self->get_models(%{ $self->db_args });
65 $self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
71 $self->{requirement_spec} = SL::DB::RequirementSpec->new;
72 $self->render('requirement_spec/form', title => t8('Create a new requirement spec'));
77 $self->render('requirement_spec/form', title => t8('Edit requirement spec'));
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);
86 $self->render('requirement_spec/show', title => t8('Show requirement spec'));
92 $self->{requirement_spec} = SL::DB::RequirementSpec->new;
93 $self->create_or_update;
98 $self->create_or_update;
104 if (eval { $self->{requirement_spec}->delete; 1; }) {
105 flash_later('info', t8('The requirement spec has been deleted.'));
107 flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
110 $self->redirect_to(action => 'list');
116 SL::DB::RequirementSpec->reorder_list(@{ $::form->{requirement_spec_id} || [] });
118 $self->render('1;', { type => 'js', inline => 1 });
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);
136 sub load_requirement_spec {
138 $self->{requirement_spec} = SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec";
141 sub load_select_options {
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 ] );
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);
154 sub load_search_select_options {
157 $self->projects(SL::DB::Manager::Project->get_all_sorted);
164 sub create_or_update {
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');
170 $self->{requirement_spec}->assign_attributes(%{ $params });
172 my @errors = $self->{requirement_spec}->validate;
175 flash('error', @errors);
176 $self->render('requirement_spec/form', title => $title);
180 $self->{requirement_spec}->save;
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');
186 sub setup_db_args_from_filter {
189 $self->{filter} = {};
190 my %args = parse_filter(
192 with_objects => [ 'customer', 'type', 'status', 'project' ],
193 launder_to => $self->{filter},
198 @{ $args{where} || [] },
199 is_template => $self->is_template
202 $self->db_args(\%args);
208 my $callback = $self->get_callback;
210 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
211 $self->{report} = $report;
213 my @columns = qw(title customer status type projectnumber);
214 my @sortable = qw(title customer status type projectnumber);
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 } },
226 map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
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,
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);
244 $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;