/**
  * Assign Service
  *
  * Create a HostingServicePurchaseDBO and add it to the database
  */
 function assign_service()
 {
     // If this HostingService requires a unique IP, make sure the user selected one
     if ($this->post['service']->getUniqueIP() == "Required" && !isset($this->post['ipaddress'])) {
         throw new FieldMissingException("ipaddress");
     }
     // If this HostingService requires a domain, make sure the user selected one
     if ($this->post['service']->isDomainRequired() && !isset($this->post['domainname'])) {
         throw new FieldMissingException("domainname");
     }
     // Create new HostingServicePurchase DBO
     $serverID = isset($this->post['server']) ? $this->post['server']->getID() : null;
     $purchase_dbo = new HostingServicePurchaseDBO();
     $purchase_dbo->setAccountID($this->get['account']->getID());
     $purchase_dbo->setPurchasable($this->post['service']);
     $purchase_dbo->setTerm(isset($this->post['term']) ? $this->post['term']->getTermLength() : null);
     $purchase_dbo->setServerID($serverID);
     $purchase_dbo->setDate(DBConnection::format_datetime($this->post['date']));
     $purchase_dbo->setDomainName($this->post['domainname']);
     $purchase_dbo->setNote($this->post['note']);
     // Save purchase
     add_HostingServicePurchaseDBO($purchase_dbo);
     // If an IP address was selected, assign that IP address to this purchase
     if (isset($this->post['ipaddress'])) {
         if ($this->post['ipaddress']->getServerID() != $serverID) {
             // Roll-back
             delete_HostingServicePurchaseDBO($purchase_dbo);
             throw new SWUserException("[IP_MISMATCH]");
         }
         // Update IP Address record
         $this->post['ipaddress']->setPurchaseID($purchase_dbo->getID());
         try {
             update_IPAddressDBO($this->post['ipaddress']);
         } catch (DBException $e) {
             // Roll-back
             delete_HostingServicePurchaseDBO($purchase_dbo);
             throw new SWUserException("[DB_IP_UPDATE_FAILED]");
         }
     }
     // Success
     $this->setMessage(array("type" => "[HOSTING_ASSIGNED]"));
     $this->gotoPage("accounts_view_account", null, "action=services&account=" . $this->get['account']->getID());
 }
/**
 * Delete HostingServicePurchaseDBO from database
 *
 * @param HostingServicePurchaseDBO &$dbo HostingServicePurchaseDBO to delete
 * @return True on success
 */
function delete_HostingServicePurchaseDBO(&$dbo)
{
    $DB = DBConnection::getDBConnection();
    // Release any IP Addresses assigned to this purchase
    try {
        $ip_dbo_array = load_array_IPAddressDBO("purchaseid = " . $dbo->getID());
        foreach ($ip_dbo_array as $ip_dbo) {
            // Remove IP address from this purchase
            $ip_dbo->setPurchaseID(0);
            update_IPAddressDBO($ip_dbo);
        }
    } catch (DBNoRowsFoundException $e) {
    }
    // Build DELETE query
    $sql = $DB->build_delete_sql("hostingservicepurchase", "id = " . $dbo->getID());
    // Run query
    if (!mysql_query($sql, $DB->handle())) {
        throw new DBException(mysql_error($DB->handle()));
    }
}