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

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

</%once>
<%args>

	$name		=> undef
	$rusname	=> undef
	$check		=> undef

</%args>
<%init>

  my $fields = $check ? $json->decode($check) : {};
  my @fields;
  while ( my ($reg_id, $cost) = each %$fields ) {
	push @fields, { region => $reg_id, cost => $cost };
  }
  @fields = sort { int($a->{cost}) <=> int($b->{cost})  } @fields;

  my $regions = $keeper->get_documents (
			class	=> 'webshop::Area',
			status	=> 1,
			order_by	=> 'name',
			return_mode	=> 'array_ref',
		);
  my %pid = map { $_->pid => 1 } @$regions;
  my @pid = keys %pid;
  my $countries = @pid ? $keeper->get_documents (
				id	=> \@pid,
				class	=> 'webshop::Country',
				return_mode	=> 'hash_ref',
		) : {};
  $m->out('');
  $m->out('<table width="95%" style="margin:5px 0;padding:5 6 5 6;border:1px solid #ccc;font-size:70%;font-family:Tahoma;background-color:#f5f5f5;color:#000;">');
  $m->out('<tr bgcolor="silver" valign="top"><th width="40%"><h5 style="margin:0; padding:4px; font-size:11px; color:blue;">Регион:</h5></th>');
  $m->out('<th width="60%"><h5 style="margin:0; padding:4px; font-size:11px; color:blue;">Стоимость доставки (число):</h5></th></tr>');

  my $max = scalar @fields + 4;
  for my $i ( 1..$max ) {
	my $rusname = "$rusname N$i";
	my $object = $fields[$i-1];
	$m->comp('.field',
		name    => $name,
		object  => $object,
		number  => $i,
		regions	=> $regions,
		countries	=> $countries,
	);
  }
  $m->out('</table>');

</%init>
<%def .field>
<%args>

	$name	=> undef
	$object	=> undef
	$number	=> undef
	$regions	=> []
	$countries	=> {}

</%args>
<%init>
</%init>
<tr valign="top">
<td>
<select name="<% $name.'_area_'.$number %>" style="width:97%">
<option value="">--- не выбран ---</option>
% foreach my $region ( @$regions ) {
%	my $selected = ref $object && $object->{region} == $region->id ? ' selected' : '';
%	my $country = $region->pid && exists $countries->{$region->pid} ? $countries->{$region->pid} : undef;
<option value="<% $region->id %>"<% $selected %>><% $region->name.( ref $country ? ' ('.$country->name.')' : '' ) %></option>
% }
</select>
</td>
<td><input type="text" name="<% $name.'_cost_'.$number %>" value="<% (ref $object ? $object->{cost} : '') |h %>" style="width:97%;"></td>
</tr>
</%def>