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