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