bouffeatulm/inc/UsersIn.class.php

126 lines
4.1 KiB
PHP
Raw Normal View History

2013-09-07 23:56:31 +02:00
<?php
require_once('data/config.php');
require_once('Storage.class.php');
class UsersIn extends Storage {
protected $invoice_id = 0, $users_list;
//users_list is an array of users_id and number of guest per user
2013-09-08 15:54:44 +02:00
protected $TABLE_NAME = "Users_in_invoices";
2013-09-07 23:56:31 +02:00
protected $fields = array(
'invoice_id'=>'int',
'user_id'=>'int',
'guests'=>'int'
);
public function __construct() {
parent::__construct();
$users_list = array();
}
// Getters
// =======
public function getInvoiceId() {
return $this->invoice_id;
}
public function get() {
return $this->users_list;
}
// Setters
// =======
public function setInvoiceId($id) {
$this->invoice_id = (int) $id;
}
public function set($users_in) {
$this->users_list = $users_in;
}
// Maps htmlspecialchars on the class before display
// =================================================
public function secureDisplay() {
$this->invoice_id = (int) $this->invoice_id;
$temp_array = array();
foreach($this->users_list as $user=>$guests) {
$temp_array[(int) $user] = (int) $guests;
}
$this->users_in = $temp_array;
return $this;
}
2013-09-08 00:41:49 +02:00
// Override load() method
// ======================
public function load() {
$query = 'SELECT ';
$i = false;
foreach($this->fields as $field=>$type) {
2013-09-08 00:41:49 +02:00
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);
2013-09-08 15:54:44 +02:00
$query->bindParam(':invoice_id', $this->invoice_id);
2013-09-08 00:41:49 +02:00
foreach($this->users_list as $user=>$guests) {
2013-09-08 15:54:44 +02:00
$query->bindParam(':user_id', intval($user));
$query->bindParam(':guests', intval($guests));
2013-09-08 00:41:49 +02:00
$query->execute();
}
}
2013-09-08 18:36:59 +02:00
// Override delete() method
// ========================
public function delete() {
$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();
}
2013-09-07 23:56:31 +02:00
}