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