Pflichtenhefte: Auflisten von Abschnitten
[kivitendo-erp.git] / SL / Controller / RequirementSpecItem.pm
1 package SL::Controller::RequirementSpecItem;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Time::HiRes ();
8
9 use SL::DB::RequirementSpec;
10 use SL::DB::RequirementSpecItem;
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 item visible_item visible_section) ],
18 );
19
20 # __PACKAGE__->run_before('load_requirement_spec');
21 __PACKAGE__->run_before('load_requirement_spec_item', only => [qw(dragged_and_dropped edit_section update_section)]);
22
23 #
24 # actions
25 #
26
27 sub action_ajax_list {
28   my ($self) = @_;
29
30   my $js = SL::ClientJS->new;
31
32   if (!$::form->{clicked_id}) {
33     # Clicked on "sections" in the tree. Do nothing.
34     return $self->render($js);
35   }
36
37   $self->init_visible_section($::form->{current_content_id}, $::form->{current_content_type});
38   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{clicked_id})->load->get_section);
39
40   if (!$self->visible_section || ($self->visible_section->id != $self->item->id)) {
41     my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->item);
42     $js->html('#column-content', $html)
43        ->val('#current_content_type', $self->item->get_type)
44        ->val('#current_content_id', $self->item->id);
45   }
46
47   $self->render($js);
48 }
49
50 sub action_new {
51   my ($self) = @_;
52
53   eval {
54     my $type         = ($::form->{item_type} || '') =~ m/^ (?: section | (?: sub-)? function-block ) $/x ? $::form->{item_type} : die "Invalid item_type";
55     $self->{item}    = SL::DB::RequirementSpecItem->new(requirement_spec_id => $::form->{requirement_spec_id});
56     my $section_form = $self->presenter->render("requirement_spec_item/_${type}_form", id => create_random_id(), title => t8('Create a new section'));
57
58     $self->render(\to_json({ status => 'ok', html => $section_form }), { type => 'json' });
59     1;
60   } or do {
61     $self->render(\to_json({ status => 'failed', error => "Exception:\n" . format_exception() }), { type => 'json' });
62   }
63 }
64
65 sub action_create {
66   my ($self) = @_;
67
68   my $type = ($::form->{item_type} || '') =~ m/^ (?: section | (?: sub-)? function-block ) $/x ? $::form->{item_type} : die "Invalid item_type";
69
70   $self->render(\to_json({ status => 'failed', error => 'not good, not good' }), { type => 'json' });
71 }
72
73 sub action_dragged_and_dropped {
74   my ($self)       = @_;
75
76   $::lxdebug->dump(0, "form", $::form);
77
78   my $dropped_item = SL::DB::RequirementSpecItem->new(id => $::form->{dropped_id})->load || die "No such dropped item";
79   my $position     = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position} : die "Unknown 'position' parameter";
80
81   $self->item->db->do_transaction(sub {
82     $self->item->remove_from_list;
83     $self->item->parent_id($position =~ m/before|after/ ? $dropped_item->parent_id : $dropped_item->id);
84     $self->item->add_to_list(position => $position, reference => $dropped_item->id);
85   });
86
87   $self->render(\'', { type => 'json' });
88 }
89
90 sub action_edit_section {
91   my ($self, %params) = @_;
92   $self->render('requirement_spec_item/_section_form', { layout => 0 });
93 }
94
95 sub action_update_section {
96   my ($self, %params) = @_;
97
98   $self->item->update_attributes(title => $::form->{title}, description => $::form->{description});
99
100   my $result = {
101     id          => $self->item->id,
102     header_html => $self->render('requirement_spec_item/_section_header', { layout => 0, output => 0 }, requirement_spec_item => $self->item),
103     node_name   => join(' ', map { $_ || '' } ($self->item->fb_number, $self->item->title)),
104   };
105   $self->render(\to_json($result), { type => 'json' });
106 }
107
108 #
109 # filters
110 #
111
112 sub load_requirement_spec {
113   my ($self) = @_;
114   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load || die "No such requirement spec");
115 }
116
117 sub load_requirement_spec_item {
118   my ($self) = @_;
119   $self->item(SL::DB::RequirementSpecItem->new(id => $::form->{id})->load || die "No such requirement spec item");
120 }
121
122 #
123 # helpers
124 #
125
126 sub create_random_id {
127   return join '-', Time::HiRes::gettimeofday();
128 }
129
130 sub format_exception {
131   return join "\n", (split m/\n/, $@)[0..4];
132 }
133
134 sub init_visible_section {
135   my ($self, $content_id, $content_type) = @_;
136
137   return undef unless $content_id;
138   return undef unless $content_type =~ m/section|function-block/;
139
140   $self->visible_item(SL::DB::RequirementSpecItem->new(id => $content_id)->load);
141   return $self->visible_section($self->visible_item->get_section);
142 }
143
144 1;