__NAME__ db_maintenance

[comment]
	I suppose this is odd, but I am always looking for better ways. 8-)
[/comment]

[set nevair]1[/set]
[if session logged_in]
[and session login_table ne __UI_ACCESS_TABLE__]
[then]
	[calc]
		return if $CGI->{item_id} ne $Session->{username};
		return if delete $CGI->{deleterecords};
		delete $Scratch->{nevair};
		return;
	[/calc]
[/then]
[else]
	[if-mm !tables =d]
		[seti ui_error]Not allowed to delete data from table [cgi mv_data_table].[/seti]
		[bounce page="__UI_BASE__/error"]
	[else]
		[set nevair][/set]
	[/else]
	[/if-mm]
[/else]
[/if]

[if cgi mv_blob_function]
[and !scratch nevair]
	[flag type=write tables="[cgi mv_data_table]"]
	[perl tables="[cgi mv_data_table]"]
	return if $Scratch->{nevair};
	my $db = $Db{$CGI->{mv_data_table}}
		or push (@errors, "no table") and return;
	my $key = $CGI->{item_id};
	my $col = $CGI->{mv_blob_field};

	$db->record_exists($key)
		or push (@errors, errmsg("no record %s", $key) ) and return;
	$db->column_exists($col)
		or push (@errors, errmsg("no column", $col)    ) and return;
	my $function = delete $CGI->{mv_blob_function}
		or push (@errors, errmsg("no function")) and return;

	my $string = $db->field($key, $col);
	my $blob;
	if($string) {
		$blob = $Tag->calc($string);
		if(! $blob) {
			push @errors, errmsg("Bad reference in col %s", $col);
			return;
		}
	}
	else {
		push @errors, errmsg("Nothing to manage, blob empty.");
		return;
	}
	my $nick	 = $CGI->{mv_blob_nick};
	my $saveit;

	if($function eq 'delete') {
		return "no nick to delete" unless $nick;
		my @keys = split /\0/, $CGI->{mv_blob_nick};
		for(@keys) {
			delete $blob->{$_}    and $saveit = 1;
		}
	}
	elsif ($function eq 'rename') {
		my $new = $CGI->{mv_blob_nick_new}
			or do {
				push @errors, errmsg("no new nick to apply");
				return;
				};
		my $hash = delete $blob->{$nick}
			or do {
				push @errors, errmsg("nick %s does not exist", $nick);
				return;
				};
		$blob->{newnick} = $hash;
		$saveit = 1;
	}

	if($saveit) {
		$db->set_field($key, $col, $Tag->uneval( { ref => $blob } ));
	}

	[/perl]
	[/else]
	[/if-mm]
[/if]

[if cgi deleterecords]
[and !scratch nevair]
	[flag type=write table="[cgi mv_data_table] [cgi ui_delete_tables]"]

    [tmp mod_tables][/tmp]
    [calc]
        # get the tables to pass to the next perl tag
        my @tables = qw/[cgi mv_data_table] [cgi ui_delete_tables]/;
        map { s/:.*//; } @tables;
        $Scratch->{'mod_tables'} = join(' ',@tables);
    [/calc]

	[perl tables="[scratch mod_tables]"]
		my $idp;
		return unless delete $CGI->{deleterecords};
		for(qw/ui_delete_id item_id/) {
			next if ! $CGI->{$_};
			$idp = $_;
			last;
		}

#Log("using idp=$idp, value=$CGI->{$idp}");
		$return unless $idp;

		delete $Scratch->{ui_location};
		$Config->{NoSearch} = '';
		
		my $out = '';
		my @deltables = split /[\s0,]/, $CGI->{ui_delete_tables};
		unshift @deltables, $CGI->{mv_data_table};
		for(@{$CGI_array->{$idp}}) {
			my $key = $_;
			my $db;
			for(@deltables) {
				my ($t, $col) = split /:/, $_;
				next unless $t;
				if ( $Tag->if_mm('!tables', "$t=d") ) {
					push @errors, "Not authorized to delete from table $t";
					next;
				}
				unless ($db = $Db{$t}) {
					push @errors, "Table $t not available.";
					next;
				}
				if($col) {
					next unless length ($key);
					$key = $db->quote($key, $col);
					my $num = $db->query("delete from $t where $col = $key");
					if($num > 0) {
						$out .= "deleted $num records from $t where $col = $key</BR>";
					}
					else {
						$out .= "No records in $t where $col = $key</BR>";
					}
				}
				else {
					next unless $db->record_exists($key);
					$db->delete_record($key)
						or do {
							push @errors, $@;
							next;
						};
					$out .= "deleted $key from $t<br>";
				}
			}
		}

	[/perl]
[/if]

[perl]
	if(@errors) {
		my $plural = @errors > 1 ? 's' : '';
		$out .= "<FONT CLASS=error>Error$plural:<UL><LI>" .
				join ("<LI>", @errors)                    .
				"</UL></FONT><BR>";
	}
	$Scratch->{ui_failure} = $out;
	return;
[/perl]
