Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | include_once "User.php"; |
| 4 | include_once "config.php"; |
| 5 | |
| 6 | use \User as User; |
| 7 | |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 8 | class DB |
Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 9 | { |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 10 | |
| 11 | public $pdo; |
| 12 | public $log; |
| 13 | |
| 14 | function __construct($log) |
| 15 | { |
| 16 | global $DB, $DB_USER, $DB_PASS; |
| 17 | $this->log = $log; |
| 18 | try { |
Marc Kupietz | 49f677c | 2023-03-10 08:29:41 +0100 | [diff] [blame] | 19 | $this->pdo = new PDO($DB, $DB_USER, $DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") ); |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 20 | if($log) |
| 21 | $log->info("Connected to database $DB"); |
| 22 | } catch (PDOException $e) { |
| 23 | echo $e->getMessage(); |
| 24 | if($log) |
| 25 | $log->error($e->getMessage()); |
Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 26 | } |
| 27 | } |
Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 28 | |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 29 | function add_user(User $user) |
| 30 | { |
| 31 | $columns = $this->getColumnNames("person"); |
| 32 | foreach ((array) $user as $key => $value) { |
| 33 | if (array_key_exists($key, (array) $columns)) { |
| 34 | $a[$key] = $value; |
| 35 | } |
Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 36 | } |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 37 | if ($user->participation_confirmed) { |
| 38 | $a["participation_confirmed_at"] = date("Y-m-d H:i:s UTC"); |
| 39 | } |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 40 | $keys = array_keys($a); |
| 41 | $sql = "INSERT INTO person (" . implode(", ", $keys) . ") \n"; |
| 42 | $sql .= "VALUES ( :" . implode(", :", $keys) . ")"; |
| 43 | $q = $this->pdo->prepare($sql); |
| 44 | $i = 1; |
| 45 | foreach ($a as $value) |
| 46 | $q->bindParam($i++, $value); |
| 47 | $this->log->debug($sql); |
Marc Kupietz | 49f677c | 2023-03-10 08:29:41 +0100 | [diff] [blame] | 48 | $res = $q->execute($a); |
| 49 | $user->id = $this->pdo->lastInsertId(); |
| 50 | return $res; |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | function getColumnNames($table) |
| 54 | { |
| 55 | $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table"; |
| 56 | try { |
| 57 | $stmt = $this->pdo->prepare($sql); |
| 58 | $stmt->bindValue(':table', $table, PDO::PARAM_STR); |
| 59 | $stmt->execute(); |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 60 | $persons = array(); |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 61 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 62 | $persons[$row['COLUMN_NAME']] = $row['COLUMN_NAME']; |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 63 | } |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 64 | return $persons; |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 65 | } catch (PDOException $pe) { |
Marc Kupietz | ddec941 | 2023-04-15 14:00:06 +0200 | [diff] [blame] | 66 | $this->log->error("Could perform query `$sql´: " . $pe->getMessage()); |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Marc Kupietz | f97e86f | 2023-03-10 13:54:23 +0100 | [diff] [blame] | 70 | function updateInvoice($user) { |
| 71 | $this->log->debug("updateInvoice"); |
| 72 | $sql = "UPDATE person SET invoice = :invoice WHERE id = :id;"; |
| 73 | $stmt = $this->pdo->prepare($sql); |
| 74 | |
| 75 | $stmt->bindParam(':invoice', $user->invoice, PDO::PARAM_LOB); |
| 76 | $stmt->bindParam(':id', $user->id); |
| 77 | |
| 78 | return $stmt->execute(); |
| 79 | } |
| 80 | |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 81 | function mail_count($email) |
| 82 | { |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 83 | $sql = "SELECT COUNT(*) FROM person WHERE email = :email AND participation_confirmed"; |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 84 | try { |
| 85 | $stmt = $this->pdo->prepare($sql); |
| 86 | $stmt->bindValue(':email', $email, PDO::PARAM_STR); |
| 87 | $stmt->execute(); |
| 88 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 89 | return $row['COUNT(*)']; |
| 90 | } catch (PDOException $pe) { |
Marc Kupietz | ddec941 | 2023-04-15 14:00:06 +0200 | [diff] [blame] | 91 | $this->log->error("Could perform query `$sql´: " . $pe->getMessage()); |
Marc Kupietz | 145f5b9 | 2023-03-09 20:39:31 +0100 | [diff] [blame] | 92 | } |
Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 93 | } |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 94 | |
Marc Kupietz | 9e5df24 | 2023-04-10 18:48:56 +0200 | [diff] [blame] | 95 | function dinner_count() |
| 96 | { |
| 97 | $sql = "SELECT COUNT(*) FROM person WHERE conference_dinner"; |
| 98 | try { |
| 99 | $stmt = $this->pdo->prepare($sql); |
| 100 | $stmt->execute(); |
| 101 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 102 | return $row['COUNT(*)']; |
| 103 | } catch (PDOException $pe) { |
Marc Kupietz | ddec941 | 2023-04-15 14:00:06 +0200 | [diff] [blame] | 104 | $this->log->error("Could perform query `$sql´: " . $pe->getMessage()); |
Marc Kupietz | 9e5df24 | 2023-04-10 18:48:56 +0200 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 108 | function get_persons($filter) |
| 109 | { |
| 110 | $sql = "SELECT *, NULL as invoice FROM person " . ($filter? "WHERE " . $filter : ''); |
| 111 | |
| 112 | try { |
| 113 | $stmt = $this->pdo->prepare($sql); |
| 114 | $stmt->execute(); |
| 115 | $persons = array(); |
| 116 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 117 | $user = new User(); |
| 118 | $user->init_from_array($row); |
| 119 | $persons[] = $user; |
| 120 | } |
| 121 | return $persons; |
| 122 | } catch (PDOException $pe) { |
Marc Kupietz | ddec941 | 2023-04-15 14:00:06 +0200 | [diff] [blame] | 123 | $this->log->error("Could perform query `$sql´: " . $pe->getMessage()); |
Marc Kupietz | 7d728fe | 2023-03-28 17:32:18 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
Marc Kupietz | b5ad935 | 2023-03-09 08:15:35 +0100 | [diff] [blame] | 126 | } |