Files
FamilyBook/templates/kin/tree.php
T
laoyang 02f5d072a4 家谱卡片重做 / 首页控制台 / 名册改列表 / 重复日程与旅程公开
用户 2026-10-03 提的四条:

① 家谱卡片重做
   · 去掉「⇄ 姻亲」按钮,改成**每个人**卡片右下角都有「以 TA 为中心」
     (点了以他重画一张家谱,涵盖原来的姻亲场景)。
   · 头像外框改**长方形**(96×72,宽度贴合头像),object-fit: contain 不裁切,
     姓名/称呼在头像下方。
   · 修「点右下角按钮误进成员页」:卡片不再整张是 <a>,
     只有姓名是链接,右下角是独立的 <button>(物理上分开,不抢点击)。

② 家谱文案与按钮清理
   · 去掉副标题与下方图例说明;整块去掉「看全部 / 回到我 / 回到我的家谱」
     (顺带消掉"出现两个回到我的家谱")。镜头由库自动 fit,拖拽/滚轮即可。

③ 首页控制台
   · 标题下显示个人 slogan;去掉右侧三个统计;「我的资料/我的家谱」换成图标按钮;
     去掉与导航条重复的「名册」按钮。
   · 去掉「接下来的日子」(生日归到"未来七天"里,不再重复)。
   · 新增统计信息块:按时段随机问候 + 今日天气(Open-Meteo,免 key,可留空不显示)
     + 近七天日程/待办/逾期数。
   · 新增各模块小结卡(任务/日程/纪念日/观影/旅程/财务/名册/家谱/资源)。
   · 个人设置新增「首页上的模块」:可勾选与拖动排序(每人一份,存 user_settings)。
   · 保留「往年的今天」。

④ 名册改列表
   · 卡片网格 → **竖向列表**:左头像、中间姓名+与我的关系+手机号+生日。
   · 修拖动「只能往后不能往前」:落点改为显式记录"插到谁前面/后面",
     并排除被拖元素自身。
   · 去掉那句拖动提示(能力保留)。
   · 搜索新增**姓名首字母**检索(y / yy / yyq 都命中「杨永全」):
     新 app/Pinyin.php,intl 优先 + 内置常用字表兜底(本地无 intl 也能测);
     纯字母/数字查询词不走 SQL LIKE(拼音与加密手机号在库里都比不了)。

附带上两轮的收尾:
   · 菜单点外部自动关闭(全局事件委托,覆盖所有 details.menu)。
   · 旅程 is_public:列表与日历只显示公开的;详情页不拦(不丢数据)。
   · 重复日程:新 app/Repeat.php(RRULE 解析/生成/展开,含"工作日"),
     新表 calendar_event_skips(RFC 的 EXDATE);日历按规则展开;
     编辑支持三档「全部 / 这一次及以后 / 仅此一次」。
   · 结束时间可添加/去掉,并随开始时间联动。

验证:端到端 772 项通过 / 0 失败;几何 368、交互 97、移动端 516、家谱 26、
升级 123 全绿;SQL 列名静态校验 0 处可疑;部署 11 条线上探针全过。
2026-10-03 11:30:48 +08:00

