92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
require_once dirname(__DIR__) . '/db_conn.php';
|
|
|
|
$memberId = trim((string)($_SESSION['member_id'] ?? ''));
|
|
$sysCompCode = trim((string)($_SESSION['sys_comp_code'] ?? ''));
|
|
|
|
if ($memberId === '') {
|
|
http_response_code(401);
|
|
echo json_encode(['success' => false, 'message' => '로그인이 필요합니다.'], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'message' => 'POST only'], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$contentId = trim((string)($_POST['content_id'] ?? ''));
|
|
$isActive = trim((string)($_POST['is_active'] ?? ''));
|
|
|
|
if ($contentId === '') {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'content_id 필수'], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// is_active: '1' → 활성, 그 외 → '0' 비활성
|
|
$activeValue = ($isActive === '1') ? '1' : '0';
|
|
|
|
try {
|
|
$pdo = db_conn();
|
|
$pdo->exec("SET NAMES 'utf8mb4'");
|
|
|
|
// sys_comp_code가 세션에 없으면 DB에서 조회
|
|
if ($sysCompCode === '') {
|
|
$stmtUser = $pdo->prepare('SELECT sys_comp_code FROM edu_users WHERE member_id = ? ORDER BY sys_comp_code LIMIT 1');
|
|
$stmtUser->execute([$memberId]);
|
|
$row = $stmtUser->fetch();
|
|
$sysCompCode = (string)($row['sys_comp_code'] ?? '');
|
|
}
|
|
|
|
if ($sysCompCode === '') {
|
|
echo json_encode(['success' => false, 'message' => '회사코드 확인 불가'], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// 기존 레코드 확인
|
|
$stmtCheck = $pdo->prepare(
|
|
'SELECT content_id, is_active
|
|
FROM edu_content_wishlist
|
|
WHERE content_id = ?
|
|
AND member_id = ?
|
|
AND sys_comp_code = ?'
|
|
);
|
|
$stmtCheck->execute([$contentId, $memberId, $sysCompCode]);
|
|
$existing = $stmtCheck->fetch();
|
|
|
|
if ($existing) {
|
|
// UPDATE
|
|
$stmtUpdate = $pdo->prepare(
|
|
'UPDATE edu_content_wishlist
|
|
SET is_active = ?, updated_at = NOW()
|
|
WHERE content_id = ?
|
|
AND member_id = ?
|
|
AND sys_comp_code = ?'
|
|
);
|
|
$stmtUpdate->execute([$activeValue, $contentId, $memberId, $sysCompCode]);
|
|
} else {
|
|
// INSERT
|
|
$stmtInsert = $pdo->prepare(
|
|
'INSERT INTO edu_content_wishlist
|
|
(content_id, member_id, sys_comp_code, is_active, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, NOW(), NOW())'
|
|
);
|
|
$stmtInsert->execute([$contentId, $memberId, $sysCompCode, $activeValue]);
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'is_active' => $activeValue], JSON_UNESCAPED_UNICODE);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => '서버 오류'], JSON_UNESCAPED_UNICODE);
|
|
} |