1 package SL::Controller::RequirementSpec;
5 use parent qw(SL::Controller::Base);
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;
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;
21 use Rose::Object::MakeMethods::Generic
23 scalar => [ qw(requirement_spec requirement_spec_item customers projects types statuses db_args flat_filter is_template) ],
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) ]);
31 __PACKAGE__->get_models_url_params('flat_filter');
32 __PACKAGE__->make_paginated(
33 MODEL => 'RequirementSpec',
34 PAGINATE_ARGS => 'db_args',
38 __PACKAGE__->make_sorted(
39 MODEL => 'RequirementSpec',
42 DEFAULT_BY => 'customer',
45 customer => t8('Customer'),
47 type => t8('Requirement Spec Type'),
48 status => t8('Requirement Spec Status'),
49 projectnumber => t8('Project Number'),
59 $self->setup_db_args_from_filter;
60 $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
62 $self->prepare_report;
64 my $requirement_specs = $self->get_models(%{ $self->db_args });
66 $self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
72 $self->requirement_spec(SL::DB::RequirementSpec->new);
73 $self->render('requirement_spec/new', title => t8('Create a new requirement spec'));
76 sub action_ajax_edit {
79 $self->render('requirement_spec/_form', { layout => 0 }, submit_as => 'ajax');
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);
88 $self->render('requirement_spec/show', title => t8('Show requirement spec'));
94 $self->requirement_spec(SL::DB::RequirementSpec->new);
95 $self->create_or_update;
100 $self->create_or_update;
106 if (eval { $self->requirement_spec->delete; 1; }) {
107 flash_later('info', t8('The requirement spec has been deleted.'));
109 flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
112 $self->redirect_to(action => 'list');
118 SL::DB::RequirementSpec->reorder_list(@{ $::form->{requirement_spec_id} || [] });
120 $self->render('1;', { type => 'js', inline => 1 });
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);
138 sub load_requirement_spec {
140 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec");
143 sub load_select_options {
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 ] );
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);
156 sub load_search_select_options {
159 $self->projects(SL::DB::Manager::Project->get_all_sorted);
166 sub create_or_update {
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');
172 $self->requirement_spec->assign_attributes(%{ $params });
174 my @errors = $self->requirement_spec->validate;
177 return SL::ClientJS->new->error(@errors)->render($self) if $::request->is_ajax;
179 flash('error', @errors);
180 $self->render('requirement_spec/new', title => $title);
184 $self->requirement_spec->save;
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.'))
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);
198 sub setup_db_args_from_filter {
201 $self->{filter} = {};
202 my %args = parse_filter(
204 with_objects => [ 'customer', 'type', 'status', 'project' ],
205 launder_to => $self->{filter},
210 @{ $args{where} || [] },
211 is_template => $self->is_template
214 $self->db_args(\%args);
220 my $callback = $self->get_callback;
222 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
223 $self->{report} = $report;
225 my @columns = qw(title customer status type projectnumber);
226 my @sortable = qw(title customer status type projectnumber);
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 } },
238 map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
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,
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);
256 $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;