initial
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
use Reservair\Logger\Logger;
|
||||
|
||||
class RsvFormDefinitionRepository {
|
||||
private string $table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_form_definition';
|
||||
}
|
||||
|
||||
public function add(string $name, array $definition): int {
|
||||
return Db::insert($this->table, [
|
||||
'name' => $name,
|
||||
'definition' => json_encode($definition),
|
||||
]);
|
||||
}
|
||||
|
||||
public function get_all(?int $limit = null, int $skip = 0): array {
|
||||
if ($limit === null) {
|
||||
return Db::get_results(
|
||||
"SELECT form_id, name FROM {$this->table} ORDER BY form_id ASC",
|
||||
[],
|
||||
ARRAY_A
|
||||
);
|
||||
}
|
||||
return Db::get_results(
|
||||
"SELECT form_id, name FROM {$this->table} ORDER BY form_id ASC LIMIT %d OFFSET %d",
|
||||
[$limit, $skip],
|
||||
ARRAY_A
|
||||
);
|
||||
}
|
||||
|
||||
public function count_all(): int {
|
||||
return (int) Db::get_var("SELECT COUNT(*) FROM {$this->table}");
|
||||
}
|
||||
|
||||
public function update(int $id, string $name, array $definition): void {
|
||||
Db::update(
|
||||
$this->table,
|
||||
['name' => $name, 'definition' => json_encode($definition)],
|
||||
['form_id' => $id]
|
||||
);
|
||||
}
|
||||
|
||||
public function delete(int $id): void {
|
||||
Db::delete($this->table, ['form_id' => $id]);
|
||||
}
|
||||
|
||||
public function get(int $id): ?array {
|
||||
$row = Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE form_id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row['definition'] = json_decode($row['definition'], true);
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
|
||||
class RsvFormSubmitRepository {
|
||||
private string $table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_form_submit';
|
||||
}
|
||||
|
||||
public function add(int $form_id, array $values): int {
|
||||
return Db::insert($this->table, [
|
||||
'form_id' => $form_id,
|
||||
'values' => json_encode($values),
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(int $id): void {
|
||||
Db::delete($this->table, ['form_submit_id' => $id]);
|
||||
}
|
||||
|
||||
public function get(int $id): ?array {
|
||||
$row = Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE form_submit_id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row['values'] = json_decode($row['values'], true);
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
|
||||
class RsvReservationRepository {
|
||||
private string $table;
|
||||
private string $timetable_reservations_table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_reservation';
|
||||
$this->timetable_reservations_table = Db::prefix() . 'rsv_timetable_reservation';
|
||||
}
|
||||
|
||||
public function get_all(?int $limit = null, int $skip = 0) {
|
||||
if ($limit === null) {
|
||||
return Db::get_results("SELECT * FROM {$this->table} ORDER BY id DESC");
|
||||
}
|
||||
return Db::get_results(
|
||||
"SELECT * FROM {$this->table} ORDER BY id DESC LIMIT %d OFFSET %d",
|
||||
[$limit, $skip]
|
||||
);
|
||||
}
|
||||
|
||||
public function count_all(): int {
|
||||
return (int) Db::get_var("SELECT COUNT(*) FROM {$this->table}");
|
||||
}
|
||||
|
||||
public function get(int $id) {
|
||||
return Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE id = %d",
|
||||
[$id]
|
||||
);
|
||||
}
|
||||
|
||||
public function get_detail(int $id): ?array {
|
||||
$reservation = Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if ($reservation === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$form_submit_table = Db::prefix() . 'rsv_form_submit';
|
||||
$form_submit = Db::get_row(
|
||||
"SELECT `values` FROM {$form_submit_table} WHERE form_submit_id = %d",
|
||||
[(int) $reservation['form_submit_id']],
|
||||
ARRAY_A
|
||||
);
|
||||
$reservation['form_values'] = $form_submit ? json_decode($form_submit['values'], true) : null;
|
||||
|
||||
$reservation['timetable_reservations'] = Db::get_results(
|
||||
"SELECT id, timetable_id, `start_utc`, `end_utc` FROM {$this->timetable_reservations_table} WHERE reservation_id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$confirmation_table = Db::prefix() . 'rsv_timetable_reservation_confirmation';
|
||||
$reservation['pending_confirmation'] = (int) Db::get_var(
|
||||
"SELECT COUNT(*) FROM {$confirmation_table} c
|
||||
JOIN {$this->timetable_reservations_table} tr ON tr.id = c.timetable_reservation_id
|
||||
WHERE tr.reservation_id = %d",
|
||||
[$id]
|
||||
) > 0;
|
||||
|
||||
return $reservation;
|
||||
}
|
||||
|
||||
public function insert(array $data): int {
|
||||
return Db::insert($this->table, $data);
|
||||
}
|
||||
|
||||
public function delete(int $id): void {
|
||||
Db::delete($this->table, ['id' => $id]);
|
||||
}
|
||||
|
||||
public function count_subitems(int $reservation_id): int {
|
||||
return (int) Db::get_var(
|
||||
"SELECT COUNT(*) FROM {$this->timetable_reservations_table} WHERE reservation_id = %d",
|
||||
[$reservation_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function are_subitems_confirmed(int $reservation_id): bool {
|
||||
$result = Db::get_row(
|
||||
"SELECT MIN(rtr.is_confirmed) AS is_confirmed
|
||||
FROM {$this->table} AS rr
|
||||
LEFT JOIN {$this->timetable_reservations_table} AS rtr ON rr.id = rtr.reservation_id
|
||||
WHERE rr.id = %d",
|
||||
[$reservation_id]
|
||||
);
|
||||
return $result !== null && $result->is_confirmed == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the reservation has reached a terminal state. `is_confirmed` is
|
||||
* NULL while pending, 1 once confirmed and 0 once refused, so any non-NULL
|
||||
* value means the outcome is settled.
|
||||
*/
|
||||
public function is_resolved(int $reservation_id): bool {
|
||||
$result = Db::get_row(
|
||||
"SELECT is_confirmed FROM {$this->table} WHERE id = %d",
|
||||
[$reservation_id]
|
||||
);
|
||||
return $result !== null && $result->is_confirmed !== null && $result->is_confirmed;
|
||||
}
|
||||
|
||||
public function set_resolved_state(int $reservation_id, bool $confirmed): void {
|
||||
Db::update(
|
||||
$this->table,
|
||||
['is_confirmed' => $confirmed ? 1 : 0],
|
||||
['id' => $reservation_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function get_form_submit_id(int $reservation_id): ?int {
|
||||
$result = Db::get_var(
|
||||
"SELECT form_submit_id FROM {$this->table} WHERE id = %d",
|
||||
[$reservation_id]
|
||||
);
|
||||
return $result !== null ? (int) $result : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
|
||||
class RsvTimetableCapacityRepository {
|
||||
private string $table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_timetable_capacity';
|
||||
}
|
||||
|
||||
public function get_all($timetable_id, ?int $limit = null, int $skip = 0): array {
|
||||
if ($limit === null) {
|
||||
return Db::get_results(
|
||||
"SELECT * FROM {$this->table} WHERE timetable_id = %d ORDER BY id",
|
||||
[$timetable_id]
|
||||
);
|
||||
}
|
||||
return Db::get_results(
|
||||
"SELECT * FROM {$this->table} WHERE timetable_id = %d ORDER BY id LIMIT %d OFFSET %d",
|
||||
[$timetable_id, $limit, $skip]
|
||||
);
|
||||
}
|
||||
|
||||
public function count_all($timetable_id): int {
|
||||
return (int) Db::get_var(
|
||||
"SELECT COUNT(*) FROM {$this->table} WHERE timetable_id = %d",
|
||||
[$timetable_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function get(int $id): ?RsvTimetableCapacity {
|
||||
$row = Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
return $row === null ? null : RsvTimetableCapacity::from_array($row);
|
||||
}
|
||||
|
||||
public function create(RsvTimetableCapacity $capacity): int {
|
||||
return Db::insert($this->table, $capacity->to_array());
|
||||
}
|
||||
|
||||
public function delete(int $id): void {
|
||||
Db::delete($this->table, ['id' => $id]);
|
||||
}
|
||||
|
||||
public function update(int $id, RsvTimetableCapacity $capacity): int {
|
||||
$capacity->id = $id;
|
||||
return Db::update($this->table, $capacity->to_array(), ['id' => $id]);
|
||||
}
|
||||
|
||||
public function get_overlapping_capacity(int $timetable_id, DateTime $start, DateTime $end): array {
|
||||
$start_str = (clone $start)->setTimezone(wp_timezone())->format('Y-m-d H:i:s');
|
||||
$end_str = (clone $end)->setTimezone(wp_timezone())->format('Y-m-d H:i:s');
|
||||
|
||||
return Db::get_results(
|
||||
"SELECT * FROM {$this->table}
|
||||
WHERE timetable_id = %d
|
||||
AND (
|
||||
date = DATE(%s)
|
||||
OR (
|
||||
repeat_period_in_days > 0
|
||||
AND DATEDIFF(DATE(%s), date) >= 0
|
||||
AND MOD(DATEDIFF(DATE(%s), date), repeat_period_in_days) = 0
|
||||
)
|
||||
)
|
||||
AND DATE_ADD(DATE(%s), INTERVAL start_time MINUTE) < %s
|
||||
AND DATE_ADD(DATE(%s), INTERVAL end_time MINUTE) > %s
|
||||
ORDER BY start_time",
|
||||
[$timetable_id, $start_str, $start_str, $start_str, $start_str, $end_str, $start_str, $start_str]
|
||||
);
|
||||
}
|
||||
|
||||
public function get_capacities_for_date(int $timetable_id, DateTime $date) : array {
|
||||
$row = Db::get_results(
|
||||
"SELECT *
|
||||
FROM {$this->table}
|
||||
WHERE timetable_id = %d
|
||||
AND (date = DATE(%s) OR (repeat_period_in_days > 0 AND MOD(DATEDIFF(date, %s), repeat_period_in_days) = 0))
|
||||
ORDER BY start_time ASC",
|
||||
[$timetable_id, $date->format('Y-m-d'), $date->format('Y-m-d')],
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
return array_map(fn($x) => RsvTimetableCapacity::from_array($x), $row);
|
||||
}
|
||||
|
||||
public function get_available_range_for_date(int $timetable_id, DateTime $date) {
|
||||
if ($date === null) {
|
||||
throw new InvalidArgumentException('Invalid date');
|
||||
}
|
||||
|
||||
return Db::get_row(
|
||||
"SELECT MAX(start_time) AS `from`, MIN(end_time) AS `to`
|
||||
FROM {$this->table}
|
||||
WHERE timetable_id = %d
|
||||
AND (date = DATE(%s) OR (repeat_period_in_days > 0 AND MOD(DATEDIFF(date, %s), repeat_period_in_days) = 0))",
|
||||
[$timetable_id, $date->format('Y-m-d'), $date->format('Y-m-d')]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
|
||||
class RsvTimetableRepository {
|
||||
private string $table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_timetable';
|
||||
}
|
||||
|
||||
public function get_all(?int $limit = null, int $skip = 0): array {
|
||||
if ($limit === null) {
|
||||
return Db::get_results("SELECT * FROM {$this->table} ORDER BY id");
|
||||
}
|
||||
return Db::get_results(
|
||||
"SELECT * FROM {$this->table} ORDER BY id LIMIT %d OFFSET %d",
|
||||
[$limit, $skip]
|
||||
);
|
||||
}
|
||||
|
||||
public function count_all(): int {
|
||||
return (int) Db::get_var("SELECT COUNT(*) FROM {$this->table}");
|
||||
}
|
||||
|
||||
public function get(int $id): ?RsvTimetable {
|
||||
$row = Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
);
|
||||
if ($row === null) {
|
||||
return null;
|
||||
}
|
||||
return new RsvTimetable($row);
|
||||
}
|
||||
|
||||
public function create(RsvTimetable $timetable): int {
|
||||
return Db::insert($this->table, [
|
||||
'name' => $timetable->name,
|
||||
'block_size' => $timetable->block_size,
|
||||
'maintainer_email' => $timetable->maintainer_email,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(int $id, RsvTimetable $timetable): int {
|
||||
return Db::update(
|
||||
$this->table,
|
||||
[
|
||||
'name' => $timetable->name,
|
||||
'block_size' => $timetable->block_size,
|
||||
'maintainer_email' => $timetable->maintainer_email,
|
||||
],
|
||||
['id' => $id]
|
||||
);
|
||||
}
|
||||
|
||||
public function get_all_maintainer_emails(): array {
|
||||
return Db::get_col(
|
||||
"SELECT DISTINCT maintainer_email FROM {$this->table}
|
||||
WHERE maintainer_email IS NOT NULL AND maintainer_email != ''"
|
||||
);
|
||||
}
|
||||
|
||||
public function get_maintainer_email(int $id): ?string {
|
||||
return Db::get_var(
|
||||
"SELECT maintainer_email FROM {$this->table} WHERE id = %d",
|
||||
[$id]
|
||||
) ?: null;
|
||||
}
|
||||
|
||||
public function set_google_calendar_id(int $id, ?string $calendar_id): void {
|
||||
Db::update(
|
||||
$this->table,
|
||||
['google_calendar_id' => $calendar_id],
|
||||
['id' => $id]
|
||||
);
|
||||
}
|
||||
|
||||
public function delete(int $id): int {
|
||||
return Db::delete($this->table, ['id' => $id]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
use Reservair\Database\Db;
|
||||
|
||||
class RsvTimetableReservationRepository {
|
||||
private string $table;
|
||||
private string $confirmation_table;
|
||||
|
||||
public function __construct() {
|
||||
$this->table = Db::prefix() . 'rsv_timetable_reservation';
|
||||
$this->confirmation_table = Db::prefix() . 'rsv_timetable_reservation_confirmation';
|
||||
}
|
||||
|
||||
public function get_all(): array {
|
||||
return array_map(
|
||||
'RsvTimetableReservation::from_array',
|
||||
Db::get_results("SELECT * FROM {$this->table}")
|
||||
);
|
||||
}
|
||||
|
||||
public function get(int $id): RsvTimetableReservation {
|
||||
return RsvTimetableReservation::from_array(Db::get_row(
|
||||
"SELECT * FROM {$this->table} WHERE id = %d",
|
||||
[$id],
|
||||
ARRAY_A
|
||||
));
|
||||
}
|
||||
|
||||
public function get_overlapping(int $timetable_id, DateTime $start_utc, DateTime $end_utc): array {
|
||||
return array_map(
|
||||
'RsvTimetableReservation::from_array',
|
||||
Db::get_results(
|
||||
"SELECT * FROM {$this->table}
|
||||
WHERE timetable_id = %d
|
||||
AND `start_utc` < %s
|
||||
AND `end_utc` > %s",
|
||||
[$timetable_id, $end_utc, $start_utc]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function insert(array $data): int {
|
||||
return Db::insert($this->table, $data);
|
||||
}
|
||||
|
||||
public function insert_confirmation(int $reservation_id, int $timetable_reservation_id, string $code): void {
|
||||
Db::insert($this->confirmation_table, [
|
||||
'reservation_id' => $reservation_id,
|
||||
'timetable_reservation_id' => $timetable_reservation_id,
|
||||
'code' => $code,
|
||||
]);
|
||||
}
|
||||
|
||||
public function get_confirmation(string $code): ?array {
|
||||
return Db::get_row(
|
||||
"SELECT c.*
|
||||
FROM {$this->confirmation_table} c
|
||||
JOIN {$this->table} tr ON tr.id = c.timetable_reservation_id
|
||||
WHERE c.code = %s
|
||||
LIMIT 1",
|
||||
[$code],
|
||||
ARRAY_A
|
||||
);
|
||||
}
|
||||
|
||||
public function set_confirmed(int $timetable_reservation_id, bool $state): void {
|
||||
Db::update(
|
||||
$this->table,
|
||||
['is_confirmed' => $state ? 1 : 0],
|
||||
['id' => $timetable_reservation_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function delete_confirmation(string $code): void {
|
||||
Db::delete($this->confirmation_table, ['code' => $code]);
|
||||
}
|
||||
|
||||
public function has_pending_confirmation(int $reservation_id): bool {
|
||||
return (int) Db::get_var(
|
||||
"SELECT COUNT(*) FROM {$this->confirmation_table} c
|
||||
JOIN {$this->table} tr ON tr.id = c.timetable_reservation_id
|
||||
WHERE tr.reservation_id = %d",
|
||||
[$reservation_id]
|
||||
) > 0;
|
||||
}
|
||||
|
||||
public function get_confirmation_code(int $reservation_id): ?string {
|
||||
return Db::get_var(
|
||||
"SELECT c.code FROM {$this->confirmation_table} c
|
||||
JOIN {$this->table} tr ON tr.id = c.timetable_reservation_id
|
||||
WHERE tr.reservation_id = %d
|
||||
LIMIT 1",
|
||||
[$reservation_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function get_by_timetable(int $timetable_id, ?int $limit = null, int $skip = 0): array {
|
||||
$sql = "SELECT tr.*, c.timetable_reservation_id AS pending_confirmation_id
|
||||
FROM {$this->table} tr
|
||||
LEFT JOIN {$this->confirmation_table} c ON c.timetable_reservation_id = tr.id
|
||||
WHERE tr.timetable_id = %d
|
||||
ORDER BY tr.start_utc DESC";
|
||||
|
||||
if ($limit === null) {
|
||||
return Db::get_results($sql, [$timetable_id], ARRAY_A);
|
||||
}
|
||||
return Db::get_results($sql . " LIMIT %d OFFSET %d", [$timetable_id, $limit, $skip], ARRAY_A);
|
||||
}
|
||||
|
||||
public function count_by_timetable(int $timetable_id): int {
|
||||
return (int) Db::get_var(
|
||||
"SELECT COUNT(*) FROM {$this->table} WHERE timetable_id = %d",
|
||||
[$timetable_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function get_reservations_on_date(int $timetable_id, DateTime $date_utc): array {
|
||||
return array_map(
|
||||
'RsvTimetableReservation::from_array',
|
||||
Db::get_results(
|
||||
"SELECT * FROM {$this->table}
|
||||
WHERE timetable_id = %d AND DATE(`start_utc`) = DATE(%s)
|
||||
ORDER BY `start_utc`",
|
||||
[$timetable_id, $date_utc->format('Y-m-d')],
|
||||
ARRAY_A
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user