Inital commit of default page
authorRichard Whitehouse <github@richardwhiuk.com>
Thu, 3 Feb 2011 14:31:22 +0000 (14:31 +0000)
committerRichard Whitehouse <github@richardwhiuk.com>
Thu, 3 Feb 2011 14:31:22 +0000 (14:31 +0000)
index.php
pages/index.php

index e8b091dde98395376c2bd4d0bfce7f4a8b4755b2..1bc10e5bd5d4d22b13dc723f25c1eb1ea1d19b82 100644 (file)
--- 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')) {
index ef6a52c6dd300bdd4f61eb14114db3b8967f16d9..4e0a8865a8bc11d729b631c293c5cfa4110604d9 100644 (file)
@@ -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;
+               }
+
+       }
+
 
 }