From 0eba71c1dc43e89e4ddb4572e05b42d2b7160a94 Mon Sep 17 00:00:00 2001 From: Phyks Date: Thu, 5 Sep 2013 23:07:40 +0200 Subject: [PATCH] Updated Storage class to handle load with conditions --- TODO | 14 +++++--------- inc/Invoices.class.php | 15 +++------------ inc/Storage.class.php | 31 +++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index bf637c4..8fb68a2 100755 --- a/TODO +++ b/TODO @@ -1,22 +1,18 @@ -* i18n +* i18n : i18n of errors in index.php and use a different template for each translation +* Don't cache the username +* JSON output inc/Invoices.class.php : ======================== -* Storage ? -* Modify load() method to handle complex queries (such as WHERE date < DATE_1 AND date > DATE_2) - -========== -* Don't cache the username -========== +* Storage of users in ?!? Manage paybacks : ================= -* TODO : Payback system +* TODO : Payback system (class should be ok) TODO : ====== * Add / Edit a bill -* JSON output To test : ========= diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php index 665f902..a2c1661 100644 --- a/inc/Invoices.class.php +++ b/inc/Invoices.class.php @@ -1,10 +1,11 @@ 'key', 'date'=>'date', - 'users_in'=>'string', // TODO 'buyer'=>'int', 'amount'=>'float', 'what'=>'text' ); public function __construct() { + $users_in = new UsersIn(); parent::__construct(); } @@ -32,10 +33,6 @@ return $this->date->format($format); } - public function getUsersIn() { - return $this->users_in; - } - public function getGuests() { return $this->guests; } @@ -64,10 +61,6 @@ $this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute); } - public function setUsersIn($users_in) { - $this->users_in = $users_in; - } - public function setGuests($guests) { $this->guests = $guests; } @@ -91,7 +84,6 @@ $this->what = htmlspecialchars($this->what); $this->amount = (float) $this->amount; $this->buyer = (int) $this->buyer; - $this->users_in = htmlspecialchars($this->users_in); $this->date = htmlspecialchars($this->date); return $this; @@ -108,7 +100,6 @@ $this->setWhat($data['what']); $this->setAmount($data['amount']); $this->setBuyer($data['buyer']); - $this->setUsersIn($data['users_in']); $this->setDate($data['date']); } } diff --git a/inc/Storage.class.php b/inc/Storage.class.php index 1dbd5f4..bd8f193 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -116,7 +116,23 @@ class Storage { foreach($fields as $field=>$value) { if($i) { $query .= ' WHERE '; $i = false; } else { $query .= ' AND '; } - $query .= $field.'=:'.$field; + if(!is_array($value)) { + $value = array($value); + } + + foreach($value as $value_array) { + if($value_array == 'AND' || $value_array = 'OR') { + $query .= ' '.$value_array.' '; + continue; + } + + if(substr($value_array, 0, 1) == "<") + $query .= $field.'<:'.$field; + elseif(substr($value_array, 0, 1) == ">") + $query .= $field.'>:'.$field; + else + $query .= $field.'=:'.$field; + } } } @@ -124,10 +140,21 @@ class Storage { if(!empty($fields) && is_array($fields)) { foreach($fields as $field=>$value) { + if(!is_array($value)) + $value = array($value); + if($fields[$field] == 'date') $value = $value->format('Y-m-d H:i:s'); - $query->bindParam(':'.$field, $value); + foreach($value as $value_array) { + if($value_array == 'AND' || $value_array == 'OR') + continue; + + if(substr($value, 0, 1) == ">" || substr($value, 0, 1) == "<") + $query->bindParam(':'.$field, substr($value, 0, 1); + else + $query->bindParam(':'.$field, $value); + } } }