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