Pflichtenheftitems und Abschnitte bearbeiten
[kivitendo-erp.git] / SL / DB / RequirementSpecItem.pm
1 package SL::DB::RequirementSpecItem;
2
3 use strict;
4
5 use SL::DB::MetaSetup::RequirementSpecItem;
6 use SL::DB::Manager::RequirementSpecItem;
7 use SL::DB::Helper::ActsAsList;
8 use SL::DB::Helper::AttrDuration;
9
10 __PACKAGE__->meta->add_relationship(
11   children     => {
12     type       => 'one to many',
13     class      => 'SL::DB::RequirementSpecItem',
14     column_map => { id => 'parent_id' },
15   },
16   dependencies => {
17     map_class  => 'SL::DB::RequirementSpecDependency',
18     map_from   => 'depending_item',
19     map_to     => 'depended_item',
20     type       => 'many to many',
21   },
22   dependents   => {
23     map_class  => 'SL::DB::RequirementSpecDependency',
24     map_from   => 'depended_item',
25     map_to     => 'depending_item',
26     type       => 'many to many',
27   },
28 );
29
30 __PACKAGE__->meta->initialize;
31
32 __PACKAGE__->configure_acts_as_list(group_by => [qw(requirement_spec_id parent_id)]);
33 __PACKAGE__->attr_duration(qw(time_estimation));
34
35 __PACKAGE__->before_delete(\&_before_delete_delete_children);
36
37 sub _before_delete_delete_children {
38   my ($self) = @_;
39
40   foreach my $child (@{ SL::DB::Manager::RequirementSpecItem->get_all(where => [ parent_id => $self->id ]) }) {
41     my $result = $child->delete;
42     return $result if !$result;
43   }
44
45   1;
46 }
47
48 sub validate {
49   my ($self) = @_;
50
51   my @errors;
52   push @errors, t8('The title is missing.') if !$self->parent_id && !$self->title;
53
54   return @errors;
55 }
56
57 sub sorted_children {
58   my ($self) = @_;
59
60   return [ sort { $a->position <=> $b->position } @{ $self->children } ];
61 }
62
63 sub get_section {
64   my ($self) = @_;
65
66   $self = $self->parent while $self->parent_id;
67
68   return $self;
69 }
70
71 sub get_type {
72   my ($self) = @_;
73
74   return 'section' if !$self->parent_id;
75   return $self->parent->parent_id ? 'sub-function-block' : 'function-block';
76 }
77
78 1;