<?
include("lib/mysqldb.php");
$img_dir = "photos/";
$pub_dir = "publications/";
$files_dir = "files/";

//==================================================================
//						   DISPLAY_HOME_PAGE
//==================================================================
function display_home_page() {
/*  Gets the group's mission statement and displays
 *  it in a table.
 */
global $sql;

$result_mission = $sql->exec("SELECT * FROM mission WHERE groupName='espresso'");

echo "
	<BR/>
	<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\" WIDTH=\"650\">
		<TR>
			<TD CLASS=\"mission_title\">
				MISSION STATEMENT
			</TD>
		</TR>
		<TR>
			<TD CLASS=\"mission_data\">"
				.$result_mission[0]->missionStatement.
			"</TD>
		</TR>
		 <TR>
			<TD CLASS=\"mission_title\">
				&nbsp;
			</TD>
		</TR>
														 
	</TABLE>


	<BR/>
	
	<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\" BORDER=\"0\" WIDTH=\"650\">
		<TR>
			<TD><a href=\"images/research_model_diagram.png\" target=\"_blank\"><img src=\"images/research_model_diagram_small.png\" alt=\"Espresso Research Model Diagram\" border=\"1\" width=\"650\" height=\"437\" ></a></TD>
		</TR>										<TR>
			<TD style=\"text-align: center\">The Espresso Research Model &nbsp;(<a href=\"images/research_model_diagram.png\" target=\"_blank\">Click to zoom in</a>)</TD>
		</TR>
	</TABLE>
";
}

function obfuscate($orig) {
    $obfus = '';
    for ($i = 0; $i < strlen($orig); $i++) {
        $obfus .= '&#' . ord($orig{$i}) . ';';
    }
    return $obfus;
}

function email_link($email) {
    return '<a href="'.obfuscate("mailto:").obfuscate($email).'">'.obfuscate($email).'</a>';
}



