blob: 182a1c7ce829d19d969b2373be6f31150690c6ad [file] [log] [blame]
<?php
include_once "User.php";
include_once "config.php";
use \User as User;
class DB
{
public $pdo;
public $log;
function __construct($log)
{
global $DB, $DB_USER, $DB_PASS;
$this->log = $log;
try {
$this->pdo = new PDO($DB, $DB_USER, $DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
if($log)
$log->info("Connected to database $DB");
} catch (PDOException $e) {
echo $e->getMessage();
if($log)
$log->error($e->getMessage());
}
}
function add_user(User $user)
{
$columns = $this->getColumnNames("person");
foreach ((array) $user as $key => $value) {
if (array_key_exists($key, (array) $columns)) {
$a[$key] = $value;
}
}
$a["participation_confirmed_at"] = date("Y-m-d H:i:s UTC");
$keys = array_keys($a);
$sql = "INSERT INTO person (" . implode(", ", $keys) . ") \n";
$sql .= "VALUES ( :" . implode(", :", $keys) . ")";
$q = $this->pdo->prepare($sql);
$i = 1;
foreach ($a as $value)
$q->bindParam($i++, $value);
$this->log->debug($sql);
$res = $q->execute($a);
$user->id = $this->pdo->lastInsertId();
return $res;
}
function getColumnNames($table)
{
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':table', $table, PDO::PARAM_STR);
$stmt->execute();
$output = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$output[$row['COLUMN_NAME']] = $row['COLUMN_NAME'];
}
return $output;
} catch (PDOException $pe) {
trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR);
}
}
function updateInvoice($user) {
$this->log->debug("updateInvoice");
$sql = "UPDATE person SET invoice = :invoice WHERE id = :id;";
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':invoice', $user->invoice, PDO::PARAM_LOB);
$stmt->bindParam(':id', $user->id);
return $stmt->execute();
}
function mail_count($email)
{
$sql = "SELECT COUNT(*) FROM person WHERE email = :email";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['COUNT(*)'];
} catch (PDOException $pe) {
trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR);
}
}
}