Pflichtenheftitem: nach Update evtl. auch Parent-Bottom neu rendern
[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 Time::HiRes ();
8
9 use SL::DB::RequirementSpec;
10 use SL::DB::RequirementSpecComplexity;
11 use SL::DB::RequirementSpecItem;
12 use SL::DB::RequirementSpecRisk;
13 use SL::Helper::Flash;
14 use SL::JSON;
15 use SL::Locale::String;
16
17 use Rose::Object::MakeMethods::Generic
18 (
19   scalar                  => [ qw(requirement_spec item visible_item visible_section) ],
20   'scalar --get_set_init' => [ qw(complexities risks) ],
21 );
22
23 # __PACKAGE__->run_before('load_requirement_spec');
24 __PACKAGE__->run_before('load_requirement_spec_item', only => [qw(dragged_and_dropped ajax_update ajax_edit)]);
25
26 #
27 # actions
28 #
29
30 sub action_ajax_list {
31   my ($self) = @_;
32
33   my $js = SL::ClientJS->new;
34
35   if (!$::form->{clicked_id}) {
36     # Clicked on "sections" in the tree. Do nothing.
37     return $self->render($js);
38   }
39
40   $self->init_visible_section($::form->{current_content_id}, $::form->{current_content_type});
41   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
42
43   if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
44     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
45     $js->html('#column-content', $html)
46        ->val('#current_content_type', $self->item->get_type)
47        ->val('#current_content_id', $self->item->id);
48   }
49
50   $self->render($js);
51 }
52
53 sub action_dragged_and_dropped {
54   my ($self)       = @_;
55
56   my $dropped_item = SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load || die "No such dropped item";
57   my $position     = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
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 => $dropped_item->id);
63   });
64
65   $self->render(\'', { type => 'json' });
66 }
67
68 sub action_ajax_edit {
69   my ($self, %params) = @_;
70
71   $::lxdebug->dump(0, "form", $::form);
72
73   $self->init_visible_section($::form->{current_content_id}, $::form->{current_content_type});
74   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load);
75
76   my $js = SL::ClientJS->new;
77
78   die "TODO: edit section" if $self->item->get_type =~ m/section/;
79
80   if (!$self->visible_section || ($self->visible_section->id != $self->item->get_section->id)) {
81     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
82     $js->html('#column-content', $html);
83   }
84
85   if ($self->item->get_type =~ m/function-block/) {
86     my $create_item = sub {
87       [ $_[0]->id, $self->presenter->truncate(join(' ', grep { $_ } ($_[1], $_[0]->fb_number, $_[0]->description))) ]
88     };
89     my @dependencies =
90       map { [ $_->fb_number . ' ' . $_->title,
91               [ map { ( $create_item->($_),
92                         map { $create_item->($_, '->') } @{ $_->sorted_children })
93                     } @{ $_->sorted_children } ] ]
94           } @{ $self->item->requirement_spec->sections };
95
96     my @selected_dependencies = map { $_->id } @{ $self->item->dependencies };
97
98     my $html                  = $self->render('requirement_spec_item/_function_block_form', { output => 0 }, DEPENDENCIES => \@dependencies, SELECTED_DEPENDENCIES => \@selected_dependencies);
99     my $id_base               = $self->item->get_type . '-' . $self->item->id;
100     my $content_top_id        = '#' . $self->item->get_type . '-content-top-' . $self->item->id;
101
102     $js->hide($content_top_id)
103        ->remove("#edit_${id_base}_form")
104        ->insertAfter($html, $content_top_id)
105        ->jstree->select_node('#tree', '#fb-' . $self->item->id)
106        ->focus("#edit_${id_base}_description")
107        ->val('#current_content_type', $self->item->get_type)
108        ->val('#current_content_id', $self->item->id)
109        ->render($self);
110   }
111 }
112
113 sub action_ajax_update {
114   my ($self, %params) = @_;
115
116   my $js         = SL::ClientJS->new;
117   my $prefix     = $::form->{form_prefix} || 'text_block';
118   my $attributes = $::form->{$prefix}     || {};
119
120   foreach (qw(requirement_spec_id parent_id position)) {
121     delete $attributes->{$_} if !defined $attributes->{$_};
122   }
123
124   my @errors = $self->item->assign_attributes(%{ $attributes })->validate;
125   return $js->error(@errors)->render($self) if @errors;
126
127   $self->item->save;
128
129   my $id_prefix    = $self->item->get_type eq 'function-block' ? '' : 'sub-';
130   my $html_top     = $self->render('requirement_spec_item/_function_block_content_top',    { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
131   my $html_bottom  = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $self->item, id_prefix => $id_prefix);
132   $id_prefix      .= 'function-block-content-';
133
134   my $js = SL::ClientJS->new
135     ->remove('#' . $prefix . '_form')
136     ->replaceWith('#' . $id_prefix . 'top-'    . $self->item->id, $html_top)
137     ->replaceWith('#' . $id_prefix . 'bottom-' . $self->item->id, $html_bottom)
138     ->jstree->rename_node('#tree', '#fb-' . $self->item->id, $::request->presenter->requirement_spec_item_tree_node_title($self->item));
139
140
141   if ($self->item->get_type eq 'sub-function-block') {
142     my $parent_html_bottom = $self->render('requirement_spec_item/_function_block_content_bottom', { output => 0 }, requirement_spec_item => $self->item->parent->load);
143     $js->replaceWith('#function-block-content-bottom-' . $self->item->parent->id, $parent_html_bottom);
144   }
145
146   $js->render($self);
147 }
148
149 #
150 # filters
151 #
152
153 sub load_requirement_spec {
154   my ($self) = @_;
155   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
156 }
157
158 sub load_requirement_spec_item {
159   my ($self) = @_;
160   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
161 }
162
163 #
164 # helpers
165 #
166
167 sub create_random_id {
168   return join '-', Time::HiRes::gettimeofday();
169 }
170
171 sub format_exception {
172   return join "\n", (split m/\n/, $@)[0..4];
173 }
174
175 sub init_visible_section {
176   my ($self, $content_id, $content_type) = @_;
177
178   return undef unless $content_id;
179   return undef unless $content_type =~ m/section|function-block/;
180
181   $self->visible_item(SL::DB::RequirementSpecItem->new(id => $content_id)->load);
182   return $self->visible_section($self->visible_item->get_section);
183 }
184
185 sub init_complexities {
186   my ($self) = @_;
187
188   return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
189 }
190
191 sub init_risks {
192   my ($self) = @_;
193
194   return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
195 }
196
197 1;