* Add enclair_db option to UserDB.pm. Allows logging of enclair password
[interchange.git] / code / SystemTag / error.coretag
1 # Copyright 2002-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: error.coretag,v 1.11 2009-02-10 12:16:49 thunder Exp $
9
10 ### This is in package Vend::Interpolate, and may make reference
11 ### to variables in that module
12 UserTag error               Order        name
13 UserTag error               addAttr
14 UserTag error               PosNumber    1
15 UserTag error               Version      $Revision: 1.11 $
16 UserTag error               Routine      <<EOR
17 sub set_error {
18         my ($error, $var, $opt) = @_;
19         $var = 'default' unless $var;
20         $opt = { keep => 1 } if ! $opt;
21         my $ref = $Vend::Session->{errors};
22         if($ref->{$var} and ! $opt->{overwrite}) {
23                 $ref->{$var} .= errmsg(" AND ");
24         }
25         else {
26                 $ref->{$var} = '';
27         }
28         
29         $ref->{$var} .= $error;
30         return tag_error($var, $opt);
31 }
32
33 sub tag_error {
34         my($var, $opt) = @_;
35         $Vend::Session->{errors} = {}
36                 unless defined $Vend::Session->{errors};
37         if($opt->{set}) {
38                 $opt->{keep} = 1 unless defined $opt->{keep};
39                 my $error = delete $opt->{set};
40                 return set_error($error, $var, $opt);
41         }
42         my $err_ref = $Vend::Session->{errors};
43         my $text;
44         my @errors;
45         my $found_error = '';
46
47         if($opt->{auto}) {
48                 $opt->{all} = 1;
49                 $opt->{show_error} = 1;
50                 $opt->{std_label} = 0;
51                 $opt->{show_var} = 1
52                         unless defined $opt->{show_var};
53                 $opt->{joiner} = '<li>'
54                         unless length $opt->{joiner};
55                 $opt->{text} ||= '%s';
56                 $opt->{list_container} ||= 'ul';
57                 my $out = '';
58                 $out .= "<$opt->{list_container}";
59                 for(qw/ class style extra /) {
60                         next unless $opt->{$_};
61                         if($_ eq 'extra') {
62                                 $out .= ' ' . $opt->{$_};
63                         }
64                         else {
65                                 $out .= ' ' . qq{$_="$opt->{$_}"};
66                         }
67                 }
68                 $out .= '>';
69                 $out .= $opt->{joiner};
70                 $opt->{header} ||= $out;
71                 $opt->{footer} ||= "</$opt->{list_container}>";
72         }
73
74         $text = $opt->{text} if $opt->{text};
75
76 #::logDebug("tag_error: var=$var text=$text opt=" . ::uneval($opt));
77 #::logDebug("tag_error: var=$var text=$text");
78         if($opt->{all}) {
79                 $opt->{joiner} = "\n" unless defined $opt->{joiner};
80                 for(sort keys %$err_ref) {
81                         my $err = $err_ref->{$_};
82                         delete $err_ref->{$_} unless $opt->{keep};
83                         next unless $err;
84                         $found_error++;
85                         my $string = '';
86                         if ($opt->{show_label}) {
87                                 if ($string = $Vend::Session->{errorlabels}{$_}) {
88                                         $string =~ s/[:\s]+$//;
89                                         $string .= " ($_)" if $opt->{show_var};
90                                         $string .= ": ";
91                                 } else {
92                                         # Use the variable name unless Locale has a default label.
93                                         my $label = errmsg("error_label_${_}");
94                                         $label = $_ if $label eq "error_label_${_}";
95                                         $string .= "($label): ";
96                                 }
97                         } else {
98                                 $string .= "$_: " if $opt->{show_var};
99                         }
100                         $string .= $err;
101                         push @errors, $string;
102                 }
103 #::logDebug("error all=1 found=$found_error contents='@errors'");
104                 return $found_error unless $text || $opt->{show_error};
105                 $text .= "%s" if $text !~ /\%s/;
106                 $text = pull_else($text, $found_error);
107
108                 return '' unless @errors;
109                 @errors = map { filter_value($opt->{filter}, $_) } @errors
110                         if $opt->{filter};
111                 my $etext = sprintf $text, join($opt->{joiner}, @errors);
112                 return join "", $opt->{header}, $etext, $opt->{footer};
113         }
114         $found_error = ! (not $err_ref->{$var});
115         my $err = $err_ref->{$var} || '';
116         delete $err_ref->{$var} unless $opt->{keep};
117 #::logDebug("error found=$found_error contents='$err'");
118         return !(not $found_error)
119                 unless $opt->{std_label} || $text || $opt->{show_error};
120         $err = filter_value($opt->{filter}, $err)
121                 if $opt->{filter};
122         if($opt->{std_label}) {
123                 # store the error label in user's session for later
124                 # possible use in [error show_label=1] calls
125                 $Vend::Session->{errorlabels}{$var} = $opt->{std_label};
126                 if($text) {
127                         # do nothing
128                 }
129                 elsif(defined $::Variable->{MV_ERROR_STD_LABEL}) {
130                         $text = $::Variable->{MV_ERROR_STD_LABEL};
131                 }
132                 else {
133                         my $contrast = $::Variable->{CSS_CONTRAST} || 'mv_contrast';
134                         $text = <<EOF;
135 <span class="$contrast">{LABEL} <small><i>(%s)</i></small></span>
136 [else]{REQUIRED <b>}{LABEL}{REQUIRED </b>}[/else]
137 EOF
138                 }
139                 $text =~ s/{LABEL}/$opt->{std_label}/g;
140                 $text =~ s/{REQUIRED\s+([^}]*)}/$opt->{required} ? $1 : ''/ge;
141                 $err =~ s/\s+$//;
142         }
143         $text = '' unless defined $text;
144         $text .= '%s' unless ($text =~ /\%s/ ||
145                                 length $::Variable->{MV_ERROR_STD_LABEL});
146
147         $text = pull_else($text, $found_error);
148         $text =~ s/\%s/$err/;
149         return $text;
150 }
151
152 sub {
153         return tag_error(@_);
154 }
155 EOR