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