9. Click this button to populate:
|
[button text="Populate"]
[flag type=write table="area"]
[flag type=write table="cat"]
[seti ui_failure]
[perl tables="area cat [cgi area_table]"]
my $go_page = $CGI->{mv_nextpage};
$CGI->{mv_nextpage} = '@@MV_PAGE@@';
my $ptab = $CGI->{area_table}
or return "no area source table";
my $pdb = $Db{$ptab}
or return "area source table bad";
my $adb = $Db{area}
or return "no area table";
my $cdb = $Db{cat}
or return "no cat table";
my $acol = $CGI->{area_column};
my $ccol = $CGI->{cat_column};
if(! $acol and ! $ccol ) {
return "no column to populate either";
}
my $selector = '';
my $selcode = '';
my %area_defaults = (
name => undef,
which_page => $CGI->{which_page} || 'all',
sort => '05',
sel => $CGI->{sel} || 'left',
display_type => 'name',
);
my %catmap;
my $narrow;
my $lt;
my $search_code;
if($CGI->{cat_type} eq 'simple') {
$lt = 'simple';
$selector = 'category=~CATEGORY~';
$search_code = '';
}
elsif($CGI->{cat_type} eq 'narrow') {
$narrow = 1;
$lt = 'simple';
$selector = <{search_code};
}
my %cat_defaults = (
link_type => $lt,
selector => $selector,
search => $search_code,
sort => '05',
display_type => 'name'
);
if($CGI->{nuke}) {
$adb->query('delete from area');
$cdb->query('delete from cat');
}
if($acol) {
$adb->config('AUTO_NUMBER', '1000') if ! $adb->config('_Auto_number');
return "area column $acol doesn't exist"
unless $pdb->column_exists($acol);
my $ary = $pdb->query({
sql => "select distinct $acol from $ptab order by $acol",
ml => 'all',
})
or return "area column query failed";
for(@$ary) {
my ($area) = (@$_);
my $key = $adb->set_row('');
return "no autonumbering available" if ! $key;
$catmap{$area} = $key;
for(keys %area_defaults) {
$adb->set_field($key, $_, $area_defaults{$_} || $area);
}
}
}
my %catdone;
if($ccol) {
$cdb->config('AUTO_NUMBER', '1000') if ! $cdb->config('_Auto_number');
return "cat column $ccol doesn't exist"
unless $pdb->column_exists($ccol);
my ($ary, $fh) = $pdb->query({
sql => "select $acol, $ccol from $ptab order by $ccol",
ml => 'all',
})
or return "cat column query failed";
foreach my $row (@$ary) {
my $area = $row->[$fh->{$acol}];
my $cat = $row->[$fh->{$ccol}];
my $donetag = $narrow ? "$area:$cat" : $cat;
next if $catdone{$donetag}++;
my $key = $cdb->set_row('');
return "no cat autonumbering available" if ! $key;
my %defaults = %cat_defaults;
$defaults{name} = $cat;
$defaults{sel} = $catmap{$area};
$defaults{search} =~ s/~ (\w+) ~/$row->[$fh->{lc $1}]/gx;
$defaults{selector} =~ s/~ (\w+) ~/$row->[$fh->{lc $1}]/gx;
for(keys %defaults) {
Log("setting col=$_ key=$key = $defaults{$_}");
$cdb->set_field($key, $_, $defaults{$_});
}
}
}
$CGI->{mv_nextpage} = $go_page;
my $out = $Tag->uneval( { ref => \%catmap } );
$out =~ s/,/,\n/g;
#return '' . $Tag->uneval( { ref => \%catmap } ) . "";
return;
[/perl]
[/seti]
[/button]
|