Pflichtenhefte: nach Änderung an Textblöcken Version invalidieren
[kivitendo-erp.git] / SL / Controller / RequirementSpecTextBlock.pm
1 package SL::Controller::RequirementSpecTextBlock;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Time::HiRes ();
8
9 use SL::ClientJS;
10 use SL::Clipboard;
11 use SL::DB::RequirementSpec;
12 use SL::DB::RequirementSpecPredefinedText;
13 use SL::DB::RequirementSpecTextBlock;
14 use SL::Helper::Flash;
15 use SL::Locale::String;
16
17 use Rose::Object::MakeMethods::Generic
18 (
19   scalar                  => [ qw(text_block) ],
20   'scalar --get_set_init' => [ qw(predefined_texts) ],
21 );
22
23 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete ajax_flag dragged_and_dropped ajax_copy)]);
24
25 #
26 # actions
27 #
28
29 sub action_ajax_list {
30   my ($self) = @_;
31
32   my $result        = { };
33   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
34   my $new_where;
35
36   if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
37     $new_where = $1 eq 'front' ? 0 : 1;
38
39   } else {
40     $new_where = $self->output_position_from_id($::form->{clicked_id});
41   }
42
43   # $::lxdebug->message(0, "cur $current_where new $new_where");
44
45   my $js = SL::ClientJS->new;
46
47   if (!defined($current_where) || ($new_where != $current_where)) {
48     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
49     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
50
51     $js->html('#column-content', $html)
52        ->val('#current_content_type', 'text-blocks-' . (0 == $new_where ? 'front' : 'back'))
53        ->val('#current_content_id',   $::form->{clicked_id});
54   }
55
56   $self->render($js);
57 }
58
59 sub action_ajax_add {
60   my ($self) = @_;
61
62   my $js            = SL::ClientJS->new;
63
64   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
65   my $new_where     = $self->output_position_from_id($::form->{id})                                                  // $::form->{output_position};
66
67   if ($new_where != $current_where) {
68     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
69     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
70
71     $js->html('#column-content', $html);
72   }
73
74   $self->text_block(SL::DB::RequirementSpecTextBlock->new(
75     requirement_spec_id => $::form->{requirement_spec_id},
76     output_position     => $::form->{output_position},
77   ));
78
79   my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
80   my $html    = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $::form->{id});
81
82   $self->invalidate_version($js)
83      ->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
84      ->focus('#' . $id_base . '_title')
85      ->render($self);
86 }
87
88 sub action_ajax_edit {
89   my ($self) = @_;
90
91   my $js = SL::ClientJS->new;
92
93   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
94   if ($self->text_block->output_position != $current_where) {
95     my $text_blocks = $self->text_block->get_full_list;
96     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
97
98     $js->html('#column-content', $html)
99        ->val('#current_content_type', 'text-block')
100        ->val('#current_content_id',   $self->text_block->id);
101   }
102
103   my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
104
105   $js->hide('#text-block-' . $self->text_block->id)
106      ->remove('#edit_text_block_' . $self->text_block->id . '_form')
107      ->insertAfter($html, '#text-block-' . $self->text_block->id)
108      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
109      ->focus('#edit_text_block_' . $self->text_block->id . '_title')
110      ->render($self);
111 }
112
113 sub action_ajax_create {
114   my ($self, %params) = @_;
115
116   my $attributes   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
117   my $insert_after = delete $attributes->{insert_after};
118
119   my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
120   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
121
122   $self->text_block->save;
123   $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
124
125   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
126   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
127
128   $self->invalidate_version
129     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
130     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
131     ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
132     ->render($self);
133 }
134
135 sub action_ajax_update {
136   my ($self, %params) = @_;
137
138   my $prefix     = $::form->{form_prefix} || 'text_block';
139   my $attributes = $::form->{$prefix}     || {};
140
141   foreach (qw(requirement_spec_id output_position position)) {
142     delete $attributes->{$_} if !defined $attributes->{$_};
143   }
144
145   my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
146   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
147
148   $self->text_block->save;
149
150   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
151
152   $self->invalidate_version
153     ->remove('#' . $prefix . '_form')
154     ->replaceWith('#text-block-' . $self->text_block->id, $html)
155     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
156     ->render($self);
157 }
158
159 sub action_ajax_delete {
160   my ($self) = @_;
161
162   my $js = SL::ClientJS->new;
163
164   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
165   if ($self->text_block->output_position == $current_where) {
166     $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
167        ->remove('#text-block-' . $self->text_block->id);
168
169     $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
170   }
171
172   $self->text_block->delete;
173
174   $self->invalidate_version($js)
175      ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
176      ->render($self);
177 }
178
179 sub action_ajax_flag {
180   my ($self) = @_;
181
182   $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
183
184   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
185
186   SL::ClientJS->new
187    ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
188    ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
189    ->render($self);
190 }
191
192 sub action_dragged_and_dropped {
193   my ($self)       = @_;
194
195   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
196   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
197
198   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
199   my $old_where          = $self->text_block->output_position;
200
201   $self->text_block->db->do_transaction(sub {
202     1;
203     $self->text_block->remove_from_list;
204     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
205     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
206   });
207
208   # $::lxdebug->dump(0, "form", $::form);
209
210   return $self->invalidate_version->render($self) if $::form->{current_content_type} !~ m/^text-block/;
211
212   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
213   my $new_where     = $self->text_block->output_position;
214   my $id            = $self->text_block->id;
215   my $js            = SL::ClientJS->new;
216
217   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
218   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
219     # The currently selected text block is dragged to the opposite
220     # text block location. Re-render the whole content column.
221     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
222     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
223
224     $js->val('#current_content_type', 'text-blocks-' . ($new_where == 0 ? 'front' : 'back'))
225        ->html('#column-content', $html);
226
227   } else {
228     if ($old_where == $current_where) {
229       $js->remove('#text-block-' . $self->text_block->id);
230
231       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
232         $js->show('#text-block-list-empty');
233       }
234     }
235
236     if ($new_where == $current_where) {
237       $js->hide('#text-block-list-empty');
238
239       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
240       $html                =~ s/^\s+//;
241       my $prior_text_block = $self->text_block->get_previous_in_list;
242
243       if ($prior_text_block) {
244         $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
245       } else {
246         $js->prependTo($html, '#text-block-list');
247       }
248     }
249   }
250
251   $self->invalidate_version($js)
252     ->render($self);
253 }
254
255 sub action_ajax_copy {
256   my ($self, %params) = @_;
257
258   SL::Clipboard->new->copy($self->text_block);
259   SL::ClientJS->new->render($self);
260 }
261
262 sub action_ajax_paste {
263   my ($self, %params) = @_;
264
265   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecTextBlock$/);
266   if (!$copied) {
267     return SL::ClientJS->new
268       ->error(t8("The clipboard does not contain anything that can be pasted here."))
269       ->render($self);
270   }
271
272   my $js                      = SL::ClientJS->new;
273   my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
274   my $new_output_position     = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
275   my $front_back              = 0 == $new_output_position ? 'front' : 'back';
276
277   $self->text_block($copied->to_object);
278   $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
279   $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
280
281   if ($current_output_position == $new_output_position) {
282     my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
283     $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
284   }
285
286   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
287   $self->invalidate_version($js)
288     ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
289     ->render($self);
290 }
291
292 #
293 # filters
294 #
295
296 sub load_requirement_spec_text_block {
297   my ($self) = @_;
298   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
299 }
300
301 #
302 # helpers
303 #
304
305 sub output_position_from_id {
306   my ($self, $id, $type, %params) = @_;
307
308   if ($type) {
309     return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
310     return undef                 if $type !~ m/text-block/;
311   }
312
313   my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
314
315   return $text_block ? $text_block->output_position : undef;
316 }
317
318 sub init_predefined_texts {
319   return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
320 }
321
322 sub invalidate_version {
323   my ($self, $js) = @_;
324
325   $js           ||= SL::ClientJS->new;
326   my $html        = $self->render('requirement_spec/_version', { output => 0 },
327                                   requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
328   return $js->html('#requirement_spec_version', $html);
329 }
330
331 1;