//===================================================================
//						DISPLAY_GROUP_MEMBERS
//===================================================================
function display_group_members($int_ext) {
/*  Receives the button name as a parameter. Gets the link Name from the links database
 *  to display on top of the page. Gets the key from the links database and uses it to
 *  search the members database.
 */
global $sql, $img_dir;
	
	$result_title = $sql->exec("SELECT * FROM links WHERE button='$int_ext'");
	$int_ext = $result_title[0]->keyField;
	$result_members = $sql->exec("SELECT * FROM members WHERE intExtMember='$int_ext' ORDER BY mid ASC");

	insert_page_header($result_title[0]->linkName);

	if (sizeof($result_members) != 0) {
		for ($i = 0; $i < sizeof($result_members); $i++) {
echo "		<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
			<TR>
				<TD CLASS=\"mission_title\" COLSPAN=\"3\">
					&nbsp;
				</TD>
			</TR>
			<TR>
				<TD CLASS=\"table_image\" ROWSPAN=\"5\" WIDTH=\"80\">";
				if ($result_members[$i]->picName == "") $img_src = "default.jpg";
				else 					$img_src = $result_members[$i]->picName;
echo "					<IMG SRC=\"$img_dir".$img_src."\" WIDTH=\"75\" HEIGHT=\"100\" BORDER=\"0\">
				</TD>
				<TD CLASS=\"table_title\" WIDTH=\"110\">
					Name
				</TD>
				<TD CLASS=\"table_data\">
					".$result_members[$i]->title." ".$result_members[$i]->firstName." ".$result_members[$i]->lastName."
				</TD>
			</TR>
			<TR>
				<TD CLASS=\"table_title\" WIDTH=\"110\">
					Field Title
				</TD>
				<TD CLASS=\"table_data\">
					".$result_members[$i]->fieldTitle."
				</TD>
			</TR>
			<TR>
				<TD CLASS=\"table_title\" WIDTH=\"110\">
					Degree Obtained
				</TD>
				<TD CLASS=\"table_data\">
					".$result_members[$i]->degreeObtained."
				</TD>	
			</TR>
			<TR>
				<TD CLASS=\"table_title\" WIDTH=\"110\">
					Current Degree
				</TD>
				<TD CLASS=\"table_data\">
					".$result_members[$i]->degreeCurrent."
				</TD>
			</TR>
			<TR>
				<TD CLASS=\"table_title\" WIDTH=\"110\">
					email
				</TD>
				<TD CLASS=\"table_data\">".email_link($result_members[$i]->email)."
					
				</TD>
			</TR>
		</TABLE>\n";
			if(strlen($result_members[$i]->abstract) > 0) {
echo "<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
			<TR>
				<TD CLASS=\"table_title\" COLSPAN=\"3\">
					<A HREF=\"Javascript:expand('".$result_members[$i]->mid."')\">(show/hide) Abstract</A>
				</TD>
			</TR>
			<TR>
				<TD CLASS=\"table_data\" ID=\"".$result_members[$i]->mid."_0\" COLSPAN=\"3\" STYLE=\"display:".(($int_ext !== "internal")?"none":"").";\">
					".$result_members[$i]->abstract."
				</TD>
			</TR>
		</TABLE>
		<BR/>";
			}else{
echo "<BR>\n";
			}
		}
	}
}
function display_polls(){
	
	if ($_GET["espresso_soft"] != "true"){
		$return_path = "http://espresso.cs.up.ac.za/main.php";
	}else{
		$return_path = "http://www.espresso-soft.org/main.php";
	}
	
?>
	<TABLE CELLSPACING="1" CELLPADDING="3" BORDER="0" WIDTH="650">
		<TR>
			<TD CLASS="table_pgheader">
				Polls
			</TD>
		</TR>
	</TABLE>
	<BR/>
	<TABLE CELLSPACING="1" CELLPADDING="3" BORDER="0" WIDTH="650">
		<TR>
			<TD CLASS="news_title" COLSPAN="3">The following polls are currently active or have closed within the last month:</TD>
			
		</TR>
		<TR>
			<TD CLASS="table_data"><ul><? 
			$sql = "SELECT polls.*, TO_DAYS(endDate) >= TO_DAYS('".date('Y-m-d')."') as isActive, SUM(poll_publications.votes) as totalVotes, MAX(poll_publications.votes) as maxVotes FROM poll_publications LEFT JOIN polls ON (polls.id=poll_publications.poll_ref ) WHERE TO_DAYS(startDate) <= TO_DAYS('".date('Y-m-d')."') AND TO_DAYS(endDate) >= TO_DAYS('".date('Y-m-d',strtotime('1 month ago'))."') AND id IS NOT NULL GROUP BY poll_ref";
			$rs = mysql_query($sql);

			if (mysql_num_rows($rs) == 0){
				echo "There are currently no active polls";
			}else while ($row = mysql_fetch_array($rs)){
				echo "<li>".$row["title"]." (running from ".$row["startDate"]." to ".$row["endDate"].")</li>";
			}
		
		 ?></ul></TD>
		</TR><TR><TD><BR></TD></TR>
  </TABLE>
  <?
  $rs= mysql_query($sql);
  while($top_row = mysql_fetch_array($rs)){

	$hasvoted = isset($_COOKIE[$top_row["digest"]]) || $top_row["isActive"]== '0';?>
	<form method="POST" enctype="multipart/form-data" action="vote.php?espresso_soft=<?=($_GET["espresso_soft"] == "true")?"true":"false"?>">
  <input type="hidden" name="pollID" value="<?=$top_row["id"]?>" id="pollID" />
	<TABLE CELLSPACING="1" CELLPADDING="3" BORDER="0" WIDTH="650">
	<TR>
		<TD CLASS="news_title" COLSPAN="<?=($hasvoted)?3:4?>"><?=$top_row["title"]?></TD>
		
	</TR>
	<TR><TD class="table_data" colspan="<?=($hasvoted)?3:4?>"><?=$top_row["message"]?><BR/><BR/></TD></TR>
		<?

			$sql = "SELECT * FROM poll_publications WHERE poll_ref='".$top_row["id"]."'";
			$rs2 = mysql_query($sql);
			while ($row = mysql_fetch_array($rs2)){?>
			<TR>
		<?if(!$hasvoted)
			{?><td class="table_data" style="vertical-align:top"><input type="radio" name="vote" id="vote" value="<?=$row["poll_publication_id"]?>"></td><?}?>
		<td class="table_data">
			<?if ($row["publication_ref"] != "-1" || $row["link"] != ""){?>
			<a href="<?=($row["publication_ref"] != "-1")?$return_path."?button=publications#".$row["publication_ref"]:$row["link"]?>" <?=($row["publication_ref"] == "-1")?"target='_blank'":""?> ><?=$row["title"]?></a>
			<?}else { echo $row["title"]; }/*if there is a link*/
			if ($row["description"] != ""){
				?>[&nbsp;<a href="" onclick="expand('<?=$row["poll_publication_id"]?>'); return false;">Show&nbsp;Abstract</a>&nbsp;]<br/>
			<div style="display:none; margin-left:10px;" id="<?=$row["poll_publication_id"]?>_0"><b>Abstract:</b><br/><?=$row["description"]?></div>
			<?
			}
			?>
			
		</TD><td class="table_data" style="vertical-align:top"><?=($top_row["totalVotes"] > 0)?round($row["votes"] / $top_row["totalVotes"] *100):0?>&nbsp;%</td>
		<TD class="table_data"  style="vertical-align:top"><img width="<?=($top_row["totalVotes"] > 0)?round($row["votes"] / $top_row["totalVotes"] *100):0?>px;" src="<?=($row["votes"] == $top_row["maxVotes"])?"redbar":"bluebar"?>.gif" style="height:15px;"/></TD>
		</TR>

			<?}
			if($top_row["isActive"] == "0"){?>
				<TR><td class="table_data" colspan="4" style="text-align:center;">This poll has closed.</td></TR><?
			}else if ($hasvoted){
				?><TR><TD class="table_data" style="text-align:center" colspan="3">You have all ready voted in this poll.</TD></TR><?
			} else {?>
				<TR><td class="table_data" colspan="4" align="right"><input type="submit" name="submit" value="Vote"/></td></TR><?
			}
		?>

<TR><TD><BR></TD></TR>
  </TABLE>
  	</form>
<?
	}
}
//===================================================================
//						     DISPLAY_PROJECTS
//===================================================================
function display_projects($proj_name) {
/*  Displays the different projects. $proj that comes in as a parameter is the button
 *  field in the links table. It should co-incide with the alias field in the projects
 *  table i.e. they should be the same.
 */
 global $sql;
 
 $result_projects = $sql->exec("SELECT * FROM projects WHERE alias='$proj_name'");
 insert_page_header("Projects");

 echo "
 <TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
 	<TR>
		<TD CLASS=\"mission_title\" COLSPAN=\"3\">
			&nbsp;
		</TD>
	</TR>
 	<TR>
		<TD CLASS=\"table_title\" WIDTH=\"100\">
			Project Title
		</TD>
		<TD CLASS=\"table_data\">"
			.$result_projects[0]->title.
		"</TD>
	</TR>
	<TR>
		<TD CLASS=\"table_title\" WIDTH=\"100\">
			Project Members
		</TD>
		<TD CLASS=\"table_data\">"
			.$result_projects[0]->members.
		"</TD>
	</TR>
</TABLE>
<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
	<TR>
		<TD CLASS=\"table_title\" WIDTH=\"100\">
			Abstract
		</TD>
	</TR>
	<TR>
		<TD CLASS=\"table_data\">"
			.$result_projects[0]->abstract.
		"</TD>
	</TR>
</TABLE>
 ";
}

