Pflichtenheftabschnitte: Nach Anlegen Form fürs Anlegen eines Funktionsblocks anzeigen
[kivitendo-erp.git] / SL / Controller / RequirementSpecItem.pm
1 package SL::Controller::RequirementSpecItem;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Carp;
8 use List::MoreUtils qw(apply);
9 use List::Util qw(first);
10 use Time::HiRes ();
11
12 use SL::Clipboard;
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;
19 use SL::JSON;
20 use SL::Locale::String;
21
22 use Rose::Object::MakeMethods::Generic
23 (
24   scalar                  => [ qw(item visible_item visible_section clicked_item sections) ],
25   'scalar --get_set_init' => [ qw(complexities risks js) ],
26 );
27
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');
31
32 #
33 # actions
34 #
35
36 sub action_ajax_list {
37   my ($self) = @_;
38
39   if (!$::form->{clicked_id}) {
40     # Clicked on "sections" in the tree. Do nothing.
41     return $self->render($self->js);
42   }
43
44   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load;
45   $self->item($clicked_item->section);
46
47   if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
48     $self->render_list($self->item, $clicked_item);
49   } else {
50     $self->select_node($clicked_item);
51   }
52
53   $self->render($self->js);
54 }
55
56 sub insert_new_item_in_section_view {
57   my ($self) = @_;
58
59   $self->js->hide('#section-list-empty');
60
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;
66
67   if ($next_item) {
68     $self->js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
69   } else {
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;
73   }
74
75   $self->replace_bottom($self->item->parent) if $new_type eq 'sub-function-block';
76 }
77
78 sub action_dragged_and_dropped {
79   my ($self)              = @_;
80
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;
83
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;
87   my $new_type            = $position =~ m/before|after/ ? $dropped_item->item_type : $dropped_item->child_type;
88
89   $self->item->db->do_transaction(sub {
90     $self->item->remove_from_list;
91     $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
92     $self->item->item_type($new_type);
93     $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
94   });
95
96   $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
97   my $new_section         = $self->item->section;
98   my $new_visible_section = SL::DB::RequirementSpecItem->new(id => $self->visible_item->id)->load->section;
99
100   return $self->invalidate_version->render($self) if !$old_visible_section || ($new_type eq 'section');
101
102   # From here on $old_visible_section is definitely set.
103
104   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
105   my $old_section = $old_parent->section;
106
107   # Section debug info:
108   # $::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
109   #                       . " PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
110
111   # Item debug info:
112   # $::lxdebug->message(0, 'item id: ' . $self->item->id . " new type: $new_type old type: $old_type #old children: " . scalar(@{ $old_parent->children }));
113
114   if ($old_visible_section->id != $new_visible_section->id) {
115     # The currently visible item has been dragged to a different section.
116     $self->invalidate_version;
117     return $self->render_list($new_section, $self->item)
118       ->render($self);
119   }
120
121   if ($old_visible_section->id == $old_section->id) {
122     my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
123     $self->js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
124
125     if ($old_type eq 'sub-function-block') {
126       $self->replace_bottom($old_parent) ;
127       $self->js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
128
129     } elsif (0 == scalar(@{ $old_section->children })) {
130       $self->js->show('#section-list-empty');
131     }
132   }
133
134   if ($old_visible_section->id == $new_section->id) {
135     $self->insert_new_item_in_section_view($self->js);
136   }
137
138   # $::lxdebug->dump(0, "js", $self->js->to_array);
139
140   $self->invalidate_version->render($self);
141 }
142
143 sub action_ajax_add_section {
144   my ($self, %params) = @_;
145
146   die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
147
148   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, item_type => 'section'));
149
150   my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->section->id : undef;
151   my $html         = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
152
153   $self->js
154     ->remove('#new_section_form')
155     ->hide('#column-content > *')
156     ->appendTo($html, '#column-content')
157     ->focus('#new_section_title')
158     ->render($self);
159 }
160
161 sub action_ajax_add_function_block {
162   my ($self, %params) = @_;
163
164   return $self->add_function_block('function-block');
165 }
166
167 sub action_ajax_add_sub_function_block {
168   my ($self, %params) = @_;
169
170   return $self->add_function_block('sub-function-block');
171 }
172
173 sub action_ajax_create {
174   my ($self, %params) = @_;
175
176   my $prefix          = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
177   my $attributes      = $::form->{$prefix}     || die "Missing parameter group '${prefix}'";
178   my $insert_after    = delete $attributes->{insert_after};
179
180   if (!$attributes->{parent_id}) {
181     $attributes->{order_part_id} ||= $::instance_conf->get_requirement_spec_section_order_part_id;
182   }
183
184   my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
185   return $self->js->error(@errors)->render($self) if @errors;
186
187   $self->item->save;
188   $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
189
190   my $type = $self->item->item_type;
191
192   if ($type eq 'section') {
193     my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
194     $self->invalidate_version;
195     $self->render_list($self->item)
196       ->hide('#section-list-empty')
197       ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
198       ->jstree->select_node('#tree', '#fb-' . $self->item->id);
199     return $self->add_new_item_form_after_create
200       ->render($self);
201   }
202
203   my $template = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $type);
204   my $html     = $self->render($template, { output => 0 }, requirement_spec_item => $self->item, id_prefix => $type eq 'function-block' ? '' : 'sub-');
205   my $node     = $self->presenter->requirement_spec_item_jstree_data($self->item);
206
207   $self->js
208     ->replaceWith('#' . $prefix . '_form', $html)
209     ->hide('#section-list-empty')
210     ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#fb-' . $self->item->parent_id, 'last'), $node)
211     ->jstree->select_node('#tree', '#fb-' . $self->item->id);
212
213   $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
214
215   $self->add_new_item_form_after_create;
216
217   $self->invalidate_version->render($self);
218 }
219
220 sub action_ajax_edit {
221   my ($self, %params) = @_;
222
223   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
224
225   if (!$self->is_item_visible) {
226     # Show section/item to edit if it is not visible.
227
228     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item->section);
229     $self->js->html('#column-content', $html);
230   }
231
232   if ($self->item->item_type =~ m/section/) {
233     # Edit the section header, not an item.
234     my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
235
236     $self->js
237       ->hide('#section-header-' . $self->item->id)
238       ->remove("#edit_section_form")
239       ->insertAfter($html, '#section-header-' . $self->item->id)
240       ->jstree->select_node('#tree', '#fb-' . $self->item->id)
241       ->focus("#edit_section_title")
242       ->val('#current_content_type', 'section')
243       ->val('#current_content_id',   $self->item->id)
244       ->render($self);
245     return;
246   }
247
248   # Edit a function block or a sub function block
249   my @dependencies          = $self->create_dependencies;
250   my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
251
252   my $html                  = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
253   my $id_base               = 'edit_function_block_' . $self->item->id;
254   my $content_top_id        = '#' . $self->item->item_type . '-content-top-' . $self->item->id;
255
256   $self->js
257     ->hide($content_top_id)
258     ->remove("#${id_base}_form")
259     ->insertAfter($html, $content_top_id)
260     ->jstree->select_node('#tree', '#fb-' . $self->item->id)
261     ->focus("#${id_base}_description")
262     ->val('#current_content_type', $self->item->item_type)
263     ->val('#current_content_id', $self->item->id)
264     ->render($self);
265 }
266
267 sub action_ajax_update {
268   my ($self, %params) = @_;
269
270   my $prefix     = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
271   my $attributes = $::form->{$prefix}     || {};
272
273   foreach (qw(requirement_spec_id parent_id position)) {
274     delete $attributes->{$_} if !defined $attributes->{$_};
275   }
276
277   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
278   return $self->js->error(@errors)->render($self) if @errors;
279
280   $self->item->save;
281
282   my $type = $self->item->item_type;
283
284   if ($type eq 'section') {
285     # Updated section, now update section header.
286
287     my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
288
289     return $self->invalidate_version
290       ->remove('#edit_section_form')
291       ->html('#section-header-' . $self->item->id, $html)
292       ->show('#section-header-' . $self->item->id)
293       ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
294       ->render($self);
295   }
296
297   # Updated function block or sub function block. Update (sub)
298   # function block and potentially the bottom of the parent function
299   # block.
300
301   my $id_prefix    = $type eq 'function-block' ? '' : 'sub-';
302   my $html_top     = $self->render('requirement_spec_item/_function_block_content_top',    { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
303   $id_prefix      .= 'function-block-content-';
304
305   $self->js
306     ->remove('#' . $prefix . '_form')
307     ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
308     ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
309
310   $self->replace_bottom($self->item, id_prefix => $id_prefix);
311   $self->replace_bottom($self->item->parent) if $type eq 'sub-function-block';
312
313   $self->invalidate_version->render($self);
314 }
315
316 sub action_ajax_delete {
317   my ($self) = @_;
318
319   my $full_list = $self->item->get_full_list;
320
321   $self->item->delete;
322
323   if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
324     # Currently visible section is deleted.
325
326     my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
327     if ($new_section) {
328       $self->render_list($new_section);
329
330     } else {
331       my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
332       $self->js
333         ->html('#column-content',      $html)
334         ->val('#current_content_type', '')
335         ->val('#current_content_id',   '')
336     }
337
338   } elsif ($self->is_item_visible) {
339     # Item in currently visible section is deleted.
340
341     my $type = $self->item->item_type;
342     $self->js->remove('#edit_function_block_' . $self->item->id . '_form')
343              ->remove('#' . $type . '-' . $self->item->id);
344
345     $self->replace_bottom($self->item->parent_id) if $type eq 'sub-function-block';
346
347     if (1 == scalar @{ $full_list }) {
348       if ($type eq 'function-block') {
349         $self->js->show('#section-list-empty');
350       } elsif ($type eq 'sub-function-block') {
351         $self->js->hide('#sub-function-block-container-' . $self->item->parent_id);
352       }
353     }
354   }
355
356   $self->invalidate_version
357     ->jstree->delete_node('#tree', '#fb-' . $self->item->id)
358     ->render($self);
359 }
360
361 sub action_ajax_flag {
362   my ($self) = @_;
363
364   $self->item->update_attributes(is_flagged => !$self->item->is_flagged);
365
366   $self->js
367    ->action_if($self->is_item_visible, 'toggleClass', '#' . $self->item->item_type . '-' . $self->item->id, 'flagged')
368    ->toggleClass('#fb-' . $self->item->id, 'flagged')
369    ->render($self);
370 }
371
372 sub action_ajax_copy {
373   my ($self, %params) = @_;
374
375   SL::Clipboard->new->copy($self->item);
376   $self->render($self->js);
377 }
378
379 sub determine_paste_position {
380   my ($self) = @_;
381
382   if ($self->item->item_type eq 'section') {
383     # Sections are always pasted either directly after the
384     # clicked-upon section or at the very end.
385     return $self->clicked_item ? (undef, $self->clicked_item->section->id) : ();
386
387   } elsif ($self->item->item_type eq 'function-block') {
388     # A function block:
389     # - paste on section list: insert into last section as last element
390     # - paste on section: insert into that section as last element
391     # - paste on function block: insert after clicked-upon element
392     # - paste on sub function block: insert after parent function block of clicked-upon element
393     return !$self->clicked_item                                ? ( $self->sections->[-1]->id,              undef                          )
394          :  $self->clicked_item->item_type eq 'section'        ? ( $self->clicked_item->id,                undef                          )
395          :  $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->parent_id,         $self->clicked_item->id        )
396          :                                                       ( $self->clicked_item->parent->parent_id, $self->clicked_item->parent_id );
397
398   } else {                      # sub-function-block
399     # A sub function block:
400     # - paste on section list: promote to function block and insert into last section as last element
401     # - paste on section: promote to function block and insert into that section as last element
402     # - paste on function block: insert as last element in clicked-upon element
403     # - paste on sub function block: insert after clicked-upon element
404
405     # Promote sub function blocks to function blocks when pasting on a
406     # section or the section list.
407     $self->item->item_type('function-block') if !$self->clicked_item || ($self->clicked_item->item_type eq 'section');
408
409     return !$self->clicked_item                                ? ( $self->sections->[-1]->id,      undef                   )
410          :  $self->clicked_item->item_type eq 'section'        ? ( $self->clicked_item->id,        undef                   )
411          :  $self->clicked_item->item_type eq 'function-block' ? ( $self->clicked_item->id,        undef                   )
412          :                                                       ( $self->clicked_item->parent_id, $self->clicked_item->id );
413   }
414 }
415
416 sub assign_requirement_spec_id_rec {
417   my ($self, $item) = @_;
418
419   $item->assign_attributes(requirement_spec_id => $::form->{requirement_spec_id}, fb_number => undef);
420   $self->assign_requirement_spec_id_rec($_) for @{ $item->children || [] };
421
422   return $item;
423 }
424
425 sub create_and_insert_node_rec {
426   my ($self, $item, $new_parent_id, $insert_after) = @_;
427
428   my $node = $self->presenter->requirement_spec_item_jstree_data($item);
429   $self->js->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : $new_parent_id ? ('#fb-' . $new_parent_id, 'last') : ('#sections', 'last'), $node);
430
431   $self->create_and_insert_node_rec($_, $item->id) for @{ $item->children || [] };
432
433   $self->js->jstree->open_node('#tree', '#fb-' . $item->id);
434 }
435
436 sub action_ajax_paste {
437   my ($self, %params) = @_;
438
439   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecItem$/);
440
441   if (!$copied) {
442     return $self->js->error(t8("The clipboard does not contain anything that can be pasted here."))
443                     ->render($self);
444   }
445
446   $self->item($self->assign_requirement_spec_id_rec($copied->to_object));
447   my $req_spec = SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load;
448   $self->sections($req_spec->sections);
449
450   if (($self->item->item_type ne 'section') && !@{ $self->sections }) {
451     return $self->js->error(t8("You cannot paste function blocks or sub function blocks if there is no section."))
452                     ->render($self);
453   }
454
455   $self->clicked_item($::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load : undef);
456
457   my ($new_parent_id, $insert_after) = $self->determine_paste_position;
458
459   # Store result in database.
460   $self->item->update_attributes(parent_id => $new_parent_id);
461   $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
462
463   # Update the tree: create the node for all pasted objects.
464   $self->create_and_insert_node_rec($self->item, $new_parent_id, $insert_after);
465
466   # Pasting the very first section?
467   if (!@{ $self->sections }) {
468     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
469     $self->js->html('#column-content', $html)
470              ->jstree->select_node('#tree', '#fb-' . $self->item->id)
471   }
472
473   # Update the current view if required.
474   $self->insert_new_item_in_section_view($self->js) if $self->is_item_visible;
475
476   $self->invalidate_version->render($self);
477 }
478
479 #
480 # filters
481 #
482
483 sub check_auth {
484   my ($self) = @_;
485   $::auth->assert('sales_quotation_edit');
486 }
487
488 sub load_requirement_spec_item {
489   my ($self) = @_;
490   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
491 }
492
493 #
494 # helpers
495 #
496
497 sub create_random_id {
498   return join '-', Time::HiRes::gettimeofday();
499 }
500
501 sub format_exception {
502   return join "\n", (split m/\n/, $@)[0..4];
503 }
504
505 sub init_complexities {
506   my ($self) = @_;
507
508   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
509 }
510
511 sub init_risks {
512   my ($self) = @_;
513
514   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
515 }
516
517 sub init_js {
518   my ($self) = @_;
519   $self->js(SL::ClientJS->new);
520 }
521
522 sub replace_bottom {
523   my ($self, $item_or_id) = @_;
524
525   my $item      = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
526   my $id_prefix = $item->item_type eq 'function-block' ? '' : 'sub-';
527   my $html      = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
528   return $self->js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
529 }
530
531 sub render_list {
532   my ($self, $item, $item_to_select) = @_;
533
534   my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
535   $self->js->html('#column-content', $html);
536   $self->select_node($item_to_select || $item);
537 }
538
539 sub select_node {
540   my ($self, $item) = @_;
541
542   $self->js
543     ->val( '#current_content_type', $item->item_type)
544     ->val( '#current_content_id',   $item->id)
545     ->jstree->select_node('#tree', '#fb-' . $item->id);
546 }
547
548 sub create_dependency_item {
549   my $self = shift;
550   [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
551 }
552
553 sub create_dependencies {
554   my ($self) = @_;
555
556   return map { [ $_->fb_number . ' ' . $_->title,
557                  [ map { ( $self->create_dependency_item($_),
558                            map { $self->create_dependency_item($_, '->') } @{ $_->children_sorted })
559                        } @{ $_->children_sorted } ] ]
560              } @{ $self->item->requirement_spec->sections };
561 }
562
563 sub ensure_section_is_shown {
564   my ($self, %params) = @_;
565
566   return $self->js if $self->is_item_visible;
567
568   # Show section/item to edit if it is not visible.
569   my $new_section = $self->item->section;
570   my $html        = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
571
572   return $self->js
573     ->html('#column-content', $html)
574     ->val('#current_content_type', 'section')
575     ->val('#current_content_id',   $new_section->id)
576     ->jstree->select_node('#tree', '#fb-' . $new_section->id);
577 }
578
579 sub add_new_item_form {
580   my ($self, %params) = @_;
581
582   for (qw(insert_position display_reference)) {
583     croak "Missing parameter $_" if !$params{$_};
584   }
585   croak "Missing parameter insert_reference" if ($params{insert_position} eq 'insertAfter') && !$params{insert_reference};
586
587   my $id_base = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
588   my $html    = $self->render(
589     'requirement_spec_item/_function_block_form',
590     { output => 0 },
591     DEPENDENCIES          => [ $self->create_dependencies ],
592     SELECTED_DEPENDENCIES => [],
593     requirement_spec_item => $self->item,
594     id_base               => $id_base,
595     insert_after          => $params{insert_position} eq 'insertAfter' ? $params{insert_reference} : undef,
596   );
597
598   return $self->js
599     ->action($params{insert_position}, $html, $params{display_reference})
600     ->focus("#${id_base}_description");
601 }
602
603 sub add_new_item_form_after_create {
604   my ($self, %params) = @_;
605
606   my $created_item    = $self->item;
607   my $is_section      = $created_item->item_type eq 'section';
608
609   $self->item(SL::DB::RequirementSpecItem->new(
610     requirement_spec_id => $created_item->requirement_spec_id,
611     parent_id           => $is_section ? $created_item->id : $created_item->parent_id,
612     item_type           => $is_section ? 'function-block'  : $created_item->item_type,
613   ));
614
615   $self->add_new_item_form(
616     insert_reference  => $created_item->id,
617     insert_position   => $is_section ? 'appendTo'      : 'insertAfter',
618     display_reference => $is_section ? '#section-list' : '#' . $created_item->item_type . '-' . $created_item->id,
619   );
620 }
621
622 sub add_function_block {
623   my ($self, $new_type) = @_;
624
625   my $clicked_id = $::form->{id} || ($self->visible_item ? $self->visible_item->id : undef);
626
627   die "Invalid new_type '$new_type'"               if $new_type !~ m/^(?:sub-)?function-block$/;
628   die "Missing parameter 'id' and no visible item" if !$clicked_id;
629   die "Missing parameter 'requirement_spec_id'"    if !$::form->{requirement_spec_id};
630
631   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $clicked_id)->load;
632   my $clicked_type = $clicked_item->item_type;
633
634   die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
635
636   my $case = "${clicked_type}:${new_type}";
637
638   my ($insert_position, $insert_reference, $parent_id, $display_reference)
639     = $case eq 'section:function-block'                ? ( 'appendTo',    $clicked_item->id,        $clicked_item->id,                '#section-list'                  )
640     : $case eq 'function-block:function-block'         ? ( 'insertAfter', $clicked_item->id,        $clicked_item->parent_id,         '#function-block-'               )
641     : $case eq 'function-block:sub-function-block'     ? ( 'appendTo'  ,  $clicked_item->id,        $clicked_item->id,                '#sub-function-block-container-' )
642     : $case eq 'sub-function-block:function-block'     ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-'               )
643     : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id,        $clicked_item->parent_id,         '#sub-function-block-'           )
644     :                                                    die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
645
646   $display_reference .= $insert_reference if $display_reference =~ m/-$/;
647
648   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id, item_type => $new_type));
649
650   $self->ensure_section_is_shown;
651   $self->add_new_item_form(insert_position => $insert_position, insert_reference => $insert_reference, display_reference => $display_reference);
652
653   $self->js->show('#sub-function-block-container-' . $parent_id) if $new_type eq 'sub-function-block';
654
655   $self->js->render($self);
656 }
657
658 sub is_item_visible {
659   my ($self, $item) = @_;
660
661   $item ||= $self->item;
662   return $self->visible_section && ($self->visible_section->id == $item->section->id);
663 }
664
665 sub invalidate_version {
666   my ($self) = @_;
667
668   my $html   = $self->render('requirement_spec/_version', { output => 0 },
669                              requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->item->requirement_spec_id)->load);
670   return $self->js->html('#requirement_spec_version', $html);
671 }
672
673 1;