<?PHP 

// do I need to do additional security checks to make sure the script is being called appropriately?

include("config.php");

if(is_logged_in($user)){
  $username = base64_decode($_SESSION['user']);
  if($username != "Slowman" && $username != "Rappstar" && $username != "Herbert") { 
    header('Location:https://slowtwitch.com/calendar/');
    exit();
  }
} else { 
  header('Location:https://slowtwitch.com/calendar/');
  exit(); 
}

// set the page title
$pagetitle = "Admin: Race Tag Clear";

// set meta tags
$meta_keywords = "";
$meta_description = "";
?>

<? include("include_common_head.php"); ?>
<? include("include_common_content.php"); ?>

<!-- Start outer -->
<div id="outer">

<!-- BODY -->
<div class="content">
<!-- LEFT COLUMN -->

<!-- End left column -->
<!-- Content col -->

<h1 class="content-title"><strong>Clearing Race Tags&hellip;</strong></h1>
<div class="indent">
<?
// select any races that we haven't cleared that are past due.
$sql = "SELECT uid FROM gforum_Triathlons WHERE UNIX_TIMESTAMP(date) < ".time()." AND racetags != ''";
			  
$results = mysql_query($sql) or die(mysql_error());

// counter of races which we are clearing
$tags_cleared = 0;

echo "Processing races&hellip;<br />";

while($row = mysql_fetch_array($results)) {
	// select users who are in the exploded tags
	$tagresults = mysql_query("SELECT racetags FROM gforum_Triathlons WHERE uid=$row[uid]");
	$tagrow = mysql_fetch_array($tagresults);
	$tags = explode(",", $tagrow[racetags]);

    // DEBUG: which UIDs are we processing
    echo ("RACE_UID: <a href=\"https://slowtwitch.com/calendar/individual.php?uid=".$row[uid]."\" target=\"_blank\">".$row[uid]."</a><br />");

	echo "<div class='indent'>Processing users&hellip;<br />";
	foreach ($tags AS $temp_useruid) {
		if ($temp_useruid != '') {
			// DEBUG: which USERUID are we processing
			echo ("USER_UID: ".$temp_useruid."<br />");
			$profileresults = mysql_query("SELECT user_racetags FROM ".$prefix."User WHERE user_id='$temp_useruid'") or die(mysql_error());
			$profilerow = mysql_fetch_array($profileresults);
			$profiletags = explode(",", $profilerow[user_racetags]); 
	
			//clear the race from the users tags
			$key = array_search($row[uid], $profiletags);
			unset($profiletags[$key]);
			$profiletags = implode(",", $profiletags);
			mysql_query("UPDATE ".$prefix."User SET user_racetags='$profiletags' WHERE user_id=$temp_useruid");
		}
    }
    echo "</div>";
    
    //clear the race
	mysql_query("UPDATE gforum_Triathlons SET racetags='' WHERE uid=$row[uid]") or die(mysql_error());
	
	// increment the counter of races cleared
	$tags_cleared = $tags_cleared + 1;
}

echo "Done processing races.<br />";

// DEBUG: how many tags were cleared
echo ("Racetags cleared: ".$tags_cleared."<br />");

//header('Location: https://slowtwitch.com/calendar/?tags_cleared='.$tags_cleared.'.php');

?>

</div>
<!-- End content col -->

</div>
<!-- End BODY -->
</div>
<!-- End Outer -->

<? include("include_common_footer.php"); ?>