9322c32b76b3fc8f4ba2bc700086eadc625fa550
[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 );
16
17 __PACKAGE__->meta->initialize;
18
19 __PACKAGE__->configure_acts_as_list(group_by => [qw(requirement_spec_id parent_id)]);
20
21 __PACKAGE__->before_delete(\&_before_delete_delete_children);
22
23 sub _before_delete_delete_children {
24   my ($self) = @_;
25
26   foreach my $child (@{ SL::DB::Manager::RequirementSpecItem->get_all(where => [ parent_id => $self->id ]) }) {
27     my $result = $child->delete;
28     return $result if !$result;
29   }
30
31   1;
32 }
33
34 sub sorted_children {
35   my ($self) = @_;
36
37   return [ sort { $a->position <=> $b->position } @{ $self->children } ];
38 }
39
40 1;