# 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.
# 
# $Id: error.coretag,v 1.11 2009-02-10 12:16:49 thunder Exp $

### 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};
		return set_error($error, $var, $opt);
	}
	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