//===================================================================
//						 DISPLAY_PUBLICATIONS
//===================================================================
function display_publications() {
/*  Displays all the publications.
 */
  global $sql, $pub_dir;
	$result_publications = $sql->exec("SELECT publications.*, publications_types.description FROM publications LEFT JOIN publications_types ON publications.type=publications_types.pid ORDER BY date DESC");
  insert_page_header("Publications");

  for ($i = 0; $i < sizeof($result_publications); $i++) {
  echo	"<a name='".$result_publications[$i]->pid."'/>
  <TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
	<TR>
		<TD CLASS=\"publication_title\" COLSPAN=\"3\">";
		
		if($result_publications[$i]->fileName != NULL) {
			echo "
			<A HREF=\"".$pub_dir.$result_publications[$i]->fileName."\">
				download
			</A>";
		} else {
			echo "&nbsp;";
		}
  echo "
		</TD>
	</TR>
	<TR>
		<TD CLASS=\"table_data\"><B>"
			.$result_publications[$i]->title.	
		"</B></TD>
	</TR>
	<TR>
		<TD CLASS=\"table_data\">"
			.$result_publications[$i]->author."<BR /> ".date("Y",strtotime($result_publications[$i]->date))." - ".$result_publications[$i]->description.
		"</TD>
	</TR>";
	
	if ($result_publications[$i]->reference != "") {
  echo	"<TR>
		<TD CLASS=\"table_data\"><I>"
			.$result_publications[$i]->reference.
		"</I></TD>
	</TR>";
	}
if(strlen($result_publications[$i]->abstract) > 0) {
  echo  "
  	<TR>
		<TD CLASS=\"table_title\">
			<A HREF=\"Javascript:expand('".$result_publications[$i]->pid."')\">
				(show/hide) Abstract
			</A>
		</TD>
	</TR>
  	<TR>
  		<TD CLASS=\"table_data\" ID=\"".$result_publications[$i]->pid."_0\" STYLE=\"display:none;\">"
			.$result_publications[$i]->abstract.
		"</TD>
  	</TR>
  </TABLE>
  <BR/>";
  }else {
	echo "</TABLE>\n</BR>\n";
  }
}
}

