Revision 260 (by ahitrov, 2012/12/05 20:52:56) Regions: sql and fieldtype
<%once>

   use JSON::XS;
   my $json = JSON::XS->new;

</%once>
<%args>

	$name	=> undef
	$SETS	=> undef

</%args>
<%init>

    return undef	if (! ref($SETS));
    return undef	if (! $name);

    my $FIELDS = {};
    while (my ($element,$value) = each %{$SETS}) {
	if ( $element =~ /^${name}_area_(\d+)$/ ) {
		my $index = $1;
		$value =~ s/^\s+//s;
		$value =~ s/\s+$//s;
		if ( defined $value && $value =~ /^\d+$/ ) {
			$FIELDS->{$index}{area} = $value;
			$FIELDS->{$index}{index} = $index;
		}
	} elsif ( $element =~ /^${name}_cost_(\d+)$/ ) {
		my $index = $1;
		$value =~ s/^\s+//s;
		$value =~ s/\s+$//s;
		if ( defined $value && ($value =~ /^\d+$/ || $value =~ /^\d+[\.\,]\d+$/) ) {
			$value =~ s/\,/\./;
			$FIELDS->{$index}{cost} = $value;
			$FIELDS->{$index}{index} = $index;
		}
	}
    }
    my %FIELDS = map { $_->{area} => $_->{cost} } sort { int($a->{index}) <=> int($b->{index}) } grep { $_->{area} && defined $_->{cost} } values %$FIELDS;
    return $json->encode(\%FIELDS);

</%init>