Löschen von Textblöcken
[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 SL::ClientJS;
8 use SL::DB::RequirementSpec;
9 use SL::DB::RequirementSpecPredefinedText;
10 use SL::DB::RequirementSpecTextBlock;
11 use SL::Helper::Flash;
12 use SL::JSON;
13 use SL::Locale::String;
14
15 use Rose::Object::MakeMethods::Generic
16 (
17  scalar => [ qw(requirement_spec text_block) ],
18 );
19
20 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete dragged_and_dropped)]);
21
22 #
23 # actions
24 #
25
26 sub action_ajax_list {
27   my ($self) = @_;
28
29   my $result        = { };
30   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
31   my $new_where;
32
33   if ($::form->{clicked_type} =~ m/^textblocks-(front|back)/) {
34     $new_where = $1 eq 'front' ? 0 : 1;
35
36   } else {
37     $new_where = $self->output_position_from_id($::form->{clicked_id});
38   }
39
40   # $::lxdebug->message(0, "cur $current_where new $new_where");
41
42   my $js = SL::ClientJS->new;
43
44   if (!defined($current_where) || ($new_where != $current_where)) {
45     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where, requirement_spec_id => $::form->{requirement_spec_id} ]);
46     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
47
48     $js->html('#column-content', $html)
49   }
50
51   $self->render($js);
52 }
53
54 sub action_ajax_edit {
55   my ($self) = @_;
56
57   my $js = SL::ClientJS->new;
58
59   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
60   if ($self->text_block->output_position != $current_where) {
61     my $text_blocks = $self->text_block->get_full_list;
62     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $self->text_block->output_position);
63
64     $js->html('#column-content', $html)
65        ->val('#current_content_type', 'text-block')
66        ->val('#current_content_id',   $self->text_block->id);
67   }
68
69   my $predefined_texts = SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
70   my $html             = $self->render('requirement_spec_text_block/_form', { output => 0 }, PREDEFINED_TEXTS => $predefined_texts);
71
72   $js->hide('#text-block-' . $self->text_block->id)
73      ->remove('#edit_text_block_' . $self->text_block->id . '_form')
74      ->insertAfter($html, '#text-block-' . $self->text_block->id)
75      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
76      ->render($self);
77 }
78
79 sub action_ajax_update {
80   my ($self, %params) = @_;
81
82   my $prefix     = $::form->{form_prefix} || 'text_block';
83   my $attributes = $::form->{$prefix}     || {};
84
85   foreach (qw(requirement_spec_id output_position)) {
86     delete $attributes->{$_} if !defined $attributes->{$_};
87   }
88
89   $self->text_block->update_attributes(%{ $attributes });
90
91   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
92
93   SL::ClientJS->new
94     ->remove('#' . $prefix . '_form')
95     ->replaceWith('#text-block-' . $self->text_block->id, $html)
96     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
97     ->render($self);
98 }
99
100 sub action_ajax_delete {
101   my ($self) = @_;
102
103   my $js = SL::ClientJS->new;
104
105   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
106   if ($self->text_block->output_position == $current_where) {
107     $js->remove('#edit_text_block_' . $self->text_block->id . '_form')
108        ->remove('#text-block-' . $self->text_block->id);
109
110     $js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
111   }
112
113   $self->text_block->delete;
114
115   $js->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
116      ->render($self);
117 }
118
119 sub action_dragged_and_dropped {
120   my ($self)       = @_;
121
122   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
123   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
124
125   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ textblocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
126   my $old_where          = $self->text_block->output_position;
127
128   $self->text_block->db->do_transaction(sub {
129     1;
130     $self->text_block->remove_from_list;
131     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'textblocks-front' ? 0 : 1);
132     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
133   });
134
135   return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^textblock/;
136
137   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
138   my $new_where     = $self->text_block->output_position;
139   my $id            = $self->text_block->id;
140   my $js            = SL::ClientJS->new;
141
142   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
143   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
144     # The currently selected text block is dragged to the opposite
145     # text block location. Re-render the whole content column.
146     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
147     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
148
149     $js->val('#current_content_type', 'textblocks-' . ($new_where == 0 ? 'front' : 'back'))
150        ->html('#column-content', $html);
151
152   } else {
153     if ($old_where == $current_where) {
154       $js->remove('#text-block-' . $self->text_block->id);
155
156       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
157         $js->show('#text-block-list-empty');
158       }
159     }
160
161     if ($new_where == $current_where) {
162       $js->hide('#text-block-list-empty');
163
164       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
165       $html                =~ s/^\s+//;
166       my $prior_text_block = $self->text_block->get_previous_in_list;
167
168       if ($prior_text_block) {
169         $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
170       } else {
171         $js->appendTo($html, '#text-block-list');
172       }
173     }
174   }
175
176   $self->render($js);
177 }
178
179 #
180 # filters
181 #
182
183 sub load_requirement_spec {
184   my ($self) = @_;
185   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
186 }
187
188 sub load_requirement_spec_text_block {
189   my ($self) = @_;
190   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
191 }
192
193 #
194 # helpers
195 #
196
197 sub output_position_from_id {
198   my ($self, $id, $type, %params) = @_;
199
200   if (!$id) {
201     return $params{default} unless $type =~ m/-(front|back)/;
202     return $1 eq 'front' ? 0 : 1;
203   }
204
205   my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
206
207   return $params{default}             unless $text_block;
208   return $text_block->output_position unless $params{as} eq 'text';
209   return 1 == $text_block->output_position ? 'front' : 'back';
210 }
211
212 1;