From e090f35b69591c9f6bb833c79b7dd882251f2f03 Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Thu, 3 Feb 2011 14:31:22 +0000 Subject: [PATCH] Inital commit of default page --- index.php | 149 +++++++++++++++++++++++++++++++++++++++++++++++- pages/index.php | 58 +++++++++++++++++++ 2 files changed, 205 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index e8b091d..1bc10e5 100644 --- a/index.php +++ b/index.php @@ -22,6 +22,117 @@ class Template { } +class Config { + + private $data; + + public function __construct($data){ + $this->data = $data; + } + + public static function Load(){ + if(!file_exists('config.php')){ + throw new Exception('Config File Not Found'); + } + $config = require_once('config.php'); + return new Config($config); + } + + public function get($key){ + if(!isset($this->data[$key])){ + throw new Exception('Config key \'' . $key . '\' not found.'); + } + return $this->data[$key]; + } + +} + +class Database extends mysqli { + + public function __construct($config){ + parent::__construct($config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket']); + if($this->connect_error) { + throw new Exception('Database Connection Error: ' . $this->connect_error); + } + } + + public function begin(){ + $this->query('START TRANSACTION'); + } + + public function query($sql){ + $result = parent::query($sql); + + if($result === false){ + throw new Exception('Query Error: ' . $this->error); + } + + return $result; + } + + public function prepare($sql){ + $result = parent::prepare($sql); + + if($result === false){ + throw new Exception('Query Error: ' . $this->error); + } + + return $result; + + } + + public static function Load(){ + return new Database(Ticketing::Get()->config()->get('database')); + } + +} + +class Order { + + private $id; + private $user; + private $amount; + private $status; + + // STATUS: 0 - IN PROGRESS, 1 - CONFIRMED, 2 - PAID + + public static function Get_Confirmed_By_User($user){ + $query = 'SELECT `id`, `user`, `time`, `status` FROM `order` WHERE `user` = ? AND `status` != 0'; + $stmt = Ticketing::Get()->database()->prepare($query); + $stmt->bind_param('s', $user); + $stmt->execute(); + $stmt->bind_result($id, $user, $time, $status); + $orders = array(); + while($stmt->fetch()){ + $orders[$id] = new Order($id, $user, $amount, $status); + } + $stmt->close(); + return $orders; + } + + public static function Get_Unconfirmed_By_User($user){ + $query = 'SELECT `id`, `user`, `time`, `status` FROM `order` WHERE `user` = ? AND `status` = 0'; + $stmt = Ticketing::Get()->database()->prepare($query); + $stmt->bind_param('s', $user); + $stmt->execute(); + $stmt->bind_result($id, $user, $time, $status); + $orders = array(); + while($stmt->fetch()){ + $orders[$id] = new Order($id, $user, $amount, $status); + } + $order = array_shift($orders); + foreach($orders as $o){ + $order->merge($o); + } + return $order; + } + + private function merge(){ + throw new Exception('Unsupported'); + } + +} + class Ticketing { private static $instance; @@ -40,14 +151,48 @@ class Ticketing { if(!isset($this->config)){ $this->config = Config::Load(); } - return $config; + return $this->config; + } + + private $user; + + public function user(){ + if(!isset($this->user)){ + if(!isset($_SERVER['REMOTE_USER'])){ + throw new Exception('User undefined'); + } + $this->user = $_SERVER['REMOTE_USER']; + } + return $this->user; + } + + private $database; + + public function database(){ + if(!isset($this->database)){ + $this->database = Database::Load(); + } + return $this->database; } + private $args; + + public function arg($k){ + if(!isset($this->args)){ + $this->args = explode('/', isset($_GET['page']) ? $_GET['page'] : ''); + } + if(isset($this->args[$k])){ + return $this->args[$k]; + } else { + return ''; + } + } + private $type; public function type(){ if(!isset($this->type)){ - $type = isset($_GET['page']) ? $_GET['page'] : null; + $type = basename($this->arg(0)); if(isset($type) && file_exists('pages/' . $type . '.php') && file_exists('templates/' . $type . '.php')){ $this->type = $type; } elseif(file_exists('pages/index.php') && file_exists('templates/index.php')) { diff --git a/pages/index.php b/pages/index.php index ef6a52c..4e0a886 100644 --- a/pages/index.php +++ b/pages/index.php @@ -2,5 +2,63 @@ class Page_Index extends Page { + public function logic($template){ + + $system = Ticketing::Get(); + + try { + $system->database()->begin(); + + $current_orders = Order::Get_Confirmed_By_User($system->user()); + $template->previous = array(); + $current = null; + + foreach($current_orders as $o){ + $purchases = Purchase::Get_By_Order($o); + + $template->previous[$o->id()] = array( + 'number' => $o->id(), + 'paid' => $o->paid(), + 'url' => $system->url('order',$o->id()) + ); + + foreach($purchases as $p){ + $template->previous[$o->id()]['purchases'][$p->id()] = array( + 'name' => $o->name(), + 'price' => $o->price(), + 'dining' => $o->dining() + ); + } + } + + $current = Order::Get_Unconfirmed_By_User($system->user()); + + $template->new = array(); + + if($current != null){ + + $purchases = Purchase::Get_By_Order($o); + + foreach($purchases as $p){ + $template->new[$p->id()] = array( + 'name' => $o->name(), + 'price' => $o->price(), + 'dining' => $o->dining(), + 'delete' => $system->url('delete', $p->id()) + ); + } + + } + + + $system->database()->commit(); + + } catch (Exception $e){ + $system->database()->rollback(); + throw $e; + } + + } + } -- 2.34.1