false, 'message' => 'not_logged_in'], JSON_UNESCAPED_UNICODE); exit; } $input = json_decode((string)file_get_contents('php://input'), true); if (!is_array($input)) { $input = $_POST; } file_put_contents($debugLog, "INPUT: " . json_encode($input, JSON_UNESCAPED_UNICODE) . "\n", FILE_APPEND); $contentId = trim((string)($input['content_id'] ?? '')); $comment = trim((string)($input['comment'] ?? '')); $commentId = (int)($input['id'] ?? 0); if ($contentId === '') { file_put_contents($debugLog, "ERROR: content_id is empty\n", FILE_APPEND); http_response_code(400); echo json_encode(['success' => false, 'message' => 'content_id_required'], JSON_UNESCAPED_UNICODE); exit; } if ($comment === '') { file_put_contents($debugLog, "ERROR: comment is empty\n", FILE_APPEND); http_response_code(400); echo json_encode(['success' => false, 'message' => 'comment_required'], JSON_UNESCAPED_UNICODE); exit; } if (function_exists('mb_substr')) { $comment = mb_substr($comment, 0, 255, 'UTF-8'); } else { $comment = substr($comment, 0, 255); } try { $pdo = db_conn(); if ($sysCompCode === '') { file_put_contents($debugLog, "ERROR: sys_comp_code is empty\n", FILE_APPEND); http_response_code(401); echo json_encode(['success' => false, 'message' => 'sys_comp_code_missing'], JSON_UNESCAPED_UNICODE); exit; } $stmtCategory = $pdo->prepare('SELECT category_code FROM edu_contents WHERE content_id = ? LIMIT 1'); $stmtCategory->execute([$contentId]); $categoryCode = strtoupper((string)($stmtCategory->fetchColumn() ?: '')); if ($categoryCode === '') { http_response_code(404); echo json_encode(['success' => false, 'message' => 'content_not_found'], JSON_UNESCAPED_UNICODE); exit; } $pdo->beginTransaction(); $savedId = 0; $mode = 'insert'; // ── 마이클래스(CA10001): edu_learning_histories.comment 컬럼에 UPDATE ── if ($categoryCode === 'CA10001') { $sqlLh = 'UPDATE edu_learning_histories SET comment = ? WHERE content_id = ? AND member_id = ? AND sys_comp_code = ?'; $paramsLh = [$comment, $contentId, $memberId, $sysCompCode]; $stmtLh = $pdo->prepare($sqlLh); $stmtLh->execute($paramsLh); if ($stmtLh->rowCount() < 1) { $pdo->rollBack(); echo json_encode(['success' => false, 'message' => '학습 이력이 없어 소감을 저장할 수 없습니다.'], JSON_UNESCAPED_UNICODE); exit; } $pdo->commit(); echo json_encode([ 'success' => true, 'mode' => 'update', 'category' => $categoryCode, ], JSON_UNESCAPED_UNICODE); exit; } // ── 기타 카테고리: 기존 edu_comments 테이블 사용 ── if ($commentId > 0) { $stmtUpdate = $pdo->prepare( 'UPDATE edu_comments SET comment = ?, updated_by = ?, updated_at = NOW() WHERE id = ? AND member_id = ? AND sys_comp_code = ?' ); $stmtUpdate->execute([$comment, $memberId, $commentId, $memberId, $sysCompCode]); if ($stmtUpdate->rowCount() < 1) { $pdo->rollBack(); http_response_code(403); echo json_encode(['success' => false, 'message' => 'forbidden_or_not_found'], JSON_UNESCAPED_UNICODE); exit; } $savedId = $commentId; $mode = 'update'; } else { $stmtInsert = $pdo->prepare( 'INSERT INTO edu_comments (parent_id, sys_comp_code, member_id, comment, content_id, created_by, created_at, updated_by, updated_at) VALUES (NULL, ?, ?, ?, ?, ?, NOW(), ?, NOW())' ); $stmtInsert->execute([$sysCompCode, $memberId, $comment, $contentId, $memberId, $memberId]); $savedId = (int)$pdo->lastInsertId(); $mode = 'insert'; } $pdo->commit(); echo json_encode([ 'success' => true, 'id' => $savedId, 'mode' => $mode, 'category' => $categoryCode, ], JSON_UNESCAPED_UNICODE); } catch (Throwable $e) { if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) { $pdo->rollBack(); } error_log('[save_comment] ' . $e->getMessage()); http_response_code(500); echo json_encode(['success' => false, 'message' => 'server_error'], JSON_UNESCAPED_UNICODE); }