Pflichtenhefte: Drag & Drop 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::RequirementSpecTextBlock;
10 use SL::Helper::Flash;
11 use SL::JSON;
12 use SL::Locale::String;
13
14 use Rose::Object::MakeMethods::Generic
15 (
16  scalar => [ qw(requirement_spec text_block) ],
17 );
18
19 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(dragged_and_dropped)]);
20
21 #
22 # actions
23 #
24
25 sub action_ajax_list {
26   my ($self) = @_;
27
28   my $result        = { };
29   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
30   my $new_where;
31
32   if ($::form->{clicked_type} =~ m/^textblocks-(front|back)/) {
33     $new_where = $1 eq 'front' ? 0 : 1;
34
35   } else {
36     $new_where = $self->output_position_from_id($::form->{clicked_id});
37   }
38
39   # $::lxdebug->message(0, "cur $current_where new $new_where");
40
41   my $js = SL::ClientJS->new;
42
43   if (!defined($current_where) || ($new_where != $current_where)) {
44     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
45     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where, nownow => DateTime->now_local);
46
47     $js->html('#column-content', $html)
48   }
49
50   $self->render($js);
51 }
52
53 sub action_dragged_and_dropped {
54   my ($self)       = @_;
55
56   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
57   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
58
59   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ textblocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
60   my $old_where          = $self->text_block->output_position;
61
62   $self->text_block->db->do_transaction(sub {
63     1;
64     $self->text_block->remove_from_list;
65     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'textblocks-front' ? 0 : 1);
66     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
67   });
68
69   return $self->render(\'', { type => 'json' }) if $::form->{current_content_type} !~ m/^textblock/;
70
71   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
72   my $new_where     = $self->text_block->output_position;
73   my $id            = $self->text_block->id;
74   my $js            = SL::ClientJS->new;
75
76   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
77   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
78     # The currently selected text block is dragged to the opposite
79     # text block location. Re-render the whole content column.
80     my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $new_where ]);
81     my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $new_where);
82
83     $js->val('#current_content_type', 'textblocks-' . ($new_where == 0 ? 'front' : 'back'))
84        ->html('#column-content', $html);
85
86   } else {
87     if ($old_where == $current_where) {
88       $js->remove('#text-block-' . $self->text_block->id);
89
90       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
91         $js->show('#text-block-list-empty');
92       }
93     }
94
95     if ($new_where == $current_where) {
96       $js->hide('#text-block-list-empty');
97
98       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
99       $html                =~ s/^\s+//;
100       my $prior_text_block = $self->text_block->get_previous_in_list;
101
102       if ($prior_text_block) {
103         $js->insertAfter($html, '#text-block-' . $prior_text_block->id);
104       } else {
105         $js->appendTo($html, '#text-block-list');
106       }
107     }
108   }
109
110   $::lxdebug->message(0, "old $old_where current $current_where new $new_where");
111
112   $::lxdebug->dump(0, "actions", $js);
113
114   $self->render($js);
115 }
116
117 #
118 # filters
119 #
120
121 sub load_requirement_spec {
122   my ($self) = @_;
123   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
124 }
125
126 sub load_requirement_spec_text_block {
127   my ($self) = @_;
128   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
129 }
130
131 #
132 # helpers
133 #
134
135 sub output_position_from_id {
136   my ($self, $id, $type, %params) = @_;
137
138   if (!$id) {
139     return $params{default} unless $type =~ m/-(front|back)/;
140     return $1 eq 'front' ? 0 : 1;
141   }
142
143   my $text_block = SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id);
144
145   return $params{default}             unless $text_block;
146   return $text_block->output_position unless $params{as} eq 'text';
147   return 1 == $text_block->output_position ? 'front' : 'back';
148 }
149
150 1;