325 lines
15 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 我的家谱:以我为中心,上一代 / 这一代 / 下一代所有有关系的人。
*
* v21 —— 回退到 family-chart 库(用户 2026-10-02 明确要求)。
*
* 之前自研布局叠了多层修补后位置漂移,用户决定还是交给成熟库来做。
* 数据准备要点:
* · 用 familyMap 把「上两代 / 本代 / 下两代」所有有关系的人(含姻亲旁支)全捞出来;
* · 转成 family-chart 的 Datum 格式 {id, data, rels: {parents, spouses, children}};
* · 「我」排数组第一位(库把 data[0].id 当 main_id);
* · 子女按长幼排序后写进 rels.children,库按数组顺序摆左右。
*/
$graph = $graph ?? \App\Kinship::graph((int)\App\Auth::familyId());
$people = $people ?? [];
// ---- ① 以我为中心,检索**所有有关系的人**(不设代数上限)----
// ⚠️ 用户明确要求「把以我为中心的关联数据全部取出来」。
// familyMap 的 $up/$down 只影响**代数标号**(超出就压到边界那一档),
// 遍历本身是走遍整个连通图的 —— 所以这两个值不放宽的话,
// 曾祖辈与祖辈会被压成同一代,排序就乱了。给足余量。
// ⚠️ v24 第 12 条:中心是 $focusId(默认=我)。点姻亲入口进来时,中心换成那个人,
// 这样 TA 那一侧的姻亲(岳父母等)才画得出来。
$focusId = (int)($focusId ?? $meId);
if (!isset($people[$focusId]) || !is_array($people[$focusId])) {
$focusId = (int)$meId;
}
$map = \App\Kinship::familyMap((int)\App\Auth::familyId(), $focusId, $people, 9, 9);
$gen = $map['gen'];
$units = $map['units'];
$unitOf = $map['unit_of'];
if (!isset($people[$focusId]) || !is_array($people[$focusId])) {
$canChart = false;
} else {
$onTree = array_fill_keys(array_map('intval', array_keys($gen)), true);
// ---- ② 树根 = 「我」往上走到的最高那一代 ----
//
// family-chart 画的是「树根的祖先链 + 后代 + 树根的兄弟姐妹 + 各人配偶」。
// 树根 = 我 时,伯父/堂兄/嫂子/侄子 不在「我的后代」里,也不会被画出来;
// 把树根换成我的最高祖先,整棵树就变成「这位祖先的全部后代」,
// 上一代、这一代、下一代的血亲与旁支才能全部落进来。
// 代价是「我」不再是几何中心 —— 用「回到我」按钮 + 加载后自动居中补回来。
$rootId = (int)$focusId;
for ($guard = 0; $guard < 8; $guard++) {
$up = \App\Kinship::parentIds($graph, $rootId);
$next = 0;
foreach ($up as $p) {
$p = (int)$p;
if (isset($onTree[$p]) && (int)$gen[$p] < (int)$gen[$rootId]) {
$next = $p;
break;
}
}
if ($next === 0) { break; }
$rootId = $next;
}
// ---- ③ 连边:子女一律挂在「夫妻」上(normalizeLinks 的对称补齐)----
// 这样库才会把连线从夫妻横线中点垂下来,母亲登录也能看到孩子。
$links = \App\Kinship::normalizeLinks($graph, array_keys($onTree));
// ---- ④ 转成 family-chart 的 Datum 格式 ----
$rankKey = static function (int $id) use ($people): string {
return \App\Kinship::rankValueFor($people, $id);
};
// 树根排最前(库默认以 data[0].id 为 main_id);其余按「代数 → 长幼」跟上
$order = [$rootId];
$rest = [];
foreach (array_keys($onTree) as $pid) {
$pid = (int)$pid;
if ($pid !== $rootId) { $rest[] = $pid; }
}
usort($rest, static function (int $a, int $b) use ($gen, $rankKey): int {
return ((int)$gen[$a] <=> (int)$gen[$b])
?: strcmp($rankKey($a), $rankKey($b))
?: ($a <=> $b);
});
$order = array_merge($order, $rest);
$mapIds = static function (array $ids) use ($onTree): array {
$out = [];
foreach ($ids as $x) {
$x = (int)$x;
if (isset($onTree[$x])) { $out[] = (string)$x; }
}
return array_values(array_unique($out));
};
$datum = [];
foreach ($order as $pid) {
if (!isset($people[$pid]) || !is_array($people[$pid])) { continue; }
$p = $people[$pid];
$parents = $mapIds($links['parents'][$pid] ?? []);
$spouses = $mapIds($links['spouses'][$pid] ?? []);
$kids = $mapIds($links['children'][$pid] ?? []);
// 子女按长幼排序:库按 rels.children 的数组顺序摆左右
$kidsInt = array_map('intval', $kids);
usort($kidsInt, static fn(int $a, int $b): int => strcmp($rankKey($a), $rankKey($b)) ?: ($a <=> $b));
$kids = array_map('strval', $kidsInt);
$g = strtolower((string)($p['gender'] ?? ''));
$gender = $g === 'm' ? 'M' : ($g === 'f' ? 'F' : 'U');
$datum[] = [
'id' => (string)$pid,
'data' => [
'gender' => $gender,
'name' => \App\Nickname::display((int)$meId, $p) ?: (string)($p['name'] ?? ''),
'label' => \App\Kinship::label($graph, (int)$meId, $pid, $people),
'avatar' => avatar_url($p['avatar'] ?? null),
// 「我」在图上高亮;中心那个人另有标记($focusId)
'me' => $pid === (int)$focusId,
'is_me_viewer' => $pid === (int)$meId,
'ph' => (int)($p['is_placeholder'] ?? 0) === 1,
],
'rels' => [
'parents' => $parents,
'spouses' => $spouses,
'children' => $kids,
],
];
}
// v24 第 12 条:本图上「有姻亲但没画出来」的人 —— 卡片上给个入口
$branchOuts = \App\Kinship::branchOuts($graph, $people, $onTree);
foreach ($datum as &$dn) {
$did = (int)$dn['id'];
if (isset($branchOuts[$did])) {
$dn['data']['branch'] = $branchOuts[$did]['name'];
}
}
unset($dn);
$canChart = count($datum) > 1;
}
$meName = (string)($people[$meId]['name'] ?? '自己');
$datumJson = json_encode(
$datum ?? [],
JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
);
?>
<?php /* 用户 2026-10-03 第 2 条:标题下的那句说明**去掉**;
右下角的「返回我的家谱」也去掉 —— 与页面里那个工具条重复,
会出现两个"回到我的家谱"(用户明确点名)。现在整页只有标题。 */ ?>
<div class="page-head">
<h1><?= $focusId === (int)$meId ? '我的家谱' : e((string)($people[$focusId]['name'] ?? '') . ' 的家谱') ?></h1>
</div>
<?php if (!$canChart): ?>
<div class="empty">
<b>还没连上任何亲属关系</b>
到「<?= e($meName) ?>」的资料页,把父母、配偶、子女填进去,
这张家谱就会自动长出来。兄弟姐妹不用单独填 —— 只要你们有同一对父母,就会自动认出来。
</div>
<?php else: ?>
<?php /* 用户 2026-10-03 第 2 条:「看全部 / 回到我 / 回到我的家谱」这些按钮
全部去掉。镜头由库自动 fit(updateTree 的 tree_position: 'fit'),
用户要调整时直接拖拽/滚轮即可,不需要常驻按钮。 */
/* ⚠️ 顺带说明:品牌名「家谱」已从导航条撤走,但**模块本身还在** ——
个人设置里能勾回来,且 URL 直接访问 r=kin 永远有效。
这一点写在这里是为了防止有人"顺手把 kin 路由删掉"。 */ ?>
<!-- ⚠️ `f3` 这个类名是**库的 CSS 作用域**,不是我们起的名字。
官方示例就是 `<div id="FamilyChart" class="f3">` + 引 family-chart.css;
少了它,库里 `.f3 svg.main_svg{width:100%;height:100%}` 这条撑满规则不生效,
SVG 会退化成浏览器默认的 300×150(连线全被裁掉,而 HTML 卡片层不受影响 →
表现为「卡在、线没了」)。 -->
<div class="kin-chart f3" id="kinChart" data-datum="<?= e($datumJson) ?>"
data-focus="<?= e((string)$focusId) ?>" data-me-viewer="<?= e((string)$meId) ?>"></div>
<!-- 官方用法是「容器带 class="f3" + 引 family-chart.css」(见 family-chart 官方示例)。
之前没引这份 CSS,靠自己在 app.css 里手补 `.kin-chart svg.main_svg{width/height:100%}`
绕过去 —— 属于"调用方法不对"。现在按官方来。
库的深色主题(--male-color / --background-color…)在 app.css 里覆盖成家书的宣纸色。 -->
<link rel="stylesheet" href="<?= e(asset('/assets/vendor/family-chart/family-chart.css')) ?>">
<script src="<?= e(asset('/assets/vendor/family-chart/d3.min.js')) ?>"></script>
<script src="<?= e(asset('/assets/vendor/family-chart/family-chart.min.js')) ?>"></script>
<script>
(function () {
var root = document.getElementById('kinChart');
if (!root || typeof window.f3 === 'undefined') { return; }
var raw;
try {
raw = JSON.parse(root.getAttribute('data-datum') || '[]');
} catch (err) {
raw = [];
}
if (!raw.length) { return; }
function esc(s) {
return String(s == null ? '' : s)
.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
.replace(/"/g, '&quot;').replace(/'/g, '&#39;');
}
var chart = f3.createChart('#kinChart', raw)
.setTransitionTime(600)
.setOrientationVertical()
.setShowSiblingsOfMain(true)
// ⚠️ 官方示例**不设**祖先/后代的代数上限 —— 库在"未设置"时
// 会跳过深度截断,等于**不限代数**,有多少画多少。
// 之前写死了具体代数,真实家庭一超过就被砍掉。
// 用户要求「全部取出来」,所以这里也不设。
.setCardXSpacing(160)
.setCardYSpacing(180);
chart.setCardHtml()
.setStyle('rect')
.setCardDim({ w: 118, h: 132, mini_tree: false })
.setCardInnerHtmlCreator(function (d) {
var p = (d.data && d.data.data) || {};
var id = (d.data && d.data.id) || '';
var cls = 'kin-card' + (p.me ? ' is-me' : '') + (p.ph ? ' is-ph' : '');
/* 用户 2026-10-03 第 1 条:
① 卡片**不再整张是链接** —— 原来整卡是 <a>,右下角那个小按钮也在
<a> 里,手指/鼠标稍偏一点就先触发外层链接跳去成员页(用户反馈
"容易误触"的根因)。现在卡片是 <div>,只有「姓名」那一块是链接,
右下角按钮是独立的 <button>,两者物理上分开,不会互相抢点击。
② 头像外框改**长方形**、宽度贴合头像本身(CSS 里 .kin-ph 定宽,
img 用 object-fit: contain 不裁切),信息在头像**下方**。
③ 右下角按钮:每个人都有一个 → 以他为中心重画一张家谱
(原来只有"姻亲不在图上"的人才有,现在人人都有)。 */
return '<div class="' + cls + '" data-kin-id="' + esc(id) + '">'
+ '<span class="kin-ph"><img src="' + esc(p.avatar) + '" alt=""></span>'
+ '<a class="kin-nm" href="/index.php?r=people/show&id='
+ encodeURIComponent(id) + '" title="打开 ' + esc(p.name) + ' 的资料">'
+ esc(p.name) + '</a>'
+ '<span class="kin-lb">' + esc(p.label) + '</span>'
+ (p.ph ? '<span class="kin-mark">待确认</span>' : '')
+ '<button type="button" class="kin-focus" data-focus-id="' + esc(id) + '"'
+ ' title="以 ' + esc(p.name) + ' 为中心看家谱">以 TA 为中心</button>'
+ '</div>';
});
chart.editTree().setCanAdd(false);
chart.setSingleParentEmptyCard(false);
chart.updateTree({ initial: true, tree_position: 'fit' });
window.kinChart = chart;
// family-chart 的 zoom 行为绑在 #f3Canvas 上,SVG 与 HTML 卡片层由库的 onZoom 同步。
// 必须拿到 f3Canvas 才能正确驱动镜头;直接操作 svg.__zoomObj 或只动 svg 都会让
// HTML 卡片层不同步。
var f3Canvas = root.querySelector('#f3Canvas');
function zoomObj() {
if (!f3Canvas) { f3Canvas = root.querySelector('#f3Canvas'); }
// __zoomObj 挂在 #f3Canvas 上;库的 d() helper 也会往父节点找
var el = f3Canvas && f3Canvas.__zoomObj ? f3Canvas
: (f3Canvas && f3Canvas.parentNode && f3Canvas.parentNode.__zoomObj ? f3Canvas.parentNode : null);
return el ? el.__zoomObj : null;
}
function applyTransform(t) {
var z = zoomObj(), d3 = window.d3;
if (!z || !d3 || !f3Canvas) return;
d3.select(f3Canvas).call(z.transform, t);
}
// 把镜头挪到指定人:用库算出的树坐标,固定比例 0.9,避免「看全部」时缩得太小
function centerOn(id) {
var d3 = window.d3;
if (!d3) return;
var tree = chart.store.getTree();
if (!tree || !tree.data) return;
var node = tree.data.find(function (n) { return n.data && n.data.id === id; });
if (!node) return;
var rr = root.getBoundingClientRect();
var k = 0.9;
var tx = rr.width / 2 - node.x * k;
var ty = rr.height / 2 - node.y * k;
// d3 zoom 的 {x,y} 是「缩放后再平移」的屏幕像素偏移,
// 所以先 translate(tx,ty) 再 scale(k) 才能得到 screen = node * k + offset。
applyTransform(d3.zoomIdentity.translate(tx, ty).scale(k));
}
window.kinCenterOn = centerOn;
var focusId = root.getAttribute('data-focus') || '';
var meId = root.getAttribute('data-me-viewer') || focusId;
// ⚠️ 中心是别人时,「回到我」要跳到 focus=我 的图,而不是在图里找我的坐标
var centerTarget = root.getAttribute('data-me-viewer') || focusId;
// fit 动画完成后把镜头挪到「我」(树根是最高祖先,我不再是几何中心)
if (meId) {
setTimeout(function () { centerOn(focusId); }, 900);
// 万一 zoom 对象还没初始化好,再补一次
setTimeout(function () { centerOn(focusId); }, 1400);
}
/* 用户 2026-10-03 第 2 条:工具条三个按钮(看全部/回到我/回到我的家谱)已撤。
镜头由库自动 fit;下面只保留卡片右下角那个「以他为中心」的入口。 */
/* 用户第 1 条:每张卡片右下角一个「以他为中心」的按钮 —— 点了就以他重画一张家谱。
⚠️ 这个元素在卡片的 <a> 里,必须 preventDefault + stopPropagation,
否则会先触发外面那个链接跳去成员页(用户反馈"容易误触"就是这个)。
另外用 mousedown/pointerdown/click 三处都拦,触摸设备上才稳。 */
Array.prototype.forEach.call(root.querySelectorAll('.kin-focus'), function (btn) {
function go(e) {
e.preventDefault();
e.stopPropagation();
var bid = btn.getAttribute('data-focus-id') || '';
if (bid) { window.location.href = '/index.php?r=kin&focus=' + encodeURIComponent(bid); }
}
btn.addEventListener('click', go);
btn.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') { go(e); }
});
['mousedown', 'pointerdown', 'touchstart'].forEach(function (ev) {
btn.addEventListener(ev, function (e) { e.stopPropagation(); }, { passive: true });
});
});
})();
</script>
<?php endif; ?>