CVar-Accessor für Optionen und Flags
[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 SL::DB::MetaSetup::CustomVariableConfig;
9 use SL::DB::Manager::CustomVariableConfig;
10 use SL::DB::Helper::ActsAsList;
11
12 __PACKAGE__->meta->initialize;
13
14 __PACKAGE__->configure_acts_as_list(group_by => [qw(module)]);
15
16 sub validate {
17   my ($self) = @_;
18
19   my @errors;
20   push @errors, $::locale->text('The name is missing.')        if !$self->name;
21   push @errors, $::locale->text('The description is missing.') if !$self->description;
22   push @errors, $::locale->text('The type is missing.')        if !$self->type;
23   push @errors, $::locale->text('The option field is empty.')  if (($self->type || '') eq 'select') && !$self->options;
24
25   return @errors;
26 }
27
28 use constant OPTION_DEFAULTS =>
29   {
30     MAXLENGTH => 75,
31     WIDTH => 30,
32     HEIGHT => 5,
33   };
34
35 sub processed_options {
36   my ($self) = @_;
37
38   if( exists($self->{processed_options_cache}) ) {
39     return $self->{processed_options_cache};
40   }
41
42   my $ops = $self->options;
43   my $ret;
44
45   if ( $self->type eq 'select' ) {
46     my @op_array = split('##', $ops);
47     $ret = \@op_array;
48   }
49   else {
50     $ret = {%{$self->OPTION_DEFAULTS}};
51     while ( $ops =~ /\s*([^=\s]+)\s*=\s*([^\s]*)(?:\s*|$)/g ) {
52       $ret->{$1} = $2;
53     }
54   }
55
56   $self->{processed_options_cache} = $ret;
57
58   return $ret;
59 }
60
61 sub processed_flags {
62   my ($self) = @_;
63
64   if( exists($self->{processed_flags_cache}) ) {
65     return $self->{processed_flags_cache};
66   }
67
68   my $flags = $self->flags;
69   my $ret;
70
71   foreach my $flag (split m/:/, $flags) {
72     if ( $flag =~ m/(.*?)=(.*)/ ) {
73       $ret->{$1} = $2;
74     } else {
75       $ret->{$flag} = 1;
76     }
77   }
78
79   $self->{processed_flags_cache} = $ret;
80
81   return $ret;
82 }
83
84 sub has_flag {
85   my ($self, $flag) = @_;
86
87   return $self->processed_flags()->{$flag};
88 }
89
90 1;