<?php
include 'rb-include.php';
rb_header();

if ($rdf_doc) {
	$r = pg_exec($db, "select id from rdf_files where url='$rdf_doc' and owner='$uid'");
	if (pg_numrows($r) == 0) {
		pg_exec($db, "insert into rdf_files (url, owner) values ('$rdf_doc', '$uid')");
		pg_freeresult($r);
		$r = pg_exec($db, "select id from rdf_files where url='$rdf_doc' and owner='$uid'");
	}
	$rdf_file_created = pg_result($r, 0, 0);
	pg_freeresult($r);

	$artists_res = pg_exec($db, "select id, name from artists where owner='$uid' and rdf_file is null");
	for ($artist = 0; $artist < pg_numrows($artists_res); $artist++) {
		pg_exec($db, "update artists set rdf_file='$rdf_file_created', rdf_id='artist" . pg_result($artists_res, $artist, 0) . "' where id='" . pg_result($artists_res, $artist, 0) . "'");
	}
	pg_freeresult($artists_res);

	$songs_res = pg_exec($db, "select id, title from songs where owner='$uid' and rdf_file is null");
	for ($song = 0; $song < pg_numrows($songs_res); $song++) {
		pg_exec($db, "update songs set rdf_file='$rdf_file_created', rdf_id='song" . pg_result($songs_res, $song, 0) . "' where id='" . pg_result($songs_res, $song, 0) . "'");
	}
	pg_freeresult($songs_res);

	$afs_res = pg_exec($db, "select id  from audiofiles where owner='$uid' and rdf_id is null");
	for ($af = 0; $af < pg_numrows($afs_res); $af++) {
		pg_exec($db, "update audiofiles set rdf_id='audio" . pg_result($afs_res, $af, 0) . "' where id='" . pg_result($afs_res, $af, 0) . "'");
	}
	pg_freeresult($afs_res);
}

$rdf_files_res = pg_exec($db, "select id, url from rdf_files where owner='$uid'");
if (pg_numrows($rdf_files_res) == 0) {
	echo '<p>The final step in publishing music on Gnomoradio involves the creation of one or more RDF files to describe the music and artists.</p>', "\n";
	echo '<p>There are a few options in doing this.  The first option is to simply put all information in a single RDF file.  This is the recommended method for beginning users and people who plan to publish a small number of songs.</p>', "\n";
	echo '<p>To do so, please enter the location of the exact file where you plan to upload the RDF file on a web server: (an example location is <tt>http://www.tommymagik.com/discography/science.rdf</tt>)</p>', "\n";
	echo '<form action="rdf-finalize.php" method="post"><input name="rdf_doc" size="50"/><input type="submit" value="Create file"/></form>', "\n";
} else {
	$songs_res = pg_exec($db, "select title from songs where owner='$uid' and rdf_file is null");
	$artists_res = pg_exec($db, "select name from artists where owner='$uid' and rdf_file is null");
	$albums_res = pg_exec($db, "select title from albums where owner='$uid' and rdf_file is null");

	if (pg_numrows($songs_res) == 0
	    && pg_numrows($artists_res) == 0
	    && pg_numrows($albums_res) == 0) {
		echo '<p>Congratulations.  Your RDF files have been generated, and now you only need to download each link below and upload it to the corresponding location on your web server.  If you need to change any of the locations below, please <a href="rdf-edit-files.php">click here</a> before downloading any of the files.</p>', "\n";

		echo "<ul>\n";
		for ($rdf_file = 0; $rdf_file < pg_numrows($rdf_files_res); $rdf_file++) {
			echo '<li><font size="-1"><a href="rdf-output.php?id=', pg_result($rdf_files_res, $rdf_file, 0), '">', pg_result($rdf_files_res, $rdf_file, 1), '</a></font></li>', "\n";
		}
		echo "</ul>\n";
	} else {
		echo '<p>You have added more songs to your profile, and you must assign them to an RDF file.  To assign them to an existing RDF file, please choose from the options below:</p>';
		echo '<ul>', "\n";
		for ($rdf_file = 0; $rdf_file < pg_numrows($rdf_files_res); $rdf_file++) {
			echo '<li><font size="-1"><a href="rdf-finalize.php?rdf_doc=', urlencode(pg_result($rdf_files_res, $rdf_file, 1)), '">', pg_result($rdf_files_res, $rdf_file, 1), '</a></font></li>', "\n";
		}
		echo '</ul>', "\n";
		echo '<p>If instead you would like to create a new RDF file with these songs, please enter the exact location where you plan to upload the RDF file on a web server: (an example location is <tt>http://www.tommymagik.com/discography/science.rdf</tt>)</p>', "\n";
		echo '<form action="rdf-finalize.php" method="post"><input name="rdf_doc" size="50"/><input type="submit" value="Create file"/></form>', "\n";
	}

	pg_freeresult($songs_res);
	pg_freeresult($artists_res);
	pg_freeresult($albums_res);
}
pg_freeresult($rdf_files_res);

rb_footer();
?>
