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