59 lines
1.7 KiB
PHP
59 lines
1.7 KiB
PHP
<?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),
|
|
]);
|
|
}
|
|
|
|
/** Store the derived template context (field values, slots, pricing) for a submission. */
|
|
public function set_computed(int $id, array $computed): void {
|
|
Db::update($this->table, ['computed' => json_encode($computed)], ['form_submit_id' => $id]);
|
|
}
|
|
|
|
/**
|
|
* The derived template context of the most recent submission for a form,
|
|
* or null when the form has no submission carrying computed data.
|
|
*
|
|
* @return array<string,mixed>|null
|
|
*/
|
|
public function latest_computed(int $form_id): ?array {
|
|
$value = Db::get_var(
|
|
"SELECT computed FROM {$this->table}
|
|
WHERE form_id = %d AND computed IS NOT NULL
|
|
ORDER BY form_submit_id DESC LIMIT 1",
|
|
[$form_id]
|
|
);
|
|
return $value === null ? null : json_decode($value, true);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|