| <?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; |
| } |
| } |
| if ($user->participation_confirmed) { |
| $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(); |
| $persons = array(); |
| while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| $persons[$row['COLUMN_NAME']] = $row['COLUMN_NAME']; |
| } |
| return $persons; |
| } 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 AND participation_confirmed"; |
| 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); |
| } |
| } |
| |
| function get_persons($filter) |
| { |
| $sql = "SELECT *, NULL as invoice FROM person " . ($filter? "WHERE " . $filter : ''); |
| |
| try { |
| $stmt = $this->pdo->prepare($sql); |
| $stmt->execute(); |
| $persons = array(); |
| while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| $user = new User(); |
| $user->init_from_array($row); |
| $persons[] = $user; |
| } |
| return $persons; |
| } catch (PDOException $pe) { |
| trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR); |
| } |
| } |
| } |