<?php
$GLOBALS['User_ID'] = $_COOKIE["User_ID"];
if (is_null($GLOBALS['User_ID'])) { $GLOBALS['User_ID'] = 0; }

	//$db = mysql_connect("127.0.0.1", "root", "523i12");	//connect to DB
	$db = mysql_connect("localhost", "newalexandria", "523i12"); //connect to DB
	mysql_select_db("newalexandria",$db);	//make active
	
	// Clear tmp_fragmentsusers table
	if (strlen($GLOBALS['User_ID']) > 0) {
		$sql = "DELETE FROM tmp_fragmentsusers WHERE User_ID = ".$GLOBALS['User_ID'];
		$result = mysql_query($sql);
	}

//echo count($_GET['Dream_ID'])."<BR>".count($_GET['Sel_User_ID'])."<BR>";

	$sql = "SELECT UD.Dream_ID, F.Fragment_ID, F.words, DF.sort FROM dreamsfragments DF INNER JOIN fragments F ON DF.Fragment_ID = F.Fragment_ID INNER JOIN usersdreams UD ON UD.Dream_ID = DF.Dream_ID ";
	if (count($_GET['Sel_Dream_ID']) > 0) {
		$sql = $sql."WHERE UD.Dream_ID = ".$_GET['Sel_Dream_ID'][0]." ";
		for ($i = 1; $i < count($_GET['Sel_Dream_ID']); $i++) {
			$sql = $sql."OR UD.Dream_ID = ".$_GET['Sel_Dream_ID'][$i]." ";
		}
	} elseif (count($_GET['Sel_User_ID']) > 0) {
		$sql = $sql."WHERE UD.User_ID = ".$_GET['Sel_User_ID'][0]." ";
		for ($i = 1; $i < count($_GET['Sel_User_ID']); $i++) {
			$sql = $sql."OR UD.User_ID = ".$_GET['Sel_User_ID'][$i]." ";
		}
	}
	$sql = $sql."ORDER BY RAND() LIMIT ";
	$result = mysql_query($sql."4");
	
// echo $sql."<BR><BR>";

function sentence ($last)  // Build a sentence
  {
  	$totalqts = 4;
	$nmbr = rand(2,$totalqts);

	//$sql = "SELECT * FROM fragments WHERE Dream_ID = 2 ORDER BY RAND() LIMIT ".$nmbr;
	$sql = "SELECT UD.Dream_ID, F.Fragment_ID, F.words, DF.sort FROM dreamsfragments DF INNER JOIN fragments F ON DF.Fragment_ID = F.Fragment_ID INNER JOIN usersdreams UD ON UD.Dream_ID = DF.Dream_ID ";
	if (count($_GET['Sel_Dream_ID']) > 0) {
		$sql = $sql."WHERE UD.Dream_ID = ".$_GET['Sel_Dream_ID'][0]." ";
		for ($i = 1; $i < count($_GET['Sel_Dream_ID']); $i++) {
			$sql = $sql."OR UD.Dream_ID = ".$_GET['Sel_Dream_ID'][$i]." ";
		}
	} elseif (count($_GET['Sel_User_ID']) > 0) {
		$sql = $sql."WHERE UD.User_ID = ".$_GET['Sel_User_ID'][0]." ";
		for ($i = 1; $i < count($_GET['Sel_User_ID']); $i++) {
			$sql = $sql."OR UD.User_ID = ".$_GET['Sel_User_ID'][$i]." ";
		}
	}
	$sql = $sql."ORDER BY RAND() LIMIT ".$nmbr;

// echo $sql."<BR>";  //  SQL DEBUG

//	echo $sql;
	$result = mysql_query($sql);
	
    if (mysql_affected_rows()>0) {
		$strOut = "";
		
		$rows = mysql_affected_rows();

		for ($i=1; $i<=$rows; $i++) {
			$curRow = mysql_fetch_array($result);
			if (substr($curRow['words'], (strlen($curRow['words'])-1), 1)=="?") {
				$punctuation = "?";
				$curRow['words'] = substr($curRow['words'], 0, (strlen($curRow['words'])-1));
			} elseif (substr($curRow['words'], (strlen($curRow['words'])-1), 1)=="!") {
				$punctuation = "!";
				$curRow['words'] = substr($curRow['words'], 0, (strlen($curRow['words'])-1));
			} else {
				$punctuation = ".";
			}

			$strOut = $strOut.strtolower(substr($curRow['words'], 0, 1)).substr($curRow['words'], 1, (strlen($curRow['words'])-1))." ";
			
			// Get the next Sort value to insert 
			$sql = "SELECT COALESCE(MAX(sort)+1, 1) sort FROM tmp_fragmentsusers WHERE User_ID = ".$GLOBALS['User_ID'];
			$sortRow = mysql_fetch_array(mysql_query($sql));
			$sort = $sortRow['sort'];
			
			// Set fragment type
			if (( $last==1 ) && ( $i==$rows )) {
				$type = "B";
			} elseif ( $i==$rows ) {
				$type = "P";
			} else {
				$type = "";
			}
			
			// Create the tmp User->Fragment relationship
			$sql = "INSERT INTO tmp_fragmentsusers (Fragment_ID, User_ID, sort, type) VALUES (".$curRow['Fragment_ID'].", ".$GLOBALS['User_ID'].", ".$sort.", '".$type."')";
			$result2 = mysql_query($sql);
//			echo $sql."<BR>";
		 }
		 $strOut = rtrim($strOut);
		 $strOut = ucfirst($strOut).$punctuation;
		return $strOut;
	} else {
		return "Hiccup ";
	}
	
/*

	// Get the next Sort value to insert 
	$sql = "SELECT COALESCE(MAX(sort)+1, 1) sort FROM usersdreams WHERE User_ID = ".$GLOBALS['User_ID'];
	$curRow = mysql_fetch_array(mysql_query($sql));
	$sort = $curRow['sort'];


	// Create the User->Fragment relationship
	$sql = "INSERT INTO tmp_fragmentsusers (Fragment_ID, User_ID, sort) VALUES (".$Fragment_ID.", ".$GLOBALS['User_ID'].", ".$sort.")";
	$result = mysql_query($sql);
		
*/	
	
  }
  	
