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