- Use the prize name, rather than the external_identifier to identify the prize. It only has to be unique within each award, which it will be (all prizes should have a name).

This commit is contained in:
dave 2009-09-14 05:33:49 +00:00
parent 6a3eacaf9a
commit 347d2d5a86

View File

@ -153,32 +153,38 @@
//update the prizes //update the prizes
$prizes = $award['prizes']; $prizes = $award['prizes'];
if(is_array($prizes) && count($prizes) > 0) { if(!is_array($prizes)) {
echo i18n("Number of prizes: %1",array(count($prizes)))."<br />"; continue;
$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;
} }
echo i18n("Number of prizes: %1",array(count($prizes)))."<br />";
/* Get existing prizes */
$pq=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='$award_id'");
$existingprizes=array();
while($pr=mysql_fetch_assoc($pq))
$existingprizes[$pr['prize']]=$pr;
/* Iterate over the downloaded pizes */
foreach($prizes AS $prize) { foreach($prizes AS $prize) {
//if it doesn't exist, add it //if it doesn't exist, add it
if(!array_key_exists($prize['identifier'],$existingprizes)) { if(!array_key_exists($prize['prize_en'],$existingprizes)) {
/* Add a base entry, then update it below, yes it's two sql queries, /* 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 * but it's much shorter code, and means changing things in only
* one spot */ * one spot */
echo "&nbsp;".i18n("Adding prize %1",array($prize['identifier']))."<br />"; echo "&nbsp;".i18n("Adding prize %1",array($prize['prize_en']))."<br />";
mysql_query("INSERT INTO award_prizes (award_awards_id,year,externa_identifier) $p = mysql_escape_string(stripslashes($prize['prize_en']));
VALUES ('$award_id','$year',".mysql_escape_string($prize['identifier'])."')"); mysql_query("INSERT INTO award_prizes (award_awards_id,prize,year,external_identifier)
VALUES ('$award_id','$p','$year','$p')");
$prize_id = mysql_insert_id();
} else { } else {
$ep=$existingprizes[$prize['identifier']]; $ep=$existingprizes[$prize['prize_en']];
echo "&nbsp;".i18n("Updating prize %1",array($ep->external_identifier))."<br />"; echo "&nbsp;".i18n("Updating prize %1",array($ep['prize']))."<br />";
$prize_id = $ep['id'];
//remove it from the list
unset($existingprizes[$ep['prize']]);
} }
if(!array_key_exists($prize['identifier'],$existingprizes)) {
$ep=$existingprizes[$prize['identifier']];
mysql_query("UPDATE award_prizes SET mysql_query("UPDATE award_prizes SET
cash='".intval($prize['cash'])."', cash='".intval($prize['cash'])."',
scholarship='".intval($prize['scholarship'])."', scholarship='".intval($prize['scholarship'])."',
@ -187,20 +193,16 @@
number='".intval($prize['number'])."', number='".intval($prize['number'])."',
`order`='".intval($prize['ord'])."' `order`='".intval($prize['ord'])."'
WHERE WHERE
id='$ep->id'"); id='$prize_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 //FIXME: update the translations
} }
/* Delete local entries that weren't downloaded */
foreach($existingprizes AS $ep) {
echo "&nbsp;".i18n("Removing prize %1",array($ep['prize']))."<br />";
mysql_query("DELETE FROM award_prizes WHERE id='{$ep['id']}'");
}
} }
echo "<br />"; echo "<br />";