function paragraph ()  // Build a paragraph
  {
  	$totalqts = 7;
	$nmbr = rand(1,$totalqts);

	$strOut = "";
	for ($i=1; $i<=$nmbr; $i++) {
		// Print the sentences; if it's the last sentence, the the function that		
		$strOut = $strOut.sentence( (($i==$nmbr) ? 1 : 0) )."  "; //.(($i==$nmbr) ? 1 : 0);
	 }
	 $strOut = rtrim($strOut)."<BR><BR>";
	return $strOut;
  }
  	
//	$sql = "SELECT * FROM fragments WHERE Dream_ID = 2 ORDER BY sort";


?>
<html><head><title>Dream Loom</title>

<link type="text/css" rel="stylesheet" href="style.css"> 
</head>

<body>

	<?php 
	// If there's no Dream_ID then we're making a new dream
	if (strlen($_GET['Dream_ID']) == 0) {
		//  Build the overall page
		if (mysql_affected_rows()==0) {
			echo "This collective has no dreams";
		} else {
			$totalqts = 5;
			$nmbr = rand(2,$totalqts);
		
			$strOut = "";
			for ($i=1; $i<=$nmbr; $i++) {
					echo paragraph();
				}
		}	?>
<BR><BR><a href="dream.php">More, Please</a>

<BR><BR><a href="dream-contrib-UI.php?x=1">Remember this one</a>

<BR><BR><BR><a href="dream-build.php">Choose different sources</a>
<?php
	// If there is a Dream_ID then we're viewing an existing dream
	} else {
		$sql = "SELECT F2.words title, F.words, DF.type FROM dreamsfragments DF INNER JOIN fragments F ON DF.Fragment_ID = F.Fragment_ID INNER JOIN dreams D ON DF.Dream_ID = D.Dream_ID INNER JOIN fragments F2 ON D.Name_Fragment_ID = F2.Fragment_ID WHERE D.Dream_ID = ".$Dream_ID." ORDER BY DF.sort";
		$result = mysql_query($sql);
		
		if (mysql_affected_rows()>0) {
			$curRow = mysql_fetch_array($result);
			echo "<BR>".$curRow['title']."<BR>";
			echo "<BR>".$curRow['words'];
			
			while ($curRow = mysql_fetch_array($result)) {
				echo "&nbsp;".$curRow['words'];
				if ($curRow['type']=="P") {
					echo ".&nbsp;&nbsp;";
				} elseif ($curRow['type']=="B") {
					echo ".<BR><BR>";
				}
			}
		}
		?>
<BR><BR><BR><a href="dream-view-UI.php">View a different dream</a>
<?php
	}   ?>
			
<BR><a href="home.php">back</a>

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1203790-1";
urchinTracker();
</script></body></html>

<!--  -->
<!-- RANDOM DB IMAGE CODE IN PHP -->
<!--  -->
<!-- db=mysql_connect("localhost","enygma","password"); -->
<!-- mysql_select_db("images",$db); -->
<!-- $result=mysql_query("select * from images"); -->
<!-- $row=mysql_num_rows($result); -->
<!-- srand((double)microtime()*1000000); -->
<!-- $random=rand(1,$row); -->
<!-- $imagenum=mysql_query("select * from images where ID=$random"); -->
<!-- while ($image=mysql_fetch_array($imagenum)) { -->
<!-- printf("<a href='$image[2]'><img src='$image[0]' border=0></a>"); -->
<!-- } -->
<!--  -->
<!--  -->