//===================================================================
//					       DISPLAY_COLLABORATIONS	 
//===================================================================
function display_collaborations() {

insert_page_header("Collaborations");

}

//===================================================================
//					 DISPLAY_ORGANIZATIONAL_CHART
//===================================================================
function display_organizational_chart() {
/*  displays the organizational chart.
 */
global $files_dir;
$filename = "interactions.pdf";

insert_page_header("Organizational Chart");

echo	"
  <TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
	<TR>
		<TD CLASS=\"table_title\">
			&nbsp;
		</TD>
		<TD CLASS=\"table_data\">
			<A HREF=\"$files_dir$filename\">download</A>
		</TD>
	</TR>
  </TABLE>";
}

//===================================================================
//						   INSERT_PAGE_HEADER
//===================================================================
function insert_page_header($heading) {
/*  Displays the page header on top of the page.
 */ 
 
echo "
	<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
		<TR>
			<TD CLASS=\"table_pgheader\">
				$heading
			</TD>
		</TR>
	</TABLE>
	<BR/>
";
}


//===================================================================
//						 DISPLAY_NEWS
//===================================================================
function display_news() {
/*  Displays all the news.
 */
  global $sql, $pub_dir;
  $result_news = $sql->exec("SELECT * FROM news ORDER BY date DESC");
  insert_page_header("News");

  for ($i = 0; $i < sizeof($result_news); $i++) {
  echo	"<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
	<TR>
		<TD CLASS=\"news_title\" COLSPAN=\"3\">".$result_news[$i]->date."&nbsp;&nbsp;&nbsp;".$result_news[$i]->title."</TD>
	</TR>
	<TR>
		<TD CLASS=\"table_data\">"
			.$result_news[$i]->message.	
		"</TD>
	</TR><TR><TD><BR></TD></TR>
  </TABLE>
  ";
  }
}
function display_project_proposals(){
  
	$sql = "SELECT * FROM project_proposals WHERE active=1 ORDER BY subject_code desc";
	
	$rs = mysql_query($sql);
	
	echo "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"650\">\n";
	echo "	<tbody>\n";
	echo "		<tr><td class=\"table_pgheader\">Project Proposals</td></tr>\n";
	echo "	</tbody>\n";
	echo "</table>\n";
	
	while($row = mysql_fetch_array($rs)){
		echo "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"650\">\n";
		echo "	<tbody>\n";
		echo "		<tr><td class=\"table_title\" style=\"text-align:center\">".$row["subject_code"]."</td></tr>\n";
		echo "	</tbody>\n";
		echo "</table>\n";
		
		echo "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"650\">\n";
		echo "	<tbody>\n";
		echo "		<tr>\n";
		echo "			<td class=\"table_title\">Research Question</td>\n";
		echo "			<td class=\"table_data\" width=\"100%\">".$row["question"]."</td>\n";
		echo "		</tr>\n";
		
		echo "		<tr>\n";
		echo "			<td class=\"table_title\">Abstract</td>\n";
		echo "			<td class=\"table_data\">".$row["abstract"]."</td>\n";
		echo "		</tr>\n";
				
		echo "		<tr>\n";
		echo "			<td class=\"table_title\">Outcomes</td>\n";
		echo "			<td class=\"table_data\">\n";
		echo $row["outcomes"];
		echo "			</td>\n";
		echo "		</tr>\n";
	
		echo "		<tr>\n";
		echo "			<td class=\"table_title\">Skills required</td>\n";
		echo "			<td class=\"table_data\">".$row["skills"]."</td>\n";
		echo "		</tr>\n";
		echo "	</tbody>\n";
		echo "</table><BR/>\n";
	}

}


