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 :
========================
* 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 :
=========

View File

@ -1,10 +1,11 @@
<?php
// TODO : Handle users_in
require_once('data/config.php');
require_once('Storage.class.php');
require_once('UsersIn.class.php');
class Invoice extends Storage {
protected $id = 0, $date, $users_in, $guests, $buyer, $amount, $what;
// users_in is an array of user ids
// date is a DateTime object
// buyer is a User object
// guests is an array with same keys as users_in
@ -12,13 +13,13 @@
protected $fields = array(
'id'=>'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']);
}
}

View File

@ -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);
}
}
}