diff --git a/TODO b/TODO index 03d9367..4fe0609 100755 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ inc/Invoices.class.php : ======================== -* Storage of users in ?!? * Add / Edit a bill Manage paybacks : diff --git a/inc/Storage.class.php b/inc/Storage.class.php index e3c87f6..5232d61 100644 --- a/inc/Storage.class.php +++ b/inc/Storage.class.php @@ -41,6 +41,10 @@ class Storage { return $this->db; } + public function getConnection() { + return $this->connection; + } + // Setters // ======= public function setHost($host) { diff --git a/inc/UsersIn.class.php b/inc/UsersIn.class.php index 3a7f911..fb32814 100644 --- a/inc/UsersIn.class.php +++ b/inc/UsersIn.class.php @@ -1,5 +1,4 @@ fields as $field->$type) { + if($i) { $query .= ','; } else { $i = true; } + + $query .= $field; + } + + $query .= ' FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE invoice_id=:invoice_id'; + + $query = $this->getConnection()->prepare($query); + $query->bindParam(':invoice_id', $this->invoice_id); + $query->execute(); + + $results = $query->fetchAll(); + $this->users_list = array(); + foreach($results as $result) { + $this->users_list[(int) $result['user_id']] = (int) $result['guests']; + } + } + + // Override save() method + // ====================== + public function save() { + // TODO : Optimize ? + + $query = 'SELECT COUNT(invoice_id) FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE invoice_id=:invoice_id'; + $query = $this->getConnection()->prepare($query); + $query->bindParam(':invoice_id', $this->invoice_id); + $query->execute(); + + $count = $query->fetchColumn(0); + + if($count != 0) { + // If there are already some records, delete them first + $query = $this->getConnection()->prepare('DELETE FROM '.MYSQL_PREFIX.$this->TABLE_NAME.' WHERE invoice_id=:invoice_id'); + $query->bindParam(':invoice_id', $this->invoice_id); + $query->execute(); + } + + $query = 'INSERT INTO '.MYSQL_PREFIX.$this->TABLE_NAME.'('; + $i = false; + foreach($this->fields as $field=>$type) { + if($i) { $query .= ','; } else { $i = true;} + $query .= $field; + } + + $query .= ') VALUES(:invoice_id, :user_id, :guests)'; + + $query = $this->getConnection()->prepare($query); + + $user = -1; + $guests = -1; + $query->bindParam(':user_id', (int) $user); + $query->bindParam(':guests', (int) $guests); + + foreach($this->users_list as $user=>$guests) { + $query->execute(); + } + } }