Files
2026-06-07 00:33:58 +09:00

117 lines
3.9 KiB
PHP

<?php
require_once __DIR__ . '/../app/lib/auth.php';
require_once __DIR__ . '/../app/lib/db.php';
require_once __DIR__ . '/../app/lib/helpers.php';
check_auth();
$pdo = db();
$uid = user_id();
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$name = trim($_POST['name'] ?? '');
$type = $_POST['category_type'] ?? '';
$sort = (int)($_POST['sort_order'] ?? 0);
if ($name === '') {
throw new RuntimeException('카테고리명을 입력하세요.');
}
if (!in_array($type, ['income','expense','transfer'], true)) {
throw new RuntimeException('카테고리 유형이 올바르지 않습니다.');
}
$stmt = $pdo->prepare("
INSERT INTO categories (user_id, category_type, name, sort_order, is_active)
VALUES (?, ?, ?, ?, 1)
");
$stmt->execute([$uid, $type, $name, $sort]);
redirect('/categories.php');
} catch (Throwable $e) {
$error = $e->getMessage();
}
}
$stmt = $pdo->prepare("
SELECT *
FROM categories
WHERE user_id = ?
ORDER BY category_type, sort_order, id
");
$stmt->execute([$uid]);
$list = $stmt->fetchAll();
require __DIR__ . '/../app/views/header.php';
?>
<div class="page-head">
<h2>카테고리 관리</h2>
</div>
<?php if ($error): ?>
<div class="alert alert-danger"><?= h($error) ?></div>
<?php endif; ?>
<div class="row g-4">
<div class="col-12 col-lg-4">
<div class="card border-0 shadow-sm">
<div class="card-body">
<form method="post" class="row g-3">
<div class="col-12">
<label class="form-label">유형</label>
<select name="category_type" class="form-select">
<option value="expense">지출</option>
<option value="income">수입</option>
<option value="transfer">이동</option>
</select>
</div>
<div class="col-12">
<label class="form-label">카테고리명</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="col-12">
<label class="form-label">정렬순서</label>
<input type="number" name="sort_order" class="form-control" value="0">
</div>
<div class="col-12">
<button class="btn btn-primary">추가</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-12 col-lg-8">
<div class="card border-0 shadow-sm">
<div class="card-body mobile-scroll">
<table class="table mb-0">
<thead>
<tr>
<th>ID</th>
<th>유형</th>
<th>이름</th>
<th>정렬</th>
<th>활성</th>
</tr>
</thead>
<tbody>
<?php foreach ($list as $row): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= h($row['category_type']) ?></td>
<td><?= h($row['name']) ?></td>
<td><?= $row['sort_order'] ?></td>
<td><?= $row['is_active'] ? 'Y' : 'N' ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php require __DIR__ . '/../app/views/footer.php'; ?>