marei: new koma-names + fallback for outdated versions
[kivitendo-erp.git] / SL / Controller / RequirementSpecVersion.pm
1 package SL::Controller::RequirementSpecVersion;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Carp;
8 use List::MoreUtils qw(any);
9
10 use SL::DB::Customer;
11 use SL::DB::Project;
12 use SL::DB::RequirementSpec;
13 use SL::DB::RequirementSpecVersion;
14 use SL::Helper::Flash;
15 use SL::Locale::String;
16
17 use Rose::Object::MakeMethods::Generic
18 (
19   'scalar --get_set_init' => [ qw(requirement_spec version) ],
20 );
21
22 __PACKAGE__->run_before('check_auth');
23
24 #
25 # actions
26 #
27
28 sub action_list {
29   my ($self, %params) = @_;
30
31   $self->render('requirement_spec_version/list', { layout => 0 });
32 }
33
34 sub action_new {
35   my ($self) = @_;
36
37   $self->version(SL::DB::RequirementSpecVersion->new);
38
39   my $previous_version = $self->requirement_spec->highest_version;
40
41   if (!$previous_version) {
42     $self->version->description(t8('Initial version.'));
43
44   } else {
45     my %differences = $self->calculate_differences(current => $self->requirement_spec, previous => $previous_version);
46
47     my @lines;
48
49     my $fb_diff = $differences{function_blocks};
50     push @lines, t8('Added sections and function blocks: #1',   $::locale->language_join([ map { $_->fb_number         } @{ $fb_diff->{additions} } ])) if @{ $fb_diff->{additions} };
51     push @lines, t8('Changed sections and function blocks: #1', $::locale->language_join([ map { $_->fb_number         } @{ $fb_diff->{changes}   } ])) if @{ $fb_diff->{changes}   };
52     push @lines, t8('Removed sections and function blocks: #1', $::locale->language_join([ map { $_->fb_number         } @{ $fb_diff->{removals}  } ])) if @{ $fb_diff->{removals}  };
53
54     my $tb_diff = $differences{text_blocks};
55     push @lines, t8('Added text blocks: #1',                    $::locale->language_join([ map { '"' . $_->title . '"' } @{ $tb_diff->{additions} } ])) if @{ $tb_diff->{additions} };
56     push @lines, t8('Changed text blocks: #1',                  $::locale->language_join([ map { '"' . $_->title . '"' } @{ $tb_diff->{changes}   } ])) if @{ $tb_diff->{changes}   };
57     push @lines, t8('Removed text blocks: #1',                  $::locale->language_join([ map { '"' . $_->title . '"' } @{ $tb_diff->{removals}  } ])) if @{ $tb_diff->{removals}  };
58
59     $self->version->description(@lines ? join("\n", @lines) : t8('No changes since previous version.'));
60   }
61
62   $self->render('requirement_spec_version/new', { layout => 0 });
63 }
64
65 sub action_create {
66   my ($self, %params) = @_;
67
68   my %attributes = %{ delete($::form->{rs_version}) || {} };
69   my @errors     = SL::DB::RequirementSpecVersion->new(%attributes, version_number => 1)->validate;
70
71   return $self->js->error(@errors)->render if @errors;
72
73   my $db     = $self->requirement_spec->db;
74   my @result = $self->requirement_spec->create_version(%attributes);
75
76   if (!@result) {
77     $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
78     return $self->js->error($::locale->text('Saving failed. Error message from the database: #1'), $db->error)->render;
79   }
80
81   $self->version($result[0]);
82   my $version_info_html = $self->render('requirement_spec/_version',     { output => 0 }, requirement_spec => $self->requirement_spec);
83   my $version_list_html = $self->render('requirement_spec_version/list', { output => 0 });
84
85   $self->js
86     ->html('#requirement_spec_version', $version_info_html)
87     ->html('#versioned_copies_list',    $version_list_html)
88     ->dialog->close('#jqueryui_popup_dialog')
89     ->render;
90 }
91
92 #
93 # filters
94 #
95
96 sub check_auth {
97   my ($self, %params) = @_;
98   $::auth->assert('requirement_spec_edit');
99 }
100
101 #
102 # helpers
103 #
104
105 sub init_requirement_spec {
106   my ($self) = @_;
107   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load) if $::form->{requirement_spec_id};
108 }
109
110 sub init_version {
111   my ($self) = @_;
112   $self->version(SL::DB::RequirementSpecVersion->new(id => $::form->{id})->load) if $::form->{id};
113 }
114
115 sub has_item_changed {
116   my ($previous, $current) = @_;
117   croak "Missing previous/current" if !$previous || !$current;
118
119   return 1 if any { ($previous->$_ || '') ne ($current->$_ || '') } qw(item_type fb_number title description complexity_id risk_id);
120   return 0 if !$current->parent_id;
121   return $previous->parent->fb_number ne $current->parent->fb_number;
122 }
123
124 sub has_text_block_changed {
125   my ($previous, $current) = @_;
126   croak "Missing previous/current" if !$previous || !$current;
127   return any { ($previous->$_ || '') ne ($current->$_ || '') } qw(title text);
128 }
129
130 sub compare_items {
131   return -1 if ($a->item_type eq 'section') && ($b->item_type ne 'section');
132   return +1 if ($a->item_type ne 'section') && ($b->item_type eq 'section');
133   return $a->fb_number cmp $b->fb_number;
134 }
135
136 sub calculate_differences {
137   my ($self, %params) = @_;
138
139   my %differences = (
140     function_blocks => {
141       additions => [],
142       changes   => [],
143       removals  => [],
144     },
145     text_blocks => {
146       additions => [],
147       changes   => [],
148       removals  => [],
149     },
150   );
151
152   return %differences if !$params{previous} || !$params{current};
153
154   my @previous_items                         = sort compare_items @{ $params{previous}->items };
155   my @current_items                          = sort compare_items @{ $params{current}->items  };
156
157   my @previous_text_blocks                   = sort { lc $a->title cmp lc $b->title } @{ $params{previous}->text_blocks };
158   my @current_text_blocks                    = sort { lc $a->title cmp lc $b->title } @{ $params{current}->text_blocks  };
159
160   my %previous_items_map                     = map { $_->fb_number => $_ } @previous_items;
161   my %current_items_map                      = map { $_->fb_number => $_ } @current_items;
162
163   my %previous_text_blocks_map               = map { $_->title     => $_ } @previous_text_blocks;
164   my %current_text_blocks_map                = map { $_->title     => $_ } @current_text_blocks;
165
166   $differences{function_blocks}->{additions} = [ grep { !$previous_items_map{ $_->fb_number }                                                                         } @current_items        ];
167   $differences{function_blocks}->{removals}  = [ grep { !$current_items_map{  $_->fb_number }                                                                         } @previous_items       ];
168   $differences{function_blocks}->{changes}   = [ grep {  $previous_items_map{ $_->fb_number }       && has_item_changed($previous_items_map{ $_->fb_number }, $_)     } @current_items        ];
169
170   $differences{text_blocks}->{additions}     = [ grep { !$previous_text_blocks_map{ $_->title }                                                                       } @current_text_blocks  ];
171   $differences{text_blocks}->{removals}      = [ grep { !$current_text_blocks_map{  $_->title }                                                                       } @previous_text_blocks ];
172   $differences{text_blocks}->{changes}       = [ grep {  $previous_text_blocks_map{ $_->title } && has_text_block_changed($previous_text_blocks_map{ $_->title }, $_) } @current_text_blocks  ];
173
174   return %differences;
175 }
176
177 1;