131 lines
4.4 KiB
PHP
131 lines
4.4 KiB
PHP
<?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->format('Y-m-d H:i:s'), $start_utc->format('Y-m-d H:i:s')],
|
|
ARRAY_A
|
|
)
|
|
);
|
|
}
|
|
|
|
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
|
|
)
|
|
);
|
|
}
|
|
}
|