115 lines
3.6 KiB
PHP
115 lines
3.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use Reservair\Database\Db;
|
||
|
|
|
||
|
|
class RsvMembershipProgramRepository {
|
||
|
|
private string $table_program;
|
||
|
|
private string $table_key;
|
||
|
|
|
||
|
|
public function __construct() {
|
||
|
|
$this->table_program = Db::prefix() . 'rsv_membership_program';
|
||
|
|
$this->table_key = Db::prefix() . 'rsv_membership_key';
|
||
|
|
}
|
||
|
|
|
||
|
|
public function all(?int $limit = null, int $skip = 0): array {
|
||
|
|
if ($limit === null) {
|
||
|
|
$rows = Db::get_results(
|
||
|
|
"SELECT * FROM {$this->table_program} ORDER BY id DESC",
|
||
|
|
[],
|
||
|
|
ARRAY_A
|
||
|
|
);
|
||
|
|
} else {
|
||
|
|
$rows = Db::get_results(
|
||
|
|
"SELECT * FROM {$this->table_program} ORDER BY id DESC LIMIT %d OFFSET %d",
|
||
|
|
[$limit, $skip],
|
||
|
|
ARRAY_A
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return array_map(fn($row) => RsvMembershipProgram::from_array($row), $rows);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function count_all(): int {
|
||
|
|
return (int) Db::get_var("SELECT COUNT(*) FROM {$this->table_program}");
|
||
|
|
}
|
||
|
|
|
||
|
|
public function get(int $id): ?array {
|
||
|
|
return Db::get_row(
|
||
|
|
"SELECT * FROM {$this->table_program} WHERE id = %d",
|
||
|
|
[$id],
|
||
|
|
ARRAY_A
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add(string $name, bool $active): int {
|
||
|
|
return Db::insert($this->table_program, [
|
||
|
|
'name' => $name,
|
||
|
|
'active' => $active ? 1 : 0,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(int $id, string $name, bool $active): int {
|
||
|
|
return Db::update(
|
||
|
|
$this->table_program,
|
||
|
|
[
|
||
|
|
'name' => $name,
|
||
|
|
'active' => $active ? 1 : 0,
|
||
|
|
],
|
||
|
|
['id' => $id]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete(int $id): void {
|
||
|
|
Db::delete($this->table_program, ['id' => $id]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function keys(int $program_id, ?int $limit = null, int $skip = 0): array {
|
||
|
|
if ($limit === null) {
|
||
|
|
$rows = Db::get_results(
|
||
|
|
"SELECT * FROM {$this->table_key} WHERE program_id = %d ORDER BY id",
|
||
|
|
[$program_id],
|
||
|
|
ARRAY_A
|
||
|
|
);
|
||
|
|
} else {
|
||
|
|
$rows = Db::get_results(
|
||
|
|
"SELECT * FROM {$this->table_key} WHERE program_id = %d ORDER BY id LIMIT %d OFFSET %d",
|
||
|
|
[$program_id, $limit, $skip],
|
||
|
|
ARRAY_A
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return array_map(fn($row) => RsvMembershipKey::from_array($row), $rows);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function count_keys(int $program_id): int {
|
||
|
|
return (int) Db::get_var(
|
||
|
|
"SELECT COUNT(*) FROM {$this->table_key} WHERE program_id = %d",
|
||
|
|
[$program_id]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function normalize_key(string $key_value): string {
|
||
|
|
return preg_replace('/\s+/', ' ', strtolower(trim($key_value)));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_key(int $program_id, string $key_value): int {
|
||
|
|
return Db::insert($this->table_key, [
|
||
|
|
'program_id' => $program_id,
|
||
|
|
'key_value' => $key_value,
|
||
|
|
'key_normalized_value' => $this->normalize_key($key_value)
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete_key(int $key_id): void {
|
||
|
|
Db::delete($this->table_key, ['id' => $key_id]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function key_exists(int $program_id, string $key_value): bool {
|
||
|
|
return Db::get_var(
|
||
|
|
"SELECT 1 FROM {$this->table_key} k
|
||
|
|
INNER JOIN {$this->table_program} p ON p.id = k.program_id
|
||
|
|
WHERE p.active = 1 AND k.program_id = %d AND k.key_normalized_value = %s
|
||
|
|
LIMIT 1",
|
||
|
|
[$program_id, $this->normalize_key($key_value)]
|
||
|
|
) !== null;
|
||
|
|
}
|
||
|
|
}
|