226 lines
12 KiB
PHP
226 lines
12 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/db.php';
|
|
require_once __DIR__ . '/../includes/utils.php';
|
|
require_once __DIR__ . '/../includes/persona.php';
|
|
require_once __DIR__ . '/../includes/grocery_helpers.php';
|
|
|
|
$canManage = count($people) === 0
|
|
|| (
|
|
$activePerson !== null
|
|
&& ($activePerson['role'] ?? '') === ROLE_HEAD
|
|
&& isHohVerified()
|
|
);
|
|
|
|
$permanenceOptions = [
|
|
'permanent' => 'Permanent',
|
|
'weekly' => 'Weekly',
|
|
'biweekly' => 'Bi-weekly',
|
|
'monthly' => 'Monthly',
|
|
'quarterly' => 'Quarterly',
|
|
'yearly' => 'Yearly',
|
|
];
|
|
?>
|
|
|
|
<div id="settings" class="tab-content">
|
|
<h2 class="mb-3">Family settings</h2>
|
|
<p class="text-muted">People and chore currency defaults apply across the whole hub. Heads of household verify with a PIN when switching profiles at the top of the page.</p>
|
|
|
|
<?php if (count($people) === 0): ?>
|
|
<div class="card mb-4 border-primary" id="firstSetupCard">
|
|
<div class="card-header bg-primary text-white">First-time setup</div>
|
|
<div class="card-body">
|
|
<p>Create the first <strong>Head of household</strong> profile so you can manage family members and settings.</p>
|
|
<form id="firstHeadForm" class="row g-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label" for="first_name">Name</label>
|
|
<input class="form-control" id="first_name" name="name" required autocomplete="name">
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label" for="first_pin">PIN (4+ characters)</label>
|
|
<input type="password" class="form-control" id="first_pin" name="pin" required minlength="4" autocomplete="new-password">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="first_favoriteColor">Favorite color</label>
|
|
<input type="color" class="form-control form-control-color" id="first_favoriteColor" name="favoriteColor" value="#4a90e2">
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-primary btn-lg">Create profile</button>
|
|
</div>
|
|
<div class="col-12">
|
|
<div class="alert d-none" id="firstHeadFeedback" role="status"></div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">Chore economy</div>
|
|
<div class="card-body">
|
|
<?php if (!$canManage && count($people) > 0): ?>
|
|
<p class="text-muted">Switch to a verified Head of household to edit these settings.</p>
|
|
<?php endif; ?>
|
|
<form id="familySettingsForm" class="row g-3">
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="currency_symbol">Currency symbol</label>
|
|
<input class="form-control" id="currency_symbol" name="currency_symbol" maxlength="8"
|
|
value="<?= sanitizeInput($familySettings['currency_symbol'] ?? '') ?>"
|
|
<?= !$canManage ? 'disabled' : '' ?>>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="currency_name">Currency name</label>
|
|
<input class="form-control" id="currency_name" name="currency_name"
|
|
value="<?= sanitizeInput($familySettings['currency_name'] ?? '') ?>"
|
|
<?= !$canManage ? 'disabled' : '' ?>>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="currency_permanence">Balance reset</label>
|
|
<select class="form-select" id="currency_permanence" name="currency_permanence" <?= !$canManage ? 'disabled' : '' ?>>
|
|
<?php foreach ($permanenceOptions as $val => $label): ?>
|
|
<option value="<?= htmlspecialchars($val, ENT_QUOTES, 'UTF-8') ?>" <?= ($familySettings['currency_permanence'] ?? '') === $val ? 'selected' : '' ?>>
|
|
<?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label" for="timezone">Timezone</label>
|
|
<input class="form-control" id="timezone" name="timezone"
|
|
value="<?= sanitizeInput($familySettings['timezone'] ?? 'UTC') ?>"
|
|
<?= !$canManage ? 'disabled' : '' ?>>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label" for="week_starts_on">Week starts on (0 = Sunday)</label>
|
|
<input type="number" class="form-control" id="week_starts_on" name="week_starts_on" min="0" max="6"
|
|
value="<?= (int) ($familySettings['week_starts_on'] ?? 0) ?>"
|
|
<?= !$canManage ? 'disabled' : '' ?>>
|
|
</div>
|
|
<?php if ($canManage): ?>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-primary">Save settings</button>
|
|
</div>
|
|
<?php endif; ?>
|
|
<div class="col-12">
|
|
<div class="alert d-none" id="familySettingsFeedback" role="status"></div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
$storesList = normalizeStoresList(readJsonFile('stores.json'));
|
|
?>
|
|
<div class="card mb-4">
|
|
<div class="card-header">Grocery stores</div>
|
|
<div class="card-body">
|
|
<p class="small text-muted">Each store has its own list in the <strong>Grocery list</strong> tab.</p>
|
|
<?php if ($canManage): ?>
|
|
<form id="storeAddForm" class="row g-2 align-items-end mb-3">
|
|
<div class="col-md-8">
|
|
<label class="form-label" for="new_store_name">New store name</label>
|
|
<input class="form-control" id="new_store_name" required maxlength="80" placeholder="e.g. Costco">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<button type="submit" class="btn btn-primary">Add store</button>
|
|
</div>
|
|
<div class="col-12">
|
|
<div class="alert d-none" id="storeAddFeedback" role="status"></div>
|
|
</div>
|
|
</form>
|
|
<?php else: ?>
|
|
<p class="text-muted small">Only a verified Head of household can manage stores.</p>
|
|
<?php endif; ?>
|
|
<?php if (count($storesList) === 0): ?>
|
|
<p class="text-muted mb-0">No stores yet. Add one above (or open the Grocery list tab to get a default Home store).</p>
|
|
<?php else: ?>
|
|
<ul class="list-group">
|
|
<?php foreach ($storesList as $st): ?>
|
|
<?php $stid = htmlspecialchars((string) ($st['id'] ?? ''), ENT_QUOTES, 'UTF-8'); ?>
|
|
<li class="list-group-item d-flex flex-wrap justify-content-between align-items-center gap-2">
|
|
<span><?= sanitizeInput($st['name'] ?? '') ?></span>
|
|
<?php if ($canManage): ?>
|
|
<span class="btn-group">
|
|
<button type="button" class="btn btn-sm btn-outline-secondary btn-store-rename" data-id="<?= $stid ?>" data-name="<?= htmlspecialchars((string) ($st['name'] ?? ''), ENT_QUOTES, 'UTF-8') ?>">Rename</button>
|
|
<button type="button" class="btn btn-sm btn-outline-danger btn-store-delete" data-id="<?= $stid ?>">Delete</button>
|
|
</span>
|
|
<?php endif; ?>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (count($people) > 0): ?>
|
|
<div class="card mb-4">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<span>People</span>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if ($canManage): ?>
|
|
<form id="addPersonForm" class="row g-3 mb-4 pb-3 border-bottom">
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="add_name">Name</label>
|
|
<input class="form-control" id="add_name" required>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="add_role">Role</label>
|
|
<select class="form-select" id="add_role">
|
|
<option value="<?= ROLE_CHILD ?>">Child</option>
|
|
<option value="<?= ROLE_ADULT ?>">Adult</option>
|
|
<option value="<?= ROLE_HEAD ?>">Head of household</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4" id="add_pin_wrap">
|
|
<label class="form-label" for="add_pin">PIN (required for Head of household)</label>
|
|
<input type="password" class="form-control" id="add_pin" minlength="4" autocomplete="new-password">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="add_favoriteColor">Favorite color</label>
|
|
<input type="color" class="form-control form-control-color" id="add_favoriteColor" value="#4a90e2">
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-success">Add person</button>
|
|
</div>
|
|
<div class="col-12">
|
|
<div class="alert d-none" id="addPersonFeedback" role="status"></div>
|
|
</div>
|
|
</form>
|
|
<?php else: ?>
|
|
<p class="text-muted">Only a verified Head of household can add or edit people.</p>
|
|
<?php endif; ?>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table align-middle" id="peopleTable">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Role</th>
|
|
<th class="text-end">Balance</th>
|
|
<?php if ($canManage): ?><th></th><?php endif; ?>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($people as $p): ?>
|
|
<tr data-person-id="<?= htmlspecialchars($p['id'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
|
|
<td><?= sanitizeInput($p['name'] ?? '') ?></td>
|
|
<td><?= sanitizeInput($p['role'] ?? '') ?></td>
|
|
<td class="text-end"><?= sanitizeInput((string) ($p['currency_balance'] ?? 0)) ?> <?= sanitizeInput($familySettings['currency_symbol'] ?? '') ?></td>
|
|
<?php if ($canManage): ?>
|
|
<td class="text-end text-nowrap">
|
|
<?php if (($p['role'] ?? '') === ROLE_HEAD): ?>
|
|
<button type="button" class="btn btn-sm btn-outline-secondary btn-reset-pin" data-id="<?= htmlspecialchars($p['id'] ?? '', ENT_QUOTES, 'UTF-8') ?>">Reset PIN</button>
|
|
<?php endif; ?>
|
|
<button type="button" class="btn btn-sm btn-outline-danger btn-delete-person" data-id="<?= htmlspecialchars($p['id'] ?? '', ENT_QUOTES, 'UTF-8') ?>">Remove</button>
|
|
</td>
|
|
<?php endif; ?>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|