Pflichtenhefte: Kontextmenüs für Items/Textblöcke auf größerer Fläche anzeigen
[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::RequirementSpecPredefinedText;
16 use SL::DB::RequirementSpecTextBlock;
17 use SL::Helper::Flash;
18 use SL::Locale::String;
19
20 use Rose::Object::MakeMethods::Generic
21 (
22   scalar                  => [ qw(text_block) ],
23   'scalar --get_set_init' => [ qw(predefined_texts js) ],
24 );
25
26 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete ajax_flag dragged_and_dropped ajax_copy)]);
27
28 #
29 # actions
30 #
31
32 sub action_ajax_list {
33   my ($self) = @_;
34
35   my $result        = { };
36   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
37   my $new_where;
38
39   if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
40     $new_where = $1 eq 'front' ? 0 : 1;
41
42   } else {
43     $new_where = $self->output_position_from_id($::form->{clicked_id});
44   }
45
46   # $::lxdebug->message(0, "cur $current_where new $new_where");
47
48   $self->show_list(output_position => $new_where, id => $::form->{clicked_id}) if ($new_where != ($current_where // -1));
49
50   $self->render($self->js);
51 }
52
53 sub action_ajax_add {
54   my ($self) = @_;
55
56   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
57   my $new_where     = $self->output_position_from_id($::form->{id})                                                  // $::form->{output_position};
58
59   $self->show_list(output_position => $new_where) if $new_where != $current_where;
60
61   $self->add_new_text_block_form(output_position => $new_where, insert_after_id => $::form->{id}, requirement_spec_id => $::form->{requirement_spec_id});
62
63   $self->invalidate_version->render($self);
64 }
65
66 sub action_ajax_edit {
67   my ($self) = @_;
68
69   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
70
71   if ($self->text_block->output_position != $current_where) {
72     $self->show_list(output_position => $self->text_block->output_position, id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id);
73   }
74
75   my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
76
77   $self->js
78      ->hide('#text-block-' . $self->text_block->id)
79      ->remove('#edit_text_block_' . $self->text_block->id . '_form')
80      ->insertAfter($html, '#text-block-' . $self->text_block->id)
81      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
82      ->focus('#edit_text_block_' . $self->text_block->id . '_title')
83      ->render($self);
84 }
85
86 sub action_ajax_create {
87   my ($self, %params) = @_;
88
89   my $attributes   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
90   my $insert_after = delete $attributes->{insert_after};
91
92   my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
93   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
94
95   $self->text_block->save;
96   $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
97
98   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
99   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
100
101   $self->invalidate_version
102     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
103     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
104     ->jstree->select_node('#tree', '#tb-' . $self->text_block->id);
105   $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)
106     ->render($self);
107 }
108
109 sub action_ajax_update {
110   my ($self, %params) = @_;
111
112   my $prefix     = $::form->{form_prefix} || 'text_block';
113   my $attributes = $::form->{$prefix}     || {};
114
115   foreach (qw(requirement_spec_id output_position position)) {
116     delete $attributes->{$_} if !defined $attributes->{$_};
117   }
118
119   my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
120   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
121
122   $self->text_block->save;
123
124   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
125
126   $self->invalidate_version
127     ->remove('#' . $prefix . '_form')
128     ->replaceWith('#text-block-' . $self->text_block->id, $html)
129     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
130     ->render($self);
131 }
132
133 sub action_ajax_delete {
134   my ($self) = @_;
135
136   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
137   if ($self->text_block->output_position == $current_where) {
138     $self->js
139        ->remove('#edit_text_block_' . $self->text_block->id . '_form')
140        ->remove('#text-block-' . $self->text_block->id);
141
142     $self->js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
143   }
144
145   $self->text_block->delete;
146
147   $self->invalidate_version
148      ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
149      ->render($self);
150 }
151
152 sub action_ajax_flag {
153   my ($self) = @_;
154
155   $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
156
157   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
158
159   SL::ClientJS->new
160    ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
161    ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
162    ->render($self);
163 }
164
165 sub action_dragged_and_dropped {
166   my ($self)       = @_;
167
168   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
169   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
170
171   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
172   my $old_where          = $self->text_block->output_position;
173
174   $self->text_block->db->do_transaction(sub {
175     1;
176     $self->text_block->remove_from_list;
177     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
178     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
179   });
180
181   # $::lxdebug->dump(0, "form", $::form);
182
183   $self->invalidate_version
184     ->jstree->open_node('#tree', '#tb-' . (!$self->text_block->output_position ? 'front' : 'back'));
185
186   return $self->js->render($self) if $::form->{current_content_type} !~ m/^text-block/;
187
188   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
189   my $new_where     = $self->text_block->output_position;
190   my $id            = $self->text_block->id;
191
192   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
193   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
194     # The currently selected text block is dragged to the opposite
195     # text block location. Re-render the whole content column.
196     $self->show_list(output_position => $new_where, id => $id);
197
198   } else {
199     if ($old_where == $current_where) {
200       $self->js->remove('#text-block-' . $self->text_block->id);
201
202       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
203         $self->js->show('#text-block-list-empty');
204       }
205     }
206
207     if ($new_where == $current_where) {
208       $self->js->hide('#text-block-list-empty');
209
210       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
211       $html                =~ s/^\s+//;
212       my $prior_text_block = $self->text_block->get_previous_in_list;
213
214       if ($prior_text_block) {
215         $self->js->insertAfter($html, '#text-block-' . $prior_text_block->id);
216       } else {
217         $self->js->prependTo($html, '#text-block-list');
218       }
219     }
220   }
221
222   $self->js->render($self);
223 }
224
225 sub action_ajax_copy {
226   my ($self, %params) = @_;
227
228   SL::Clipboard->new->copy($self->text_block);
229   SL::ClientJS->new->render($self);
230 }
231
232 sub action_ajax_paste {
233   my ($self, %params) = @_;
234
235   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecTextBlock$/);
236   if (!$copied) {
237     return SL::ClientJS->new
238       ->error(t8("The clipboard does not contain anything that can be pasted here."))
239       ->render($self);
240   }
241
242   my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
243   my $new_output_position     = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
244   my $front_back              = 0 == $new_output_position ? 'front' : 'back';
245
246   $self->text_block($copied->to_object);
247   $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
248   $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
249
250   if ($current_output_position == $new_output_position) {
251     my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
252     $self->js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
253   }
254
255   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
256   $self->invalidate_version
257     ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
258     ->render($self);
259 }
260
261 #
262 # filters
263 #
264
265 sub load_requirement_spec_text_block {
266   my ($self) = @_;
267   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
268 }
269
270 #
271 # helpers
272 #
273
274 sub output_position_from_id {
275   my ($self, $id, $type, %params) = @_;
276
277   if ($type) {
278     return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
279     return undef                 if $type !~ m/text-block/;
280   }
281
282   my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
283
284   return $text_block ? $text_block->output_position : undef;
285 }
286
287 sub init_predefined_texts {
288   return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
289 }
290
291 sub init_js {
292   my ($self) = @_;
293   $self->js(SL::ClientJS->new);
294 }
295
296 sub invalidate_version {
297   my ($self) = @_;
298
299   my $html   = $self->render('requirement_spec/_version', { output => 0 },
300                              requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
301   return $self->js->html('#requirement_spec_version', $html);
302 }
303
304 sub add_new_text_block_form {
305   my ($self, %params) = @_;
306
307   croak "Missing parameter output_position"     unless defined($params{output_position}) && ($params{output_position} ne '');
308   croak "Missing parameter requirement_spec_id" unless $params{requirement_spec_id};
309
310   $self->text_block(SL::DB::RequirementSpecTextBlock->new(
311     requirement_spec_id => $params{requirement_spec_id},
312     output_position     => $params{output_position},
313   ));
314
315   my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
316   my $html    = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $params{insert_after_id});
317
318   $self->js
319      ->action($params{insert_after_id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($params{insert_after_id} || 'list'))
320      ->focus('#' . $id_base . '_title');
321 }
322
323 sub show_list {
324   my $self   = shift;
325   my %params = Params::Validate::validate(@_, { output_position => 1, id => 0, requirement_spec_id => 0, });
326
327   $params{requirement_spec_id} ||= $::form->{requirement_spec_id};
328   croak "Unknown requirement_spec_id" if !$params{requirement_spec_id};
329
330   my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $params{output_position}, requirement_spec_id => $params{requirement_spec_id} ]);
331   my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $params{output_position});
332
333   $self->js->html('#column-content', $html);
334
335   if ($params{id}) {
336     $self->js
337      ->val('#current_content_type', 'text-blocks-' . (0 == $params{output_position} ? 'front' : 'back'))
338      ->val('#current_content_id',   $params{id});
339   }
340
341   return $self->set_function_blocks_tab_menu_class(class => 'text-block-context-menu');
342 }
343
344 1;