Pflichtenheftitems: Masken für neue (Unter)Funktionsblöcke
[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::DB::RequirementSpec;
13 use SL::DB::RequirementSpecComplexity;
14 use SL::DB::RequirementSpecItem;
15 use SL::DB::RequirementSpecRisk;
16 use SL::Helper::Flash;
17 use SL::JSON;
18 use SL::Locale::String;
19
20 use Rose::Object::MakeMethods::Generic
21 (
22   scalar                  => [ qw(item visible_item visible_section) ],
23   'scalar --get_set_init' => [ qw(complexities risks) ],
24 );
25
26 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete) ]);
27 __PACKAGE__->run_before('init_visible_section');
28
29 #
30 # actions
31 #
32
33 sub action_ajax_list {
34   my ($self, $js) = @_;
35
36   my $js = SL::ClientJS->new;
37
38   if (!$::form->{clicked_id}) {
39     # Clicked on "sections" in the tree. Do nothing.
40     return $self->render($js);
41   }
42
43   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
44
45   $self->render_list($js, $self->item) if !$self->visible_section || ($self->visible_section->id != $self->item->id);
46
47   $self->render($js);
48 }
49
50 sub action_dragged_and_dropped {
51   my ($self)             = @_;
52
53   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                             : die "Unknown 'position' parameter";
54   my $dropped_item       = $::form->{dropped_id}                                  ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
55
56   my $visible_section_id = $self->visible_section ? $self->visible_section->id : undef;
57   my $old_parent_id      = $self->item->parent_id;
58   my $old_type           = $self->item->get_type;
59
60   $self->item->db->do_transaction(sub {
61     $self->item->remove_from_list;
62     $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
63     $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
64   });
65
66   my $js = SL::ClientJS->new;
67
68   $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
69   my $new_section = $self->item->get_section;
70   my $new_type    = $self->item->get_type;
71
72   return $self->render($js) if !$visible_section_id || ($new_type eq 'section');
73
74   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
75   my $old_section = $old_parent->get_section;
76
77   # $::lxdebug->message(0, "old sec ID " . $old_section->id . " new " . $new_section->id . " visible $visible_section_id PARENT: old " . $old_parent->id . " new " . $self->item->parent_id . '/' . $self->item->parent->id);
78
79   if ($visible_section_id == $old_section->id) {
80     my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
81     $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
82
83     if ($old_type eq 'sub-function-block') {
84       $self->replace_bottom($js, $old_parent) ;
85       $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
86
87     } elsif (0 == scalar(@{ $old_section->children })) {
88       $js->show('#section-list-empty');
89     }
90   }
91
92   if ($visible_section_id == $new_section->id) {
93     $js->hide('#section-list-empty');
94
95     my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
96     my $template  = 'requirement_spec_item/_' . (apply { s/-/_/g; $_ } $new_type);
97     my $html      = "" . $self->render($template, { output => 0 }, requirement_spec_item => $self->item);
98     my $next_item = $self->item->get_next_in_list;
99
100     if ($next_item) {
101       $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
102     } else {
103       my $parent_is_section = $self->item->parent->get_type eq 'section';
104       $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
105       $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
106     }
107
108     $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
109   }
110
111   # $::lxdebug->dump(0, "js", $js->to_array);
112
113   $self->render($js);
114 }
115
116 sub action_ajax_add_section {
117   my ($self, %params) = @_;
118
119   die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
120
121   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}));
122
123   my $insert_after = $::form->{id} ? SL::DB::RequirementSpecItem->new(id => $::form->{id})->load->get_section->id : undef;
124   my $html         = $self->render('requirement_spec_item/_section_form', { output => 0 }, id_base => 'new_section', insert_after => $insert_after);
125
126   SL::ClientJS->new
127     ->remove('#new_section_form')
128     ->hide('#column-content > *')
129     ->appendTo($html, '#column-content')
130     ->focus('#new_section_title')
131     ->render($self);
132 }
133
134 sub action_ajax_add_function_block {
135   my ($self, %params) = @_;
136
137   return $self->add_function_block('function-block');
138 }
139
140 sub action_ajax_add_sub_function_block {
141   my ($self, %params) = @_;
142
143   return $self->add_function_block('sub-function-block');
144 }
145
146 sub action_ajax_create {
147   my ($self, %params) = @_;
148
149   my $js              = SL::ClientJS->new;
150   my $prefix          = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
151   my $attributes      = $::form->{$prefix}     || die "Missing parameter group '${prefix}'";
152   my $insert_after    = delete $attributes->{insert_after};
153
154   my @errors = $self->item(SL::DB::RequirementSpecItem->new(%{ $attributes }))->validate;
155   return $js->error(@errors)->render($self) if @errors;
156
157   $self->item->save;
158   $self->item->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
159
160   my $type = $self->item->get_type;
161
162   if ($type eq 'section') {
163     my $node = $self->presenter->requirement_spec_item_jstree_data($self->item);
164     return $self->render_list($js, $self->item)
165       ->jstree->create_node('#tree', $insert_after ? ('#fb-' . $insert_after, 'after') : ('#sections', 'last'), $node)
166       ->jstree->select_node('#tree', '#fb-' . $self->item->id)
167       ->render($self);
168   }
169
170   die 'TODO: create item';
171 }
172
173 sub action_ajax_edit {
174   my ($self, %params) = @_;
175
176   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
177
178   my $js = SL::ClientJS->new;
179
180   if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
181     # Show section/item to edit if it is not visible.
182
183     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
184     $js->html('#column-content', $html);
185   }
186
187   if ($self->item->get_type =~ m/section/) {
188     # Edit the section header, not an item.
189     my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
190
191     $js->hide('#section-header-' . $self->item->id)
192        ->remove("#edit_section_form")
193        ->insertAfter($html, '#section-header-' . $self->item->id)
194        ->jstree->select_node('#tree', '#fb-' . $self->item->id)
195        ->focus("#edit_section_title")
196        ->val('#current_content_type', 'section')
197        ->val('#current_content_id',   $self->item->id)
198        ->render($self);
199     return;
200   }
201
202   # Edit a function block or a sub function block
203   my @dependencies          = $self->create_dependencies;
204   my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
205
206   my $html                  = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
207   my $id_base               = 'edit_function_block_' . $self->item->id;
208   my $content_top_id        = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
209
210   $js->hide($content_top_id)
211      ->remove("#${id_base}_form")
212      ->insertAfter($html, $content_top_id)
213      ->jstree->select_node('#tree', '#fb-' . $self->item->id)
214      ->focus("#${id_base}_description")
215      ->val('#current_content_type', $self->item->get_type)
216      ->val('#current_content_id', $self->item->id)
217      ->render($self);
218 }
219
220 sub action_ajax_update {
221   my ($self, %params) = @_;
222
223   my $js         = SL::ClientJS->new;
224   my $prefix     = $::form->{form_prefix} || die "Missing parameter 'form_prefix'";
225   my $attributes = $::form->{$prefix}     || {};
226
227   foreach (qw(requirement_spec_id parent_id position)) {
228     delete $attributes->{$_} if !defined $attributes->{$_};
229   }
230
231   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
232   return $js->error(@errors)->render($self) if @errors;
233
234   $self->item->save;
235
236   my $type = $self->item->get_type;
237
238   if ($type eq 'section') {
239     # Updated section, now update section header.
240
241     my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
242
243     return SL::ClientJS->new
244       ->remove('#edit_section_form')
245       ->html('#section-header-' . $self->item->id, $html)
246       ->show('#section-header-' . $self->item->id)
247       ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
248       ->render($self);
249   }
250
251   # Updated function block or sub function block. Update (sub)
252   # function block and potentially the bottom of the parent function
253   # block.
254
255   my $id_prefix    = $type eq 'function-block' ? '' : 'sub-';
256   my $html_top     = $self->render('requirement_spec_item/_function_block_content_top',    { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
257   $id_prefix      .= 'function-block-content-';
258
259   my $js = SL::ClientJS->new
260     ->remove('#' . $prefix . '_form')
261     ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
262     ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
263
264   $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
265   $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
266
267   $js->render($self);
268 }
269
270 sub action_ajax_delete {
271   my ($self) = @_;
272
273   my $js = SL::ClientJS->new;
274
275   $self->item->delete;
276
277   if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
278     # Currently visible section is deleted.
279
280     my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
281     if ($new_section) {
282       $self->render_list($js, $new_section);
283
284     } else {
285       my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
286       $js->html('#column-content', $html)
287          ->val('#current_content_type', '')
288          ->val('#current_content_id', '')
289     }
290
291   } elsif ($self->visible_section && ($self->visible_section->id == $self->item->get_section->id)) {
292     # Item in currently visible section is deleted.
293
294     my $type = $self->item->get_type;
295     $js->remove('#edit_function_block_' . $self->item->id . '_form')
296        ->remove('#' . $type . '-' . $self->item->id);
297
298     $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
299
300     if (1 == scalar @{ $self->item->get_full_list }) {
301       if ($type eq 'function-block') {
302         $js->show('#section-list-empty');
303       } elsif ($type eq 'sub-function-block') {
304         $js->hide('#sub-function-block-container-' . $self->item->parent_id);
305       }
306     }
307   }
308
309   $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
310      ->render($self);
311 }
312
313 #
314 # filters
315 #
316
317 sub load_requirement_spec {
318   my ($self) = @_;
319   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
320 }
321
322 sub load_requirement_spec_item {
323   my ($self) = @_;
324   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
325 }
326
327 #
328 # helpers
329 #
330
331 sub create_random_id {
332   return join '-', Time::HiRes::gettimeofday();
333 }
334
335 sub format_exception {
336   return join "\n", (split m/\n/, $@)[0..4];
337 }
338
339 sub init_visible_section {
340   my ($self)       = @_;
341
342   my $content_id   = $::form->{current_content_id};
343   my $content_type = $::form->{current_content_type};
344
345   return undef unless $content_id;
346   return undef unless $content_type =~ m/section|function-block/;
347
348   $self->visible_item(SL::DB::Manager::RequirementSpecItem->find_by(id => $content_id));
349   return undef unless $self->visible_item;
350
351   return $self->visible_section($self->visible_item->get_section);
352 }
353
354 sub init_complexities {
355   my ($self) = @_;
356
357   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
358 }
359
360 sub init_risks {
361   my ($self) = @_;
362
363   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
364 }
365
366 sub replace_bottom {
367   my ($self, $js, $item_or_id) = @_;
368
369   my $item      = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
370   my $id_prefix = $item->get_type eq 'function-block' ? '' : 'sub-';
371   my $html      = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
372   return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
373 }
374
375 sub render_list {
376   my ($self, $js, $item) = @_;
377
378   my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
379   $js->html('#column-content',      $html)
380      ->val( '#current_content_type', $item->get_type)
381      ->val( '#current_content_id',   $item->id)
382      ->jstree->select_node('#tree', '#fb-' . $item->id);
383 }
384
385 sub create_dependency_item {
386   my $self = shift;
387   [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ];
388 }
389
390 sub create_dependencies {
391   my ($self) = @_;
392
393   return map { [ $_->fb_number . ' ' . $_->title,
394                  [ map { ( $self->create_dependency_item($_),
395                            map { $self->create_dependency_item($_, '->') } @{ $_->sorted_children })
396                        } @{ $_->sorted_children } ] ]
397              } @{ $self->item->requirement_spec->sections };
398 }
399
400 sub add_function_block {
401   my ($self, $new_type) = @_;
402
403   die "Invalid new_type '$new_type'"            if $new_type !~ m/^(?:sub-)?function-block$/;
404   die "Missing parameter 'id'"                  if !$::form->{id};
405   die "Missing parameter 'requirement_spec_id'" if !$::form->{requirement_spec_id};
406
407   my $clicked_item = SL::DB::RequirementSpecItem->new(id => $::form->{id})->load;
408   my $clicked_type = $clicked_item->get_type;
409
410   die "Invalid clicked_type '$clicked_type'" if $clicked_type !~ m/^(?: section | (?:sub-)? function-block )$/x;
411
412   my $case = "${clicked_type}:${new_type}";
413
414   my ($insert_position, $insert_reference, $parent_id, $display_reference)
415     = $case eq 'section:function-block'                ? ( 'appendTo',    $clicked_item->id,        $clicked_item->id,                '#section-list'                  )
416     : $case eq 'function-block:function-block'         ? ( 'insertAfter', $clicked_item->id,        $clicked_item->parent_id,         '#function-block-'               )
417     : $case eq 'function-block:sub-function-block'     ? ( 'appendTo'  ,  $clicked_item->id,        $clicked_item->id,                '#sub-function-block-container-' )
418     : $case eq 'sub-function-block:function-block'     ? ( 'insertAfter', $clicked_item->parent_id, $clicked_item->parent->parent_id, '#function-block-'               )
419     : $case eq 'sub-function-block:sub-function-block' ? ( 'insertAfter', $clicked_item->id,        $clicked_item->parent_id,         '#sub-function-block-'           )
420     :                                                    die "Invalid combination of 'clicked_type (section)/new_type ($new_type)'";
421
422   $self->item(SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id}, parent_id => $parent_id));
423
424   $display_reference .= $insert_reference if $display_reference =~ m/-$/;
425   my $id_base         = join('_', 'new_function_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
426   my $html            = $self->render(
427     'requirement_spec_item/_function_block_form',
428     { output => 0 },
429     DEPENDENCIES          => [ $self->create_dependencies ],
430     SELECTED_DEPENDENCIES => [],
431     requirement_spec_item => $self->item,
432     id_base               => $id_base,
433     insert_after          => $insert_reference,
434   );
435
436   my $js = SL::ClientJS->new;
437
438   my $new_section = $self->item->get_section;
439   if (!$self->visible_section || ($self->visible_section->id != $new_section->id)) {
440     # Show section/item to edit if it is not visible.
441
442     $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $new_section);
443     $js->html('#column-content', $html)
444        ->val('#current_content_type', 'section')
445        ->val('#current_content_id',   $new_section->id)
446        ->jstree->select_node('#tree', '#fb-' . $new_section->id);
447   }
448
449   # $::lxdebug->message(0, "alright! clicked ID " . $::form->{id} . " type $clicked_type new_type $new_type insert_pos $insert_position ref " . ($insert_reference // '<undef>') . " parent $parent_id display_ref $display_reference");
450
451   $js->action($insert_position, $html, $display_reference)
452      ->focus("#${id_base}_description")
453      ->render($self);
454 }
455
456 1;