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