diff --git a/admin/award_download.php b/admin/award_download.php index c0f0b6b..9be7fde 100644 --- a/admin/award_download.php +++ b/admin/award_download.php @@ -153,59 +153,61 @@ //update the prizes $prizes = $award['prizes']; - if(is_array($prizes) && count($prizes) > 0) { - echo i18n("Number of prizes: %1",array(count($prizes)))."
"; - $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; + if(!is_array($prizes)) { + continue; + } + + echo i18n("Number of prizes: %1",array(count($prizes)))."
"; + /* 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) { + //if it doesn't exist, add it + if(!array_key_exists($prize['prize_en'],$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 " ".i18n("Adding prize %1",array($prize['prize_en']))."
"; + $p = mysql_escape_string(stripslashes($prize['prize_en'])); + 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 { + $ep=$existingprizes[$prize['prize_en']]; + echo " ".i18n("Updating prize %1",array($ep['prize']))."
"; + $prize_id = $ep['id']; + //remove it from the list + unset($existingprizes[$ep['prize']]); } - 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 " ".i18n("Adding prize %1",array($prize['identifier']))."
"; - 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 " ".i18n("Updating prize %1",array($ep->external_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='$prize_id'"); - 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 " ".i18n("Removing prize %1",array($ep->external_identifier))."
"; - mysql_query("DELETE FROM award_prizes WHERE id='$ep->id'"); - } - } //FIXME: update the translations } + + /* Delete local entries that weren't downloaded */ + foreach($existingprizes AS $ep) { + echo " ".i18n("Removing prize %1",array($ep['prize']))."
"; + mysql_query("DELETE FROM award_prizes WHERE id='{$ep['id']}'"); + } } echo "
"; -//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))."
"; mysql_query("DELETE FROM award_prizes WHERE award_awards_id='$aid'");