/**
  * Set this as awarded
  * 
  * Unsets any others and then sets itself
  * 
  */
 public function setAwarded()
 {
     //loading resources
     $listing =& $this->getOneToOne('byrdlist_listings');
     $awarded =& $listing->getAwarded();
     $owner = eFactory::getUser($listing->author_id());
     $author = eFactory::getUser($this->author_id());
     if ($awarded && !$awarded->isNew()) {
         $awarded->awarded = 0;
         $awarded->store();
     }
     $this->awarded = 1;
     $this->store();
     require_once EBOOK_HELPERS . DS . 'mailer.php';
     if ($listing->type() == 'donation') {
         BMailer::donationAwarded_Owner($owner, $listing, $author);
         BMailer::donationAwarded_Requester($author, $listing);
     } else {
         BMailer::needAwarded_Owner($owner, $listing, $author);
         BMailer::needAwarded_Requester($author, $listing);
     }
     return true;
 }
 /**
  * Finalize this expired listing
  * 
  * 1. Send out an email
  * 2. Set as expired
  * 
  */
 public function finalizeListing()
 {
     //reasons to fail
     if (!$this->isClosed()) {
         return false;
     }
     // if the clock has NOT timed out
     if ($this->expired()) {
         return false;
     }
     // if its recorded in the database ALREADY
     //loading resources
     $author =& eFactory::getUser($this->author_id);
     $winner =& $this->getWinner();
     require_once EBOOK_HELPERS . DS . 'mailer.php';
     switch ($this->type()) {
         case 'auction':
             if ($this->isEmpty()) {
                 BMailer::listingExpiredEmpty($author, $this);
             } else {
                 BMailer::listingExpiredWinner($author, $this);
                 BMailer::listingWinner($winner, $this);
             }
             break;
         case 'donation':
             if ($this->isEmpty()) {
                 BMailer::donationExpiredEmpty($author, $this);
             } else {
                 BMailer::donationExpiredWinner($author, $this);
             }
             break;
         case 'need':
             if ($this->isEmpty()) {
                 BMailer::needExpiredEmpty($author, $this);
             } else {
                 BMailer::needExpiredWinner($author, $this);
             }
             break;
         case 'financial':
             if ($this->isEmpty()) {
                 BMailer::financialExpiredEmpty($author, $this);
             } else {
                 BMailer::financialExpiredWinner($author, $this);
             }
             break;
     }
     $this->expired = 1;
     $this->store();
     return true;
 }
 /**
  * Set the Payment to be Published
  * 
  * Other responsibilities come with this transaction.
  * This is only set to published if a properly validated paypal amount comes back.
  */
 public function setPublished($num = null)
 {
     //reasons to fail
     if (is_null($num)) {
         return false;
     }
     if ($num < 1) {
         return false;
     }
     //loading resources
     require_once EBOOK_HELPERS . DS . 'mailer.php';
     $listings =& $this->getOneToMany('byrdlist_listings');
     //reasons to fail
     if (!$listings) {
         return false;
     }
     if (empty($listings)) {
         return false;
     }
     foreach ($listings as $listing) {
         //reasons to fail
         if ($listing->type() != 'auction') {
             continue;
         }
         //loading resources
         $payments = $listing->getOneToMany('byrdlist_payments');
         //reasons to fail
         if (empty($payments)) {
             break;
         }
         //initializing variables
         $total = $this->amount();
         foreach ($payments as $id => $payment) {
             if (!$payment->published()) {
                 continue;
             }
             $total = $total + $payment->amount();
         }
         //loading resources
         $lastbid =& $listing->last_bid();
         $bidauthor =& eFactory::getUser($lastbid->author_id());
         $author =& eFactory::getUser($listing->author_id());
         //more reasons to fail
         if ($total < $lastbid->amount()) {
             break;
         }
         BMailer::auctionPaidInFull($bidauthor, $listing);
         BMailer::notifyListingOwnerAuctionPaidInFull($author, $listing);
     }
     $this->published = 1;
     $this->store();
     return true;
 }