Pflichtenhefttextblöcke: Anordnung Bilder via Drag&Drop ändern können
[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 Carp;
8 use Params::Validate ();
9 use Time::HiRes ();
10
11 use SL::ClientJS;
12 use SL::Clipboard;
13 use SL::Controller::Helper::RequirementSpec;
14 use SL::DB::RequirementSpec;
15 use SL::DB::RequirementSpecPicture;
16 use SL::DB::RequirementSpecPredefinedText;
17 use SL::DB::RequirementSpecTextBlock;
18 use SL::Helper::Flash;
19 use SL::Locale::String;
20
21 use constant SORTABLE_PICTURE_LIST => 'kivi.requirement_spec.make_text_block_picture_lists_sortable';
22
23 use Rose::Object::MakeMethods::Generic
24 (
25   scalar                  => [ qw(text_block) ],
26   'scalar --get_set_init' => [ qw(predefined_texts js picture) ],
27 );
28
29 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete ajax_flag dragged_and_dropped ajax_copy ajax_add_picture)]);
30
31 #
32 # actions
33 #
34
35 sub action_ajax_list {
36   my ($self) = @_;
37
38   my $result        = { };
39   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
40   my $new_where;
41
42   if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
43     $new_where = $1 eq 'front' ? 0 : 1;
44
45   } else {
46     $new_where = $self->output_position_from_id($::form->{clicked_id});
47   }
48
49   # $::lxdebug->message(0, "cur $current_where new $new_where");
50
51   $self->show_list(output_position => $new_where, id => $::form->{clicked_id}, set_type => 1) if ($new_where != ($current_where // -1));
52
53   $self->js
54     ->run(SORTABLE_PICTURE_LIST())
55     ->render($self);
56 }
57
58 sub action_ajax_add {
59   my ($self) = @_;
60
61   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
62   my $new_where     = $self->output_position_from_id($::form->{id})                                                  // $::form->{output_position};
63
64   $self->show_list(output_position => $new_where) if $new_where != $current_where;
65
66   $self->add_new_text_block_form(output_position => $new_where, insert_after_id => $::form->{id}, requirement_spec_id => $::form->{requirement_spec_id});
67
68   $self->invalidate_version->render($self);
69 }
70
71 sub action_ajax_edit {
72   my ($self) = @_;
73
74   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
75
76   if ($self->text_block->output_position != $current_where) {
77     $self->show_list(output_position => $self->text_block->output_position, id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id);
78   }
79
80   my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
81
82   $self->js
83      ->hide('#text-block-' . $self->text_block->id)
84      ->remove('#edit_text_block_' . $self->text_block->id . '_form')
85      ->insertAfter($html, '#text-block-' . $self->text_block->id)
86      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
87      ->focus('#edit_text_block_' . $self->text_block->id . '_title')
88      ->render($self);
89 }
90
91 sub action_ajax_create {
92   my ($self, %params) = @_;
93
94   my $attributes   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
95   my $insert_after = delete $attributes->{insert_after};
96
97   my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
98   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
99
100   $self->text_block->save;
101   $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
102
103   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
104   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
105
106   $self->invalidate_version
107     ->hide('#text-block-list-empty')
108     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
109     ->run(SORTABLE_PICTURE_LIST())
110     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
111     ->jstree->select_node('#tree', '#tb-' . $self->text_block->id);
112   $self->add_new_text_block_form(output_position => $self->text_block->output_position, insert_after_id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id)
113     ->render($self);
114 }
115
116 sub action_ajax_update {
117   my ($self, %params) = @_;
118
119   my $prefix     = $::form->{form_prefix} || 'text_block';
120   my $attributes = $::form->{$prefix}     || {};
121
122   foreach (qw(requirement_spec_id output_position position)) {
123     delete $attributes->{$_} if !defined $attributes->{$_};
124   }
125
126   my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
127   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
128
129   $self->text_block->save;
130
131   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
132
133   $self->invalidate_version
134     ->remove('#' . $prefix . '_form')
135     ->replaceWith('#text-block-' . $self->text_block->id, $html)
136     ->run(SORTABLE_PICTURE_LIST())
137     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
138     ->render($self);
139 }
140
141 sub action_ajax_delete {
142   my ($self) = @_;
143
144   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
145   if ($self->text_block->output_position == $current_where) {
146     $self->js
147        ->remove('#edit_text_block_' . $self->text_block->id . '_form')
148        ->remove('#text-block-' . $self->text_block->id);
149
150     $self->js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
151   }
152
153   $self->text_block->delete;
154
155   $self->invalidate_version
156      ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
157      ->render($self);
158 }
159
160 sub action_ajax_flag {
161   my ($self) = @_;
162
163   $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
164
165   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
166
167   SL::ClientJS->new
168    ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
169    ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
170    ->render($self);
171 }
172
173 sub action_dragged_and_dropped {
174   my ($self)       = @_;
175
176   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
177   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
178
179   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
180   my $old_where          = $self->text_block->output_position;
181
182   $self->text_block->db->do_transaction(sub {
183     1;
184     $self->text_block->remove_from_list;
185     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
186     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
187   });
188
189   # $::lxdebug->dump(0, "form", $::form);
190
191   $self->invalidate_version
192     ->jstree->open_node('#tree', '#tb-' . (!$self->text_block->output_position ? 'front' : 'back'));
193
194   return $self->js->render($self) if $::form->{current_content_type} !~ m/^text-block/;
195
196   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
197   my $new_where     = $self->text_block->output_position;
198   my $id            = $self->text_block->id;
199
200   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
201   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
202     # The currently selected text block is dragged to the opposite
203     # text block location. Re-render the whole content column.
204     $self->show_list(output_position => $new_where, id => $id);
205
206   } else {
207     if ($old_where == $current_where) {
208       $self->js->remove('#text-block-' . $self->text_block->id);
209
210       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
211         $self->js->show('#text-block-list-empty');
212       }
213     }
214
215     if ($new_where == $current_where) {
216       $self->js->hide('#text-block-list-empty');
217
218       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
219       $html                =~ s/^\s+//;
220       my $prior_text_block = $self->text_block->get_previous_in_list;
221
222       if ($prior_text_block) {
223         $self->js->insertAfter($html, '#text-block-' . $prior_text_block->id);
224       } else {
225         $self->js->prependTo($html, '#text-block-list');
226       }
227     }
228   }
229
230   $self->js
231     ->run(SORTABLE_PICTURE_LIST())
232     ->render($self);
233 }
234
235 sub action_ajax_copy {
236   my ($self, %params) = @_;
237
238   SL::Clipboard->new->copy($self->text_block);
239   SL::ClientJS->new->render($self);
240 }
241
242 sub action_ajax_paste {
243   my ($self, %params) = @_;
244
245   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpec(?:TextBlock|Picture)$/);
246   if (!$copied) {
247     return SL::ClientJS->new
248       ->error(t8("The clipboard does not contain anything that can be pasted here."))
249       ->render($self);
250   }
251
252   if (ref($copied) =~ m/Picture$/) {
253     $self->load_requirement_spec_text_block;
254     return $self->paste_picture($copied);
255   }
256
257   my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
258   my $new_output_position     = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
259   my $front_back              = 0 == $new_output_position ? 'front' : 'back';
260
261   $self->text_block($copied->to_object);
262   $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
263   $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
264
265   if ($current_output_position == $new_output_position) {
266     my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
267     $self->js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
268   }
269
270   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
271   $self->invalidate_version
272     ->run(SORTABLE_PICTURE_LIST())
273     ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
274     ->render($self);
275 }
276
277 #
278 # actions for pictures
279 #
280
281 sub action_ajax_add_picture {
282   my ($self) = @_;
283
284   $self->picture(SL::DB::RequirementSpecPicture->new);
285   $self->render('requirement_spec_text_block/_picture_form', { layout => 0 });
286 }
287
288 sub action_ajax_edit_picture {
289   my ($self) = @_;
290
291   $self->text_block($self->picture->text_block);
292   $self->render('requirement_spec_text_block/_picture_form', { layout => 0 });
293 }
294
295 sub action_ajax_create_picture {
296   my ($self, %params)              = @_;
297
298   my $attributes                   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
299   $attributes->{picture_file_name} = ((($::form->{ATTACHMENTS} || {})->{ $::form->{form_prefix} } || {})->{picture_content} || {})->{filename};
300   my @errors                       = $self->picture(SL::DB::RequirementSpecPicture->new(%{ $attributes }))->validate;
301
302   return $self->js->error(@errors)->render($self) if @errors;
303
304   $self->picture->save;
305
306   $self->text_block($self->picture->text_block);
307   my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
308
309   $self->invalidate_version
310     ->dialog->close('#jqueryui_popup_dialog')
311     ->append('#text-block-' . $self->text_block->id . '-pictures', $html)
312     ->show('#text-block-' . $self->text_block->id . '-pictures')
313     ->render($self);
314 }
315
316 sub action_ajax_update_picture {
317   my ($self)     = @_;
318
319   my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
320
321   if (!$attributes->{picture_content}) {
322     delete $attributes->{picture_content};
323   } else {
324     $attributes->{picture_file_name} = ((($::form->{ATTACHMENTS} || {})->{ $::form->{form_prefix} } || {})->{picture_content} || {})->{filename};
325   }
326
327   $self->picture->assign_attributes(%{ $attributes });
328   my @errors = $self->picture->validate;
329
330   return $self->js->error(@errors)->render($self) if @errors;
331
332   $self->picture->save;
333
334   $self->text_block($self->picture->text_block);
335   my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
336
337   $self->invalidate_version
338     ->dialog->close('#jqueryui_popup_dialog')
339     ->replaceWith('#text-block-picture-' . $self->picture->id, $html)
340     ->show('#text-block-' . $self->text_block->id . '-pictures')
341     ->render($self);
342 }
343
344 sub action_ajax_delete_picture {
345   my ($self) = @_;
346
347   $self->picture->delete;
348   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $self->picture->text_block_id)->load);
349
350   $self->invalidate_version
351     ->remove('#text-block-picture-' . $self->picture->id)
352     ->action_if(!@{ $self->text_block->pictures }, 'hide', '#text-block-' . $self->text_block->id . '-pictures')
353     ->render($self);
354 }
355
356 sub action_ajax_download_picture {
357   my ($self) = @_;
358
359   $self->send_file(\$self->picture->{picture_content}, type => $self->picture->picture_content_type, name => $self->picture->picture_file_name);
360 }
361
362 sub action_ajax_copy_picture {
363   my ($self, %params) = @_;
364
365   SL::Clipboard->new->copy($self->picture);
366   SL::ClientJS->new->render($self);
367 }
368
369 sub action_ajax_paste_picture {
370   my ($self, %params) = @_;
371
372   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecPicture$/);
373   if (!$copied) {
374     return SL::ClientJS->new
375       ->error(t8("The clipboard does not contain anything that can be pasted here."))
376       ->render($self);
377   }
378
379   $self->text_block($self->picture->text_block);   # Save text block via the picture the user clicked on
380
381   $self->paste_picture($copied);
382 }
383
384 sub action_reorder_pictures {
385   my ($self) = @_;
386
387   SL::DB::RequirementSpecPicture->reorder_list(@{ $::form->{picture_id} || [] });
388
389   $self->render(\'', { type => 'json' });
390 }
391
392 #
393 # filters
394 #
395
396 sub load_requirement_spec_text_block {
397   my ($self) = @_;
398   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
399 }
400
401 #
402 # helpers
403 #
404
405 sub output_position_from_id {
406   my ($self, $id, $type, %params) = @_;
407
408   if ($type) {
409     return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
410     return undef                 if $type !~ m/text-block/;
411   }
412
413   my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
414
415   return $text_block ? $text_block->output_position : undef;
416 }
417
418 sub init_predefined_texts {
419   return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
420 }
421
422 sub init_picture {
423   return SL::DB::RequirementSpecPicture->new(id => $::form->{picture_id} || $::form->{id})->load;
424 }
425
426 sub init_js {
427   my ($self) = @_;
428   $self->js(SL::ClientJS->new);
429 }
430
431 sub invalidate_version {
432   my ($self) = @_;
433
434   my $html   = $self->render('requirement_spec/_version', { output => 0 },
435                              requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
436   return $self->js->html('#requirement_spec_version', $html);
437 }
438
439 sub add_new_text_block_form {
440   my ($self, %params) = @_;
441
442   croak "Missing parameter output_position"     unless defined($params{output_position}) && ($params{output_position} ne '');
443   croak "Missing parameter requirement_spec_id" unless $params{requirement_spec_id};
444
445   $self->text_block(SL::DB::RequirementSpecTextBlock->new(
446     requirement_spec_id => $params{requirement_spec_id},
447     output_position     => $params{output_position},
448   ));
449
450   my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
451   my $html    = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $params{insert_after_id});
452
453   $self->js
454      ->action($params{insert_after_id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($params{insert_after_id} || 'list'))
455      ->focus('#' . $id_base . '_title');
456 }
457
458 sub show_list {
459   my $self   = shift;
460   my %params = Params::Validate::validate(@_, { output_position => 1, id => 0, requirement_spec_id => 0, set_type => 0, });
461
462   $params{requirement_spec_id} ||= $::form->{requirement_spec_id};
463   croak "Unknown requirement_spec_id" if !$params{requirement_spec_id};
464
465   my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $params{output_position}, requirement_spec_id => $params{requirement_spec_id} ]);
466   my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $params{output_position});
467
468   $self->js->html('#column-content', $html);
469
470   $self->js->val('#current_content_type', 'text-blocks-' . (0 == $params{output_position} ? 'front' : 'back')) if $params{id} || $params{set_type};
471   $self->js->val('#current_content_id',   $params{id})                                                         if $params{id};
472
473   return $self->set_function_blocks_tab_menu_class(class => 'text-block-context-menu');
474 }
475
476 sub paste_picture {
477   my ($self, $copied) = @_;
478
479   if (!$self->text_block->db->do_transaction(sub {
480     1;
481     $self->picture($copied->to_object)->save;        # Create new picture from copied data and save
482     $self->text_block->add_pictures($self->picture); # Add new picture to text block
483     $self->text_block->save;
484   })) {
485     $::lxdebug->message(LXDebug::WARN(), "Error: " . $self->text_block->db->error);
486     return $self->js->error($::locale->text('Saving failed. Error message from the database: #1', $self->text_block->db->error))->render($self);
487   }
488
489   my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
490
491   $self->invalidate_version
492     ->append('#text-block-' . $self->text_block->id . '-pictures', $html)
493     ->show('#text-block-' . $self->text_block->id . '-pictures')
494     ->render($self);
495 }
496
497 1;