CVars löschen muss auch die validity löschen
[kivitendo-erp.git] / SL / CVar.pm
1 package CVar;
2
3 use strict;
4
5 use List::Util qw(first);
6 use Data::Dumper;
7
8 use SL::DBUtils;
9 use SL::MoreCommon qw(listify);
10
11 sub get_configs {
12   $main::lxdebug->enter_sub();
13
14   my $self     = shift;
15   my %params   = @_;
16
17   my $myconfig = \%main::myconfig;
18   my $form     = $main::form;
19
20   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
21
22   my ($where, @values);
23   if ($params{module}) {
24     $where = 'WHERE module = ?';
25     push @values, $params{module};
26   }
27
28   my $query    = <<SQL;
29     SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
30     FROM custom_variable_configs $where ORDER BY sortkey
31 SQL
32
33   $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
34   if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
35     my $configs  = selectall_hashref_query($form, $dbh, $query, @values);
36
37     foreach my $config (@{ $configs }) {
38       if ($config->{type} eq 'select') {
39         $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
40
41       } elsif ($config->{type} eq 'number') {
42         $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
43
44       }
45
46       $self->_unpack_flags($config);
47     }
48     $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
49   }
50
51   $main::lxdebug->leave_sub();
52
53   return $::form->{CVAR_CONFIGS}->{$params{module}};
54 }
55
56 sub get_config {
57   $main::lxdebug->enter_sub();
58
59   my $self     = shift;
60   my %params   = @_;
61
62   Common::check_params(\%params, qw(id));
63
64   my $myconfig = \%main::myconfig;
65   my $form     = $main::form;
66
67   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
68
69   my $query    = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
70
71   my $config   = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
72
73   $self->_unpack_flags($config);
74
75   $main::lxdebug->leave_sub();
76
77   return $config;
78 }
79
80 sub _unpack_flags {
81   $main::lxdebug->enter_sub();
82
83   my $self   = shift;
84   my $config = shift;
85
86   foreach my $flag (split m/:/, $config->{flags}) {
87     if ($flag =~ m/(.*?)=(.*)/) {
88       $config->{"flag_${1}"}    = $2;
89     } else {
90       $config->{"flag_${flag}"} = 1;
91     }
92   }
93
94   $main::lxdebug->leave_sub();
95 }
96
97 sub save_config {
98   $main::lxdebug->enter_sub();
99
100   my $self     = shift;
101   my %params   = @_;
102
103   Common::check_params(\%params, qw(module config));
104
105   my $myconfig = \%main::myconfig;
106   my $form     = $main::form;
107
108   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
109
110   my $q_id     = qq|SELECT nextval('custom_variable_configs_id')|;
111   my $h_id     = prepare_query($form, $dbh, $q_id);
112
113   my $q_new    =
114     qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
115        VALUES                              (?,    ?,           ?,    ?,             ?,       ?,          ?,           ?,                   ?,      ?,     ?,
116          (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
117   my $h_new    = prepare_query($form, $dbh, $q_new);
118
119   my $q_update =
120     qq|UPDATE custom_variable_configs SET
121          name        = ?, description         = ?,
122          type        = ?, default_value       = ?,
123          options     = ?, searchable          = ?,
124          includeable = ?, included_by_default = ?,
125          module      = ?, flags               = ?
126        WHERE id  = ?|;
127   my $h_update = prepare_query($form, $dbh, $q_update);
128
129   my @configs;
130   if ('ARRAY' eq ref $params{config}) {
131     @configs = @{ $params{config} };
132   } else {
133     @configs = ($params{config});
134   }
135
136   foreach my $config (@configs) {
137     my ($h_actual, $q_actual);
138
139     if (!$config->{id}) {
140       do_statement($form, $h_id, $q_id);
141       ($config->{id}) = $h_id->fetchrow_array();
142
143       $h_actual       = $h_new;
144       $q_actual       = $q_new;
145
146     } else {
147       $h_actual       = $h_update;
148       $q_actual       = $q_update;
149     }
150
151     do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
152                  $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
153                  $params{module}, $config->{flags}, conv_i($config->{id}));
154   }
155
156   $h_id->finish();
157   $h_new->finish();
158   $h_update->finish();
159
160   $dbh->commit();
161
162   $main::lxdebug->leave_sub();
163 }
164
165 sub delete_config {
166   $main::lxdebug->enter_sub();
167
168   my $self     = shift;
169   my %params   = @_;
170
171   Common::check_params(\%params, qw(id));
172
173   my $myconfig = \%main::myconfig;
174   my $form     = $main::form;
175
176   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
177
178   do_query($form, $dbh, qq|DELETE FROM custom_variables          WHERE config_id = ?|, conv_i($params{id}));
179   do_query($form, $dbh, qq|DELETE FROM custom_variables_validity WHERE config_id = ?|, conv_i($params{id}));
180   do_query($form, $dbh, qq|DELETE FROM custom_variable_configs   WHERE id        = ?|, conv_i($params{id}));
181
182   $dbh->commit();
183
184   $main::lxdebug->leave_sub();
185 }
186
187 sub get_custom_variables {
188   $main::lxdebug->enter_sub();
189
190   my $self     = shift;
191   my %params   = @_;
192
193   Common::check_params(\%params, qw(module));
194
195   my $myconfig = \%main::myconfig;
196   my $form     = $main::form;
197
198   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
199
200   my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
201
202   my $q_var    =
203     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
204        FROM custom_variables
205        WHERE (config_id = ?) AND (trans_id = ?)|;
206   $q_var      .= qq| AND (sub_module = ?)| if $params{sub_module};
207   my $h_var    = prepare_query($form, $dbh, $q_var);
208
209   my $custom_variables = $self->get_configs(module => $params{module});
210
211   foreach my $cvar (@{ $custom_variables }) {
212     if ($cvar->{type} eq 'textfield') {
213       $cvar->{width}  = 30;
214       $cvar->{height} =  5;
215
216       $cvar->{width}  = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
217       $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
218
219     } elsif ($cvar->{type} eq 'text') {
220       $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
221
222     } elsif ($cvar->{type} eq 'number') {
223       $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
224
225     } elsif ($cvar->{type} eq 'select') {
226       $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
227     }
228
229     my ($act_var, $valid);
230     if ($params{trans_id}) {
231       my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
232       push @values, $params{sub_module} if $params{sub_module};
233
234       do_statement($form, $h_var, $q_var, @values);
235       $act_var = $h_var->fetchrow_hashref();
236
237       $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
238     }
239
240     if ($act_var) {
241       $cvar->{value} = $cvar->{type} eq 'date'      ? $act_var->{date_value}
242                      : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
243                      : $cvar->{type} eq 'number'    ? $act_var->{number_value}
244                      : $cvar->{type} eq 'customer'  ? $act_var->{number_value}
245                      : $cvar->{type} eq 'bool'      ? $act_var->{bool_value}
246                      :                                $act_var->{text_value};
247       $cvar->{valid} = $valid;
248     } else {
249       $cvar->{valid}  =  1;
250
251       if ($cvar->{type} eq 'date') {
252         if ($cvar->{default_value} eq 'NOW') {
253           $cvar->{value} = $cvar->{current_date};
254         } else {
255           $cvar->{value} = $cvar->{default_value};
256         }
257
258       } elsif ($cvar->{type} eq 'timestamp') {
259         if ($cvar->{default_value} eq 'NOW') {
260           $cvar->{value} = $cvar->{current_timestamp};
261         } else {
262           $cvar->{value} = $cvar->{default_value};
263         }
264
265       } elsif ($cvar->{type} eq 'bool') {
266         $cvar->{value} = $cvar->{default_value} * 1;
267
268       } elsif ($cvar->{type} eq 'number') {
269         $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
270
271       } else {
272         $cvar->{value} = $cvar->{default_value};
273       }
274     }
275
276     if ($cvar->{type} eq 'number') {
277       $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
278     } elsif ($cvar->{type} eq 'customer') {
279       require SL::DB::Customer;
280       $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
281     }
282   }
283
284   $h_var->finish();
285
286   $main::lxdebug->leave_sub();
287
288   return $custom_variables;
289 }
290
291 sub save_custom_variables {
292   $main::lxdebug->enter_sub();
293
294   my $self     = shift;
295   my %params   = @_;
296
297   Common::check_params(\%params, qw(module trans_id variables));
298
299   my $myconfig = \%main::myconfig;
300   my $form     = $main::form;
301
302   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
303
304   my @configs  = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
305
306   my $query    =
307     qq|DELETE FROM custom_variables
308        WHERE (trans_id  = ?)
309          AND (config_id IN (SELECT DISTINCT id
310                             FROM custom_variable_configs
311                             WHERE module = ?))|;
312   my @values   = (conv_i($params{trans_id}), $params{module});
313
314   if ($params{sub_module}) {
315     $query .= qq| AND (sub_module = ?)|;
316     push @values, $params{sub_module};
317   }
318
319   do_query($form, $dbh, $query, @values);
320
321   $query  =
322     qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
323        VALUES                       (?,         ?,          ?,        ?,          ?,               ?,          ?)|;
324   my $sth = prepare_query($form, $dbh, $query);
325
326   foreach my $config (@configs) {
327     my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
328
329     my $value  = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
330
331     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
332       push @values, undef, undef, $value, undef;
333
334     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
335       push @values, undef, conv_date($value), undef, undef;
336
337     } elsif ($config->{type} eq 'number') {
338       push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
339
340     } elsif ($config->{type} eq 'bool') {
341       push @values, $value ? 't' : 'f', undef, undef, undef;
342     } elsif ($config->{type} eq 'customer') {
343       push @values, undef, undef, undef, $value * 1;
344     }
345
346     do_statement($form, $sth, $query, @values);
347
348     if ($params{save_validity}) {
349       my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
350       $self->save_custom_variables_validity(trans_id  => $params{trans_id},
351                                             config_id => $config->{id},
352                                             validity  => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
353                                            );
354     }
355   }
356
357   $sth->finish();
358
359   $dbh->commit();
360
361   $main::lxdebug->leave_sub();
362 }
363
364 sub render_inputs {
365   $main::lxdebug->enter_sub(2);
366
367   my $self     = shift;
368   my %params   = @_;
369
370   Common::check_params(\%params, qw(variables));
371
372   my $myconfig = \%main::myconfig;
373   my $form     = $main::form;
374
375   my %options  = ( name_prefix       => "$params{name_prefix}",
376                    name_postfix      => "$params{name_postfix}",
377                    hide_non_editable => $params{hide_non_editable},
378                    show_disabled_message => $params{show_disabled_message},
379                  );
380
381   foreach my $var (@{ $params{variables} }) {
382     $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs',     { var => $var, %options });
383     $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
384   }
385
386   $main::lxdebug->leave_sub(2);
387 }
388
389 sub render_search_options {
390   $main::lxdebug->enter_sub();
391
392   my $self     = shift;
393   my %params   = @_;
394
395   Common::check_params(\%params, qw(variables));
396
397   my $myconfig = \%main::myconfig;
398   my $form     = $main::form;
399
400   $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
401
402   $params{include_prefix}   = 'l_' unless defined($params{include_prefix});
403   $params{include_value}  ||= '1';
404
405   my $filter  = $form->parse_html_template('amcvar/search_filter',  \%params);
406   my $include = $form->parse_html_template('amcvar/search_include', \%params);
407
408   $main::lxdebug->leave_sub();
409
410   return ($filter, $include);
411 }
412
413 sub build_filter_query {
414   $main::lxdebug->enter_sub();
415
416   my $self     = shift;
417   my %params   = @_;
418
419   Common::check_params(\%params, qw(module trans_id_field filter));
420
421   my $myconfig = \%main::myconfig;
422   my $form     = $main::form;
423
424   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
425
426   my $configs  = $self->get_configs(%params);
427
428   my (@where, @values);
429
430   foreach my $config (@{ $configs }) {
431     next unless ($config->{searchable});
432
433     my $name = "cvar_$config->{name}";
434
435     my (@sub_values, @sub_where, $not);
436
437     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
438       next unless ($params{filter}->{$name});
439
440       push @sub_where,  qq|cvar.text_value ILIKE ?|;
441       push @sub_values, '%' . $params{filter}->{$name} . '%'
442
443     } elsif ($config->{type} eq 'select') {
444       next unless ($params{filter}->{$name});
445
446       push @sub_where,  qq|cvar.text_value = ?|;
447       push @sub_values, $params{filter}->{$name};
448
449     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
450       my $name_from = "${name}_from";
451       my $name_to   = "${name}_to";
452
453       if ($params{filter}->{$name_from}) {
454         push @sub_where,  qq|cvar.timestamp_value >= ?|;
455         push @sub_values, conv_date($params{filter}->{$name_from});
456       }
457
458       if ($params{filter}->{$name_to}) {
459         push @sub_where,  qq|cvar.timestamp_value <= ?|;
460         push @sub_values, conv_date($params{filter}->{$name_to});
461       }
462
463     } elsif ($config->{type} eq 'number') {
464       next if ($params{filter}->{$name} eq '');
465
466       my $f_op = $params{filter}->{"${name}_qtyop"};
467
468       my $op;
469       if ($f_op eq '==') {
470         $op  = '=';
471
472       } elsif ($f_op eq '=/=') {
473         $not = 'NOT';
474         $op  = '<>';
475
476       } elsif ($f_op eq '<') {
477         $not = 'NOT';
478         $op  = '>=';
479
480       } elsif ($f_op eq '<=') {
481         $not = 'NOT';
482         $op  = '>';
483
484       } elsif (($f_op eq '>') || ($f_op eq '>=')) {
485         $op  = $f_op;
486
487       } else {
488         $op  = '=';
489       }
490
491       push @sub_where,  qq|cvar.number_value $op ?|;
492       push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
493
494     } elsif ($config->{type} eq 'bool') {
495       next unless ($params{filter}->{$name});
496
497       $not = 'NOT' if ($params{filter}->{$name} eq 'no');
498       push @sub_where,  qq|COALESCE(cvar.bool_value, false) = TRUE|;
499     } elsif ($config->{type} eq 'customer') {
500       next unless $params{filter}->{$name};
501
502       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|;
503       push @sub_values, "%$params{filter}->{$name}%";
504     }
505
506     if (@sub_where) {
507       add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
508
509       push @where,
510         qq|$not EXISTS(
511              SELECT cvar.id
512              FROM custom_variables cvar
513              LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
514              WHERE (cvarcfg.module = ?)
515                AND (cvarcfg.id     = ?)
516                AND (cvar.trans_id  = $params{trans_id_field})
517                AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
518       push @values, $params{module}, conv_i($config->{id}), @sub_values;
519     }
520   }
521
522   my $query = join ' AND ', @where;
523
524   $main::lxdebug->leave_sub();
525
526   return ($query, @values);
527 }
528
529 sub add_custom_variables_to_report {
530   $main::lxdebug->enter_sub();
531
532   my $self      = shift;
533   my %params    = @_;
534
535   Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
536
537   my $myconfig  = \%main::myconfig;
538   my $form      = $main::form;
539   my $locale    = $main::locale;
540
541   my $dbh       = $params{dbh} || $form->get_standard_dbh($myconfig);
542
543   my $configs   = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
544
545   if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
546     $main::lxdebug->leave_sub();
547     return;
548   }
549
550   # allow sub_module to be a coderef or a fixed value
551   if (ref $params{sub_module} ne 'CODE') {
552     my $sub_module = "$params{sub_module}";
553     $params{sub_module} = sub { $sub_module };
554   }
555
556   my %cfg_map   = map { $_->{id} => $_ } @{ $configs };
557   my @cfg_ids   = keys %cfg_map;
558
559   my $query     =
560     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
561        FROM custom_variables
562        WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
563          AND (trans_id = ?)
564          AND (sub_module = ?)|;
565   my $sth       = prepare_query($form, $dbh, $query);
566
567   foreach my $row (@{ $params{data} }) {
568     do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
569
570     while (my $ref = $sth->fetchrow_hashref()) {
571       my $cfg = $cfg_map{$ref->{config_id}};
572
573       $row->{"cvar_$cfg->{name}"} =
574           $cfg->{type} eq 'date'      ? $ref->{date_value}
575         : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
576         : $cfg->{type} eq 'number'    ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
577         : $cfg->{type} eq 'customer'  ? SL::DB::Manager::Customer->find_by(id => 1* $ref->{number_value})->name
578         : $cfg->{type} eq 'bool'      ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
579         :                               $ref->{text_value};
580     }
581   }
582
583   $sth->finish();
584
585   $main::lxdebug->leave_sub();
586 }
587
588 sub get_field_format_list {
589   $main::lxdebug->enter_sub();
590
591   my $self          = shift;
592   my %params        = @_;
593
594   Common::check_params(\%params, qw(module));
595
596   my $myconfig      = \%main::myconfig;
597   my $form          = $main::form;
598
599   my $dbh           = $params{dbh} || $form->get_standard_dbh($myconfig);
600
601   my $configs       = $self->get_configs(%params);
602
603   my $date_fields   = [];
604   my $number_fields = {};
605
606   foreach my $config (@{ $configs }) {
607     my $name = "$params{prefix}cvar_$config->{name}";
608
609     if ($config->{type} eq 'date') {
610       push @{ $date_fields }, $name;
611
612     } elsif ($config->{type} eq 'number') {
613       $number_fields->{$config->{precision}} ||= [];
614       push @{ $number_fields->{$config->{precision}} }, $name;
615     }
616   }
617
618   $main::lxdebug->leave_sub();
619
620   return ($date_fields, $number_fields);
621 }
622
623 sub save_custom_variables_validity {
624   $main::lxdebug->enter_sub();
625
626   my $self     = shift;
627   my %params   = @_;
628
629   Common::check_params(\%params, qw(config_id trans_id validity));
630
631   my $myconfig = \%main::myconfig;
632   my $form     = $main::form;
633
634   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
635
636   my (@where, @values);
637   add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
638   add_token(\@where, \@values, col => "trans_id",  val => $params{trans_id},  esc => \&conv_i);
639
640   my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
641   my $query = qq|DELETE FROM custom_variables_validity $where|;
642
643   do_query($form, $dbh, $query, @values);
644
645   $query  =
646     qq|INSERT INTO custom_variables_validity (config_id, trans_id)
647        VALUES                                (?,         ?       )|;
648   my $sth = prepare_query($form, $dbh, $query);
649
650   unless ($params{validity}) {
651     foreach my $config_id (listify $params{config_id}) {
652       foreach my $trans_id (listify $params{trans_id}) {
653         do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
654       }
655     }
656   }
657
658   $sth->finish();
659
660   $dbh->commit();
661
662   $main::lxdebug->leave_sub();
663 }
664
665 sub get_custom_variables_validity {
666   $main::lxdebug->enter_sub(2);
667
668   my $self     = shift;
669   my %params   = @_;
670
671   Common::check_params(\%params, qw(config_id trans_id));
672
673   my $myconfig = \%main::myconfig;
674   my $form     = $main::form;
675
676   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
677
678   my $query    = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
679
680   my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
681
682   $main::lxdebug->leave_sub(2);
683
684   return !$invalid;
685 }
686
687 sub custom_variables_validity_by_trans_id {
688   $main::lxdebug->enter_sub(2);
689
690   my $self     = shift;
691   my %params   = @_;
692
693   return sub { 0 } unless $params{trans_id};
694
695   my $myconfig = \%main::myconfig;
696   my $form     = $main::form;
697
698   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
699
700   my $query    = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
701
702   my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
703
704   $main::lxdebug->leave_sub(2);
705
706   return sub { !$invalids{+shift} };
707 }
708
709 1;
710
711 __END__
712
713 =head1 NAME
714
715 SL::CVar.pm - Custom Variables module
716
717 =head1 SYNOPSIS
718
719   # dealing with configs
720
721   my $all_configs = CVar->get_configs()
722   my $config      = CVar->get_config(id => '1234')
723
724   CVar->save_config($config);
725   CVar->delete->config($config)
726
727   # dealing with custom vars
728
729   CVar->get_custom_variables(module => 'ic')
730
731 =head2 VALIDITY
732
733 Suppose the following scenario:
734
735 You have a lot of parts in your database, and a set of properties cofigured. Now not every part has every of these properties, some combinations will just make no sense. In order to clean up your inputs a bit, you want to mark certain combinations as invalid, blocking them from modification and possibly display.
736
737 Validity is assumed. If you modify validity, you actually save B<invalidity>.
738 Invalidity is saved as a function of config_id, and the trans_id
739
740 In the naive way, disable an attribute for a specific id (simple)
741
742 =cut