CustomVariable-RDBO-Helfer: Dokumentation aktualisiert
[kivitendo-erp.git] / SL / DB / Helper / CustomVariables.pm
index 3c43624..6f47035 100644 (file)
@@ -26,6 +26,7 @@ sub import {
   make_cvar_alias($caller_package, %params)      if $params{cvars_alias};
   make_cvar_by_configs($caller_package, %params);
   make_cvar_by_name($caller_package, %params);
+  make_cvar_as_hashref($caller_package, %params);
 }
 
 sub save_meta_info {
@@ -42,8 +43,11 @@ sub save_meta_info {
 sub make_cvar_accessor {
   my ($caller_package, %params) = @_;
 
-  my @module_filter = $params{module} ?
-    ("config_id" => [ \"(SELECT custom_variable_configs.id FROM custom_variable_configs WHERE custom_variable_configs.module = '$params{module}')" ]) :
+  my $modules = ('ARRAY' eq ref $params{module}) ?
+      join ',', @{ $params{module} } :
+      $params{module};
+  my @module_filter = $modules ?
+    ("config_id" => [ \"(SELECT custom_variable_configs.id FROM custom_variable_configs WHERE custom_variable_configs.module IN ( '$modules' ))" ]) : # " make emacs happy
     ();
 
   $caller_package->meta->add_relationships(
@@ -122,14 +126,30 @@ sub make_cvar_by_name {
   }
 }
 
+sub make_cvar_as_hashref {
+  my ($caller_package, %params) = @_;
+
+  no strict 'refs';
+  *{ $caller_package . '::cvar_as_hashref' } = sub {
+    my ($self) = @_;
+    @_ > 1 and croak "not an accessor";
+
+    my $cvars_by_config = $self->cvars_by_config;
+
+    my %return = map {
+      $_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }
+    } @$cvars_by_config;
+
+    return \%return;
+  }
+}
+
 sub _all_configs {
   my (%params) = @_;
 
   require SL::DB::CustomVariableConfig;
 
-  $params{module}
-    ? SL::DB::Manager::CustomVariableConfig->get_all(query => [ module => $params{module} ])
-    : SL::DB::Manager::CustomVariableConfig->get_all;
+  SL::DB::Manager::CustomVariableConfig->get_all_sorted($params{module} ? (query => [ module => $params{module} ]) : ());
 }
 
 sub _overload_by_module {
@@ -245,6 +265,11 @@ Secondary classes may currently not have cvars of their own.
 This is a Rose::DB::Object::Relationship accessor, generated for cvars. Use it
 like any other OneToMany relationship.
 
+Note that unlike L</cvars_by_config> this accessor only returns
+variables that have already been created for this object. No variables
+will be autovivified for configs for which no variable has been
+created yet.
+
 =item C<cvars [ CUSTOM_VARIABLES ]>
 
 Alias to C<custom_variables>. Will only be installed if C<cvars_alias> was
@@ -289,6 +314,7 @@ vivified with the same rules as in C<cvars_by_config>.
 
 =head1 AUTHOR
 
-Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
+Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
+Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
 
 =cut