Updated Storage class to handle load with conditions

This commit is contained in:
Phyks 2013-09-05 23:07:40 +02:00
parent 94c46e4ef5
commit 0eba71c1dc
3 changed files with 37 additions and 23 deletions

14
TODO
View File

@ -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 : inc/Invoices.class.php :
======================== ========================
* Storage ? * Storage of users in ?!?
* Modify load() method to handle complex queries (such as WHERE date < DATE_1 AND date > DATE_2)
==========
* Don't cache the username
==========
Manage paybacks : Manage paybacks :
================= =================
* TODO : Payback system * TODO : Payback system (class should be ok)
TODO : TODO :
====== ======
* Add / Edit a bill * Add / Edit a bill
* JSON output
To test : To test :
========= =========

View File

@ -1,10 +1,11 @@
<?php <?php
// TODO : Handle users_in
require_once('data/config.php'); require_once('data/config.php');
require_once('Storage.class.php'); require_once('Storage.class.php');
require_once('UsersIn.class.php');
class Invoice extends Storage { class Invoice extends Storage {
protected $id = 0, $date, $users_in, $guests, $buyer, $amount, $what; protected $id = 0, $date, $users_in, $guests, $buyer, $amount, $what;
// users_in is an array of user ids
// date is a DateTime object // date is a DateTime object
// buyer is a User object // buyer is a User object
// guests is an array with same keys as users_in // guests is an array with same keys as users_in
@ -12,13 +13,13 @@
protected $fields = array( protected $fields = array(
'id'=>'key', 'id'=>'key',
'date'=>'date', 'date'=>'date',
'users_in'=>'string', // TODO
'buyer'=>'int', 'buyer'=>'int',
'amount'=>'float', 'amount'=>'float',
'what'=>'text' 'what'=>'text'
); );
public function __construct() { public function __construct() {
$users_in = new UsersIn();
parent::__construct(); parent::__construct();
} }
@ -32,10 +33,6 @@
return $this->date->format($format); return $this->date->format($format);
} }
public function getUsersIn() {
return $this->users_in;
}
public function getGuests() { public function getGuests() {
return $this->guests; return $this->guests;
} }
@ -64,10 +61,6 @@
$this->date = DateTime::createFromFormat('Y-n-j G:i', $year.'-'.(int) $month.'-'.(int) $day.' '.(int) $hour.':'.$minute); $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) { public function setGuests($guests) {
$this->guests = $guests; $this->guests = $guests;
} }
@ -91,7 +84,6 @@
$this->what = htmlspecialchars($this->what); $this->what = htmlspecialchars($this->what);
$this->amount = (float) $this->amount; $this->amount = (float) $this->amount;
$this->buyer = (int) $this->buyer; $this->buyer = (int) $this->buyer;
$this->users_in = htmlspecialchars($this->users_in);
$this->date = htmlspecialchars($this->date); $this->date = htmlspecialchars($this->date);
return $this; return $this;
@ -108,7 +100,6 @@
$this->setWhat($data['what']); $this->setWhat($data['what']);
$this->setAmount($data['amount']); $this->setAmount($data['amount']);
$this->setBuyer($data['buyer']); $this->setBuyer($data['buyer']);
$this->setUsersIn($data['users_in']);
$this->setDate($data['date']); $this->setDate($data['date']);
} }
} }

View File

@ -116,7 +116,23 @@ class Storage {
foreach($fields as $field=>$value) { foreach($fields as $field=>$value) {
if($i) { $query .= ' WHERE '; $i = false; } else { $query .= ' AND '; } 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)) { if(!empty($fields) && is_array($fields)) {
foreach($fields as $field=>$value) { foreach($fields as $field=>$value) {
if(!is_array($value))
$value = array($value);
if($fields[$field] == 'date') if($fields[$field] == 'date')
$value = $value->format('Y-m-d H:i:s'); $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);
}
} }
} }