1 package SL::Controller::RequirementSpec;
6 use parent qw(SL::Controller::Base);
12 use SL::Controller::Helper::GetModels;
13 use SL::Controller::Helper::ReportGenerator;
14 use SL::Controller::Helper::RequirementSpec;
17 use SL::DB::ProjectStatus;
18 use SL::DB::ProjectType;
19 use SL::DB::RequirementSpecComplexity;
20 use SL::DB::RequirementSpecRisk;
21 use SL::DB::RequirementSpecStatus;
22 use SL::DB::RequirementSpecType;
23 use SL::DB::RequirementSpec;
24 use SL::Helper::Flash;
25 use SL::Locale::String;
26 use SL::Template::LaTeX;
28 use Rose::Object::MakeMethods::Generic
30 scalar => [ qw(requirement_spec_item visible_item visible_section) ],
31 'scalar --get_set_init' => [ qw(requirement_spec customers types statuses complexities risks projects project_types project_statuses default_project_type default_project_status copy_source js
32 current_text_block_output_position models time_based_units) ],
35 __PACKAGE__->run_before('setup');
36 __PACKAGE__->run_before('set_default_filter_args', only => [ qw(list) ]);
39 customer => t8('Customer'),
41 type => t8('Requirement Spec Type'),
42 status => t8('Requirement Spec Status'),
43 projectnumber => t8('Project Number'),
44 version => t8('Version'),
45 mtime => t8('Last modification'),
56 $self->prepare_report;
57 $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
63 $self->requirement_spec(SL::DB::RequirementSpec->new(is_template => $::form->{is_template}));
65 if ($self->copy_source) {
66 $self->requirement_spec->$_($self->copy_source->$_) for qw(type_id status_id customer_id title hourly_rate is_template)
69 $self->render('requirement_spec/new', title => $self->requirement_spec->is_template ? t8('Create a new requirement spec template') : t8('Create a new requirement spec'));
72 sub action_ajax_show_basic_settings {
75 $self->render('requirement_spec/_show_basic_settings', { layout => 0 });
78 sub action_ajax_edit {
81 my $html = $self->render('requirement_spec/_form', { output => 0 }, submit_as => 'ajax');
84 ->hide('#basic_settings')
85 ->after('#basic_settings', $html)
89 sub action_ajax_edit_project_link {
92 my $html = $self->render('requirement_spec/_project_link_form', { output => 0 }, submit_as => 'ajax');
95 ->hide('#basic_settings')
96 ->after('#basic_settings', $html)
100 sub action_ajax_show_time_and_cost_estimate {
103 $self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
106 sub action_ajax_edit_time_and_cost_estimate {
109 my $html = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
110 my $first = ($self->requirement_spec->sections_sorted || [])->[0];
111 $first = ($first->children_sorted || [])->[0] if $first;
114 ->hide('#time_cost_estimate')
115 ->after('#time_cost_estimate', $html)
116 ->on('#time_cost_estimate INPUT[type=text]', 'keydown', 'kivi.requirement_spec.time_cost_estimate_input_key_down')
117 ->action_if($first && $first->id, 'focus', '#time_and_cost_estimate_form_complexity_id_' . $first->id)
121 sub action_ajax_save_time_and_cost_estimate {
124 $self->requirement_spec->db->do_transaction(sub {
127 foreach my $attributes (@{ $::form->{requirement_spec_items} || [] }) {
128 SL::DB::RequirementSpecItem
129 ->new(id => delete $attributes->{id})
131 ->update_attributes(%{ $attributes });
137 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load);
139 my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 }, initially_hidden => !!$::form->{keep_open});
140 $self->js->replaceWith('#time_cost_estimate', $html);
142 return $self->js->render($self) if $::form->{keep_open};
144 $self->js->remove('#time_cost_estimate_form_container');
146 if ($self->visible_section) {
147 $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
148 $self->js->html('#column-content', $html);
151 $self->js->render($self);
157 my $title = $self->requirement_spec->is_template ? t8('Show requirement spec template') : t8('Show requirement spec');
158 my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections_sorted }[0];
159 $self->requirement_spec_item($item);
161 $self->render('requirement_spec/show', title => $title);
167 $self->requirement_spec(SL::DB::RequirementSpec->new);
168 $self->create_or_update;
173 $self->create_or_update;
176 sub action_update_project_link {
178 my $action = delete($::form->{project_link_action}) || 'keep';
180 return $self->update_project_link_none_keep_existing($action) if $action =~ m{none|keep|existing};
181 return $self->update_project_link_new($action) if $action eq 'new';
182 return $self->update_project_link_create($action) if $action eq 'create';
184 die "Unknown project link action '$action'";
190 if (eval { $self->requirement_spec->delete; 1; }) {
191 flash_later('info', t8('The requirement spec has been deleted.'));
193 flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
196 $self->redirect_to(action => 'list');
199 sub action_revert_to {
200 my ($self, %params) = @_;
202 return $self->js->error(t8('Cannot revert a versioned copy.'))->render($self) if $self->requirement_spec->working_copy_id;
204 my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
206 $self->requirement_spec->copy_from($versioned_copy);
207 my $version = $versioned_copy->versions->[0];
208 $version->update_attributes(working_copy_id => $self->requirement_spec->id);
210 flash_later('info', t8('The requirement spec has been reverted to version #1.', $self->requirement_spec->version->version_number));
211 $self->js->redirect_to($self->url_for(action => 'show', id => $self->requirement_spec->id))->render($self);
214 sub action_create_pdf {
215 my ($self, %params) = @_;
217 my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec';
218 my @pictures = $self->prepare_pictures_for_printing;
219 my %result = SL::Template::LaTeX->parse_and_create_pdf("${base_name}.tex", SELF => $self, rspec => $self->requirement_spec);
221 unlink @pictures unless ($::lx_office_conf{debug} || {})->{keep_temp_files};
223 $::form->error(t8('Conversion to PDF failed: #1', $result{error})) if $result{error};
225 my $attachment_name = $self->requirement_spec->type->description . ' ' . ($self->requirement_spec->working_copy_id || $self->requirement_spec->id);
226 $attachment_name .= ' (v' . $self->requirement_spec->version->version_number . ')' if $self->requirement_spec->version;
227 $attachment_name .= '.pdf';
228 $attachment_name =~ s/[^\wäöüÄÖÜß \-\+\(\)\[\]\{\}\.,]+/_/g;
230 $self->send_file($result{file_name}, type => 'application/pdf', name => $attachment_name);
231 unlink $result{file_name};
234 sub action_select_template_to_paste {
237 my @templates = @{ SL::DB::Manager::RequirementSpec->get_all(
238 where => [ is_template => 1, SL::DB::Manager::RequirementSpec->not_empty_filter ],
239 sort_by => 'lower(requirement_specs.title)',
241 $self->render('requirement_spec/select_template_to_paste', { layout => 0 }, TEMPLATES => \@templates);
244 sub action_paste_template {
245 my ($self, %params) = @_;
247 my $template = SL::DB::RequirementSpec->new(id => $::form->{template_id})->load;
248 my %result = $self->requirement_spec->paste_template($template);
250 return $self->js->error($self->requirement_spec->error)->render($self) if !%result;
252 $self->render_pasted_text_block($_) for sort { $a->position <=> $b->position } @{ $result{text_blocks} };
253 $self->render_pasted_section($_) for sort { $a->position <=> $b->position } @{ $result{sections} };
255 if (@{ $result{sections} } && (($::form->{current_content_type} || 'sections') eq 'sections') && !$::form->{current_content_id}) {
256 $self->render_first_pasted_section_as_list($result{sections}->[0]);
259 my $parts_list = $self->render('requirement_spec_part/show', { output => 0 });
261 ->replaceWith('#additional_parts_list_container', $parts_list)
262 ->show( '#additional_parts_list_container')
263 ->remove( '#additional_parts_form_container');
265 $self->invalidate_version->render($self);
268 sub action_renumber_sections {
271 my %numbers = map { ($_ => 1) } qw(section function_block);
272 my %formats = map { my $method = "${_}_number_format"; ($_ => $self->requirement_spec->type->$method) } qw(section function_block);
273 my @items = @{ $self->requirement_spec->sections_sorted };
275 $self->requirement_spec->db->with_transaction(sub {
277 my $item = shift @items;
278 my $type = $item->parent_id ? 'function_block' : 'section';
280 $item->update_attributes(fb_number => SL::PrefixedNumber->new(number => $formats{$type} || 0)->set_to($numbers{$type}));
284 unshift @items, @{ $item->children_sorted };
287 $self->requirement_spec->invalidate_version unless $self->requirement_spec->is_template;
292 $self->redirect_to(action => 'show', id => $self->requirement_spec->id);
302 $::auth->assert('requirement_spec_edit');
303 $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
304 $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu jquery/jquery.hotkeys requirement_spec ckeditor/ckeditor ckeditor/adapters/jquery autocomplete_part);
305 $self->init_visible_section;
310 sub init_js { SL::ClientJS->new }
311 sub init_complexities { SL::DB::Manager::RequirementSpecComplexity->get_all_sorted }
312 sub init_default_project_status { SL::DB::Manager::ProjectStatus->find_by(name => 'planning') }
313 sub init_default_project_type { SL::DB::ProjectType->new(id => 1)->load }
314 sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
315 sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
316 sub init_projects { SL::DB::Manager::Project->get_all_sorted }
317 sub init_risks { SL::DB::Manager::RequirementSpecRisk->get_all_sorted }
318 sub init_statuses { SL::DB::Manager::RequirementSpecStatus->get_all_sorted }
319 sub init_time_based_units { SL::DB::Manager::Unit->time_based_units }
320 sub init_types { SL::DB::Manager::RequirementSpecType->get_all_sorted }
325 my @filter = ('!obsolete' => 1);
326 @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
328 return SL::DB::Manager::Customer->get_all_sorted(where => \@filter);
331 sub init_requirement_spec {
333 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec") if $::form->{id};
336 sub init_copy_source {
338 $self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
341 sub init_current_text_block_output_position {
343 $self->current_text_block_output_position($::form->{current_content_type} !~ m/^(?:text-blocks|tb)-(front|back)/ ? -1 : $1 eq 'front' ? 0 : 1);
350 sub create_or_update {
352 my $is_new = !$self->requirement_spec->id;
353 my $params = delete($::form->{requirement_spec}) || { };
355 $self->requirement_spec->assign_attributes(%{ $params });
357 my $title = $is_new && $self->requirement_spec->is_template ? t8('Create a new requirement spec template')
358 : $is_new ? t8('Create a new requirement spec')
359 : $self->requirement_spec->is_template ? t8('Edit requirement spec template')
360 : t8('Edit requirement spec');
362 my @errors = $self->requirement_spec->validate;
365 return $self->js->error(@errors)->render($self) if $::request->is_ajax;
367 flash('error', @errors);
368 $self->render('requirement_spec/new', title => $title);
372 my $db = $self->requirement_spec->db;
373 if (!$db->do_transaction(sub {
374 if ($self->copy_source) {
375 $self->requirement_spec($self->copy_source->create_copy(%{ $params }));
377 $self->requirement_spec->save;
380 $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
381 @errors = ($::locale->text('Saving failed. Error message from the database: #1', $db->error));
382 return $self->js->error(@errors)->render($self) if $::request->is_ajax;
384 $self->requirement_spec->id(undef) if $is_new;
385 flash('error', @errors);
386 return $self->render('requirement_spec/new', title => $title);
389 my $info = $self->requirement_spec->is_template ? t8('The requirement spec template has been saved.') : t8('The requirement spec has been saved.');
391 if ($::request->is_ajax) {
392 my $header_html = $self->render('requirement_spec/_header', { output => 0 });
393 my $basics_html = $self->render('requirement_spec/_show_basic_settings', { output => 0 });
394 return $self->invalidate_version
395 ->replaceWith('#requirement-spec-header', $header_html)
396 ->replaceWith('#basic_settings', $basics_html)
397 ->remove('#basic_settings_form')
398 ->flash('info', $info)
402 flash_later('info', $info);
403 $self->redirect_to(action => 'show', id => $self->requirement_spec->id);
409 my $is_template = $::form->{is_template};
410 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
412 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
413 $self->models->finalize; # for filter laundering
414 my $callback = $self->models->get_callback;
416 $self->{report} = $report;
418 my @columns = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime version);
419 my @sortable = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime);
422 title => { obj_link => sub { $self->url_for(action => 'show', id => $_[0]->id, callback => $callback) } },
423 mtime => { sub => sub { ($_[0]->mtime || $_[0]->itime)->to_kivitendo(precision => 'minute') } },
429 customer => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
430 sub => sub { $_[0]->customer->name } },
431 projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
432 sub => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
433 status => { sub => sub { $_[0]->status->description } },
434 type => { sub => sub { $_[0]->type->description } },
435 version => { sub => sub { $_[0]->version ? $_[0]->version->version_number : t8('Working copy without version') } },
439 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
441 $report->set_options(
442 std_column_visibility => 1,
443 controller_class => 'RequirementSpec',
444 output_format => 'HTML',
445 raw_top_info_text => $self->render('requirement_spec/report_top', { output => 0 }, is_template => $is_template),
446 raw_bottom_info_text => $self->render('requirement_spec/report_bottom', { output => 0 }, models => $self->models),
447 title => $is_template ? t8('Requirement Spec Templates') : t8('Requirement Specs'),
448 allow_pdf_export => 1,
449 allow_csv_export => 1,
451 $report->set_columns(%column_defs);
452 $report->set_column_order(@columns);
453 $report->set_export_options(qw(list filter));
454 $report->set_options_from_form;
455 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
458 sub invalidate_version {
461 my $rspec = SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load;
462 return $self->js if $rspec->is_template;
464 $rspec->invalidate_version;
466 my $html = $self->render('requirement_spec/_version', { output => 0 }, requirement_spec => $rspec);
467 return $self->js->html('#requirement_spec_version', $html);
470 sub render_pasted_text_block {
471 my ($self, $text_block, %params) = @_;
473 if ($self->current_text_block_output_position == $text_block->output_position) {
474 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $text_block);
476 ->appendTo($html, '#text-block-list')
477 ->hide('#text-block-list-empty');
480 my $node = $self->presenter->requirement_spec_text_block_jstree_data($text_block);
481 my $front_back = $text_block->output_position == 0 ? 'front' : 'back';
483 ->jstree->create_node('#tree', "#tb-${front_back}", 'last', $node)
484 ->jstree->open_node( '#tree', "#tb-${front_back}");
487 sub set_default_filter_args {
490 if (!$::form->{filter} && !$::form->{is_template}) {
491 $::form->{filter} = {
492 status_id => [ map { $_->{id} } grep { $_->name ne 'done' } @{ $self->statuses } ],
499 sub render_pasted_section {
500 my ($self, $item, $parent_id) = @_;
502 my $node = $self->presenter->requirement_spec_item_jstree_data($item);
504 ->jstree->create_node('#tree', $parent_id ? "#fb-${parent_id}" : '#sections', 'last', $node)
505 ->jstree->open_node( '#tree', $parent_id ? "#fb-${parent_id}" : '#sections');
507 $self->render_pasted_section($_, $item->id) for @{ $item->children_sorted };
510 sub render_first_pasted_section_as_list {
511 my ($self, $section, %params) = @_;
513 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $section);
515 ->html('#column-content', $html)
516 ->val( '#current_content_type', $section->item_type)
517 ->val( '#current_content_id', $section->id)
518 ->jstree->select_node('#tree', '#fb-' . $section->id);
521 sub prepare_pictures_for_printing {
525 my $userspath = File::Spec->rel2abs($::lx_office_conf{paths}->{userspath});
526 my $target = "${userspath}/kivitendo-print-requirement-spec-picture-" . Common::unique_id() . '-';
528 foreach my $picture (map { @{ $_->pictures } } @{ $self->requirement_spec->text_blocks }) {
529 my $output_file_name = $target . $picture->id . '.' . $picture->get_default_file_name_extension;
530 $picture->{print_file_name} = File::Spec->abs2rel($output_file_name, $userspath);
531 my $out = IO::File->new($output_file_name, 'w') || die("Could not create file " . $output_file_name);
533 $out->print($picture->picture_content);
536 push @files, $output_file_name;
542 sub update_project_link_none_keep_existing {
543 my ($self, $action) = @_;
545 $self->requirement_spec->update_attributes(project_id => undef) if $action eq 'none';
546 $self->requirement_spec->update_attributes(project_id => $::form->{new_project_id}) if $action eq 'existing';
548 return $self->invalidate_version
549 ->replaceWith('#basic_settings', $self->render('requirement_spec/_show_basic_settings', { output => 0 }))
550 ->remove('#project_link_form')
551 ->flash('info', t8('The project link has been updated.'))
555 sub update_project_link_new {
559 ->replaceWith('#project_link_form', $self->render('requirement_spec/_new_project_form', { output => 0 }))
563 sub update_project_link_create {
565 my $params = delete($::form->{project}) || {};
566 my $project = SL::DB::Project->new(
572 my @errors = $project->validate;
574 return $self->js->error(@errors)->render($self) if @errors;
576 my $db = $self->requirement_spec->db;
577 if (!$db->do_transaction(sub {
579 $self->requirement_spec->update_attributes(project_id => $project->id);
582 $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
583 return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render($self);
586 return $self->invalidate_version
587 ->replaceWith('#basic_settings', $self->render('requirement_spec/_show_basic_settings', { output => 0 }))
588 ->remove('#project_link_form')
589 ->flash('info', t8('The project has been created.'))
590 ->flash('info', t8('The project link has been updated.'))
597 SL::Controller::Helper::GetModels->new(
608 working_copy_id => undef,
609 is_template => $::form->{is_template} ? 1 : 0,
612 with_objects => [ 'customer', 'type', 'status', 'project' ],