Merge branch 'test' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / DB / CustomVariableConfig.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::CustomVariableConfig;
5
6 use strict;
7
8 use List::MoreUtils qw(any);
9
10 use SL::DB::MetaSetup::CustomVariableConfig;
11 use SL::DB::Manager::CustomVariableConfig;
12 use SL::DB::Helper::ActsAsList;
13
14 __PACKAGE__->meta->add_relationship(
15   partsgroups  => {
16     type       => 'many to many',
17     map_class  => 'SL::DB::CustomVariableConfigPartsgroup',
18   },
19 );
20
21 __PACKAGE__->meta->initialize;
22
23 __PACKAGE__->configure_acts_as_list(group_by => [qw(module)]);
24
25 sub validate {
26   my ($self) = @_;
27
28   my @errors;
29   push @errors, $::locale->text('The name is missing.')        if !$self->name;
30   push @errors, $::locale->text('The description is missing.') if !$self->description;
31   push @errors, $::locale->text('The type is missing.')        if !$self->type;
32   push @errors, $::locale->text('The option field is empty.')  if (($self->type || '') eq 'select') && !$self->options;
33
34   return @errors;
35 }
36
37 use constant OPTION_DEFAULTS =>
38   {
39     MAXLENGTH => 75,
40     WIDTH     => 225,
41     HEIGHT    => 90,
42   };
43
44 sub processed_options {
45   my ($self) = @_;
46
47   if( exists($self->{processed_options_cache}) ) {
48     return $self->{processed_options_cache};
49   }
50
51   my $ops = $self->options;
52   my $ret;
53
54   if ( $self->type eq 'select' ) {
55     my @op_array = split('##', $ops);
56     $ret = \@op_array;
57   }
58   else {
59     $ret = {%{$self->OPTION_DEFAULTS}};
60     while ( $ops =~ /\s*([^=\s]+)\s*=\s*([^\s]*)(?:\s*|$)/g ) {
61       $ret->{$1} = $2;
62     }
63   }
64
65   $self->{processed_options_cache} = $ret;
66
67   return $ret;
68 }
69
70 sub processed_flags {
71   my ($self) = @_;
72
73   if( exists($self->{processed_flags_cache}) ) {
74     return $self->{processed_flags_cache};
75   }
76
77   my $flags = $self->flags;
78   my $ret = {};
79
80   foreach my $flag (split m/:/, $flags) {
81     if ( $flag =~ m/(.*?)=(.*)/ ) {
82       $ret->{$1} = $2;
83     } else {
84       $ret->{$flag} = 1;
85     }
86   }
87
88   $self->{processed_flags_cache} = $ret;
89
90   return $ret;
91 }
92
93 sub has_flag {
94   my ($self, $flag) = @_;
95
96   return $self->processed_flags()->{$flag};
97 }
98
99 sub type_dependent_default_value {
100   my ($self) = @_;
101
102   return $self->default_value if $self->type ne 'select';
103   return (any { $_ eq $self->default_value } @{ $self->processed_options }) ? $self->default_value : $self->processed_options->[0];
104 }
105
106 sub value_col {
107   my ($self) = @_;
108
109   my $type = $self->type;
110
111   return {
112     bool      => 'bool_value',
113     timestamp => 'timestamp_value',
114     date      => 'timestamp_value',
115     number    => 'number_value',
116     integer   => 'number_value',
117     customer  => 'number_value',
118     vendor    => 'number_value',
119     part      => 'number_value',
120     htmlfield => 'text_value',
121     text      => 'text_value',
122     textfield => 'text_value',
123     select    => 'text_value'
124   }->{$type};
125 }
126
127 1;