science-ation/admin/award_download.php

265 lines
8.7 KiB
PHP

<?
/*
This file is part of the 'Science Fair In A Box' project
SFIAB Website: http://www.sfiab.ca
Copyright (C) 2007 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_once('../common.inc.php');
require_once('../user.inc.php');
user_auth_required('committee', 'admin');
require_once('curl.inc.php');
send_header("Download Awards",
array('Committee Main' => 'committee_main.php',
'Administration' => 'admin/index.php',
'Awards Main' => 'admin/awards.php' ));
function check_source($source)
{
global $config;
$q=mysql_query("SELECT * FROM fairs WHERE id='$source'");
$fair=mysql_fetch_assoc($q);
if(!($fair['username'] && $fair['password'])) {
echo error(i18n("Username and Password are not set for source '%1'. Please set them in the SFIAB Configuration/External Award Sources editor first",array($r->name)));
return;
}
echo i18n("Checking %1 for awards...",array($fair['name']));
echo "<br />";
if($fair['type'] == 'ysf') {
$req=array("awardrequest"=>array(
"username"=>$r->username,
"password"=>$r->password,
"year"=>$config['FAIRYEAR'],
)
);
} else {
$req['getawards'] = array('year'=>$config['FAIRYEAR']);
}
$data = curl_query($fair, $req);
if($data['error'] != 0) {
echo error("Server said: {$data['message']}<br />");
send_footer();
exit;
}
echo notice(i18n('Server said: Success'));
// echo "sending [".nl2br(htmlspecialchars($xmldata))."]";
$keys=array_keys($data);
if(!array_key_exists('awards', $data)) {
echo error(i18n("Invalid XML response. Expecting '%1' in '%2'",array("awards",join(',',array_keys($data)))));
// echo "response=".print_r($datastream);
return;
}
//get a list of all the existing awards for this external source
$aq=mysql_query("SELECT * FROM award_awards WHERE award_source_fairs_id='$source' AND year='{$config['FAIRYEAR']}'");
$existingawards=array();
while($ar=mysql_fetch_object($aq)) {
$existingawards[$ar->id] = true;
}
echo "<i>";
$ar=$response['awardresponse'][0];
$awards = $data['awards'];
$postback = $data['postback'];
echo i18n("Postback URL: %1",array($postback))." <br />";
$numawards=is_array($awards) ? count($awards) : 0;
echo i18n("Number of Awards: %1",array($numawards))." <br />";
if($numawards == 0) {
echo i18n('No awards to process').'</i> <br />';
return;
}
foreach($awards as $award) {
$identifier=$award['identifier'];
$year=$award['year'];
echo i18n("Award Identifier: %1",array($identifier))." &nbsp; ";
echo i18n("Award Year: %1",array($year))."<br />";
echo i18n("Award Name: %1",array($award['name_en']))."<br />";
if($year != $config['FAIRYEAR']) {
echo error(i18n("Award is not for the current fair year... skipping"));
echo '<br />';
continue;
}
$tq=mysql_query("SELECT * FROM award_awards WHERE
external_identifier='$identifier' AND
award_source_fairs_id='$source' AND
year='$year'");
if(mysql_num_rows($tq) == 0) {
/* Award doesn't exist, create it, then update it with the common code below */
mysql_query("INSERT INTO award_awards (award_types_id,
year, external_identifier,
award_source_fairs_id)
VALUES (2,'{$year}',
'".mysql_escape_string($identifier)."',
'$source')");
$award_id=mysql_insert_id();
} else {
echo i18n("Award already exists, updating info")."<br />";
$awardrecord=mysql_fetch_object($tq);
$award_id = $awardrecord->id;
}
//remove it from the existingawards list
unset($existingawards[$award_id]);
//check if the sponsor exists, if not, add them
$sponsor_str = mysql_escape_string($award['sponsor']);
$sponsorq=mysql_query("SELECT * FROM sponsors WHERE organization='$sponsor_str'");
if($sponsorr=mysql_fetch_object($sponsorq)) {
$sponsor_id=$sponsorr->id;
} else {
mysql_query("INSERT INTO sponsors (organization,year,notes,confirmed)
VALUES ('$sponsor_str','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')");
$sponsor_id=mysql_insert_id();
}
mysql_query("UPDATE award_awards SET
sponsors_id='$sponsor_id',
name='".mysql_escape_string($award['name_en'])."',
criteria='".mysql_escape_string($award['criteria_en'])."',
external_postback='".mysql_escape_string($postback)."'
WHERE
id='$award_id'
AND external_identifier='".mysql_escape_string($identifier)."'
AND year='$year'
");
echo mysql_error();
//update the prizes
$prizes = $award['prizes'];
if(is_array($prizes) && count($prizes) > 0) {
echo i18n("Number of prizes: %1",array(count($prizes)))."<br />";
$pq=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='$award_id'");
//get a list of all the existing prizes
$existingprizes=array();
while($pr=mysql_fetch_object($pq)) {
$existingprizes[$pr->external_identifier]=$pr;
}
foreach($prizes AS $prize) {
//if it doesn't exist, add it
if(!array_key_exists($prize['identifier'],$existingprizes)) {
/* Add a base entry, then update it below, yes it's two sql queries,
* but it's much shorter code, and means changing things in only
* one spot */
echo "&nbsp;".i18n("Adding prize %1",array($prize['identifier']))."<br />";
mysql_query("INSERT INTO award_prizes (award_awards_id,year,externa_identifier)
VALUES ('$award_id','$year',".mysql_escape_string($prize['identifier'])."')");
} else {
$ep=$existingprizes[$prize['identifier']];
echo "&nbsp;".i18n("Updating prize %1",array($ep->external_identifier))."<br />";
}
if(!array_key_exists($prize['identifier'],$existingprizes)) {
$ep=$existingprizes[$prize['identifier']];
mysql_query("UPDATE award_prizes SET
cash='".intval($prize['cash'])."',
scholarship='".intval($prize['scholarship'])."',
value='".intval($prize['value'])."',
prize='".mysql_escape_string($prize['prize_en'])."',
number='".intval($prize['number'])."',
`order`='".intval($prize['ord'])."'
WHERE
id='$ep->id'");
//remove it from the list
unset($existingprizes[$ep->external_identifier]);
}
//if an entry exists thats not in the xml -> delete it
foreach($existingprizes AS $ep) {
echo "&nbsp;".i18n("Removing prize %1",array($ep->external_identifier))."<br />";
mysql_query("DELETE FROM award_prizes WHERE id='$ep->id'");
}
}
//FIXME: update the translations
}
}
echo "<br />";
//remove any awards that are left in the $existingawards array, they must have been removed from the source
foreach($existingawards AS $aid) {
echo i18n("Removing award id %1 that was removed from external source",array($aid))."<br />";
mysql_query("DELETE FROM award_prizes WHERE award_awards_id='$aid'");
mysql_query("DELETE FROM award_awards WHERE id='$aid'");
}
echo "</i>";
}
if($_GET['action']=="check") {
if(count($_GET['check'])) {
foreach($_GET['check'] AS $checksource) {
check_source(intval($checksource));
echo "<br />";
}
} else {
echo error(i18n("No sources available to check"));
}
} else {
if(!function_exists('curl_init')) {
echo error(i18n("CURL Support Missing"));
echo notice(i18n("Your PHP installation does not support CURL. You will need to have CURL support added by your system administrator before being able to access external award sources"));
$links=false;
} else {
$links=true;
}
$q=mysql_query("SELECT * FROM fairs WHERE enable_awards='yes' ORDER BY name");
echo "<table class=\"tableview\">";
echo "<tr><th>".i18n("Source Name")."</th>";
echo "<th>".i18n("Source Location URL")."</th>";
echo "<th>".i18n("Check")."</th>";
echo "</tr>";
while($r=mysql_fetch_object($q)) {
echo "<tr>";
echo "<td>{$r->name}</td>\n";
echo "<td>{$r->url}</td>";
echo "<td align=\"center\">";
if($links)
echo "<a href=\"award_download.php?action=check&check[]={$r->id}\">".i18n("check")."</a>";
else
echo "n/a";
echo "</td>";
echo "</tr>";
$checkurl.="&check[]={$r->id}";
}
echo "</table>\n";
echo "<br />";
if($links)
echo "<a href=\"award_download.php?action=check$checkurl\">".i18n("Check all sources")."</a>";
}
send_footer();
?>