53708fd254eb24e9c109b5c5513d5a84d9abf21b
[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 Time::HiRes ();
9
10 use SL::DB::RequirementSpec;
11 use SL::DB::RequirementSpecComplexity;
12 use SL::DB::RequirementSpecItem;
13 use SL::DB::RequirementSpecRisk;
14 use SL::Helper::Flash;
15 use SL::JSON;
16 use SL::Locale::String;
17
18 use Rose::Object::MakeMethods::Generic
19 (
20   scalar                  => [ qw(requirement_spec item visible_item visible_section) ],
21   'scalar --get_set_init' => [ qw(complexities risks) ],
22 );
23
24 # __PACKAGE__->run_before('load_requirement_spec');
25 __PACKAGE__->run_before('load_requirement_spec_item', only => [ qw(dragged_and_dropped ajax_update ajax_edit) ]);
26 __PACKAGE__->run_before('init_visible_section',       only => [ qw(dragged_and_dropped ajax_list   ajax_edit) ]);
27
28 #
29 # actions
30 #
31
32 sub action_ajax_list {
33   my ($self) = @_;
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   if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
45     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
46     $js->html('#column-content', $html)
47        ->val('#current_content_type', $self->item->get_type)
48        ->val('#current_content_id', $self->item->id);
49   }
50
51   $self->render($js);
52 }
53
54 sub action_dragged_and_dropped {
55   my ($self)             = @_;
56
57   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                             : die "Unknown 'position' parameter";
58   my $dropped_item       = $::form->{dropped_id}                                  ? SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load : undef;
59
60   my $visible_section_id = $self->visible_section ? $self->visible_section->id : undef;
61   my $old_parent_id      = $self->item->parent_id;
62   my $old_type           = $self->item->get_type;
63
64   $self->item->db->do_transaction(sub {
65     $self->item->remove_from_list;
66     $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
67     $self->item->add_to_list(position => $position, reference => $::form->{dropped_id} || undef);
68   });
69
70   my $js = SL::ClientJS->new;
71
72   $self->item(SL::DB::RequirementSpecItem->new(id => $self->item->id)->load);
73   my $new_section = $self->item->get_section;
74   my $new_type    = $self->item->get_type;
75
76   return $self->render($js) if !$visible_section_id || ($new_type eq 'section');
77
78   my $old_parent  = SL::DB::RequirementSpecItem->new(id => $old_parent_id)->load;
79   my $old_section = $old_parent->get_section;
80
81   # $::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);
82
83   if ($visible_section_id == $old_section->id) {
84     my $id_prefix = $old_type eq 'sub-function-block' ? 'sub-' : '';
85     $js->remove('#' . $id_prefix . 'function-block-' . $self->item->id);
86
87     if ($old_type eq 'sub-function-block') {
88       $self->replace_bottom($js, $old_parent) ;
89       $js->hide('#sub-function-block-container-' . $old_parent->id) if 0 == scalar(@{ $old_parent->children });
90
91     } elsif (0 == scalar(@{ $old_section->children })) {
92       $js->show('#section-list-empty');
93     }
94   }
95
96   if ($visible_section_id == $new_section->id) {
97     $js->hide('#section-list-empty');
98
99     my $id_prefix = $new_type eq 'sub-function-block' ? 'sub-' : '';
100     my $template  = apply { s/-/_/g; $_ } $new_type;
101     my $html      = "" . $self->render('requirement_spec_item/_' . $template, { output => 0 }, requirement_spec_item => $self->item);
102     my $next_item = $self->item->get_next_in_list;
103
104     if ($next_item) {
105       $js->insertBefore($html, '#' . $id_prefix . 'function-block-' . $next_item->id);
106     } else {
107       my $parent_is_section = $self->item->parent->get_type eq 'section';
108       $js->appendTo($html, $parent_is_section ? '#section-list' : '#sub-function-block-container-' . $self->item->parent_id);
109       $js->show('#sub-function-block-container-' . $self->item->parent_id) if !$parent_is_section;
110     }
111
112     $self->replace_bottom($js, $self->item->parent) if $new_type eq 'sub-function-block';
113   }
114
115   # $::lxdebug->dump(0, "js", $js->to_array);
116
117   $self->render($js);
118 }
119
120 sub action_ajax_edit {
121   my ($self, %params) = @_;
122
123   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
124
125   my $js = SL::ClientJS->new;
126
127   die "TODO: edit section" if $self->item->get_type =~ m/section/;
128
129   if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
130     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
131     $js->html('#column-content', $html);
132   }
133
134   if ($self->item->get_type =~ m/function-block/) {
135     my $create_item = sub {
136       [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ]
137     };
138     my @dependencies =
139       map { [ $_->fb_number . ' ' . $_->title,
140               [ map { ( $create_item->($_),
141                         map { $create_item->($_, '->') } @{ $_->sorted_children })
142                     } @{ $_->sorted_children } ] ]
143           } @{ $self->item->requirement_spec->sections };
144
145     my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
146
147     my $html                  = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
148     my $id_base               = $self->item->get_type . '-' . $self->item->id;
149     my $content_top_id        = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
150
151     $js->hide($content_top_id)
152        ->remove("#edit_${id_base}_form")
153        ->insertAfter($html, $content_top_id)
154        ->jstree->select_node('#tree', '#fb-' . $self->item->id)
155        ->focus("#edit_${id_base}_description")
156        ->val('#current_content_type', $self->item->get_type)
157        ->val('#current_content_id', $self->item->id)
158        ->render($self);
159   }
160 }
161
162 sub action_ajax_update {
163   my ($self, %params) = @_;
164
165   my $js         = SL::ClientJS->new;
166   my $prefix     = $::form->{form_prefix} || 'function_block';
167   my $attributes = $::form->{$prefix}     || {};
168
169   foreach (qw(requirement_spec_id parent_id position)) {
170     delete $attributes->{$_} if !defined $attributes->{$_};
171   }
172
173   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
174   return $js->error(@errors)->render($self) if @errors;
175
176   $self->item->save;
177
178   my $id_prefix    = $self->item->get_type eq 'function-block' ? '' : 'sub-';
179   my $html_top     = $self->render('requirement_spec_item/_function_block_content_top',    { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
180   $id_prefix      .= 'function-block-content-';
181
182   my $js = SL::ClientJS->new
183     ->remove('#' . $prefix . '_form')
184     ->replaceWith('#' . $id_prefix . 'top-' . $self->item->id, $html_top)
185     ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
186
187   $self->replace_bottom($js, $self->item, id_prefix => $id_prefix);
188   $self->replace_bottom($js, $self->item->parent) if $self->item->get_type eq 'sub-function-block';
189
190   $js->render($self);
191 }
192
193 #
194 # filters
195 #
196
197 sub load_requirement_spec {
198   my ($self) = @_;
199   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
200 }
201
202 sub load_requirement_spec_item {
203   my ($self) = @_;
204   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
205 }
206
207 #
208 # helpers
209 #
210
211 sub create_random_id {
212   return join '-', Time::HiRes::gettimeofday();
213 }
214
215 sub format_exception {
216   return join "\n", (split m/\n/, $@)[0..4];
217 }
218
219 sub init_visible_section {
220   my ($self)       = @_;
221
222   my $content_id   = $::form->{current_content_id};
223   my $content_type = $::form->{current_content_type};
224
225   return undef unless $content_id;
226   return undef unless $content_type =~ m/section|function-block/;
227
228   $self->visible_item(SL::DB::RequirementSpecItem->new(id => $content_id)->load);
229   return $self->visible_section($self->visible_item->get_section);
230 }
231
232 sub init_complexities {
233   my ($self) = @_;
234
235   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
236 }
237
238 sub init_risks {
239   my ($self) = @_;
240
241   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
242 }
243
244 sub replace_bottom {
245   my ($self, $js, $item_or_id) = @_;
246
247   my $item      = (ref($item_or_id) ? $item_or_id : SL::DB::RequirementSpecItem->new(id => $item_or_id))->load;
248   my $id_prefix = $item->get_type eq 'function-block' ? '' : 'sub-';
249   my $html      = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $item, id_prefix => $id_prefix);
250   return $js->replaceWith('#' . $id_prefix . 'function-block-content-bottom-' . $item->id, $html);
251 }
252
253 1;