<?php
declare(strict_types=1);
require_once 'includes/config.php';
require_once 'includes/functions.php';
$cur_user = get_session_user();
$lists = get_public_lists(200);
$users_idx = read_json(USERS_INDEX);
// Group by owner, track latest updated per owner
$by_owner = [];
$owner_updated = [];
foreach ($lists as $lst) {
$u = $lst['owner'];
$by_owner[$u][] = $lst;
$ts = strtotime($lst['updated'] ?? '');
if ($ts && $ts > ($owner_updated[$u] ?? 0)) {
$owner_updated[$u] = $ts;
}
}
// Sort owners by most recently modified list, descending
uksort($by_owner, fn($a, $b) => ($owner_updated[$b] ?? 0) - ($owner_updated[$a] ?? 0));
$page_title = 'Public Directory';
require_once 'includes/header.php';
?>
<div class="page-wrap-wide" style="margin-top:12px; margin-bottom:16px;">
<div class="window">
<div class="window-title">
<span>Public To-Do List Directory</span>
<span style="font-size:11px;opacity:.8;"><?= count($lists) ?> public list<?= count($lists) !== 1 ? 's' : '' ?></span>
</div>
<div class="window-body">
<?php if (empty($lists)): ?>
<div class="text-center" style="padding:24px; color:var(--chrome-dark);">
No public lists yet. <?= $cur_user ? '<a href="dashboard.php">Create one!</a>' : '<a href="index.php">Sign in</a> to create lists.' ?>
</div>
<?php else: ?>
<?php foreach ($by_owner as $username => $owner_lists): ?>
<?php
$display_name = $users_idx[$username]['display_name'] ?? $username;
$avatar_url = get_user_avatar_url($username);
$latest_ts = $owner_updated[$username] ?? 0;
?>
<div style="margin-bottom:18px;">
<!-- Owner header -->
<div class="section-title" style="display:flex; align-items:center; gap:8px; flex-wrap:wrap;">
<?php if ($avatar_url !== ''): ?>
<a href="profile.php?user=<?= h($username) ?>">
<img src="<?= h($avatar_url) ?>" alt=""
style="width:32px; height:32px; object-fit:cover; border:2px solid var(--chrome-dark); flex-shrink:0; display:block;">
</a>
<?php else: ?>
<span style="width:32px; height:32px; background:var(--chrome-mid); border:2px solid var(--chrome-dark); display:flex; align-items:center; justify-content:center; flex-shrink:0; font-size:16px; color:var(--chrome-dark);">👤</span>
<?php endif; ?>
<div>
<a href="profile.php?user=<?= h($username) ?>"
style="color:var(--title-start); text-decoration:none; font-size:13px; font-weight:bold;">
<?= h($display_name) ?>
</a>
<span style="font-size:11px; font-weight:normal; color:var(--chrome-dark);">
@<?= h($username) ?>
</span>
<?php if ($latest_ts): ?>
<span style="font-size:10px; font-weight:normal; color:var(--chrome-dark); margin-left:8px;">
— last updated <?= date('d M Y', $latest_ts) ?>
</span>
<?php endif; ?>
</div>
</div>
<!-- Lists grid -->
<div class="list-grid">
<?php foreach ($owner_lists as $lst): ?>
<div class="list-card">
<div class="list-card-title">
<a href="<?= h(list_url($username, $lst['id'])) ?>"><?= h($lst['title']) ?></a>
<span class="badge badge-pub">PUBLIC</span>
<?php if (!empty($lst['password_hash'])): ?>
<span class="badge" style="background:#774400;color:#fff;" title="Password protected">🔒</span>
<?php endif; ?>
</div>
<div class="list-card-body">
<?= $lst['description'] ? h($lst['description']) : '<em style="color:var(--chrome-dark)">No description</em>' ?>
</div>
<div class="list-card-footer">
<span class="task-counts">
<span title="Active" style="color:var(--normal);">■ <?= $lst['active_count'] ?></span>
<span title="Completed" style="color:var(--low);">✓ <?= $lst['completed_count'] ?></span>
</span>
<span style="font-size:10px;color:var(--chrome-dark);">
<?= date('d M Y', strtotime($lst['updated'])) ?>
</span>
<div class="d-flex gap-4">
<a href="<?= h(list_url($username, $lst['id'])) ?>" class="btn btn-sm">View</a>
<a href="print.php?id=<?= h($lst['id']) ?>&owner=<?= h($username) ?>"
class="btn btn-sm" target="_blank">Print</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>