Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / SL / DB / RequirementSpecTextBlock.pm
1 package SL::DB::RequirementSpecTextBlock;
2
3 use strict;
4
5 use Carp;
6 use List::MoreUtils qw(any);
7 use Rose::DB::Object::Helpers;
8 use Rose::DB::Object::Util;
9
10 use SL::Common ();
11 use SL::DB::MetaSetup::RequirementSpecTextBlock;
12 use SL::DB::Manager::RequirementSpecTextBlock;
13 use SL::DB::Helper::ActsAsList;
14 use SL::DB::Helper::AttrHTML;
15 use SL::Locale::String;
16
17 __PACKAGE__->meta->add_relationship(
18   pictures => {
19     type         => 'one to many',
20     class        => 'SL::DB::RequirementSpecPicture',
21     column_map   => { id => 'text_block_id' },
22   },
23 );
24
25 __PACKAGE__->meta->initialize;
26
27 __PACKAGE__->configure_acts_as_list(group_by => [qw(requirement_spec_id output_position)]);
28 __PACKAGE__->attr_html('text');
29
30 __PACKAGE__->before_save(\  &_before_save_invalidate_requirement_spec_version);
31 __PACKAGE__->before_delete(\&_before_delete_invalidate_requirement_spec_version);
32
33 sub validate {
34   my ($self) = @_;
35
36   my @errors;
37   push @errors, t8('The title is missing.') if !$self->title;
38
39   return @errors;
40 }
41
42 sub _before_save_invalidate_requirement_spec_version {
43   my ($self, %params) = @_;
44
45   return 1 if !$self->requirement_spec_id || $self->requirement_spec->working_copy_id;
46
47   my %changed_columns = map { $_ => 1 } (Rose::DB::Object::Helpers::dirty_columns($self));
48
49   $self->requirement_spec->invalidate_version
50     if !Rose::DB::Object::Util::is_in_db($self) || any { $changed_columns{$_} } qw(title text position output_position);
51
52   return 1;
53 }
54
55 sub _before_delete_invalidate_requirement_spec_version {
56   my ($self, %params) = @_;
57
58   $self->requirement_spec->invalidate_version if $self->requirement_spec_id;
59
60   return 1;
61 }
62
63 sub pictures_sorted {
64   my ($self, @args) = @_;
65
66   croak "Not a writer" if @args;
67
68   return [ sort { $a->position <=> $b->position } $self->pictures ];
69 }
70
71 sub content_excerpt {
72   my ($self) = @_;
73
74   return Common::truncate($self->text_as_stripped_html // '', at => 200);
75 }
76
77 1;