__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 @deltables = split /[\s0,]/, $CGI->{ui_delete_tables};
		unshift @deltables, $CGI->{mv_data_table};
		my @out;
		if(@deltables == 1 and $deltables[0] !~ /:/) {
#Debug("In simple case");
			## Simple case
			DELREC: {
				my $t = $deltables[0];
#Debug("simple case table=$t");
				if ( $Tag->if_mm('!tables', "$t=d") ) {
					push @errors,
						errmsg("Not authorized to delete from table %s", $t);
					last DELREC;
				}

				my $db = $Db{$t};
				if($db->config('COMPOSITE_KEY')) {
					$CGI->{$idp} =~ s/-_NULL_-/\0/g;
					my $key = $CGI->{$idp};
					$key =~ s/\0/,/g;
#Debug("simple case multiple key=$key");
					my $rc = $db->delete_record($CGI->{$idp});
					if($rc) {
						push @out, errmsg("Deleted %s from %s", $key, $t);
					}
					else {
						push @errors, errmsg("Unable to delete %s: %s", $key, $@);
					}
				}
				else {
					for(@{$CGI_array->{$idp}}) {
#Debug("simple case regular key=$_");
						my $rc = $db->delete_record($_);
						if($rc) {
						  push @out, errmsg("Deleted %s from %s", $_, $t);
						}
						else {
							push @errors, errmsg("Unable to delete %s from %s: %s", $_, $t, $@);
						}
					}
				}
			}
		}
		else {
			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,
							errmsg("Not authorized to delete from table %s", $t);
						next;
					}
					unless ($db = $Db{$t}) {
						push @errors, errmsg("Table %s not available.", $t);
						next;
					}
					if($col) {
						next unless length ($key);
						my $k = $db->quote($key, $col);
						my $num = $db->query("delete from $t where $col = $k");
						if($num > 0) {
							push @out, errmsg(
										"deleted %s records from %s where %s = %s",
										$num,
										$t,
										$col,
										$k,
										);
						}
						else {
							push @errors, errmsg(
											"No records in %s where %s = %s",
											$t,
											$col,
											$k,
											);
						}
					}
					else {
						next unless $db->record_exists($key);
						$db->delete_record($key)
							or do {
								push @errors, $@;
								next;
							};
						push @out, errmsg("Deleted %s from %s", $key, $t);
					}
				}
			}

		}
		if(@errors) {
			my $str = '<ul><li>';
			$str .= join '<li>', @errors;
			$str .= '</ul>';
			$Tag->error({ name => 'db_maintenance', set => $str });
		}
		if(@out) {
			my $str = '<ul><li>';
			$str .= join '<li>', @out;
			$str .= '</ul>';
			$Tag->warnings($str);
		}
		return;
	[/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]
