diff --git a/TODO b/TODO index 7002505..e24475f 100755 --- a/TODO +++ b/TODO @@ -1,8 +1,5 @@ * i18n * handle negative amounts -* Refactor load method to avoir load_* methods ! -* Test remember_me -* TODO in files inc/Invoices.class.php : ======================== diff --git a/inc/Invoices.class.php b/inc/Invoices.class.php index 8ee596c..2c55d54 100644 --- a/inc/Invoices.class.php +++ b/inc/Invoices.class.php @@ -72,44 +72,6 @@ $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 // ================================================= public function secureDisplay() { @@ -122,4 +84,19 @@ 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']); + } } diff --git a/inc/Storage.class.php b/inc/Storage.class.php index 6dac485..a82e103 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -94,7 +94,7 @@ class Storage { // Load function // ============= - public function load($fields = NULL) { + public function load($fields = NULL, $first_only = false) { $query = 'SELECT '; $i = false; foreach($this->fields as $field=>$type) { @@ -123,7 +123,25 @@ class Storage { $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 diff --git a/inc/User.class.php b/inc/User.class.php index 2cc9cee..361d373 100644 --- a/inc/User.class.php +++ b/inc/User.class.php @@ -103,41 +103,11 @@ class User extends Storage { $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 // (a user = aunique login and display_name) // ========================================= 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; } else { diff --git a/index.php b/index.php index 93be404..d3b2556 100644 --- a/index.php +++ b/index.php @@ -176,7 +176,7 @@ if(!empty($_GET['user_id'])) { $user_id = (int) $_GET['user_id']; $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_id', (!empty($user_id) ? (int) $user_id : -1)); @@ -184,7 +184,7 @@ } else { $users_list = new User(); - $users_list = $users_list->load_users(); + $users_list = $users_list->load(); $tpl->assign('users', secureDisplay($users_list)); $tpl->assign('view', 'list_users'); @@ -277,7 +277,7 @@ case 'edit_invoice': if(!empty($_GET['id'])) { $invoice = new Invoice(); - $invoice->load_invoice(array('id'=>(int) $_GET['id'])); + $invoice->load(array('id'=>(int) $_GET['id']), true); $date_day = ''; $date_month = ''; @@ -326,7 +326,7 @@ } $users_list = new User(); - $users_list = $users_list->load_users(); + $users_list = $users_list->load(); $tpl->assign('days', range(1,31)); $tpl->assign('months', range(1, 12)); @@ -358,10 +358,10 @@ default: $users_list = new User(); - $users_list = $users_list->load_users(); + $users_list = $users_list->load(); $invoices_list = new Invoice(); - $invoices_list = $invoices_list->load_invoices(); + $invoices_list = $invoices_list->load(); $tpl->assign('users', secureDisplay($users_list)); $tpl->assign('invoices', secureDisplay($invoices_list));