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