AIPT/cms_admin/product_edit.php
2024-10-29 03:42:53 +00:00

286 lines
16 KiB
PHP

<?php
include('container/security.php');
// Fetch categories from the database
$categories_query = "SELECT * FROM categories";
$categories_result = mysqli_query($conn, $categories_query);
// Check if a product ID is provided via GET
if (isset($_GET['id'])) {
$id = intval($_GET['id']);
if ($id > 0) {
// Fetch product data
$product_query = "SELECT * FROM products WHERE id = $id";
$product_result = mysqli_query($conn, $product_query);
if ($product_result && mysqli_num_rows($product_result) > 0) {
$product = mysqli_fetch_assoc($product_result);
} else {
echo "<script>alert('Product not found.'); window.location.href = 'product_view.php';</script>";
exit();
}
} else {
echo "<script>alert('Invalid product ID.'); window.location.href = 'product_view.php';</script>";
exit();
}
} else {
echo "<script>alert('No product ID provided.'); window.location.href = 'product_view.php';</script>";
exit();
}
?>
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<!-- head links start -->
<?php include 'container/head_links.php'; ?>
<!-- head links end -->
<script src="https://cdn.tiny.cloud/1/lm04pa5dnd1x7omv4il6r3yz8vf0rxprjp20od48no7sk2wz/tinymce/7/tinymce.min.js"></script>
<script>
// TinyMCE初始化
tinymce.init({
selector: '#long_description, #cn_long_description',
language: 'zh_CN', // 设置语言为简体中文
plugins: [
'anchor', 'autolink', 'charmap', 'codesample', 'emoticons', 'image', 'link', 'lists', 'media',
'searchreplace', 'table', 'visualblocks', 'wordcount', 'checklist', 'mediaembed', 'casechange',
'export', 'formatpainter', 'pageembed', 'a11ychecker', 'tinymcespellchecker', 'permanentpen',
'powerpaste', 'advtable', 'advcode', 'editimage', 'advtemplate', 'mentions', 'tinycomments',
'tableofcontents', 'footnotes', 'mergetags', 'autocorrect', 'typography', 'inlinecss', 'markdown'
],
toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table mergetags | addcomment showcomments | spellcheckdialog a11ycheck typography | align lineheight | checklist numlist bullist indent outdent | emoticons charmap | code',
tinycomments_mode: 'embedded',
tinycomments_author: 'Author name',
mergetags_list: [
{ value: 'First.Name', title: 'First Name' },
{ value: 'Email', title: 'Email' }
],
images_upload_url: 'upload.php',
automatic_uploads: true,
images_reuse_filename: true,
file_picker_callback: function (callback, value, meta) {
if (meta.filetype === 'image') {
let input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.onchange = function () {
let file = this.files[0];
let formData = new FormData();
formData.append('file', file);
fetch('upload.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
callback(data.filePath, { alt: file.name });
} else {
alert('文件上传失败: ' + data.message);
}
})
.catch(error => {
console.error('错误:', error);
alert('上传文件出错。');
});
};
input.click();
}
},
images_upload_handler: function (blobInfo, success, failure) {
let formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
fetch('upload.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
success(data.filePath);
} else {
failure('文件上传失败: ' + data.message);
}
})
.catch(error => {
console.error('错误:', error);
failure('上传文件出错。');
});
},
content_style: "body { font-family: Arial, 'Microsoft YaHei', sans-serif; font-size: 14px; }"
});
</script>
</head>
<body>
<!-- side_bar start -->
<?php include 'container/side_bar.php'; ?>
<!-- side_bar end -->
<main class="dashboard-main">
<!-- Header start -->
<?php include 'container/header.php'; ?>
<div class="dashboard-main-body">
<div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">
<h6 class="fw-semibold mb-0">Manage AI Tool Products</h6>
<ul class="d-flex align-items-center gap-2">
<li class="fw-medium"><a href="index-2.html" class="d-flex align-items-center gap-1 hover-text-primary"><iconify-icon icon="solar:home-smile-angle-outline" class="icon text-lg"></iconify-icon>Dashboard</a></li>
<li>-</li>
<li class="fw-medium">Edit Product</li>
</ul>
</div>
<div class="row gy-4">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Edit Product</h5>
</div>
<div class="card-body">
<form action="" method="POST">
<div class="row gy-3">
<div class="col-6">
<label class="form-label">Select Category</label>
<select name="category_id" class="form-control" required>
<?php
if ($categories_result && mysqli_num_rows($categories_result) > 0) {
while ($category = mysqli_fetch_assoc($categories_result)) {
echo "<option value='" . $category['id'] . "'" . ($category['id'] == $product['category_id'] ? ' selected' : '') . ">" . htmlspecialchars($category['c_name']) . "</option>";
}
} else {
echo "<option value=''>No categories available</option>";
}
?>
</select>
</div>
<div class="col-6">
<label class="form-label">Tracking Link</label>
<input type="text" name="tracking_link" class="form-control" value="<?php echo htmlspecialchars($product['tracking_link']); ?>" required>
</div>
<!-- Product Icon -->
<div class="col-6">
<label class="form-label">Product Icon</label>
<input type="file" name="icon" id="icon" class="form-control" accept="image/*" onchange="uploadFile('icon', 'icon_preview')">
<img src="<?php echo htmlspecialchars($product['icon']); ?>" alt="Current Icon" class="img-thumbnail mt-2" width="100">
<div id="icon_preview" style="margin-top: 10px;"></div>
<input type="hidden" name="current_icon" value="<?php echo htmlspecialchars($product['icon']); ?>">
<input type="hidden" name="icon_link" id="icon_link" value="<?php echo htmlspecialchars($product['icon']); ?>">
</div>
<!-- Product Image -->
<div class="col-6">
<label class="form-label">Product Image</label>
<input type="file" name="image" id="image" class="form-control" accept="image/*" onchange="uploadFile('image', 'image_preview')">
<img src="<?php echo htmlspecialchars($product['image']); ?>" alt="Current Image" class="img-thumbnail mt-2" width="100">
<div id="image_preview" style="margin-top: 10px;"></div>
<input type="hidden" name="current_image" value="<?php echo htmlspecialchars($product['image']); ?>">
<input type="hidden" name="image_link" id="image_link" value="<?php echo htmlspecialchars($product['image']); ?>">
</div>
<div class="col-6">
<label class="form-label">EN Product Title</label>
<input type="text" name="title" class="form-control" value="<?php echo htmlspecialchars($product['title']); ?>" required>
</div>
<div class="col-6">
<label class="form-label">CN Product Title</label>
<input type="text" name="cn_title" class="form-control" value="<?php echo htmlspecialchars($product['cn_title']); ?>" required>
</div>
<div class="col-6">
<label class="form-label">EN Short Description</label>
<textarea name="short_description" class="form-control" rows="5" maxlength="200" required><?php echo htmlspecialchars($product['short_description']); ?></textarea>
</div>
<div class="col-6">
<label class="form-label">CN Short Description</label>
<textarea name="cn_short_description" class="form-control" rows="5" maxlength="200" required><?php echo htmlspecialchars($product['cn_short_description']); ?></textarea>
</div>
<div class="col-6">
<label class="form-label">EN Long Description (optional)</label>
<textarea name="long_description" id="long_description" class="form-control" rows="5"><?php echo htmlspecialchars($product['long_description']); ?></textarea>
</div>
<div class="col-6">
<label class="form-label">CN Long Description (optional)</label>
<textarea name="cn_long_description" id="cn_long_description" class="form-control" rows="5"><?php echo htmlspecialchars($product['cn_long_description']); ?></textarea>
</div>
<div class="col-12">
<button type="submit" name="update" class="btn btn-primary mt-3">Update Product</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include 'container/footer.php'; ?>
</main>
<!-- Footer Links Start -->
<?php include 'container/footer_links.php'; ?>
<script>
function uploadFile(inputId, previewId) {
const fileInput = document.getElementById(inputId);
const file = fileInput.files[0];
if (file) {
let formData = new FormData();
formData.append('file', file);
fetch('upload.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById(inputId + '_link').value = data.filePath;
const previewDiv = document.getElementById(previewId);
previewDiv.innerHTML = `<img src="${data.filePath}" alt="Uploaded Image" style="max-width: 100px;">`;
alert('File uploaded successfully!');
} else {
alert('File upload failed: ' + data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('Error uploading file.');
});
}
}
</script>
</body>
</html>
<?php
// Check if the form is submitted
if (isset($_POST['update'])) {
$id = intval($_GET['id']);
$category_id = intval($_POST['category_id']);
$title = mysqli_real_escape_string($conn, $_POST['title']);
$cn_title = mysqli_real_escape_string($conn, $_POST['cn_title']);
$tracking_link = mysqli_real_escape_string($conn, $_POST['tracking_link']);
$short_description = mysqli_real_escape_string($conn, $_POST['short_description']);
$cn_short_description = mysqli_real_escape_string($conn, $_POST['cn_short_description']);
$long_description = mysqli_real_escape_string($conn, $_POST['long_description']);
$cn_long_description = mysqli_real_escape_string($conn, $_POST['cn_long_description']);
// Use the uploaded file links
$icon_path = mysqli_real_escape_string($conn, $_POST['icon_link']);
$image_path = mysqli_real_escape_string($conn, $_POST['image_link']);
// Update the product in the database
$update_sql = "UPDATE products SET
category_id = '$category_id',
title = '$title',
cn_title = '$cn_title',
icon = '$icon_path',
image = '$image_path',
tracking_link = '$tracking_link',
short_description = '$short_description',
cn_short_description = '$cn_short_description',
long_description = '$long_description',
cn_long_description = '$cn_long_description'
WHERE id = $id";
if (mysqli_query($conn, $update_sql)) {
echo "<script>alert('Product updated successfully.'); window.location.href = 'product_edit.php?id=$id';</script>";
} else {
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'product_edit.php?id=$id&error';</script>";
}
}