Files
FamilyBook/templates/days/form.php
T

113 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* 新建 / 编辑纪念日(v24 用户第 13 条)
*
* 用户原话:「可以根据阴历和阳历来设置纪念日 …… 纪念日,需要按照阴历和阳历进行重复。」
* 所以重复方式这一栏是核心:阳历 / 农历 / 只此一次。
* 选农历时,下面的月/日填的是**农历**的月日(不是什么公历日期换算)。
*
* @param array|null $editing 编辑时的既有记录
* @param array $people 名册
*/
$e0 = $editing ?: [];
$v = static fn(string $k, $def = '') => $e0[$k] ?? $def;
$repeat = (string)($e0['repeat_type'] ?? 'solar');
// 用户 2026-10-03 第 9 条:分类下拉的选项来自**可配置**的分类清单
// (原来写死 PRESET_KINDS,用户要能自己增删 —— 见「纪念日设置」)。
$kinds = (array)($kinds ?? \App\Anniversary::PRESET_KINDS);
$curKind = (string)($e0['kind'] ?? '');
// 编辑一条用了**已被删掉的分类**的纪念日时,要把它自己也列进下拉,
// 否则保存一次就把它悄悄改成了第一个选项(用户没动它,值却变了)。
if ($curKind !== '' && !in_array($curKind, $kinds, true)) {
array_unshift($kinds, $curKind);
}
?>
<div class="page-head">
<h1><?= $editing ? '编辑纪念日' : '新建纪念日' ?></h1>
<div class="actions">
<a class="btn btn-ghost" href="<?= e(url('days')) ?>">返回</a>
</div>
</div>
<form class="card card-pad" method="post" action="<?= e(url('days/save')) ?>">
<?= csrf_field() ?>
<?php if ($editing): ?><input type="hidden" name="id" value="<?= (int)$e0['id'] ?>"><?php endif; ?>
<div class="form-grid">
<div class="field full">
<label for="an-title">叫什么 *</label>
<input class="input" id="an-title" name="title" required
value="<?= e((string)$v('title')) ?>" placeholder="如:结婚纪念日">
</div>
<div class="field">
<label for="an-repeat">按什么重复 *</label>
<select class="select" id="an-repeat" name="repeat_type">
<?php foreach (\App\Anniversary::REPEAT as $rk => $rv): ?>
<option value="<?= e($rk) ?>" <?= $repeat === $rk ? 'selected' : '' ?>><?= e($rv) ?></option>
<?php endforeach; ?>
</select>
<span class="tip">选「农历」时,下面填的是**农历**的月日(不是公历)。</span>
</div>
<div class="field">
<label for="an-kind">分类</label>
<?php /* 用 <select> 而不是 <input list=…>:分类现在是**用户自己维护的一套**,
下拉能保证选到的都是存在的分类;datalist 允许自由填,
填出来的新词不会进清单,下次别人也不知道有这个词。 */ ?>
<select class="select" id="an-kind" name="kind">
<option value="">不分类</option>
<?php foreach ($kinds as $k): ?>
<option value="<?= e((string)$k) ?>" <?= $curKind === (string)$k ? 'selected' : '' ?>><?= e((string)$k) ?></option>
<?php endforeach; ?>
</select>
<span class="tip">分类可以在「纪念日」页右侧的设置里增删。</span>
</div>
<div class="field">
<label for="an-month">月 *</label>
<input class="input" id="an-month" name="month" type="number" min="1" max="12" required
value="<?= (int)$v('month', 1) ?>">
</div>
<div class="field">
<label for="an-day">日 *</label>
<input class="input" id="an-day" name="day" type="number" min="1" max="31" required
value="<?= (int)$v('day', 1) ?>">
</div>
<div class="field">
<label>与谁相关</label>
<?php /* 第 8 条的同一诉求:人多时不要下拉里瞪眼找。换成弹窗 + 首字母联想;
保留「不指定」这一项(原下拉的第一项就是它,不能丢)。 */ ?>
<?php
$ppName = 'person_id';
$ppPeople = $people;
$ppSelected = [(int)$v('person_id', 0)];
$ppMode = 'single';
$ppOpenBtn = '选择人';
$ppTitle = '这个纪念日与谁相关';
$ppEmptyText = '不指定';
$ppAllowEmpty = true;
require __DIR__ . '/../_person_picker.php';
?>
</div>
<div class="field">
<label for="an-leap">农历闰月</label>
<label class="chk-inline">
<input type="checkbox" id="an-leap" name="is_leap" value="1" <?= !empty($e0['is_leap']) ? 'checked' : '' ?>>
<span>这个农历月是闰月</span>
</label>
</div>
<div class="field full">
<label for="an-note">备注</label>
<textarea class="input" id="an-note" name="note" rows="2"><?= e((string)$v('note')) ?></textarea>
</div>
</div>
<div class="btn-row">
<button class="btn" type="submit"><?= $editing ? '保存' : '添加' ?></button>
<a class="btn btn-ghost" href="<?= e(url('days')) ?>">取消</a>
</div>
</form>