Files
sam-kd/bendingfee/itemlist.php
hskwon aca1767eb9 초기 커밋: 5130 레거시 시스템
- URL 하드코딩 → .env APP_URL 기반 동적 URL로 변경
- DB 연결 하드코딩 → .env 기반으로 변경
- MySQL strict mode DATE 오류 수정
2025-12-10 20:14:31 +09:00

188 lines
6.3 KiB
PHP

<?php
// itemlist.php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
if (!isset($_SESSION["level"]) || $_SESSION["level"] > 5) {
sleep(1);
header("Location:" . $WebSite . "login/login_form.php");
exit;
}
include $_SERVER['DOCUMENT_ROOT'] . '/load_header.php';
$title_message = '절곡물 품목 관리';
?>
<title><?= $title_message ?></title>
<?php if($chkMobile==true) { ?>
<style>
@media (max-width: 1000px) {
body { font-size: 25px; }
.form-control, .fw-bold, .table td, .table th { font-size: 25px; }
button { font-size: 30px; }
.modal-body, .modal-title { font-size: 30px; }
}
</style>
<?php } ?>
</head>
<body>
<?php
// itemlist.php 에서 관리할 JSON 파일 경로 (파일명: models.json, 폴더: /models)
$jsonFile = $_SERVER['DOCUMENT_ROOT'] . '/models/items.json';
// JSON 파일이 존재하면 읽어오고, 없으면 빈 배열 생성
$items = [];
if (file_exists($jsonFile)) {
$jsonContent = file_get_contents($jsonFile);
$items = json_decode($jsonContent, true);
if (!is_array($items)) {
$items = [];
}
}
// POST 요청 처리: 추가, 수정, 삭제
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = isset($_POST['action']) ? $_POST['action'] : '';
// 수정 또는 삭제 시 사용되는 인덱스 (배열의 키)
$index = isset($_POST['index']) ? intval($_POST['index']) : -1;
// 입력 품목명
$item_name = isset($_POST['item_name']) ? trim($_POST['item_name']) : '';
// 새로 추가: prefix 값 (slatitem)
$slatitem = isset($_POST['slatitem']) ? trim($_POST['slatitem']) : '';
if ($action === 'insert' && $item_name !== '') {
// 신규 추가: 배열 끝에 객체 형태로 추가
$items[] = array(
"item_name" => $item_name,
"slatitem" => $slatitem
);
} elseif ($action === 'update' && $item_name !== '' && $index >= 0 && $index < count($items)) {
// 수정: 해당 인덱스의 값을 변경 (품목명과 prefix 모두 업데이트)
$items[$index]["item_name"] = $item_name;
$items[$index]["slatitem"] = $slatitem;
} elseif ($action === 'delete' && $index >= 0 && $index < count($items)) {
// 삭제: 해당 요소 제거
array_splice($items, $index, 1);
}
// JSON 파일에 저장
file_put_contents($jsonFile, json_encode($items, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// header("Location: modelslist.php");
// exit;
}
?>
<div class="container mt-3">
<div class="card">
<div class="card-header d-flex justify-content-center align-items-center text-center">
<h3 class="mb-0 text-center"><?= $title_message ?></h3>
<button type="button" class="btn btn-dark btn-sm mx-2" onclick='location.reload();' > <i class="bi bi-arrow-clockwise"></i> </button>
<button type="button" class="btn btn-dark btn-sm mx-5" onclick="self.close();"> &times; 닫기 </button>
</div>
<div class="card-body">
<!-- 신규 품목 추가 폼 -->
<div class="mb-3">
<form id="adItemlForm" method="post" action="itemlist.php" class="row g-3">
<input type="hidden" name="action" id="action" value="insert">
<input type="hidden" name="index" id="index" value="-1">
<div class="d-flex justify-content-center align-items-center text-center">
<!-- 품목명 입력 -->
<div class="col-auto mx-1">
<input type="text" name="item_name" id="item_name" class="form-control mx-1" placeholder="품목명을 입력하세요" autocomplete="off" >
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary btn-sm mx-1" id="submitBtn">등록</button>
</div>
</div>
</form>
</div>
<hr>
<div class="alert alert-danger" role="alert">
이 기능은 중지합니다. 모델 Tree에서 품목추가하면 됩니다.
</div>
<div class="d-flex justify-content-center align-items-center text-center">
</div>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>순번</th>
<th>절곡물 품목명(대분류)</th>
<th>수정/삭제</th>
</tr>
</thead>
<tbody>
<?php if (!empty($items)): ?>
<?php foreach ($items as $i => $item): ?>
<tr>
<td><?= $i + 1 ?></td>
<td>
<?php
// 품목 데이터가 배열이면 'item_name' 값, 아니면 그대로 출력
echo is_array($item) && isset($item["item_name"])
? htmlspecialchars($item["item_name"], ENT_QUOTES, 'UTF-8')
: htmlspecialchars($item, ENT_QUOTES, 'UTF-8');
?>
</td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary editBtn" data-index="<?= $i ?>">수정</button>
<button type="button" class="btn btn-sm btn-outline-danger deleteBtn" data-index="<?= $i ?>">삭제</button>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr><td colspan="4">등록된 품목이 없습니다.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div><!-- card-body -->
</div><!-- card -->
</div><!-- container -->
<!-- 반드시 새로운 페이지에 들어가야 하는 로더 숨김 스크립트 -->
<script>
$(document).ready(function(){
var loader = document.getElementById('loadingOverlay');
if(loader)
loader.style.display = 'none';
});
</script>
<script>
// jQuery 이벤트 처리
$(document).ready(function(){
// 수정 버튼 클릭 시: 해당 행의 품목명을 폼에 채워 수정 모드로 전환
$('.editBtn').on('click', function(){
var index = $(this).data('index');
var Item = $(this).closest('tr').find('td:eq(1)').text().trim();
$('#item_name').val(Item);
$('#index').val(index);
$('#action').val('update');
$('#submitBtn').text('수정');
});
// 삭제 버튼 클릭 시: 확인 후 폼 제출하여 삭제 처리
$('.deleteBtn').on('click', function(){
var index = $(this).data('index');
Swal.fire({
title: '품목 삭제',
text: "품목 삭제는 신중하셔야 합니다. 정말 삭제 하시겠습니까?",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '삭제',
cancelButtonText: '취소'
}).then((result) => {
if (result.isConfirmed) {
$('#index').val(index);
$('#action').val('delete');
$('#adItemlForm').submit();
}
});
});
});
</script>
</body>
</html>