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