function display_associate_members($int_ext) {
/*  Receives the button name as a parameter. Gets the link Name from the links database
 *  to display on top of the page. Gets the key from the links database and uses it to
 *  search the members database.
 */
global $sql, $img_dir;
	
	$result_title = $sql->exec("SELECT * FROM links WHERE button='$int_ext'");
	
	$int_ext = $result_title[0]->keyField;
	$result_members = $sql->exec("SELECT * FROM members WHERE intExtMember='$int_ext' ORDER BY mid ASC");
	insert_page_header($result_title[0]->linkName);

	if (sizeof($result_members) != 0) {
		for ($i = 0; $i < sizeof($result_members); $i++) {
			echo "		<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"0\" WIDTH=\"650\">
			<TR>
				<TD CLASS=\"mission_title\" STYLE=\"text-align: left\" width=\"100%\">".$result_members[$i]->title."  ".$result_members[$i]->firstName." ".$result_members[$i]->lastName."
				</TD>
				<TD CLASS=\"mission_title\" STYLE=\"text-align: right\" align=\"right\" WIDTH=\"1px\">
			";
			if(strlen($result_members[$i]->abstract) > 0) {
				echo "<A HREF=\"Javascript:expand('".$result_members[$i]->mid."')\">(show/hide)&nbsp;Abstract</A>
				</TD>
			</TR>
			<TR>
				<TD CLASS=\"table_data\" ID=\"".$result_members[$i]->mid."_0\" COLSPAN=\"2\" STYLE=\"colspan:2;display:;\">
					".$result_members[$i]->abstract."
				</TD>
			";
			} else {
				echo "</TD>";
			}
			echo "</TR>
			</TABLE>
			</BR>
			";
		}
	}
}

//===================================================================
//								 MAIN
//===================================================================
/*  Entry point.
 *  Determines which button is pressed and calls the appropriate
 *  function.
 */
global $sql;
require_once("sql_conn.php");
$btn = $_GET['button'];

echo "
	<HTML>
		<HEAD>
			<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"js/main.js\">
			</SCRIPT>
			<LINK HREF=\"css/main.css\" REL=\"stylesheet\" TYPE=\"text/css\">
		</HEAD>
		<BODY style=\" background-image: url('./images/web_bg.gif')\" >";

	SWITCH ($btn) {
		CASE "home"		: display_home_page();break;
		
		CASE "supervisors"	: display_group_members($btn);break;
		CASE "internal_members"	: display_group_members($btn);break;
		CASE "external_members"	: display_associate_members($btn);break;
		
		CASE "alumni"	: display_group_members($btn);break;
		
		CASE "spare_time"	: display_projects($btn);break;
		CASE "new_taxonomy"	: display_projects($btn);break;
		CASE "poll": display_polls();break;
		CASE "publications"	: display_publications();break;
		CASE "project_proposals" : display_project_proposals();break;
		CASE "collaborations"	: display_collaborations();break;
		CASE "chart"		: display_organizational_chart();break;
		CASE "news"	: display_news();break;
		
		default			: display_home_page();break;	
	}

echo "		</BODY>
	</HTML>
";


?>
