5 use List::Util qw(first);
9 use SL::MoreCommon qw(listify);
12 $main::lxdebug->enter_sub();
17 my $myconfig = \%main::myconfig;
18 my $form = $main::form;
20 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
23 if ($params{module}) {
24 $where = 'WHERE module = ?';
25 push @values, $params{module};
29 SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
30 FROM custom_variable_configs $where ORDER BY sortkey
33 if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
34 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
36 foreach my $config (@{ $configs }) {
37 if ($config->{type} eq 'select') {
38 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
40 } elsif ($config->{type} eq 'number') {
41 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
45 $self->_unpack_flags($config);
47 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
50 $main::lxdebug->leave_sub();
52 return $::form->{CVAR_CONFIGS}->{$params{module}};
56 $main::lxdebug->enter_sub();
61 Common::check_params(\%params, qw(id));
63 my $myconfig = \%main::myconfig;
64 my $form = $main::form;
66 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
68 my $query = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
70 my $config = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
72 $self->_unpack_flags($config);
74 $main::lxdebug->leave_sub();
80 $main::lxdebug->enter_sub();
85 foreach my $flag (split m/:/, $config->{flags}) {
86 if ($flag =~ m/(.*?)=(.*)/) {
87 $config->{"flag_${1}"} = $2;
89 $config->{"flag_${flag}"} = 1;
93 $main::lxdebug->leave_sub();
97 $main::lxdebug->enter_sub();
102 Common::check_params(\%params, qw(module config));
104 my $myconfig = \%main::myconfig;
105 my $form = $main::form;
107 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
109 my $q_id = qq|SELECT nextval('custom_variable_configs_id')|;
110 my $h_id = prepare_query($form, $dbh, $q_id);
113 qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
114 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
115 (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
116 my $h_new = prepare_query($form, $dbh, $q_new);
119 qq|UPDATE custom_variable_configs SET
120 name = ?, description = ?,
121 type = ?, default_value = ?,
122 options = ?, searchable = ?,
123 includeable = ?, included_by_default = ?,
124 module = ?, flags = ?
126 my $h_update = prepare_query($form, $dbh, $q_update);
129 if ('ARRAY' eq ref $params{config}) {
130 @configs = @{ $params{config} };
132 @configs = ($params{config});
135 foreach my $config (@configs) {
136 my ($h_actual, $q_actual);
138 if (!$config->{id}) {
139 do_statement($form, $h_id, $q_id);
140 ($config->{id}) = $h_id->fetchrow_array();
146 $h_actual = $h_update;
147 $q_actual = $q_update;
150 do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
151 $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
152 $params{module}, $config->{flags}, conv_i($config->{id}));
161 $main::lxdebug->leave_sub();
165 $main::lxdebug->enter_sub();
170 Common::check_params(\%params, qw(id));
172 my $myconfig = \%main::myconfig;
173 my $form = $main::form;
175 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
177 do_query($form, $dbh, qq|DELETE FROM custom_variables WHERE config_id = ?|, conv_i($params{id}));
178 do_query($form, $dbh, qq|DELETE FROM custom_variable_configs WHERE id = ?|, conv_i($params{id}));
182 $main::lxdebug->leave_sub();
185 sub get_custom_variables {
186 $main::lxdebug->enter_sub();
191 Common::check_params(\%params, qw(module));
193 my $myconfig = \%main::myconfig;
194 my $form = $main::form;
196 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
198 my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
201 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
202 FROM custom_variables
203 WHERE (config_id = ?) AND (trans_id = ?)|;
204 $q_var .= qq| AND (sub_module = ?)| if $params{sub_module};
205 my $h_var = prepare_query($form, $dbh, $q_var);
207 my $custom_variables = $self->get_configs(module => $params{module});
209 foreach my $cvar (@{ $custom_variables }) {
210 if ($cvar->{type} eq 'textfield') {
214 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
215 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
217 } elsif ($cvar->{type} eq 'text') {
218 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
220 } elsif ($cvar->{type} eq 'number') {
221 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
223 } elsif ($cvar->{type} eq 'select') {
224 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
227 my ($act_var, $valid);
228 if ($params{trans_id}) {
229 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
230 push @values, $params{sub_module} if $params{sub_module};
232 do_statement($form, $h_var, $q_var, @values);
233 $act_var = $h_var->fetchrow_hashref();
235 $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
239 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
240 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
241 : $cvar->{type} eq 'number' ? $act_var->{number_value}
242 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
243 : $act_var->{text_value};
244 $cvar->{valid} = $valid;
248 if ($cvar->{type} eq 'date') {
249 if ($cvar->{default_value} eq 'NOW') {
250 $cvar->{value} = $cvar->{current_date};
252 $cvar->{value} = $cvar->{default_value};
255 } elsif ($cvar->{type} eq 'timestamp') {
256 if ($cvar->{default_value} eq 'NOW') {
257 $cvar->{value} = $cvar->{current_timestamp};
259 $cvar->{value} = $cvar->{default_value};
262 } elsif ($cvar->{type} eq 'bool') {
263 $cvar->{value} = $cvar->{default_value} * 1;
265 } elsif ($cvar->{type} eq 'number') {
266 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
269 $cvar->{value} = $cvar->{default_value};
273 if ($cvar->{type} eq 'number') {
274 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
280 $main::lxdebug->leave_sub();
282 return $custom_variables;
285 sub save_custom_variables {
286 $main::lxdebug->enter_sub();
291 Common::check_params(\%params, qw(module trans_id variables));
293 my $myconfig = \%main::myconfig;
294 my $form = $main::form;
296 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
298 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
301 qq|DELETE FROM custom_variables
303 AND (config_id IN (SELECT DISTINCT id
304 FROM custom_variable_configs
306 my @values = (conv_i($params{trans_id}), $params{module});
308 if ($params{sub_module}) {
309 $query .= qq| AND (sub_module = ?)|;
310 push @values, $params{sub_module};
313 do_query($form, $dbh, $query, @values);
316 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
317 VALUES (?, ?, ?, ?, ?, ?, ?)|;
318 my $sth = prepare_query($form, $dbh, $query);
320 foreach my $config (@configs) {
321 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
323 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
325 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
326 push @values, undef, undef, $value, undef;
328 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
329 push @values, undef, conv_date($value), undef, undef;
331 } elsif ($config->{type} eq 'number') {
332 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
334 } elsif ($config->{type} eq 'bool') {
335 push @values, $value ? 't' : 'f', undef, undef, undef;
338 do_statement($form, $sth, $query, @values);
340 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
341 $self->save_custom_variables_validity(trans_id => $params{trans_id}, config_id => $config->{id},
342 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
350 $main::lxdebug->leave_sub();
354 $main::lxdebug->enter_sub(2);
359 Common::check_params(\%params, qw(variables));
361 my $myconfig = \%main::myconfig;
362 my $form = $main::form;
364 my %options = ( name_prefix => "$params{name_prefix}",
365 name_postfix => "$params{name_postfix}",
366 hide_non_editable => $params{hide_non_editable},
367 show_disabled_message => $params{show_disabled_message},
370 foreach my $var (@{ $params{variables} }) {
371 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
372 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
375 $main::lxdebug->leave_sub(2);
378 sub render_search_options {
379 $main::lxdebug->enter_sub();
384 Common::check_params(\%params, qw(variables));
386 my $myconfig = \%main::myconfig;
387 my $form = $main::form;
389 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
391 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
392 $params{include_value} ||= '1';
394 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
395 my $include = $form->parse_html_template('amcvar/search_include', \%params);
397 $main::lxdebug->leave_sub();
399 return ($filter, $include);
402 sub build_filter_query {
403 $main::lxdebug->enter_sub();
408 Common::check_params(\%params, qw(module trans_id_field filter));
410 my $myconfig = \%main::myconfig;
411 my $form = $main::form;
413 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
415 my $configs = $self->get_configs(%params);
417 my (@where, @values);
419 foreach my $config (@{ $configs }) {
420 next unless ($config->{searchable});
422 my $name = "cvar_$config->{name}";
424 my (@sub_values, @sub_where, $not);
426 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
427 next unless ($params{filter}->{$name});
429 push @sub_where, qq|cvar.text_value ILIKE ?|;
430 push @sub_values, '%' . $params{filter}->{$name} . '%'
432 } elsif ($config->{type} eq 'select') {
433 next unless ($params{filter}->{$name});
435 push @sub_where, qq|cvar.text_value = ?|;
436 push @sub_values, $params{filter}->{$name};
438 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
439 my $name_from = "${name}_from";
440 my $name_to = "${name}_to";
442 if ($params{filter}->{$name_from}) {
443 push @sub_where, qq|cvar.timestamp_value >= ?|;
444 push @sub_values, conv_date($params{filter}->{$name_from});
447 if ($params{filter}->{$name_to}) {
448 push @sub_where, qq|cvar.timestamp_value <= ?|;
449 push @sub_values, conv_date($params{filter}->{$name_to});
452 } elsif ($config->{type} eq 'number') {
453 next if ($params{filter}->{$name} eq '');
455 my $f_op = $params{filter}->{"${name}_qtyop"};
461 } elsif ($f_op eq '=/=') {
465 } elsif ($f_op eq '<') {
469 } elsif ($f_op eq '<=') {
473 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
480 push @sub_where, qq|cvar.number_value $op ?|;
481 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
483 } elsif ($config->{type} eq 'bool') {
484 next unless ($params{filter}->{$name});
486 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
487 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
491 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
496 FROM custom_variables cvar
497 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
498 WHERE (cvarcfg.module = ?)
500 AND (cvar.trans_id = $params{trans_id_field})
501 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
502 push @values, $params{module}, conv_i($config->{id}), @sub_values;
506 my $query = join ' AND ', @where;
508 $main::lxdebug->leave_sub();
510 return ($query, @values);
513 sub add_custom_variables_to_report {
514 $main::lxdebug->enter_sub();
519 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
521 my $myconfig = \%main::myconfig;
522 my $form = $main::form;
523 my $locale = $main::locale;
525 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
527 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
529 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
530 $main::lxdebug->leave_sub();
534 # allow sub_module to be a coderef or a fixed value
535 if (ref $params{sub_module} ne 'CODE') {
536 my $sub_module = "$params{sub_module}";
537 $params{sub_module} = sub { $sub_module };
540 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
541 my @cfg_ids = keys %cfg_map;
544 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
545 FROM custom_variables
546 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
548 AND (sub_module = ?)|;
549 my $sth = prepare_query($form, $dbh, $query);
551 foreach my $row (@{ $params{data} }) {
552 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
554 while (my $ref = $sth->fetchrow_hashref()) {
555 my $cfg = $cfg_map{$ref->{config_id}};
557 $row->{"cvar_$cfg->{name}"} =
558 $cfg->{type} eq 'date' ? $ref->{date_value}
559 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
560 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
561 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
562 : $ref->{text_value};
568 $main::lxdebug->leave_sub();
571 sub get_field_format_list {
572 $main::lxdebug->enter_sub();
577 Common::check_params(\%params, qw(module));
579 my $myconfig = \%main::myconfig;
580 my $form = $main::form;
582 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
584 my $configs = $self->get_configs(%params);
586 my $date_fields = [];
587 my $number_fields = {};
589 foreach my $config (@{ $configs }) {
590 my $name = "$params{prefix}cvar_$config->{name}";
592 if ($config->{type} eq 'date') {
593 push @{ $date_fields }, $name;
595 } elsif ($config->{type} eq 'number') {
596 $number_fields->{$config->{precision}} ||= [];
597 push @{ $number_fields->{$config->{precision}} }, $name;
601 $main::lxdebug->leave_sub();
603 return ($date_fields, $number_fields);
606 sub save_custom_variables_validity {
607 $main::lxdebug->enter_sub();
612 Common::check_params(\%params, qw(config_id trans_id validity));
614 my $myconfig = \%main::myconfig;
615 my $form = $main::form;
617 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
619 my (@where, @values);
620 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
621 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
623 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
624 my $query = qq|DELETE FROM custom_variables_validity $where|;
626 do_query($form, $dbh, $query, @values);
629 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
631 my $sth = prepare_query($form, $dbh, $query);
633 unless ($params{validity}) {
634 foreach my $config_id (listify $params{config_id}) {
635 foreach my $trans_id (listify $params{trans_id}) {
636 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
645 $main::lxdebug->leave_sub();
648 sub get_custom_variables_validity {
649 $main::lxdebug->enter_sub(2);
654 Common::check_params(\%params, qw(config_id trans_id));
656 my $myconfig = \%main::myconfig;
657 my $form = $main::form;
659 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
661 my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
663 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
665 $main::lxdebug->leave_sub(2);
670 sub custom_variables_validity_by_trans_id {
671 $main::lxdebug->enter_sub(2);
676 return sub { 0 } unless $params{trans_id};
678 my $myconfig = \%main::myconfig;
679 my $form = $main::form;
681 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
683 my $query = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
685 my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
687 $main::lxdebug->leave_sub(2);
689 return sub { !$invalids{+shift} };
698 SL::CVar.pm - Custom Variables module
702 # dealing with configs
704 my $all_configs = CVar->get_configs()
705 my $config = CVar->get_config(id => '1234')
707 CVar->save_config($config);
708 CVar->delete->config($config)
710 # dealing with custom vars
712 CVar->get_custom_variables(module => 'ic')
716 Suppose the following scenario:
718 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.
720 Validity is assumed. If you modify validity, you actually save B<invalidity>.
721 Invalidity is saved as a function of config_id, and the trans_id
723 In the naive way, disable an attribute for a specific id (simple)