class product{ /* *********************************************** Class to store product information *********************************************** */ #---------------------------------------------- # VARs #---------------------------------------------- var $id; # database primary key var $name; var $base_price; #---------------------------------------------- # Constructor #---------------------------------------------- function product($id) { $this->id = $id; $this->name = $this->DBProductGet("name",$this->id); $this->base_price = $this->DBProductGet("base_price",$this->id); } #---------------------------------------------- # Getters #---------------------------------------------- function GetId(){ return $this->id; } function GetName(){ return $this->name; } function GetBasePrice(){ return $this->base_price; } #---------------------------------------------- # Database methods #---------------------------------------------- function DBProductGet($field){ global $g_DB; return $g_DB->GetOne("SELECT $field FROM product WHERE id=\"$this->id\""); } } class order{ /* *********************************************** Class to store order form information *********************************************** */ #---------------------------------------------- # VARs #---------------------------------------------- var $id; # database primary key var $session_id; var $first_name; var $last_name; var $job_title; var $telephone; var $email; var $company; var $address1; var $address2; var $address3; var $town; var $state; var $postcode; var $country; var $no_of_users; var $product_id; var $data_storage; var $email_sent; var $order_now; var $total_price; #---------------------------------------------- # Constructor #---------------------------------------------- function order(){ # ... use the session id to get an order from the database # $this->session_id = session_id(); $this->getOrCreate(); $this->first_name = $this->DBOrderGet("first_name",$this->id); $this->last_name = $this->DBOrderGet("last_name",$this->id); $this->job_title = $this->DBOrderGet("job_title",$this->id); $this->telephone = $this->DBOrderGet("telephone",$this->id); $this->email = $this->DBOrderGet("email",$this->id); $this->company = $this->DBOrderGet("company",$this->id); $this->address1 = $this->DBOrderGet("address1",$this->id); $this->address2 = $this->DBOrderGet("address2",$this->id); $this->address3 = $this->DBOrderGet("address3",$this->id); $this->town = $this->DBOrderGet("town",$this->id); $this->state = $this->DBOrderGet("state",$this->id); $this->postcode = $this->DBOrderGet("postcode",$this->id); $this->country = $this->DBOrderGet("country",$this->id); $this->no_of_users = $this->DBOrderGet("no_of_users",$this->id); $this->product_id = $this->DBOrderGet("product_id",$this->id); $this->data_storage = $this->DBOrderGet("data_storage",$this->id); $this->email_sent = $this->DBOrderGet("email_sent",$this->id); $this->order_now = $this->DBOrderGet("order_now",$this->id); $this->total_price = $this->DBOrderGet("total_price",$this->id); } #---------------------------------------------- # Getters #---------------------------------------------- function GetId(){ return $this->id; } function GetSessionId(){ return $this->session_id; } function GetFirstName(){ if (isset($_POST['first_name'])) { $first_name = $_POST['first_name']; if ($this->first_name != $first_name) { # update database and order object $this->SetFirstName($first_name); } # } elseif ($_POST['first_name'] == "" && $this->first_name) { # $this->SetFirstName(""); } return $this->first_name; } function GetLastName(){ if (isset($_POST['last_name'])) { $last_name = $_POST['last_name']; if ($this->last_name != $last_name) { # update database and order object $this->SetLastName($last_name); } } return $this->last_name; } function GetFullName(){ return $this->first_name." ".$this->last_name; } function GetJobTitle(){ if (isset($_POST['job_title'])) { $job_title = $_POST['job_title']; if ($this->job_title != $job_title) { # update database and order object $this->SetJobTitle($job_title); } } return $this->job_title; } function GetTelephone(){ if (isset($_POST['telephone'])) { $telephone = $_POST['telephone']; if ($this->telephone != $telephone) { # update database and order object $this->SetTelephone($telephone); } } return $this->telephone; } function GetEmail(){ if (isset($_POST['email'])) { $email = $_POST['email']; if ($this->email != $email) { # update database and order object $this->SetEmail($email); } } return $this->email; } function GetCompany(){ if (isset($_POST['company'])) { $company = $_POST['company']; if ($this->company != $company) { # update database and order object $this->SetCompany($company); } } return $this->company; } function GetAddress1(){ if (isset($_POST['address1'])) { $address1 = $_POST['address1']; if ($this->address1 != $address1) { # update database and order object $this->SetAddress1($address1); } } return $this->address1; } function GetAddress2(){ if (isset($_POST['address2'])) { $address2 = $_POST['address2']; if ($this->address2 != $address2) { # update database and order object $this->SetAddress2($address2); } } return $this->address2; } function GetAddress3(){ if (isset($_POST['address3'])) { $address3 = $_POST['address3']; if ($this->address3 != $address3) { # update database and order object $this->SetAddress3($address3); } } return $this->address3; } function GetAddress(){ $address = $this->address1; if ($this->address2) $address .= "\n ".$this->address2; if ($this->address3) $address .= "\n ".$this->address3; if ($this->town) $address .= "\n ".$this->town; if ($this->state) $address .= "\n ".$this->state; if ($this->postcode) $address .= "\n ".$this->postcode; if ($this->country) $address .= "\n ".$this->country; return $address; } function GetTown(){ if (isset($_POST['town'])) { $town = $_POST['town']; if ($this->town != $town) { # update database and order object $this->SetTown($town); } } return $this->town; } function GetState(){ if (isset($_POST['state'])) { $state = $_POST['state']; if ($this->state != $state) { # update database and order object $this->SetState($state); } } return $this->state; } function GetPostcode(){ if (isset($_POST['postcode'])) { $postcode = $_POST['postcode']; if ($this->postcode != $postcode) { # update database and order object $this->SetPostcode($postcode); } } return $this->postcode; } function GetCountry(){ if (isset($_POST['country'])) { $country = $_POST['country']; if ($this->country != $country) { # update database and order object $this->SetCountry($country); } } return $this->country; } function GetNoOfUsers(){ if (isset($_POST['no_of_users'])) { $no_of_users = $_POST['no_of_users']; if (! (is_numeric($no_of_users) && intval($no_of_users))) $no_of_users = 1; } elseif ($this->no_of_users > 1) { $no_of_users = $this->no_of_users; } else { # set default $no_of_users = 1; } if ($this->no_of_users != $no_of_users) { # update database and order object $this->SetNoOfUsers($no_of_users); } return $this->no_of_users; } function GetProductId(){ $default_product_id = 5; if (isset($_POST['product_id'])) { $product_id = $_POST['product_id']; if (! (is_numeric($product_id) && intval($product_id))) $product_id = $default_product_id; } elseif ($this->product_id) { $product_id = $this->product_id; } else { # set default $product_id = $default_product_id; } if ($this->product_id != $product_id) { # update database and order object $this->SetProductId($product_id); } return $this->product_id; } function GetProduct() { return $oProduct = new product($this->product_id); } #function GetProductName() { # if (! $oProduct) $oProduct = new product($this->product_id); # return $product_name = $oProduct->GetName(); # } function GetDataStorage(){ if (isset($_POST['data_storage'])) { $data_storage = $_POST['data_storage']; if (! is_numeric($data_storage)) $data_storage = 1; } elseif ($this->data_storage) { $data_storage = $this->data_storage; } else { # set default $data_storage = 1; } if ($this->data_storage != $data_storage) { # update database and order object $this->SetDataStorage($data_storage); } return $this->data_storage; } function GetEmailSent(){ if (isset($_POST['email_sent'])) { $email_sent = $_POST['email_sent']; if ($this->email_sent != $email_sent) { # update database and order object $this->SetEmailSent($email_sent); } } return $this->email_sent; } function GetOrderNow(){ if (isset($_POST['order_now'])) { $order_now = $_POST['order_now']; if ($order_now == "yes") $order_now = 1; if ($order_now == "no") $order_now = 0; if ($this->order_now != $order_now) { # update database and order object $this->SetOrderNow($order_now); } } return $this->order_now; } function GetTotalPrice(){ $oProduct = new product($this->product_id); $product_base_price = $oProduct->GetBasePrice(); $safeword_token = 5; $data_storage_price = 5; $no_of_users = $this->no_of_users; $data_storage = $this->data_storage; $total_price = $no_of_users * ($product_base_price + $safeword_token); $total_price += ($data_storage - $no_of_users) * $data_storage_price; if ($this->total_price != $total_price) { # update database and order object $this->SetTotalPrice($total_price); } return $this->total_price; } #---------------------------------------------- # Setters #---------------------------------------------- function SetSessionId($var){ $this->session_id = $var; $this->DBOrderSet("session_id",$var); } function SetFirstName($var){ $this->first_name = $var; $this->DBOrderSet("first_name",$var); } function SetLastName($var){ $this->last_name = $var; $this->DBOrderSet("last_name",$var); } function SetJobTitle($var){ $this->job_title = $var; $this->DBOrderSet("job_title",$var); } function SetTelephone($var){ $this->telephone = $var; $this->DBOrderSet("telephone",$var); } function SetEmail($var){ $this->email = $var; $this->DBOrderSet("email",$var); } function SetCompany($var){ $this->company = $var; $this->DBOrderSet("company",$var); } function SetAddress1($var){ $this->address1 = $var; $this->DBOrderSet("address1",$var); } function SetAddress2($var){ $this->address2 = $var; $this->DBOrderSet("address2",$var); } function SetAddress3($var){ $this->address3 = $var; $this->DBOrderSet("address3",$var); } function SetTown($var){ $this->town = $var; $this->DBOrderSet("town",$var); } function SetState($var){ $this->state = $var; $this->DBOrderSet("state",$var); } function SetPostcode($var){ $this->postcode = $var; $this->DBOrderSet("postcode",$var); } function SetCountry($var){ $this->country = $var; $this->DBOrderSet("country",$var); } function SetNoOfUsers($var){ $this->no_of_users = $var; $this->DBOrderSet("no_of_users",$var); } function SetProductId($var){ $this->product_id = $var; $this->DBOrderSet("product_id",$var); } function SetDataStorage($var){ $this->data_storage = $var; $this->DBOrderSet("data_storage",$var); } function SetEmailSent($var){ $this->email_sent = $var; $this->DBOrderSet("email_sent",$var); } function SetOrderNow($var){ $this->order_now = $var; $this->DBOrderSet("order_now",$var); } function SetTotalPrice($var){ $this->total_price = $var; $this->DBOrderSet("total_price",$var); } #---------------------------------------------- # Database methods #---------------------------------------------- function getOrCreate(){ # get the current order for this php session # from the database # or insert a new order into the database # for this session # if ($this->DBgetOrderForSession()) { # check if order already exists return 1; } else { # create a new order global $g_DB; $SQL = "INSERT INTO order_forms (session_id) VALUES (\"$this->session_id\") "; if ($g_DB->Execute($SQL)){ $this->id = $g_DB->Insert_ID(); return 1; } else { echo "
Error:Could not insert new order: $SQL, ordernow_classes.php
"; return 0; } } } function DBOrderGet($field){ global $g_DB; return $g_DB->GetOne("SELECT $field FROM order_forms WHERE id=\"$this->id\""); } function DBOrderSet($field,$value){ global $g_DB; $sql = "UPDATE order_forms SET $field=\"$value\" WHERE id=\"$this->id\""; #debug echo $sql; return $g_DB->GetOne($sql); } function DBgetOrderForSession(){ # return database id if this is an existing record # if (!$this->session_id) return 0; global $g_DB; $SQL = "SELECT id FROM order_forms WHERE session_id=\"$this->session_id\""; return $this->id = $g_DB->GetOne($SQL); } } ?>