Pflichtenheftitems und Abschnitte bearbeiten
[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 List::MoreUtils qw(apply);
8 use List::Util qw(first);
9 use Time::HiRes ();
10
11 use SL::DB::RequirementSpec;
12 use SL::DB::RequirementSpecComplexity;
13 use SL::DB::RequirementSpecItem;
14 use SL::DB::RequirementSpecRisk;
15 use SL::Helper::Flash;
16 use SL::JSON;
17 use SL::Locale::String;
18
19 use Rose::Object::MakeMethods::Generic
20 (
21   scalar                  => [ qw(item visible_item visible_section) ],
22   'scalar --get_set_init' => [ qw(complexities risks) ],
23 );
24
25 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit ajax_delete) ]);
26 __PACKAGE__->run_before('init_visible_section',       only => [ qw(dragged_and_dropped ajax_list   ajax_edit ajax_delete) ]);
27
28 #
29 # actions
30 #
31
32 sub action_ajax_list {
33   my ($self, $js) = @_;
34
35   my $js = SL::ClientJS->new;
36
37   if (!$::form->{clicked_id}) {
38     # Clicked on "sections" in the tree. Do nothing.
39     return $self->render($js);
40   }
41
42   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
43
44   $self->render_list($js, $self->item) if !$self->visible_section || ($self->visible_section->id != $self->item->id);
45
46   $self->render($js);
47 }
48
49 sub action_dragged_and_dropped {
50   my ($self)             = @_;
51
52   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                             : die "Unknown 'position' parameter";
53   my $dropped_item       = $::form->{dropped_id}                                  ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
54
55   my $visible_section_id = $self->visible_section ? $self->visible_section->id : undef;
56   my $old_parent_id      = $self->item->parent_id;
57   my $old_type           = $self->item->get_type;
58
59   $self->item->db->do_transaction(sub {
60     $self->item->remove_from_list;
61     $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
62     $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
63   });
64
65   my $js = SL::ClientJS->new;
66
67   $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
68   my $new_section = $self->item->get_section;
69   my $new_type    = $self->item->get_type;
70
71   return $self->render($js) if !$visible_section_id || ($new_type eq 'section');
72
73   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
74   my $old_section = $old_parent->get_section;
75
76   # $::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);
77
78   if ($visible_section_id == $old_section->id) {
79     my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
80     $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
81
82     if ($old_type eq 'sub-function-block') {
83       $self->replace_bottom($js, $old_parent) ;
84       $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
85
86     } elsif (0 == scalar(@{ $old_section->children })) {
87       $js->show('#section-list-empty');
88     }
89   }
90
91   if ($visible_section_id == $new_section->id) {
92     $js->hide('#section-list-empty');
93
94     my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
95     my $template  = apply { s/-/_/g; $_ } $new_type;
96     my $html      = "" . $self->render('requirement_spec_item/_' . $template, { output => 0 }, requirement_spec_item => $self->item);
97     my $next_item = $self->item->get_next_in_list;
98
99     if ($next_item) {
100       $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
101     } else {
102       my $parent_is_section = $self->item->parent->get_type eq 'section';
103       $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
104       $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
105     }
106
107     $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
108   }
109
110   # $::lxdebug->dump(0, "js", $js->to_array);
111
112   $self->render($js);
113 }
114
115 sub action_ajax_add {
116   my ($self, %params) = @_;
117
118   die "TODO: add action";
119 }
120
121 sub action_ajax_edit {
122   my ($self, %params) = @_;
123
124   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
125
126   my $js = SL::ClientJS->new;
127
128   if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
129     # Show section/item to edit if it is not visible.
130
131     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
132     $js->html('#column-content', $html);
133   }
134
135   if ($self->item->get_type =~ m/section/) {
136     # Edit the section header, not an item.
137     my $html = $self->render('requirement_spec_item/_section_form', { output => 0 });
138
139     $js->hide('#section-header-' . $self->item->id)
140        ->remove("#edit_section_form")
141        ->insertAfter($html, '#section-header-' . $self->item->id)
142        ->jstree->select_node('#tree', '#fb-' . $self->item->id)
143        ->focus("#edit_section_title")
144        ->val('#current_content_type', 'section')
145        ->val('#current_content_id',   $self->item->id)
146        ->render($self);
147     return;
148   }
149
150   # Edit a function block or a sub function block
151   my $create_item = sub {
152     [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ]
153   };
154   my @dependencies =
155     map { [ $_->fb_number . ' ' . $_->title,
156             [ map { ( $create_item->($_),
157                       map { $create_item->($_, '->') } @{ $_->sorted_children })
158                   } @{ $_->sorted_children } ] ]
159         } @{ $self->item->requirement_spec->sections };
160
161   my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
162
163   my $html                  = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
164   my $id_base               = 'edit_function_block_' . $self->item->id;
165   my $content_top_id        = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
166
167   $js->hide($content_top_id)
168      ->remove("#${id_base}_form")
169      ->insertAfter($html, $content_top_id)
170      ->jstree->select_node('#tree', '#fb-' . $self->item->id)
171      ->focus("#${id_base}_description")
172      ->val('#current_content_type', $self->item->get_type)
173      ->val('#current_content_id', $self->item->id)
174      ->render($self);
175 }
176
177 sub action_ajax_update {
178   my ($self, %params) = @_;
179
180   my $js         = SL::ClientJS->new;
181   my $prefix     = $::form->{form_prefix} || 'function_block';
182   my $attributes = $::form->{$prefix}     || {};
183
184   foreach (qw(requirement_spec_id parent_id position)) {
185     delete $attributes->{$_} if !defined $attributes->{$_};
186   }
187
188   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
189   return $js->error(@errors)->render($self) if @errors;
190
191   $self->item->save;
192
193   my $type = $self->item->get_type;
194
195   if ($type eq 'section') {
196     # Updated section, now update section header.
197
198     my $html = $self->render('requirement_spec_item/_section_header', { output => 0 }, requirement_spec_item => $self->item);
199
200     return SL::ClientJS->new
201       ->remove('#edit_section_form')
202       ->html('#section-header-' . $self->item->id, $html)
203       ->show('#section-header-' . $self->item->id)
204       ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item))
205       ->render($self);
206   }
207
208   # Updated function block or sub function block. Update (sub)
209   # function block and potentially the bottom of the parent function
210   # block.
211
212   my $id_prefix    = $type eq 'function-block' ? '' : 'sub-';
213   my $html_top     = $self->render('requirement_spec_item/_function_block_content_top',    { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
214   $id_prefix      .= 'function-block-content-';
215
216   my $js = SL::ClientJS->new
217     ->remove('#' . $prefix . '_form')
218     ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
219     ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
220
221   $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
222   $self->replace_bottom($js, $self->item->parent) if $type eq 'sub-function-block';
223
224   $js->render($self);
225 }
226
227 sub action_ajax_delete {
228   my ($self) = @_;
229
230   my $js = SL::ClientJS->new;
231
232   $self->item->delete;
233
234   if ($self->visible_section && ($self->visible_section->id == $self->item->id)) {
235     # Currently visible section is deleted.
236
237     my $new_section = first { $_->id != $self->item->id } @{ $self->item->requirement_spec->sections };
238     if ($new_section) {
239       $self->render_list($js, $new_section);
240
241     } else {
242       my $html = $self->render('requirement_spec_item/_no_section', { output => 0 });
243       $js->html('#column-content', $html)
244          ->val('#current_content_type', '')
245          ->val('#current_content_id', '')
246     }
247
248   } elsif ($self->visible_section && ($self->visible_section->id == $self->item->get_section->id)) {
249     # Item in currently visible section is deleted.
250
251     my $type = $self->item->get_type;
252     $js->remove('#edit_function_block_' . $self->item->id . '_form')
253        ->remove('#' . $type . '-' . $self->item->id);
254
255     $self->replace_bottom($js, $self->item->parent_id) if $type eq 'sub-function-block';
256
257     if (1 == scalar @{ $self->item->get_full_list }) {
258       if ($type eq 'function-block') {
259         $js->show('#section-list-empty');
260       } elsif ($type eq 'sub-function-block') {
261         $js->hide('#sub-function-block-container-' . $self->item->parent_id);
262       }
263     }
264   }
265
266   $js->jstree->delete_node('#tree', '#fb-' . $self->item->id)
267      ->render($self);
268 }
269
270 #
271 # filters
272 #
273
274 sub load_requirement_spec {
275   my ($self) = @_;
276   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
277 }
278
279 sub load_requirement_spec_item {
280   my ($self) = @_;
281   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
282 }
283
284 #
285 # helpers
286 #
287
288 sub create_random_id {
289   return join '-', Time::HiRes::gettimeofday();
290 }
291
292 sub format_exception {
293   return join "\n", (split m/\n/, $@)[0..4];
294 }
295
296 sub init_visible_section {
297   my ($self)       = @_;
298
299   my $content_id   = $::form->{current_content_id};
300   my $content_type = $::form->{current_content_type};
301
302   return undef unless $content_id;
303   return undef unless $content_type =~ m/section|function-block/;
304
305   $self->visible_item(SL::DB::Manager::RequirementSpecItem->find_by(id => $content_id));
306   return undef unless $self->visible_item;
307
308   return $self->visible_section($self->visible_item->get_section);
309 }
310
311 sub init_complexities {
312   my ($self) = @_;
313
314   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
315 }
316
317 sub init_risks {
318   my ($self) = @_;
319
320   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
321 }
322
323 sub replace_bottom {
324   my ($self, $js, $item_or_id) = @_;
325
326   my $item      = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
327   my $id_prefix = $item->get_type eq 'function-block' ? '' : 'sub-';
328   my $html      = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
329   return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
330 }
331
332 sub render_list {
333   my ($self, $js, $item) = @_;
334
335   my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $item);
336   $js->html('#column-content',      $html)
337      ->val( '#current_content_type', $item->get_type)
338      ->val( '#current_content_id',   $item->id)
339      ->jstree->select_node('#tree', '#fb-' . $item->id);
340 }
341
342 1;