fb1f36445d879cf20bc4cdfdd3a10d338491d6a8
[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 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   }
52
53   $self->render($js);
54 }
55
56 sub action_ajax_add {
57   my ($self) = @_;
58
59   my $js            = SL::ClientJS->new;
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     $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   $js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'))
80      ->focus('#' . $id_base . '_title')
81      ->render($self);
82 }
83
84 sub action_ajax_edit {
85   my ($self) = @_;
86
87   my $js = SL::ClientJS->new;
88
89   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
90   if ($self->text_block->output_position != $current_where) {
91     my $text_blocks = $self->text_block->get_full_list;
92     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
93
94     $js->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   $js->hide('#text-block-' . $self->text_block->id)
102      ->remove('#edit_text_block_' . $self->text_block->id . '_form')
103      ->insertAfter($html, '#text-block-' . $self->text_block->id)
104      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
105      ->focus('#edit_text_block_' . $self->text_block->id . '_title')
106      ->render($self);
107 }
108
109 sub action_ajax_create {
110   my ($self, %params) = @_;
111
112   my $attributes   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
113   my $insert_after = delete $attributes->{insert_after};
114
115   my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
116   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
117
118   $self->text_block->save;
119   $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
120
121   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
122   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
123
124   SL::ClientJS->new
125     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
126     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
127     ->render($self);
128 }
129
130 sub action_ajax_update {
131   my ($self, %params) = @_;
132
133   my $prefix     = $::form->{form_prefix} || 'text_block';
134   my $attributes = $::form->{$prefix}     || {};
135
136   foreach (qw(requirement_spec_id output_position position)) {
137     delete $attributes->{$_} if !defined $attributes->{$_};
138   }
139
140   my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
141   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
142
143   $self->text_block->save;
144
145   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
146
147   SL::ClientJS->new
148     ->remove('#' . $prefix . '_form')
149     ->replaceWith('#text-block-' . $self->text_block->id, $html)
150     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
151     ->render($self);
152 }
153
154 sub action_ajax_delete {
155   my ($self) = @_;
156
157   my $js = SL::ClientJS->new;
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     $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
162        ->remove('#text-block-' . $self->text_block->id);
163
164     $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
165   }
166
167   $self->text_block->delete;
168
169   $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
170      ->render($self);
171 }
172
173 sub action_dragged_and_dropped {
174   my ($self)       = @_;
175
176   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
177   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
178
179   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
180   my $old_where          = $self->text_block->output_position;
181
182   $self->text_block->db->do_transaction(sub {
183     1;
184     $self->text_block->remove_from_list;
185     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
186     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
187   });
188
189   # $::lxdebug->dump(0, "form", $::form);
190
191   return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^text-block/;
192
193   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
194   my $new_where     = $self->text_block->output_position;
195   my $id            = $self->text_block->id;
196   my $js            = SL::ClientJS->new;
197
198   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
199   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
200     # The currently selected text block is dragged to the opposite
201     # text block location. Re-render the whole content column.
202     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
203     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
204
205     $js->val('#current_content_type', 'text-blocks-' . ($new_where == 0 ? 'front' : 'back'))
206        ->html('#column-content', $html);
207
208   } else {
209     if ($old_where == $current_where) {
210       $js->remove('#text-block-' . $self->text_block->id);
211
212       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
213         $js->show('#text-block-list-empty');
214       }
215     }
216
217     if ($new_where == $current_where) {
218       $js->hide('#text-block-list-empty');
219
220       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
221       $html                =~ s/^\s+//;
222       my $prior_text_block = $self->text_block->get_previous_in_list;
223
224       if ($prior_text_block) {
225         $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
226       } else {
227         $js->appendTo($html, '#text-block-list');
228       }
229     }
230   }
231
232   $self->render($js);
233 }
234
235 #
236 # filters
237 #
238
239 sub load_requirement_spec {
240   my ($self) = @_;
241   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
242 }
243
244 sub load_requirement_spec_text_block {
245   my ($self) = @_;
246   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
247 }
248
249 #
250 # helpers
251 #
252
253 sub output_position_from_id {
254   my ($self, $id, $type, %params) = @_;
255
256   if ($type) {
257     return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
258     return undef                 if $type !~ m/text-block/;
259   }
260
261   my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
262
263   return $text_block ? $text_block->output_position : undef;
264 }
265
266 sub init_predefined_texts {
267   return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
268 }
269
270 1;