1 package SL::Controller::RequirementSpec;
6 use parent qw(SL::Controller::Base);
12 use SL::Controller::Helper::GetModels;
13 use SL::Controller::Helper::Filtered;
14 use SL::Controller::Helper::Paginated;
15 use SL::Controller::Helper::Sorted;
16 use SL::Controller::Helper::ParseFilter;
17 use SL::Controller::Helper::ReportGenerator;
18 use SL::Controller::Helper::RequirementSpec;
21 use SL::DB::ProjectStatus;
22 use SL::DB::ProjectType;
23 use SL::DB::RequirementSpecComplexity;
24 use SL::DB::RequirementSpecRisk;
25 use SL::DB::RequirementSpecStatus;
26 use SL::DB::RequirementSpecType;
27 use SL::DB::RequirementSpec;
28 use SL::Helper::Flash;
29 use SL::Locale::String;
30 use SL::Template::LaTeX;
32 use Rose::Object::MakeMethods::Generic
34 scalar => [ qw(requirement_spec_item visible_item visible_section) ],
35 '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
36 current_text_block_output_position) ],
39 __PACKAGE__->run_before('setup');
41 __PACKAGE__->make_filtered(
42 MODEL => 'RequirementSpec',
43 LAUNDER_TO => 'filter'
45 __PACKAGE__->make_paginated(
46 MODEL => 'RequirementSpec',
49 __PACKAGE__->make_sorted(
50 MODEL => 'RequirementSpec',
53 DEFAULT_BY => 'customer',
56 customer => t8('Customer'),
58 type => t8('Requirement Spec Type'),
59 status => t8('Requirement Spec Status'),
60 projectnumber => t8('Project Number'),
61 version => t8('Version'),
62 mtime => t8('Last modification'),
73 my $requirement_specs = $self->get_models(
76 working_copy_id => undef,
77 is_template => $::form->{is_template} ? 1 : 0,
79 with_objects => [ 'customer', 'type', 'status', 'project' ],
82 $self->prepare_report;
83 $self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
89 $self->requirement_spec(SL::DB::RequirementSpec->new(is_template => $::form->{is_template}));
91 if ($self->copy_source) {
92 $self->requirement_spec->$_($self->copy_source->$_) for qw(type_id status_id customer_id title hourly_rate)
95 $self->render('requirement_spec/new', title => $self->requirement_spec->is_template ? t8('Create a new requirement spec template') : t8('Create a new requirement spec'));
98 sub action_ajax_show_basic_settings {
101 $self->render('requirement_spec/_show_basic_settings', { layout => 0 });
104 sub action_ajax_edit {
107 my $html = $self->render('requirement_spec/_form', { output => 0 }, submit_as => 'ajax');
110 ->hide('#basic_settings')
111 ->after('#basic_settings', $html)
115 sub action_ajax_edit_project_link {
118 my $html = $self->render('requirement_spec/_project_link_form', { output => 0 }, submit_as => 'ajax');
121 ->hide('#basic_settings')
122 ->after('#basic_settings', $html)
126 sub action_ajax_show_time_and_cost_estimate {
129 $self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
132 sub action_ajax_edit_time_and_cost_estimate {
135 my $html = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
136 my $first = ($self->requirement_spec->sections_sorted || [])->[0];
137 $first = ($first->children_sorted || [])->[0] if $first;
140 ->hide('#time_cost_estimate')
141 ->after('#time_cost_estimate', $html)
142 ->on('#time_cost_estimate INPUT[type=text]', 'keydown', 'kivi.requirement_spec.time_cost_estimate_input_key_down')
143 ->action_if($first && $first->id, 'focus', '#time_and_cost_estimate_form_complexity_id_' . $first->id)
147 sub action_ajax_save_time_and_cost_estimate {
150 $self->requirement_spec->db->do_transaction(sub {
153 foreach my $attributes (@{ $::form->{requirement_spec_items} || [] }) {
154 SL::DB::RequirementSpecItem
155 ->new(id => delete $attributes->{id})
157 ->update_attributes(%{ $attributes });
163 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load);
165 my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 }, initially_hidden => !!$::form->{keep_open});
166 $self->js->replaceWith('#time_cost_estimate', $html);
168 return $self->js->render($self) if $::form->{keep_open};
170 $self->js->remove('#time_cost_estimate_form_container');
172 if ($self->visible_section) {
173 $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
174 $self->js->html('#column-content', $html);
177 $self->js->render($self);
183 my $title = $self->requirement_spec->is_template ? t8('Show requirement spec template') : t8('Show requirement spec');
184 my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections_sorted }[0];
185 $self->requirement_spec_item($item);
187 $self->render('requirement_spec/show', title => $title);
193 $self->requirement_spec(SL::DB::RequirementSpec->new);
194 $self->create_or_update;
199 $self->create_or_update;
202 sub action_update_project_link {
204 my $action = delete($::form->{project_link_action}) || 'keep';
206 return $self->update_project_link_none_keep_existing($action) if $action =~ m{none|keep|existing};
207 return $self->update_project_link_new($action) if $action eq 'new';
208 return $self->update_project_link_create($action) if $action eq 'create';
210 die "Unknown project link action '$action'";
216 if (eval { $self->requirement_spec->delete; 1; }) {
217 flash_later('info', t8('The requirement spec has been deleted.'));
219 flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
222 $self->redirect_to(action => 'list');
225 sub action_revert_to {
226 my ($self, %params) = @_;
228 return $self->js->error(t8('Cannot revert a versioned copy.'))->render($self) if $self->requirement_spec->working_copy_id;
230 my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
232 $self->requirement_spec->copy_from($versioned_copy);
233 my $version = $versioned_copy->versions->[0];
234 $version->update_attributes(working_copy_id => $self->requirement_spec->id);
236 flash_later('info', t8('The requirement spec has been reverted to version #1.', $self->requirement_spec->version->version_number));
237 $self->js->redirect_to($self->url_for(action => 'show', id => $self->requirement_spec->id))->render($self);
240 sub action_create_pdf {
241 my ($self, %params) = @_;
243 my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec';
244 my @pictures = $self->prepare_pictures_for_printing;
245 my %result = SL::Template::LaTeX->parse_and_create_pdf("${base_name}.tex", SELF => $self, rspec => $self->requirement_spec);
247 unlink @pictures unless ($::lx_office_conf{debug} || {})->{keep_temp_files};
249 $::form->error(t8('Conversion to PDF failed: #1', $result{error})) if $result{error};
251 my $attachment_name = $self->requirement_spec->type->description . ' ' . ($self->requirement_spec->working_copy_id || $self->requirement_spec->id);
252 $attachment_name .= ' (v' . $self->requirement_spec->version->version_number . ')' if $self->requirement_spec->version;
253 $attachment_name .= '.pdf';
254 $attachment_name =~ s/[^\wäöüÄÖÜß \-\+\(\)\[\]\{\}\.,]+/_/g;
256 $self->send_file($result{file_name}, type => 'application/pdf', name => $attachment_name);
257 unlink $result{file_name};
260 sub action_select_template_to_paste {
263 my @templates = grep { @{ $_->sections } || @{ $_->text_blocks } } @{ SL::DB::Manager::RequirementSpec->get_all(where => [ is_template => 1 ], sort_by => 'lower(title)') };
264 $self->render('requirement_spec/select_template_to_paste', { layout => 0 }, TEMPLATES => \@templates);
267 sub action_paste_template {
268 my ($self, %params) = @_;
270 my $template = SL::DB::RequirementSpec->new(id => $::form->{template_id})->load;
271 my %result = $self->requirement_spec->paste_template($template);
273 return $self->js->error($self->requirement_spec->error)->render($self) if !%result;
275 $self->render_pasted_text_block($_) for sort { $a->position <=> $b->position } @{ $result{text_blocks} };
276 $self->render_pasted_section($_) for sort { $a->position <=> $b->position } @{ $result{sections} };
278 if (@{ $result{sections} } && (($::form->{current_content_type} || 'sections') eq 'sections') && !$::form->{current_content_id}) {
279 $self->render_first_pasted_section_as_list($result{sections}->[0]);
282 $self->invalidate_version->render($self);
292 $::auth->assert('requirement_spec_edit');
293 $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
294 $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu jquery/jquery.hotkeys requirement_spec ckeditor/ckeditor ckeditor/adapters/jquery);
295 $self->init_visible_section;
300 sub init_js { SL::ClientJS->new }
301 sub init_complexities { SL::DB::Manager::RequirementSpecComplexity->get_all_sorted }
302 sub init_default_project_status { SL::DB::Manager::ProjectStatus->find_by(name => 'planning') }
303 sub init_default_project_type { SL::DB::ProjectType->new(id => 1)->load }
304 sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
305 sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
306 sub init_projects { SL::DB::Manager::Project->get_all_sorted }
307 sub init_risks { SL::DB::Manager::RequirementSpecRisk->get_all_sorted }
308 sub init_statuses { SL::DB::Manager::RequirementSpecStatus->get_all_sorted }
309 sub init_types { SL::DB::Manager::RequirementSpecType->get_all_sorted }
314 my @filter = ('!obsolete' => 1);
315 @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
317 return SL::DB::Manager::Customer->get_all_sorted(where => \@filter);
320 sub init_requirement_spec {
322 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec") if $::form->{id};
325 sub init_copy_source {
327 $self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
330 sub init_current_text_block_output_position {
332 $self->current_text_block_output_position($::form->{current_content_type} !~ m/^(?:text-blocks|tb)-(front|back)/ ? -1 : $1 eq 'front' ? 0 : 1);
339 sub create_or_update {
341 my $is_new = !$self->requirement_spec->id;
342 my $params = delete($::form->{requirement_spec}) || { };
344 $self->requirement_spec->assign_attributes(%{ $params });
346 my $title = $is_new && $self->requirement_spec->is_template ? t8('Create a new requirement spec template')
347 : $is_new ? t8('Create a new requirement spec')
348 : $self->requirement_spec->is_template ? t8('Edit requirement spec template')
349 : t8('Edit requirement spec');
351 my @errors = $self->requirement_spec->validate;
354 return $self->js->error(@errors)->render($self) if $::request->is_ajax;
356 flash('error', @errors);
357 $self->render('requirement_spec/new', title => $title);
361 my $db = $self->requirement_spec->db;
362 if (!$db->do_transaction(sub {
363 if ($self->copy_source) {
364 $self->requirement_spec($self->copy_source->create_copy(%{ $params }));
366 $self->requirement_spec->save;
369 $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
370 @errors = ($::locale->text('Saving failed. Error message from the database: #1', $db->error));
371 return $self->js->error(@errors)->render($self) if $::request->is_ajax;
373 $self->requirement_spec->id(undef) if $is_new;
374 flash('error', @errors);
375 return $self->render('requirement_spec/new', title => $title);
378 my $info = $self->requirement_spec->is_template ? t8('The requirement spec template has been saved.') : t8('The requirement spec has been saved.');
380 if ($::request->is_ajax) {
381 my $header_html = $self->render('requirement_spec/_header', { output => 0 });
382 my $basics_html = $self->render('requirement_spec/_show_basic_settings', { output => 0 });
383 return $self->invalidate_version
384 ->replaceWith('#requirement-spec-header', $header_html)
385 ->replaceWith('#basic_settings', $basics_html)
386 ->remove('#basic_settings_form')
387 ->flash('info', $info)
391 flash_later('info', $info);
392 $self->redirect_to(action => 'show', id => $self->requirement_spec->id);
398 my $callback = $self->get_callback;
400 my $is_template = $::form->{is_template};
401 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
402 $self->{report} = $report;
404 my @columns = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime version);
405 my @sortable = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime);
408 title => { obj_link => sub { $self->url_for(action => 'show', id => $_[0]->id, callback => $callback) } },
409 mtime => { sub => sub { ($_[0]->mtime || $_[0]->itime)->to_kivitendo(precision => 'minute') } },
415 customer => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
416 sub => sub { $_[0]->customer->name } },
417 projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
418 sub => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
419 status => { sub => sub { $_[0]->status->description } },
420 type => { sub => sub { $_[0]->type->description } },
421 version => { sub => sub { $_[0]->version ? $_[0]->version->version_number : t8('Working copy without version') } },
425 map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
427 $report->set_options(
428 std_column_visibility => 1,
429 controller_class => 'RequirementSpec',
430 output_format => 'HTML',
431 raw_top_info_text => $self->render('requirement_spec/report_top', { output => 0 }, is_template => $is_template),
432 raw_bottom_info_text => $self->render('requirement_spec/report_bottom', { output => 0 }),
433 title => $is_template ? t8('Requirement Spec Templates') : t8('Requirement Specs'),
434 allow_pdf_export => 1,
435 allow_csv_export => 1,
437 $report->set_columns(%column_defs);
438 $report->set_column_order(@columns);
439 $report->set_export_options(qw(list filter));
440 $report->set_options_from_form;
441 $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
443 $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
446 sub invalidate_version {
449 my $rspec = SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load;
450 return $self->js if $rspec->is_template;
452 $rspec->invalidate_version;
454 my $html = $self->render('requirement_spec/_version', { output => 0 }, requirement_spec => $rspec);
455 return $self->js->html('#requirement_spec_version', $html);
458 sub render_pasted_text_block {
459 my ($self, $text_block, %params) = @_;
461 if ($self->current_text_block_output_position == $text_block->output_position) {
462 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $text_block);
464 ->appendTo($html, '#text-block-list')
465 ->hide('#text-block-list-empty');
468 my $node = $self->presenter->requirement_spec_text_block_jstree_data($text_block);
469 my $front_back = $text_block->output_position == 0 ? 'front' : 'back';
471 ->jstree->create_node('#tree', "#tb-${front_back}", 'last', $node)
472 ->jstree->open_node( '#tree', "#tb-${front_back}");
475 sub render_pasted_section {
476 my ($self, $item, $parent_id) = @_;
478 my $node = $self->presenter->requirement_spec_item_jstree_data($item);
480 ->jstree->create_node('#tree', $parent_id ? "#fb-${parent_id}" : '#sections', 'last', $node)
481 ->jstree->open_node( '#tree', $parent_id ? "#fb-${parent_id}" : '#sections');
483 $self->render_pasted_section($_, $item->id) for @{ $item->children_sorted };
486 sub render_first_pasted_section_as_list {
487 my ($self, $section, %params) = @_;
489 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $section);
491 ->html('#column-content', $html)
492 ->val( '#current_content_type', $section->item_type)
493 ->val( '#current_content_id', $section->id)
494 ->jstree->select_node('#tree', '#fb-' . $section->id);
497 sub prepare_pictures_for_printing {
501 my $userspath = File::Spec->rel2abs($::lx_office_conf{paths}->{userspath});
502 my $target = "${userspath}/kivitendo-print-requirement-spec-picture-" . Common::unique_id() . '-';
504 foreach my $picture (map { @{ $_->pictures } } @{ $self->requirement_spec->text_blocks }) {
505 my $output_file_name = $target . $picture->id . '.' . $picture->get_default_file_name_extension;
506 $picture->{print_file_name} = File::Spec->abs2rel($output_file_name, $userspath);
507 my $out = IO::File->new($output_file_name, 'w') || die("Could not create file " . $output_file_name);
509 $out->print($picture->picture_content);
512 push @files, $output_file_name;
518 sub update_project_link_none_keep_existing {
519 my ($self, $action) = @_;
521 $self->requirement_spec->update_attributes(project_id => undef) if $action eq 'none';
522 $self->requirement_spec->update_attributes(project_id => $::form->{new_project_id}) if $action eq 'existing';
524 return $self->invalidate_version
525 ->replaceWith('#basic_settings', $self->render('requirement_spec/_show_basic_settings', { output => 0 }))
526 ->remove('#project_link_form')
527 ->flash('info', t8('The project link has been updated.'))
531 sub update_project_link_new {
535 ->replaceWith('#project_link_form', $self->render('requirement_spec/_new_project_form', { output => 0 }))
539 sub update_project_link_create {
541 my $params = delete($::form->{project}) || {};
542 my $project = SL::DB::Project->new(
548 my @errors = $project->validate;
550 return $self->js->error(@errors)->render($self) if @errors;
552 my $db = $self->requirement_spec->db;
553 if (!$db->do_transaction(sub {
555 $self->requirement_spec->update_attributes(project_id => $project->id);
558 $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
559 return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render($self);
562 return $self->invalidate_version
563 ->replaceWith('#basic_settings', $self->render('requirement_spec/_show_basic_settings', { output => 0 }))
564 ->remove('#project_link_form')
565 ->flash('info', t8('The project has been created.'))
566 ->flash('info', t8('The project link has been updated.'))