<?
	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#--------------------------------------------------------------------
	# $Id: update.php3,v 1.5 2000/12/20 19:09:22 nclemons Exp $
	#--------------------------------------------------------------------
	

	# Recalculate the counters.
	#
	
	if ($f_func == "update") { ?>
	
	<tr bgcolor=#BBBBBB>
	<td width=100% bgcolor=#202020 align=center>
	<table width=50% bgcolor=#bbbbbb cellspacing=2>
		<tr>
<?
			echo "<td colspan=2 bgcolor=$sys_col[confirm_bg] align=center>";
			echo "	<font color=$sys_col[confirm_text]>";
?>
				This function will recalculate all the counters for the slips
				within Keystone, and update the tables accordingly.  It may take
				a few minutes to complete.<p>
				Do you wish to continue?<br></font>
			</td>
		</tr>
		<tr>
			<? echo "<td bgcolor=$sys_col[confirm_yes] align=center>"; ?>
			<? echo "<a href=\"nbrowse.php3?f_func=go_update\"><font color=$sys_col[confirm_text]>Yes</font></a>"; ?>
			</td>
			<? echo "<td bgcolor=$sys_col[confirm_no] align=center>" ?>
			<? echo "<a href=\"nbrowse.php3\"><font color=$sys_col[confirm_text]>No</font></a>"; ?>
			</td>
		</tr>
	</table>
	</td>
	</tr>
	
	<?
	
	} else {
	
		if ($f_func == "go_update") {
		
			#
			# we're go.  we need to run through all the slips, calculating children
			# and followups for each, and updating 'num_children' and 'num_follows'
			# for each slip
			#
			
			$tot_slipcount = 0;
			$tot_chwrong = 0;
			$tot_folwrong = 0;
			
			$main_dbh = db_query("select sid,num_children,num_follows from slips");
			while ($main_slip = db_fetch_array($main_dbh)) {
				$tot_slipcount++;
				
				# how many children do I have?
				$ch_dbh = db_query("select sid from slips where spid=$main_slip[sid]");
				$chcount = db_numrows($ch_dbh);
				
				# how many followups do I have?
				$fo_dbh = db_query("select f_sid from followups where f_sid=$main_slip[sid]");
				$focount = db_numrows($fo_dbh);
				
				# update the slips table with the proper counts.
				if ($main_slip[num_children] <> $chcount) {
					$tot_chwrong++;
					$chupd = db_query("update slips set num_children=$chcount where sid=$main_slip[sid]");
				}
				
				if ($main_slip[num_follows] <> $focount) {
					$tot_folwrong++;
					$foupd = db_query("update slips set num_follows=$focount where sid=$main_slip[sid]");
				}
			}
			echo "<tr bgcolor=#BBBBBB>";
			echo "<td width=100% bgcolor=#202020 align=center>";
			echo "<font color=#ffffff>";
			echo "<h3>Update complete</h3><br>";
			echo "Total of $tot_slipcount slips processed.<br>";
			echo "Total wrong children counts: $tot_chwrong.<br>";
			echo "Total wrong followup counts: $tot_folwrong.<br>";
			echo "<p>";
			echo "<a href=\"nbrowse.php3\">Return</a>";
			echo "</td></tr>";
		} else {
			#
			# We shouldn't have gotten here.
			#
			
			$lastresult[0]=1;
			$lastresult[1]="Unexpected condition occured! [update.php3]";
		}

	}
	
	?>
