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