#!/usr/local/bin/perl
# Show all MySQL and PostgreSQL databases owned by this domain

require './virtual-server-lib.pl';
&ReadParse();
$d = &get_domain($in{'dom'});
&can_edit_domain($d) || &error($text{'edit_ecannot'});
&can_edit_databases($d) || &error($text{'databases_ecannot'});

&ui_print_header(&domain_in($d), $text{'databases_title'}, "");

# Fix up manually deleted databases
if (&can_import_servers()) {
	@all = &all_databases($d);
	&resync_all_databases($d, \@all);
	}

# Build and show DB list
@dbs = &domain_databases($d);
($dleft, $dreason, $dmax) = &count_feature("dbs");
if (@dbs) {
	print &ui_form_start("delete_databases.cgi", "post");
	print &ui_hidden("dom", $in{'dom'}),"\n";
	print &select_all_link("d"),"\n";
	print &select_invert_link("d"),"\n";
	if ($dleft != 0) {
		print "<a href='edit_database.cgi?dom=$in{'dom'}&new=1'>",
		      $text{'databases_add'},"</a>\n";
		}
	print "<br>\n";
	print &ui_columns_start([ "", $text{'databases_db'},
				  $text{'databases_type'},
				  $text{'databases_action'} ], undef, 0,
				[ "width=5" ]);
	foreach $db (@dbs) {
		local $action;
		if ($db->{'type'} eq 'mysql' &&
		    &foreign_available("mysql")) {
			$action = "<a href='../mysql/edit_dbase.cgi?db=$db->{'name'}'>$text{'databases_man'}</a>";
			}
		elsif ($db->{'type'} eq 'postgres' &&
		       &foreign_available("postgresql")) {
			$action = "<a href='../postgresql/edit_dbase.cgi?db=$db->{'name'}'>$text{'databases_man'}</a>";
			}
		elsif ($db->{'link'}) {
			$action = "<a href='$db->{'link'}'>$text{'databases_man'}</a>";
			}
		print &ui_checked_columns_row([
			"<a href='edit_database.cgi?dom=$in{'dom'}&name=$db->{'name'}&type=$db->{'type'}'>$db->{'name'}</a>",
			$db->{'desc'},
			$action ],
			[ "width=5" ],
			"d", $db->{'type'}."_".$db->{'name'});
		}
	print &ui_columns_end();
	print &select_all_link("d"),"\n";
	print &select_invert_link("d"),"\n";
	}
else {
	print "<b>$text{'databases_none'}</b><p>\n";
	}
if ($dleft != 0) {
	print "<a href='edit_database.cgi?dom=$in{'dom'}&new=1'>",
	      $text{'databases_add'},"</a>\n";
	}
print "<br>\n";
if (@dbs) {
	print &ui_form_end([ [ "delete", $text{'databases_delete'} ] ]);
	}
if ($dleft == 0) {
	print &text('databases_noadd'.$dreason, $dmax),"<br>\n";
	}

# Show form to change database usernames
if (!$d->{'parent'} && $virtualmin_pro) {
	print "<hr>\n";
	print &ui_form_start("save_dbname.cgi");
	print &ui_hidden("dom", $in{'dom'}),"\n";
	print &ui_table_start($text{'databases_uheader'}, undef, 2);

	foreach $f (@database_features) {
		$sfunc = "set_${f}_user";
		$ufunc = "${f}_user";
		if (defined($sfunc) && $config{$f} && $d->{$f}) {
			$un = &$ufunc($d);
			print &ui_table_row($text{'feature_'.$f},
			    &ui_opt_textbox($f, undef, 20,
				&text('databases_leave', "<tt>$un</tt>")));
			}
		}

	print &ui_table_end();
	print &ui_form_end([ [ "save", $text{'save'} ] ]);
	}

# Show database import form, if there are any not owned by any user
if (&can_import_servers()) {
	foreach $dd (&list_domains()) {
		foreach $db (&domain_databases($dd)) {
			$inuse{$db->{'type'},$db->{'name'}}++;
			}
		}
	@avail = grep { !$inuse{$_->{'type'},$_->{'name'}} &&
		        !$_->{'special'} } @all;
	if (@avail) {
		print "<hr>\n";
		print &ui_form_start("import_database.cgi", "post");
		print &ui_hidden("dom", $in{'dom'}),"\n";
		print &ui_table_start($text{'databases_iheader'}, undef, 2);

		print &ui_table_row($text{'databases_ilist'},
			&ui_select("import", [ ],
			    [ map { [ "$_->{'type'} $_->{'name'}",
				      "$_->{'name'} ($_->{'desc'})"
				    ] } @avail ], 5, 1));

		print &ui_table_end();
		print &ui_form_end([ [ "ok", $text{'databases_import'} ] ]);
		}
	}

&ui_print_footer(&domain_footer_link($d),
		 "", $text{'index_return'});

