Ejemplo n.º 1
0
 public function buyTrack($trackid, $albumid)
 {
     $userid = $this->session->userdata('userid');
     $result = array("error" => FALSE);
     if (!$userid) {
         $result["error"] = "You must login!";
         echo json_encode($result);
     }
     $user = new User($userid);
     $track = Track::loadTrack($trackid);
     if ($user->getCredit() < $track->getCost()) {
         //The user can't afford the track!
         $result["error"] = "You have insufficient funds to buy the track as it costs &pound;" . sprintf("%01.2f", $track->getCost() / 100) . ", please top up your account";
         echo json_encode($result);
     } else {
         //Buy the rights to the track
         if ($user->aquireRightsToTrack($trackid, $albumid)) {
             //The track was bought successfully
             $result["bought"] = TRUE;
         } else {
             $result["error"] = "Sorry, there was a problem buying the track. Please try again later";
         }
         echo json_encode($result);
     }
 }