1 package SL::Controller::RequirementSpecItem;
5 use parent qw(SL::Controller::Base);
8 use List::MoreUtils qw(apply);
9 use List::Util qw(first);
13 use SL::Controller::Helper::RequirementSpec;
14 use SL::DB::RequirementSpec;
15 use SL::DB::RequirementSpecComplexity;
16 use SL::DB::RequirementSpecItem;
17 use SL::DB::RequirementSpecRisk;
18 use SL::Helper::Flash;
20 use SL::Locale::String;
22 use Rose::Object::MakeMethods::Generic
24 scalar => [ qw(item visible_item visible_section clicked_item sections) ],
25 'scalar --get_set_init' => [ qw(complexities risks js) ],
28 __PACKAGE__->run_before('check_auth');
29 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete ajax_flag ajax_copy) ]);
30 __PACKAGE__->run_before('init_visible_section');
36 sub action_ajax_list {
39 if (!$::form->{clicked_id}) {
40 # Clicked on "sections" in the tree. Do nothing.
41 return $self->render($self->js);
44 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load;
45 $self->item($clicked_item->section);
47 if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
48 $self->render_list($self->item, $clicked_item);
50 $self->select_node($clicked_item);
53 $self->render($self->js);
56 sub insert_new_item_in_section_view {
59 $self->js->hide('#section-list-empty');
61 my $new_type = $self->item->item_type;
62 my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
63 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type);
64 my $html = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item);
65 my $next_item = $self->item->get_next_in_list;
68 $self->js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
70 my $parent_is_section = $self->item->parent->item_type eq 'section';
71 $self->js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
72 $self->js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
75 $self->replace_bottom($self->item->parent) if $new_type eq 'sub-function-block';
78 sub action_dragged_and_dropped {
81 my $position = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
82 my $dropped_item = $::form->{dropped_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
84 my $old_visible_section = $self->visible_section ? $self->visible_section : undef;
85 my $old_parent_id = $self->item->parent_id;
86 my $old_type = $self->item->item_type;
88 $self->item->db->do_transaction(sub {
89 $self->item->remove_from_list;
90 $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
91 $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
94 $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
95 my $new_section = $self->item->section;
96 my $new_type = $self->item->item_type;
97 my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
99 return $self->invalidate_version->render($self) if !$old_visible_section || ($new_type eq 'section');
101 # From here on $old_visible_section is definitely set.
103 my $old_parent = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
104 my $old_section = $old_parent->section;
106 # $::lxdebug->message(0, "old sec ID " . $old_section->id . " new " . $new_section->id . " old visible " . $old_visible_section->id . " new visible " . $new_visible_section->id
107 # . " PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
109 if ($old_visible_section->id != $new_visible_section->id) {
110 # The currently visible item has been dragged to a different section.
111 $self->invalidate_version;
112 return $self->render_list($new_section, $self->item)
116 if ($old_visible_section->id == $old_section->id) {
117 my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
118 $self->js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
120 if ($old_type eq 'sub-function-block') {
121 $self->replace_bottom($old_parent) ;
122 $self->js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
124 } elsif (0 == scalar(@{ $old_section->children })) {
125 $self->js->show('#section-list-empty');
129 if ($old_visible_section->id == $new_section->id) {
130 $self->insert_new_item_in_section_view($self->js);
133 # $::lxdebug->dump(0, "js", $self->js->to_array);
135 $self->invalidate_version->render($self);
138 sub action_ajax_add_section {
139 my ($self, %params) = @_;
141 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
143 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
145 my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->section->id : undef;
146 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
149 ->remove('#new_section_form')
150 ->hide('#column-content > *')
151 ->appendTo($html, '#column-content')
152 ->focus('#new_section_title')
156 sub action_ajax_add_function_block {
157 my ($self, %params) = @_;
159 return $self->add_function_block('function-block');
162 sub action_ajax_add_sub_function_block {
163 my ($self, %params) = @_;
165 return $self->add_function_block('sub-function-block');
168 sub action_ajax_create {
169 my ($self, %params) = @_;
171 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
172 my $attributes = $::form->{$prefix} || die "Missing parameter group '${prefix}'";
173 my $insert_after = delete $attributes->{insert_after};
175 if (!$attributes->{parent_id}) {
176 $attributes->{order_part_id} ||= $::instance_conf->get_requirement_spec_section_order_part_id;
179 my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
180 return $self->js->error(@errors)->render($self) if @errors;
183 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
185 my $type = $self->item->item_type;
187 if ($type eq 'section') {
188 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
189 $self->invalidate_version;
190 return $self->render_list($self->item)
191 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
192 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
196 my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type);
197 my $html = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-');
198 my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
201 ->replaceWith('#' . $prefix . '_form', $html)
202 ->hide('#section-list-empty')
203 ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node)
204 ->jstree->select_node('#tree', '#fb-' . $self->item->id);
206 $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
208 $self->add_new_item_form_after_create if $type =~ m/function-block/;
210 $self->invalidate_version->render($self);
213 sub action_ajax_edit {
214 my ($self, %params) = @_;
216 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
218 if (!$self->is_item_visible) {
219 # Show section/item to edit if it is not visible.
221 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item->section);
222 $self->js->html('#column-content', $html);
225 if ($self->item->item_type =~ m/section/) {
226 # Edit the section header, not an item.
227 my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
230 ->hide('#section-header-' . $self->item->id)
231 ->remove("#edit_section_form")
232 ->insertAfter($html, '#section-header-' . $self->item->id)
233 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
234 ->focus("#edit_section_title")
235 ->val('#current_content_type', 'section')
236 ->val('#current_content_id', $self->item->id)
241 # Edit a function block or a sub function block
242 my @dependencies = $self->create_dependencies;
243 my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
245 my $html = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
246 my $id_base = 'edit_function_block_' . $self->item->id;
247 my $content_top_id = '#' . $self->item->item_type . '-content-top-' . $self->item->id;
250 ->hide($content_top_id)
251 ->remove("#${id_base}_form")
252 ->insertAfter($html, $content_top_id)
253 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
254 ->focus("#${id_base}_description")
255 ->val('#current_content_type', $self->item->item_type)
256 ->val('#current_content_id', $self->item->id)
260 sub action_ajax_update {
261 my ($self, %params) = @_;
263 my $prefix = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
264 my $attributes = $::form->{$prefix} || {};
266 foreach (qw(requirement_spec_id parent_id position)) {
267 delete $attributes->{$_} if !defined $attributes->{$_};
270 my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
271 return $self->js->error(@errors)->render($self) if @errors;
275 my $type = $self->item->item_type;
277 if ($type eq 'section') {
278 # Updated section, now update section header.
280 my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
282 return $self->invalidate_version
283 ->remove('#edit_section_form')
284 ->html('#section-header-' . $self->item->id, $html)
285 ->show('#section-header-' . $self->item->id)
286 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
290 # Updated function block or sub function block. Update (sub)
291 # function block and potentially the bottom of the parent function
294 my $id_prefix = $type eq 'function-block' ? '' : 'sub-';
295 my $html_top = $self->render('requirement_spec_item/_function_block_content_top', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
296 $id_prefix .= 'function-block-content-';
299 ->remove('#' . $prefix . '_form')
300 ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
301 ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
303 $self->replace_bottom($self->item, id_prefix => $id_prefix);
304 $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
306 $self->invalidate_version->render($self);
309 sub action_ajax_delete {
312 my $full_list = $self->item->get_full_list;
316 if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
317 # Currently visible section is deleted.
319 my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
321 $self->render_list($new_section);
324 my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
326 ->html('#column-content', $html)
327 ->val('#current_content_type', '')
328 ->val('#current_content_id', '')
331 } elsif ($self->is_item_visible) {
332 # Item in currently visible section is deleted.
334 my $type = $self->item->item_type;
335 $self->js->remove('#edit_function_block_' . $self->item->id . '_form')
336 ->remove('#' . $type . '-' . $self->item->id);
338 $self->replace_bottom($self->item->parent_id) if $type eq 'sub-function-block';
340 if (1 == scalar @{ $full_list }) {
341 if ($type eq 'function-block') {
342 $self->js->show('#section-list-empty');
343 } elsif ($type eq 'sub-function-block') {
344 $self->js->hide('#sub-function-block-container-' . $self->item->parent_id);
349 $self->invalidate_version
350 ->jstree->delete_node('#tree', '#fb-' . $self->item->id)
354 sub action_ajax_flag {
357 $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
360 ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
361 ->toggleClass('#fb-' . $self->item->id, 'flagged')
365 sub action_ajax_copy {
366 my ($self, %params) = @_;
368 SL::Clipboard->new->copy($self->item);
369 $self->render($self->js);
372 sub determine_paste_position {
375 if ($self->item->item_type eq 'section') {
376 # Sections are always pasted either directly after the
377 # clicked-upon section or at the very end.
378 return $self->clicked_item ? (undef, $self->clicked_item->section->id) : ();
380 } elsif ($self->item->item_type eq 'function-block') {
382 # - paste on section list: insert into last section as last element
383 # - paste on section: insert into that section as last element
384 # - paste on function block: insert after clicked-upon element
385 # - paste on sub function block: insert after parent function block of clicked-upon element
386 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
387 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
388 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->parent_id, $self->clicked_item->id )
389 : ( $self->clicked_item->parent->parent_id, $self->clicked_item->parent_id );
391 } else { # sub-function-block
392 # A sub function block:
393 # - paste on section list: promote to function block and insert into last section as last element
394 # - paste on section: promote to function block and insert into that section as last element
395 # - paste on function block: insert as last element in clicked-upon element
396 # - paste on sub function block: insert after clicked-upon element
398 # Promote sub function blocks to function blocks when pasting on a
399 # section or the section list.
400 $self->item->item_type('function-block') if !$self->clicked_item || ($self->clicked_item->item_type eq 'section');
402 return !$self->clicked_item ? ( $self->sections->[-1]->id, undef )
403 : $self->clicked_item->item_type eq 'section' ? ( $self->clicked_item->id, undef )
404 : $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->id, undef )
405 : ( $self->clicked_item->parent_id, $self->clicked_item->id );
409 sub assign_requirement_spec_id_rec {
410 my ($self, $item) = @_;
412 $item->assign_attributes(requirement_spec_id => $::form->{requirement_spec_id}, fb_number => undef);
413 $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
418 sub create_and_insert_node_rec {
419 my ($self, $item, $new_parent_id, $insert_after) = @_;
421 my $node = $self->presenter->requirement_spec_item_jstree_data($item);
422 $self->js->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : $new_parent_id ? ('#fb-' . $new_parent_id, 'last') : ('#sections', 'last'), $node);
424 $self->create_and_insert_node_rec($_, $item->id) for @{ $item->children || [] };
426 $self->js->jstree->open_node('#tree', '#fb-' . $item->id);
429 sub action_ajax_paste {
430 my ($self, %params) = @_;
432 my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
435 return $self->js->error(t8("The clipboard does not contain anything that can be pasted here."))
439 $self->item($self->assign_requirement_spec_id_rec($copied->to_object));
440 my $req_spec = SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load;
441 $self->sections($req_spec->sections);
443 if (($self->item->item_type ne 'section') && !@{ $self->sections }) {
444 return $self->js->error(t8("You cannot paste function blocks or sub function blocks if there is no section."))
448 $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
450 my ($new_parent_id, $insert_after) = $self->determine_paste_position;
452 # Store result in database.
453 $self->item->update_attributes(parent_id => $new_parent_id);
454 $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
456 # Update the tree: create the node for all pasted objects.
457 $self->create_and_insert_node_rec($self->item, $new_parent_id, $insert_after);
459 # Pasting the very first section?
460 if (!@{ $self->sections }) {
461 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
462 $self->js->html('#column-content', $html)
463 ->jstree->select_node('#tree', '#fb-' . $self->item->id)
466 # Update the current view if required.
467 $self->insert_new_item_in_section_view($self->js) if $self->is_item_visible;
469 $self->invalidate_version->render($self);
478 $::auth->assert('sales_quotation_edit');
481 sub load_requirement_spec_item {
483 $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
490 sub create_random_id {
491 return join '-', Time::HiRes::gettimeofday();
494 sub format_exception {
495 return join "\n", (split m/\n/, $@)[0..4];
498 sub init_complexities {
501 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
507 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
512 $self->js(SL::ClientJS->new);
516 my ($self, $item_or_id) = @_;
518 my $item = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
519 my $id_prefix = $item->item_type eq 'function-block' ? '' : 'sub-';
520 my $html = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
521 return $self->js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
525 my ($self, $item, $item_to_select) = @_;
527 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
528 $self->js->html('#column-content', $html);
529 $self->select_node($item_to_select || $item);
533 my ($self, $item) = @_;
536 ->val( '#current_content_type', $item->item_type)
537 ->val( '#current_content_id', $item->id)
538 ->jstree->select_node('#tree', '#fb-' . $item->id);
541 sub create_dependency_item {
543 [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
546 sub create_dependencies {
549 return map { [ $_->fb_number . ' ' . $_->title,
550 [ map { ( $self->create_dependency_item($_),
551 map { $self->create_dependency_item($_, '->') } @{ $_->children_sorted })
552 } @{ $_->children_sorted } ] ]
553 } @{ $self->item->requirement_spec->sections };
556 sub ensure_section_is_shown {
557 my ($self, %params) = @_;
559 return $self->js if $self->is_item_visible;
561 # Show section/item to edit if it is not visible.
562 my $new_section = $self->item->section;
563 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
566 ->html('#column-content', $html)
567 ->val('#current_content_type', 'section')
568 ->val('#current_content_id', $new_section->id)
569 ->jstree->select_node('#tree', '#fb-' . $new_section->id);
572 sub add_new_item_form {
573 my ($self, %params) = @_;
575 for (qw(insert_position display_reference)) {
576 croak "Missing parameter $_" if !$params{$_};
578 croak "Missing parameter insert_reference" if ($params{insert_position} eq 'insertAfter') && !$params{insert_reference};
580 my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
581 my $html = $self->render(
582 'requirement_spec_item/_function_block_form',
584 DEPENDENCIES => [ $self->create_dependencies ],
585 SELECTED_DEPENDENCIES => [],
586 requirement_spec_item => $self->item,
588 insert_after => $params{insert_position} eq 'insertAfter' ? $params{insert_reference} : undef,
592 ->action($params{insert_position}, $html, $params{display_reference})
593 ->focus("#${id_base}_description");
596 sub add_new_item_form_after_create {
597 my ($self, %params) = @_;
599 my $created_item = $self->item;
600 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $created_item->requirement_spec_id, parent_id => $created_item->parent_id, item_type => $created_item->item_type));
602 $self->add_new_item_form(
603 insert_position => 'insertAfter',
604 insert_reference => $created_item->id,
605 display_reference => '#' . $created_item->item_type . '-' . $created_item->id,
609 sub add_function_block {
610 my ($self, $new_type) = @_;
612 my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
614 die "Invalid new_type '$new_type'" if $new_type !~ m/^(?:sub-)?function-block$/;
615 die "Missing parameter 'id' and no visible item" if !$clicked_id;
616 die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
618 my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
619 my $clicked_type = $clicked_item->item_type;
621 die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
623 my $case = "${clicked_type}:${new_type}";
625 my ($insert_position, $insert_reference, $parent_id, $display_reference)
626 = $case eq 'section:function-block' ? ( 'appendTo', $clicked_item->id, $clicked_item->id, '#section-list' )
627 : $case eq 'function-block:function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#function-block-' )
628 : $case eq 'function-block:sub-function-block' ? ( 'appendTo' , $clicked_item->id, $clicked_item->id, '#sub-function-block-container-' )
629 : $case eq 'sub-function-block:function-block' ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-' )
630 : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id, $clicked_item->parent_id, '#sub-function-block-' )
631 : die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
633 $display_reference .= $insert_reference if $display_reference =~ m/-$/;
635 $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
637 $self->ensure_section_is_shown;
638 $self->add_new_item_form(insert_position => $insert_position, insert_reference => $insert_reference, display_reference => $display_reference);
640 $self->js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
642 $self->js->render($self);
645 sub is_item_visible {
646 my ($self, $item) = @_;
648 $item ||= $self->item;
649 return $self->visible_section && ($self->visible_section->id == $item->section->id);
652 sub invalidate_version {
655 my $html = $self->render('requirement_spec/_version', { output => 0 },
656 requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->item->requirement_spec_id)->load);
657 return $self->js->html('#requirement_spec_version', $html);