<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
   Copyright (C) 2008 James Grant <james@lightbox.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/
?>
<?
 require("../common.inc.php");
 require_once("../user.inc.php");
 user_auth_required('committee', 'admin');

 send_header("Sponsors",
		array('Committee Main' => 'committee_main.php',
			'Administration' => 'admin/index.php',
			'Fundraising Main' => 'admin/fundraising.php')
		);
 
	if($_POST['save']=="edit" || $_POST['save']=="add")
	{
		if($_POST['save']=="add")
		{
			$q=mysql_query("INSERT INTO sponsors (year) VALUES ('".$config['FAIRYEAR']."')");
			$id=mysql_insert_id();
		}
		else
			$id=$_POST['id'];


		$exec="UPDATE sponsors SET ".
			"organization='".mysql_escape_string(stripslashes($_POST['organization']))."', ".
			"address='".mysql_escape_string(stripslashes($_POST['address']))."', ".
			"city='".mysql_escape_string(stripslashes($_POST['city']))."', ".
			"province_code='".mysql_escape_string(stripslashes($_POST['province_code']))."', ".
			"postalcode='".mysql_escape_string(stripslashes($_POST['postalcode']))."', ".
			"phone='".mysql_escape_string(stripslashes($_POST['phone']))."', ".
			"tollfree='".mysql_escape_string(stripslashes($_POST['tollfree']))."', ".
			"fax='".mysql_escape_string(stripslashes($_POST['fax']))."', ".
			"email='".mysql_escape_string(stripslashes($_POST['email']))."', ".
			"website='".mysql_escape_string(stripslashes($_POST['website']))."', ".
			"notes='".mysql_escape_string(stripslashes($_POST['notes']))."', ".
			"donationpolicyurl='".mysql_escape_string(stripslashes($_POST['donationpolicyurl']))."', ".
			"fundingselectiondate='".mysql_escape_string(stripslashes($_POST['fundingselectiondate']))."', ".
			"logo='".mysql_escape_string(stripslashes($_POST['logo']))."', ".
			"waiveraccepted='".mysql_escape_string(stripslashes($_POST['waiveraccepted']))."', ".
			"taxreceiptrequired='".mysql_escape_string(stripslashes($_POST['taxreceiptrequired']))."' ".
			"WHERE id='$id'";
		mysql_query($exec);

		if($_POST['save']=="add")
			echo happy("Sponsor successfully added");
		else
			echo happy("Successfully saved changes to sponsor");
	}

	if($_GET['action']=="delete" && $_GET['delete'])
	{
		//dont allow any deleting until we figure out what we need to do, infact, i think we never should hard delete
		//this should only soft-delete so things like awards from previous years are still all linked correctly.
	//	mysql_query("DELETE FROM sponsors WHERE id='".$_GET['delete']."'");
//		echo happy("Sponsors cannot be deleted");
	}

	if($_GET['action']=="edit" || $_GET['action']=="add")
	{

		echo "<a href=\"sponsors.php\">&lt;&lt; ".i18n("Back to Award Sponsors")."</a>\n";
		if($_GET['action']=="edit")
		{
			echo "<h3>".i18n("Edit Award Sponsor")."</h3>\n";
			$buttontext="Save Sponsor";
			$q=mysql_query("SELECT * FROM sponsors WHERE id='".$_GET['edit']."'");
			$r=mysql_fetch_object($q);
		}
		else if($_GET['action']=="add")
		{
			echo "<h3>".i18n("Add New Award Sponsor")."</h3>\n";
			$buttontext="Add Sponsor";
		}
		$buttontext=i18n($buttontext);

		echo "<form method=\"post\" action=\"sponsors.php\">\n";
		echo "<input type=\"hidden\" name=\"save\" value=\"".$_GET['action']."\">\n";

		if($_GET['action']=="edit")
			echo "<input type=\"hidden\" name=\"id\" value=\"".$_GET['edit']."\">\n";

		echo "<table class=\"tableedit\">\n";
		echo "<tr><th>".i18n("Organization Name")."</th><td><input type=\"text\" id=\"organization\" name=\"organization\" value=\"".htmlspecialchars($r->organization)."\" size=\"60\" maxlength=\"128\" /><script type=\"text/javascript\">translateButton('organization');</script></td></tr>\n";
		echo "<tr><th>".i18n("Address")."</th><td><input type=\"text\" name=\"address\" value=\"".htmlspecialchars($r->address)."\" size=\"60\" maxlength=\"64\" /></td></tr>\n";
		echo "<tr><th>".i18n("City")."</th><td><input type=\"text\" name=\"city\" value=\"".htmlspecialchars($r->city)."\" size=\"32\" maxlength=\"32\" /></td></tr>\n";
		echo "<tr><th>".i18n($config['provincestate'])."</th><td>";
			emit_province_selector("province_code",$r->province_code);
		echo "</td></tr>\n";
		echo "<tr><th>".i18n($config['postalzip'])."</th><td><input type=\"text\" name=\"postalcode\" value=\"$r->postalcode\" size=\"8\" maxlength=\"7\" /></td></tr>\n";
		echo "<tr><th>".i18n("Phone")."</th><td><input type=\"text\" name=\"phone\" value=\"".htmlspecialchars($r->phone)."\" size=\"16\" maxlength=\"32\" /></td></tr>\n";
		echo "<tr><th>".i18n("Toll Free")."</th><td><input type=\"text\" name=\"tollfree\" value=\"".htmlspecialchars($r->tollfree)."\" size=\"16\" maxlength=\"32\" /></td></tr>\n";
		echo "<tr><th>".i18n("Fax")."</th><td><input type=\"text\" name=\"fax\" value=\"".htmlspecialchars($r->fax)."\" size=\"16\" maxlength=\"32\" /></td></tr>\n";
		echo "<tr><th>".i18n("Email")."</th><td><input type=\"text\" name=\"email\" value=\"".htmlspecialchars($r->email)."\" size=\"60\" maxlength=\"128\" /></td></tr>\n";
		echo "<tr><th>".i18n("Website")."</th><td><input type=\"text\" name=\"website\" value=\"".htmlspecialchars($r->website)."\" size=\"60\" maxlength=\"128\" /></td></tr>\n";
		echo "<tr><th>".i18n("Donation Policy URL")."</th><td><input type=\"text\" name=\"donationpolicyurl\" value=\"".htmlspecialchars($r->donationpolicyurl)."\" size=\"60\" maxlength=\"128\" /></td></tr>\n";
		echo "<tr><th>".i18n("Funding Selection Date")."</th><td>";
		emit_date_selector("fundingselectiondate",$r->fundingselectiondate);
		echo "</td></tr>\n";
		echo "<tr><th>".i18n("Logo")."</th><td><input type=\"file\" name=\"logo\" size=\"16\" /></td></tr>\n";
		echo "<tr><th>".i18n("Waiver Accepted")."</th><td>";
		if($r->waiveraccepted=="no") $ch="checked=\"checked\""; else $ch="";
		echo "<input type=\"radio\" $ch name=\"waiveraccepted\" value=\"no\">".i18n("No")." &nbsp;&nbsp;";
		if($r->waiveraccepted=="yes") $ch="checked=\"checked\""; else $ch="";
		echo "<input type=\"radio\" $ch name=\"waiveraccepted\" value=\"yes\">".i18n("Yes")." &nbsp;&nbsp;";
		echo "</td></tr>\n";
		echo "<tr><th>".i18n("Tax Receipt Required")."</th><td>";
		if($r->taxreceiptrequired=="no") $ch="checked=\"checked\""; else $ch="";
		echo "<input type=\"radio\" $ch name=\"taxreceiptrequired\" value=\"no\">".i18n("No")." &nbsp;&nbsp;";
		if($r->taxreceiptrequired=="yes") $ch="checked=\"checked\""; else $ch="";  
		echo "<input type=\"radio\" $ch name=\"taxreceiptrequired\" value=\"yes\">".i18n("Yes")." &nbsp;&nbsp;";
		echo "</td></tr>\n";

		echo "<tr><th>".i18n("Notes")."</th><td><textarea name=\"notes\" rows=\"4\" cols=\"60\">".htmlspecialchars($r->notes)."</textarea></td></tr>\n";
		echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"$buttontext\" /></td></tr>\n";

		echo "</table>\n";
		echo "</form>\n";



	}
	else
	{
		echo "<a href=\"sponsors.php?action=add\">Add New Sponsor</a>\n";
		echo "<br />";
		echo "<table class=\"tableview\">";
		echo "<tr>";
		echo " <th>Organization</th>";
		echo " <th># of Sponsorships</th>";
		echo " <th># of Awards</th>";
		echo " <th># of Contacts</th>";
		echo " <th>Action</th>";
		echo "</tr>\n";

		//$q=mysql_query("SELECT * FROM award_sponsors WHERE year='".$config['FAIRYEAR']."' ORDER BY organization");
		//we want to show all years, infact that year field probably shouldnt even be there.
		$q=mysql_query("SELECT * FROM sponsors ORDER BY organization");
		while($r=mysql_fetch_object($q))
		{
			echo "<tr>\n";
			echo " <td>$r->organization</td>\n";

			$numq=mysql_query("SELECT COUNT(id) AS num FROM award_awards WHERE year='".$config['FAIRYEAR']."' AND sponsors_id='$r->id'");
			$numr=mysql_fetch_object($numq);
			$numawards=$numr->num;

			$numq=mysql_query("SELECT COUNT(id) AS num FROM sponsorships WHERE year='".$config['FAIRYEAR']."' AND sponsors_id='$r->id'");
			$numr=mysql_fetch_object($numq);
			$numsponsorships=$numr->num;

			$numq=mysql_query("SELECT COUNT(users_id) AS num FROM users_sponsor, users WHERE sponsors_id='$r->id' AND users_sponsor.users_id=users.id AND users.year='".$config['FAIRYEAR']."' and users.deleted='no'");
			$numr=mysql_fetch_object($numq);
			$numcontacts=$numr->num;
			

			echo " <td align=\"center\" valign=\"top\">";
			echo "$numsponsorships &nbsp;";
			echo "<a href=\"fundraising.php?sponsors_id=$r->id\"><img alt=\"view\" border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/viewmag.".$config['icon_extension']."\"></a>";
			echo "</td>";

			echo " <td align=\"center\" valign=\"top\">";
			echo "$numawards &nbsp;";
			echo "<a href=\"award_awards.php?sponsors_id=$r->id\"><img alt=\"view\" border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/viewmag.".$config['icon_extension']."\"></a>";
			echo "</td>";

			echo " <td align=\"center\" valign=\"top\">";
			echo "$numcontacts &nbsp;";
			echo "<a href=\"sponsor_contacts.php?sponsors_id=$r->id\"><img alt=\"view\" border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/viewmag.".$config['icon_extension']."\"></a>";
			echo "</td>";
			echo " <td align=\"center\">";
			echo "<a href=\"sponsors.php?action=edit&edit=$r->id\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/edit.".$config['icon_extension']."\"></a>";
			/* cannot delete sponsors, we need to be able to link to them from other years, etc... this could be turned back on as a soft delete eventually
			echo "&nbsp;";
			echo "<a onclick=\"return confirmClick('Are you sure you want to remove this sponsor?')\" href=\"sponsors.php?action=delete&delete=$r->id\"><img border=0 src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a>";
*/

			echo " </td>\n";
			echo "</tr>\n";
		}

		echo "</table>\n";


	}

	send_footer();

?>
