/**
  * Reassigns the reference of this image - callers of this are responsible for ensuring
  * only authorized calls can be made, but the method performs full error checking of 
  * the supplied reference
  */
 function reassignGridsquare($grid_reference, &$error)
 {
     $ok = false;
     //is the reference valid?
     //old one is in $this->grid_square
     $newsq = new GridSquare();
     if (is_object($this->db)) {
         $newsq->_setDB($this->_getDB());
     }
     if ($newsq->setByFullGridRef($grid_reference, false, true, true)) {
         $db =& $this->_getDB();
         //ensure this is a real change
         if ($newsq->gridsquare_id != $this->gridsquare_id) {
             //get sequence number of target square - for a rejected image
             //we use a negative sequence number
             if ($this->moderation_status != 'rejected') {
                 $seq_no = $db->GetOne("select max(seq_no) from gridimage " . "where gridsquare_id={$newsq->gridsquare_id}");
                 $seq_no = max($seq_no + 1, 0);
             } else {
                 $seq_no = $db->GetOne("select min(seq_no) from gridimage " . "where gridsquare_id={$newsq->gridsquare_id}");
                 $seq_no = min($seq_no - 1, -1);
             }
             //was this image ftf?
             if ($this->ftf) {
                 //reset the ftf flag
                 $this->ftf = 0;
                 //need to assign ftf to another image in the square if possible
                 $next_geograph = $db->GetOne("select gridimage_id from gridimage " . "where gridsquare_id={$this->gridsquare_id} and moderation_status='geograph' " . "and gridimage_id<>{$this->gridimage_id} " . "order by gridimage_id");
                 if ($next_geograph) {
                     $db->Query("update gridimage set ftf=1 where gridimage_id={$next_geograph}");
                     $db->Query("update gridimage_search set ftf=1 where gridimage_id={$next_geograph}");
                 }
             }
             //does the image get ftf in the target square?
             if ($this->moderation_status == 'geograph') {
                 $geographs = $db->GetOne("select count(*) from gridimage " . "where gridsquare_id={$newsq->gridsquare_id} and moderation_status='geograph' and ftf = 1");
                 if ($geographs == 0) {
                     $this->ftf = 1;
                 }
             }
             $sql_set = "gridsquare_id='{$newsq->gridsquare_id}'," . "seq_no={$seq_no},ftf={$this->ftf}, ";
         }
         //if not a new square only update nateastings and natnorthings
         //we DONT use getNatEastings here because only want them if it more than 4 figure
         $east = $newsq->nateastings + 0;
         $north = $newsq->natnorthings + 0;
         $ri = $newsq->reference_index;
         //reassign image
         $db->Execute("update gridimage set {$sql_set} " . "nateastings={$east},natnorthings={$north},reference_index={$ri},natgrlen='{$newsq->natgrlen}' " . "where gridimage_id='{$this->gridimage_id}'");
         //ensure this is a real change
         if ($newsq->gridsquare_id != $this->gridsquare_id) {
             //fire an event (some of the stuff that follows
             //might be better as an event handler
             require_once 'geograph/event.class.php';
             new Event(EVENT_MOVEDPHOTO, "{$this->gridimage_id},{$this->grid_square->grid_reference},{$newsq->grid_reference}");
             //update cached data for old square and new square
             $this->grid_square->updateCounts();
             $newsq->updateCounts();
             //invalidate any cached maps
             //handled by the event above
             //update placename cached column
             //handled by the event above
             //updated cached tables
             //this isnt needed as reassignGridsquare is only called before commitChanges
             //$this->updateCachedTables();
             //updateCachedTables needs to know the new gridref for the lat/long calc!
             $this->newsq =& $newsq;
         }
         $ok = true;
     } else {
         //bad grid reference
         $ok = false;
         $error = $newsq->errormsg;
     }
     return $ok;
 }