Refactor load method
This commit is contained in:
parent
af10ae7445
commit
73b772aa61
3
TODO
3
TODO
@ -1,8 +1,5 @@
|
|||||||
* i18n
|
* i18n
|
||||||
* handle negative amounts
|
* handle negative amounts
|
||||||
* Refactor load method to avoir load_* methods !
|
|
||||||
* Test remember_me
|
|
||||||
* TODO in files
|
|
||||||
|
|
||||||
inc/Invoices.class.php :
|
inc/Invoices.class.php :
|
||||||
========================
|
========================
|
||||||
|
@ -72,44 +72,6 @@
|
|||||||
$this->what = $what;
|
$this->what = $what;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load overload => TODO
|
|
||||||
// =============
|
|
||||||
public function load_invoices($fields = NULL) {
|
|
||||||
$return = array();
|
|
||||||
$invoices = $this->load($fields);
|
|
||||||
|
|
||||||
foreach($invoices as $invoice) {
|
|
||||||
$return[$invoice['id']] = new Invoice();
|
|
||||||
|
|
||||||
$return[$invoice['id']]->setId($invoice['id']);
|
|
||||||
$return[$invoice['id']]->setDate($invoice['date']);
|
|
||||||
$return[$invoice['id']]->setUsersIn($invoice['users_in']);
|
|
||||||
$return[$invoice['id']]->setBuyer($invoice['buyer']);
|
|
||||||
$return[$invoice['id']]->setAmount($invoice['amount']);
|
|
||||||
$return[$invoice['id']]->setWhat($invoice['what']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function load_invoice($fields = NULL) {
|
|
||||||
$fetch = $this->load($fields);
|
|
||||||
|
|
||||||
if(count($fetch) > 0) {
|
|
||||||
$this->setId($fetch[0]['id']);
|
|
||||||
$this->setWhat($fetch[0]['what']);
|
|
||||||
$this->setAmount($fetch[0]['amount']);
|
|
||||||
$this->setBuyer($fetch[0]['buyer']);
|
|
||||||
$this->setUsersIn($fetch[0]['users_in']);
|
|
||||||
$this->setDate($fetch[0]['date']);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Maps htmlspecialchars on the class before display
|
// Maps htmlspecialchars on the class before display
|
||||||
// =================================================
|
// =================================================
|
||||||
public function secureDisplay() {
|
public function secureDisplay() {
|
||||||
@ -122,4 +84,19 @@
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restores object from array
|
||||||
|
// ==========================
|
||||||
|
public function sessionRestore($data, $serialized = false) {
|
||||||
|
if($serialized) {
|
||||||
|
$data = unserialize($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setId($data['id']);
|
||||||
|
$this->setWhat($data['what']);
|
||||||
|
$this->setAmount($data['amount']);
|
||||||
|
$this->setBuyer($data['buyer']);
|
||||||
|
$this->setUsersIn($data['users_in']);
|
||||||
|
$this->setDate($data['date']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ class Storage {
|
|||||||
|
|
||||||
// Load function
|
// Load function
|
||||||
// =============
|
// =============
|
||||||
public function load($fields = NULL) {
|
public function load($fields = NULL, $first_only = false) {
|
||||||
$query = 'SELECT ';
|
$query = 'SELECT ';
|
||||||
$i = false;
|
$i = false;
|
||||||
foreach($this->fields as $field=>$type) {
|
foreach($this->fields as $field=>$type) {
|
||||||
@ -123,7 +123,25 @@ class Storage {
|
|||||||
|
|
||||||
$query->execute();
|
$query->execute();
|
||||||
|
|
||||||
return $query->fetchAll();
|
$results = $query->fetchAll();
|
||||||
|
|
||||||
|
if(count($results) > 0) {
|
||||||
|
$return = array();
|
||||||
|
$class = get_class($this);
|
||||||
|
|
||||||
|
foreach($results as $result) {
|
||||||
|
$return[$result['id']] = new $class();
|
||||||
|
$return[$result['id']]->sessionRestore($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($first_only)
|
||||||
|
return $return[0];
|
||||||
|
else
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Storing function
|
// Storing function
|
||||||
|
@ -103,41 +103,11 @@ class User extends Storage {
|
|||||||
$this->setAdmin($user_data['admin']);
|
$this->setAdmin($user_data['admin']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load overload => TODO
|
|
||||||
// =============
|
|
||||||
public function load_users($fields = NULL) {
|
|
||||||
$return = array();
|
|
||||||
$users = $this->load($fields);
|
|
||||||
|
|
||||||
foreach($users as $user) {
|
|
||||||
$return[$user['id']] = new User();
|
|
||||||
$return[$user['id']]->sessionRestore($user);
|
|
||||||
}
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function load_user($fields = NULL) {
|
|
||||||
$fetch = $this->load($fields);
|
|
||||||
|
|
||||||
if(count($fetch) > 0) {
|
|
||||||
$this->setId($fetch[0]['id']);
|
|
||||||
$this->setLogin($fetch[0]['login']);
|
|
||||||
$this->setDisplayName($fetch[0]['display_name']);
|
|
||||||
$this->setPassword($fetch[0]['password']);
|
|
||||||
$this->setAdmin($fetch[0]['admin']);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check wether a user already exists or not
|
// Check wether a user already exists or not
|
||||||
// (a user = aunique login and display_name)
|
// (a user = aunique login and display_name)
|
||||||
// =========================================
|
// =========================================
|
||||||
public function isUnique() {
|
public function isUnique() {
|
||||||
if(count($this->load_users(array('login'=>$this->login))) == 0 && count($this->load_users(array('display_name'=>$this->display_name)))) {
|
if(count($this->load(array('login'=>$this->login))) == 0 && count($this->load(array('display_name'=>$this->display_name)))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
12
index.php
12
index.php
@ -176,7 +176,7 @@
|
|||||||
if(!empty($_GET['user_id'])) {
|
if(!empty($_GET['user_id'])) {
|
||||||
$user_id = (int) $_GET['user_id'];
|
$user_id = (int) $_GET['user_id'];
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$user->load_user(array('id'=>$user_id));
|
$user = $user->load(array('id'=>$user_id), true);
|
||||||
$tpl->assign('user_data', $user->secureDisplay());
|
$tpl->assign('user_data', $user->secureDisplay());
|
||||||
}
|
}
|
||||||
$tpl->assign('user_id', (!empty($user_id) ? (int) $user_id : -1));
|
$tpl->assign('user_id', (!empty($user_id) ? (int) $user_id : -1));
|
||||||
@ -184,7 +184,7 @@
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$users_list = new User();
|
$users_list = new User();
|
||||||
$users_list = $users_list->load_users();
|
$users_list = $users_list->load();
|
||||||
|
|
||||||
$tpl->assign('users', secureDisplay($users_list));
|
$tpl->assign('users', secureDisplay($users_list));
|
||||||
$tpl->assign('view', 'list_users');
|
$tpl->assign('view', 'list_users');
|
||||||
@ -277,7 +277,7 @@
|
|||||||
case 'edit_invoice':
|
case 'edit_invoice':
|
||||||
if(!empty($_GET['id'])) {
|
if(!empty($_GET['id'])) {
|
||||||
$invoice = new Invoice();
|
$invoice = new Invoice();
|
||||||
$invoice->load_invoice(array('id'=>(int) $_GET['id']));
|
$invoice->load(array('id'=>(int) $_GET['id']), true);
|
||||||
|
|
||||||
$date_day = '';
|
$date_day = '';
|
||||||
$date_month = '';
|
$date_month = '';
|
||||||
@ -326,7 +326,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
$users_list = new User();
|
$users_list = new User();
|
||||||
$users_list = $users_list->load_users();
|
$users_list = $users_list->load();
|
||||||
|
|
||||||
$tpl->assign('days', range(1,31));
|
$tpl->assign('days', range(1,31));
|
||||||
$tpl->assign('months', range(1, 12));
|
$tpl->assign('months', range(1, 12));
|
||||||
@ -358,10 +358,10 @@
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
$users_list = new User();
|
$users_list = new User();
|
||||||
$users_list = $users_list->load_users();
|
$users_list = $users_list->load();
|
||||||
|
|
||||||
$invoices_list = new Invoice();
|
$invoices_list = new Invoice();
|
||||||
$invoices_list = $invoices_list->load_invoices();
|
$invoices_list = $invoices_list->load();
|
||||||
|
|
||||||
$tpl->assign('users', secureDisplay($users_list));
|
$tpl->assign('users', secureDisplay($users_list));
|
||||||
$tpl->assign('invoices', secureDisplay($invoices_list));
|
$tpl->assign('invoices', secureDisplay($invoices_list));
|
||||||
|
Loading…
Reference in New Issue
Block a user