- Fix the sql database for hte last commit

- move code around in curl for less indentation
This commit is contained in:
dave 2009-09-08 17:39:57 +00:00
parent 62941df880
commit daf3314c03
4 changed files with 196 additions and 271 deletions

View File

@ -31,29 +31,16 @@
'Administration' => 'admin/index.php',
'Awards Main' => 'admin/awards.php' ));
function array_remove($needle,&$haystack)
{
if(in_array($needle,$haystack))
unset($haystack[array_search($needle,$haystack)]);
}
function check_source($source)
{
}
if($_GET['action']=="check") {
if(count($_GET['check'])) {
foreach($_GET['check'] AS $checksource) {
$checksource=intval($checksource);
$q=mysql_query("SELECT * FROM fairs WHERE id='$checksource'");
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)));
continue;
return;
}
echo i18n("Checking %1 for awards...",array($fair['name']));
echo "<br />";
@ -77,74 +64,98 @@
}
echo notice(i18n('Server said: Success'));
$keys=array_keys($response);
if($keys[0]=="awardresponse") {
$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_sources_id='$checksource' AND year='".$config['FAIRYEAR']."'");
$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;
$existingawards[$ar->id] = true;
}
echo "<i>";
$ar=$response['awardresponse'][0];
$postback=$ar['postback'][0];
$awards = $data['awards'];
$postback = $data['postback'];
echo i18n("Postback URL: %1",array($postback))." <br />";
if($ar['awards'][0]['award'])
$numawards=count($ar['awards'][0]['award']);
else
$numawards=0;
$numawards=is_array($awards) ? count($awards) : 0;
echo i18n("Number of Awards: %1",array($numawards))." <br />";
if($numawards>0) {
foreach($ar['awards'][0]['award'] AS $award) {
$identifier=$award['identifier'][0];
$year=$award['year'][0];
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'][0]))."<br />";
echo i18n("Award Name: %1",array($award['name_en']))."<br />";
if($year==$config['FAIRYEAR']) {
$tq=mysql_query("SELECT * FROM award_awards WHERE external_identifier='$identifier' AND award_sources_id='$checksource' AND year='$year'");
if($awardrecord=mysql_fetch_object($tq)) {
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
array_remove($awardrecord->id,$existingawards);
unset($existingawards[$award_id]);
//check if the sponsor exists, if not, add them
$sponsorq=mysql_query("SELECT * FROM sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."'");
$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 ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')");
} 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'][0])."',
criteria='".mysql_escape_string($award['criteria_en'][0])."',
name='".mysql_escape_string($award['name_en'])."',
criteria='".mysql_escape_string($award['criteria_en'])."',
external_postback='".mysql_escape_string($postback)."'
WHERE
id='$awardrecord->id'
id='$award_id'
AND external_identifier='".mysql_escape_string($identifier)."'
AND year='$year'
");
echo mysql_error();
echo mysql_error();
//update the prizes
if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0))
{
$prizes=$award['prizes'][0]['prize'];
$numprizes=count($prizes);
echo i18n("Number of prizes: %1",array($numprizes))."<br />";
$pq=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='$awardrecord->id'");
$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)) {
@ -152,51 +163,36 @@ echo mysql_error();
}
foreach($prizes AS $prize) {
//if it exists -> update it
if(array_key_exists($prize['identifier'][0],$existingprizes)) {
$ep=$existingprizes[$prize['identifier'][0]];
//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'][0])."',
scholarship='".intval($prize['scholarship'][0])."',
value='".intval($prize['value'][0])."',
prize='".mysql_escape_string($prize['prize_en'][0])."',
number='".intval($prize['number'][0])."',
`order`='".intval($prize['ord'][0])."'
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]);
}
else { //if it doesnt exist -> add it
echo "&nbsp;".i18n("Adding prize %1",array($prize['identifier'][0]))."<br />";
mysql_query("INSERT INTO award_prizes (
award_awards_id,
cash,
scholarship,
value,
prize,
number,
`order`,
`year`,
external_identifier
) VALUES (
'$awardrecord->id',
'".intval($prize['cash'][0])."',
'".intval($prize['scholarship'][0])."',
'".intval($prize['value'][0])."',
'".mysql_escape_string($prize['prize_en'][0])."',
'".intval($prize['number'][0])."',
'".intval($prize['ord'][0])."',
'$year',
'".mysql_escape_string($prize['identifier'][0])."'
)");
}
}
//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 />";
@ -205,115 +201,38 @@ echo mysql_error();
}
//FIXME: update the translations
}
else {
//check if the sponsor exists, if not, add them
$sponsorq=mysql_query("SELECT * FROM sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."'");
if($sponsorr=mysql_fetch_object($sponsorq)) {
$sponsor_id=$sponsorr->id;
}
else
{
mysql_query("INSERT INTO sponsors (organization,year,notes,confirmed) VALUES ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')");
$sponsor_id=mysql_insert_id();
}
echo i18n("Award does not exists, adding...")."<br />";
mysql_query("INSERT INTO award_awards (
award_types_id,
sponsors_id,
name,
criteria,
year,
external_identifier,
external_postback,
award_sources_id
)
VALUES (
2,
$sponsor_id,
'".mysql_escape_string($award['name_en'][0])."',
'".mysql_escape_string($award['criteria_en'][0])."',
'".$year."',
'".mysql_escape_string($identifier)."',
'".mysql_escape_string($postback)."',
'$checksource'
)");
$award_id=mysql_insert_id();
//import the prizes
if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0))
{
$prizes=$award['prizes'][0]['prize'];
$numprizes=count($prizes);
echo i18n("Number of prizes: %1",array($numprizes))."<br />";
foreach($prizes AS $prize) {
mysql_query("INSERT INTO award_prizes (
award_awards_id,
cash,
scholarship,
value,
prize,
number,
`order`,
`year`,
external_identifier
) VALUES (
'$award_id',
'".intval($prize['cash'][0])."',
'".intval($prize['scholarship'][0])."',
'".intval($prize['value'][0])."',
'".mysql_escape_string($prize['prize_en'][0])."',
'".intval($prize['number'][0])."',
'".intval($prize['ord'][0])."',
'$year',
'".mysql_escape_string($prize['identifier'][0])."'
)");
}
}
else
echo error(i18n("No prizes associated with this award"));
//FIXME: import the translations & prize translations
}
}
else
{
echo error(i18n("Award is not for the current fair year... skipping"));
}
echo "<br />";
}
}
echo "</i>";
//remove any awards that are left in the $existingawards array, they must have been removed from the source
//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'");
}
}
else
echo error(i18n("Invalid XML response. Expecting '%1', received '%2'",array("awardresponse",$keys[0])));
// echo "response=".print_r($datastream);
echo "</i>";
}
if($_GET['action']=="check") {
if(count($_GET['check'])) {
foreach($_GET['check'] AS $checksource) {
check_source(intval($checksource));
echo "<br />";
}
}
else
} else {
echo error(i18n("No sources available to check"));
}
else
{
} else {
if(!function_exists('curl_init'))
{
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
} else {
$links=true;
}
$q=mysql_query("SELECT * FROM fairs WHERE enable_awards='yes' ORDER BY name");
echo "<table class=\"tableview\">";

View File

@ -52,6 +52,8 @@
break;
}
echo "<pre>Curl Send: $str</pre>";
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control
@ -65,7 +67,7 @@
$datastream = curl_exec ($ch); /// execute the curl session and return the output to a variable $datastream
curl_close ($ch); /// close the curl session
// echo "<pre>$datastream</pre>";
// echo "<pre>Server Returned: ".urldecode($datastream)."</pre>";
switch($fair['type']) {
case 'sfiab':
@ -78,7 +80,7 @@
$ret['message'] = $datastream;
break;
}
// echo "ret: ";print_r($ret);echo "<br>";
echo "<pre>Server Returned: ";print_r($ret);echo "</pre><br>";
return $ret;
}
?>

View File

@ -1 +1 @@
129
130

4
db/db.update.130.sql Normal file
View File

@ -0,0 +1,4 @@
ALTER TABLE `award_awards` CHANGE `award_sources_id` `award_source_fairs_id` INT( 10 ) UNSIGNED NULL DEFAULT NULL;
ALTER TABLE `fairs` ADD `award_awards_ids` TEXT NOT NULL;