Files
FamilyBook/templates/books/index.php
T

165 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/**
* 书架(v24 第 14 批 用户第 5 条)。
*
* 一眼能看出三件事:**有多少书 / 现在在读哪几本 / 哪些读完了**。
* 所以状态筛选条在最上面,卡片上标着状态徽标与评分。
*
* @param array $rows 书(已带 note_count)
* @param array $cats 用过的分类(筛选用)
* @param array $counts 各状态的数量
* @param array $people 名册(筛"谁的书")
* @param string $status 当前状态筛选
* @param string $cat 当前分类筛选
* @param string $q 关键词
* @param int $owner 当前"谁的书"筛选
*/
$canWrite = \App\Auth::canWrite();
$counts = $counts ?? [];
$status = (string)($status ?? '');
$cat = (string)($cat ?? '');
$q = (string)($q ?? '');
$owner = (int)($owner ?? 0);
$people = $people ?? [];
$stList = ['' => '全部'] + \App\Book::STATUS;
?>
<div class="page-head">
<h1>读书</h1>
<span class="sub">
共 <?= (int)($counts['_all'] ?? 0) ?> 本<?= (int)($counts['reading'] ?? 0) > 0
? ' · ' . (int)$counts['reading'] . ' 本在读' : '' ?>
</span>
<div class="actions">
<?php if ($canWrite): ?>
<a class="btn" href="<?= e(url('books/new')) ?>">添加图书</a>
<?php else: ?>
<span class="link-quiet">只读账号</span>
<?php endif; ?>
<?php $bkSetCurrent = 'list'; require __DIR__ . '/_settings_menu.php'; ?>
</div>
</div>
<?php /* 状态筛选条:一段一个筛选,都在同一行(窄屏自动换行)。
这四个状态就是用户说的"每个任务都有状态"在读书这边的对应物。 */ ?>
<div class="book-filter">
<?php foreach ($stList as $k => $v): ?>
<a class="bf-btn<?= $status === (string)$k ? ' is-on' : '' ?>"
href="<?= e(url('books', array_filter([
'status' => (string)$k, 'cat' => $cat, 'q' => $q,
'owner' => $owner > 0 ? (string)$owner : '',
], static fn($x) => (string)$x !== ''))) ?>">
<?= e($v) ?><?= $k !== '' ? ' <span class="bf-n">' . (int)($counts[$k] ?? 0) . '</span>' : '' ?>
</a>
<?php endforeach; ?>
</div>
<form class="search" method="get" action="index.php">
<input type="hidden" name="r" value="books">
<?php if ($status !== ''): ?><input type="hidden" name="status" value="<?= e($status) ?>"><?php endif; ?>
<input class="input" type="text" name="q" value="<?= e($q) ?>" placeholder="搜书名 / 作者 / 出版社 / 标签 / ISBN">
<?php if ($cats): ?>
<select class="select" name="cat">
<option value="">全部分类</option>
<?php foreach ($cats as $c): ?>
<option value="<?= e((string)$c['name']) ?>" <?= $cat === (string)$c['name'] ? 'selected' : '' ?>>
<?= e((string)$c['name']) ?>(<?= (int)$c['n'] ?>)
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<?php if ($people): ?>
<select class="select" name="owner">
<option value="0">所有人的书</option>
<?php foreach ($people as $p): ?>
<option value="<?= (int)$p['id'] ?>" <?= $owner === (int)$p['id'] ? 'selected' : '' ?>>
<?= e(person_name($p)) ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<button class="btn" type="submit">筛选</button>
<?php if ($q !== '' || $cat !== '' || $owner > 0 || $status !== ''): ?>
<a class="search-clear" href="<?= e(url('books')) ?>">清除</a>
<?php endif; ?>
</form>
<?php if (!$rows): ?>
<div class="card card-pad empty">
<b><?= ($q !== '' || $cat !== '' || $owner > 0 || $status !== '') ? '没有匹配的书' : '书架还是空的' ?></b>
<?php if ($q !== '' || $cat !== '' || $owner > 0 || $status !== ''): ?>
换个条件试试,或清除筛选看全部。
<?php else: ?>
<?php if ($canWrite): ?>
点右上角「添加图书」—— 可以上传 EPUB / TXT 让它自己读出书名作者与正文,
也可以只手填一条"想读的书"。
<?php else: ?>
家人还没有往书架上放书。
<?php endif; ?>
<?php endif; ?>
</div>
<?php else: ?>
<div class="book-grid">
<?php foreach ($rows as $b): ?>
<?php
$bid = (int)$b['id'];
$st = (string)($b['status'] ?? 'wish');
$rate = $b['rating'] === null ? null : (int)$b['rating'];
?>
<div class="book-card">
<a class="book-cover" href="<?= e(url('books/show', ['id' => $bid])) ?>">
<?php if (!empty($b['cover_key'])): ?>
<img src="<?= e(media_url((string)$b['cover_key'])) ?>" alt="">
<?php else: ?>
<span class="book-cover-empty">📖</span>
<?php endif; ?>
</a>
<div class="book-body">
<a class="book-title" href="<?= e(url('books/show', ['id' => $bid])) ?>">
<?= e((string)$b['title']) ?>
</a>
<?php if (!empty($b['author'])): ?>
<div class="book-author"><?= e((string)$b['author']) ?></div>
<?php endif; ?>
<div class="book-meta">
<span class="kb-st is-<?= e($st === 'reading' ? 'doing' : ($st === 'done' ? 'done' : 'todo')) ?> is-static">
<?= e(\App\Book::statusWord($st)) ?>
</span>
<?php if ($rate !== null): ?>
<span class="book-rate" title="评分"><?= $rate ?>/10</span>
<?php endif; ?>
<?php if ((int)($b['note_count'] ?? 0) > 0): ?>
<span class="book-note-n" title="笔记">✎ <?= (int)$b['note_count'] ?></span>
<?php endif; ?>
<?php if ((int)($b['chapter_count'] ?? 0) > 0): ?>
<span class="small muted"><?= (int)$b['chapter_count'] ?> 章</span>
<?php endif; ?>
</div>
<?php if ($st === 'reading' && (int)($b['chapter_count'] ?? 0) > 0 && (int)($b['progress'] ?? 0) > 0): ?>
<?php $pct = min(100, (int)round((int)$b['progress'] * 100 / max(1, (int)$b['chapter_count']))); ?>
<div class="bk-bar"><i style="width: <?= $pct ?>%"></i></div>
<span class="small muted">读到第 <?= (int)$b['progress'] ?> 章</span>
<?php endif; ?>
<div class="book-foot">
<?php if (!empty($b['file_key'])): ?>
<a class="btn btn-sm" href="<?= e(url('books/read', ['id' => $bid])) ?>">读</a>
<?php endif; ?>
<?php if ($canWrite): ?>
<a class="btn btn-ghost btn-sm" href="<?= e(url('books/edit', ['id' => $bid])) ?>">改</a>
<?php if ($st !== 'done'): ?>
<form method="post" action="<?= e(url('books/status')) ?>" class="res-inline">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= $bid ?>">
<input type="hidden" name="status" value="done">
<?php /* 从列表点的 → 做完还回列表(不然会莫名跳进详情页) */ ?>
<input type="hidden" name="back" value="list">
<button class="btn btn-ghost btn-sm" type="submit">读完</button>
</form>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>