| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/bpkd/admin/ |
Upload File : |
<?php
$page_title = 'Manajemen Testimonial';
require_once 'config/database.php';
require_once '../includes/config.php'; // Load BASE_URL
require_once 'includes/header.php';
// Handle add
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) {
$name = sanitize($_POST['name']);
$position = sanitize($_POST['position']);
$company = sanitize($_POST['company']);
$content = sanitize($_POST['content']);
$rating = (int)$_POST['rating'];
$status = sanitize($_POST['status']);
// Handle photo upload
$photo = '';
if (isset($_FILES['photo']) && $_FILES['photo']['error'] == 0) {
$target_dir = dirname(__DIR__) . "/uploads/testimonials/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$image_name = time() . '_' . basename($_FILES['photo']['name']);
$target_file = $target_dir . $image_name;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target_file)) {
$photo = 'uploads/testimonials/' . $image_name;
}
}
$query = "INSERT INTO testimonials (name, position, company, content, photo, rating, status)
VALUES ('$name', '$position', '$company', '$content', '$photo', $rating, '$status')";
if ($conn->query($query)) {
redirect(BASE_URL . '/admin/testimonials.php?msg=added');
} else {
$error = "Error: " . $conn->error;
}
}
// Handle edit
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
$id = (int)$_POST['id'];
$name = sanitize($_POST['name']);
$position = sanitize($_POST['position']);
$company = sanitize($_POST['company']);
$content = sanitize($_POST['content']);
$rating = (int)$_POST['rating'];
$status = sanitize($_POST['status']);
$query = "UPDATE testimonials SET
name = '$name',
position = '$position',
company = '$company',
content = '$content',
rating = $rating,
status = '$status'";
// Handle photo upload
if (isset($_FILES['photo']) && $_FILES['photo']['error'] == 0) {
$target_dir = dirname(__DIR__) . "/uploads/testimonials/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$image_name = time() . '_' . basename($_FILES['photo']['name']);
$target_file = $target_dir . $image_name;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target_file)) {
$photo = 'uploads/testimonials/' . $image_name;
$query .= ", photo = '$photo'";
// Delete old photo
if (!empty($_POST['old_photo']) && file_exists("../" . $_POST['old_photo'])) {
unlink("../" . $_POST['old_photo']);
}
}
}
$query .= " WHERE id = $id";
if ($conn->query($query)) {
redirect(BASE_URL . '/admin/testimonials.php?msg=updated');
} else {
$error = "Error: " . $conn->error;
}
}
// Handle delete
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
// Get photo to delete
$result = $conn->query("SELECT photo FROM testimonials WHERE id = $id");
if ($result) {
$testi = $result->fetch_assoc();
if ($testi && $testi['photo'] && file_exists("../" . $testi['photo'])) {
unlink("../" . $testi['photo']);
}
}
$conn->query("DELETE FROM testimonials WHERE id = $id");
redirect(BASE_URL . '/admin/testimonials.php?msg=deleted');
}
// Get all testimonials
$testimonials = $conn->query("SELECT * FROM testimonials ORDER BY id DESC");
if (!$testimonials) {
die("Error in testimonials query: " . $conn->error);
}
// Get for edit
$edit_testi = null;
if (isset($_GET['edit'])) {
$id = (int)$_GET['edit'];
$result = $conn->query("SELECT * FROM testimonials WHERE id = $id");
if ($result) {
$edit_testi = $result->fetch_assoc();
}
}
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'added') $msg = '<div class="alert alert-success">Testimonial berhasil ditambahkan!</div>';
if ($_GET['msg'] == 'updated') $msg = '<div class="alert alert-success">Testimonial berhasil diupdate!</div>';
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">Testimonial berhasil dihapus!</div>';
}
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Manajemen Testimonial</h1>
</div>
<?php echo $msg; ?>
<div class="row">
<!-- Add/Edit Form -->
<div class="col-md-4">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">
<?php echo $edit_testi ? 'Edit Testimonial' : 'Tambah Testimonial Baru'; ?>
</h6>
</div>
<div class="card-body">
<form method="POST" enctype="multipart/form-data" action="<?php echo BASE_URL; ?>/admin/testimonials.php">
<?php if ($edit_testi): ?>
<input type="hidden" name="id" value="<?php echo $edit_testi['id']; ?>">
<input type="hidden" name="old_photo" value="<?php echo $edit_testi['photo']; ?>">
<?php endif; ?>
<div class="mb-3">
<label class="form-label">Nama <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="name" value="<?php echo $edit_testi['name'] ?? ''; ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Jabatan</label>
<input type="text" class="form-control" name="position" value="<?php echo $edit_testi['position'] ?? ''; ?>">
</div>
<div class="mb-3">
<label class="form-label">Perusahaan/Instansi</label>
<input type="text" class="form-control" name="company" value="<?php echo $edit_testi['company'] ?? ''; ?>">
</div>
<div class="mb-3">
<label class="form-label">Testimoni <span class="text-danger">*</span></label>
<textarea class="form-control" name="content" rows="4" required><?php echo $edit_testi['content'] ?? ''; ?></textarea>
</div>
<div class="mb-3">
<label class="form-label">Rating</label>
<select class="form-control" name="rating">
<?php for($i = 1; $i <= 5; $i++): ?>
<option value="<?php echo $i; ?>" <?php echo ($edit_testi['rating'] ?? 5) == $i ? 'selected' : ''; ?>>
<?php echo $i; ?> Bintang
</option>
<?php endfor; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Foto</label>
<?php if ($edit_testi && $edit_testi['photo']): ?>
<div class="mb-2">
<img src="<?php echo BASE_URL; ?>/admin/testimonials/<?php echo $edit_testi['photo']; ?>" style="max-height: 100px;" class="img-thumbnail">
</div>
<?php endif; ?>
<input type="file" class="form-control" name="photo" accept="image/*">
</div>
<div class="mb-3">
<label class="form-label">Status</label>
<select class="form-control" name="status">
<option value="published" <?php echo ($edit_testi['status'] ?? '') == 'published' ? 'selected' : ''; ?>>Published</option>
<option value="draft" <?php echo ($edit_testi['status'] ?? '') == 'draft' ? 'selected' : ''; ?>>Draft</option>
</select>
</div>
<button type="submit" name="<?php echo $edit_testi ? 'edit' : 'add'; ?>" class="btn btn-primary">
<i class="fas fa-save me-2"></i><?php echo $edit_testi ? 'Update' : 'Simpan'; ?>
</button>
<?php if ($edit_testi): ?>
<a href="<?php echo BASE_URL; ?>/admin/testimonials.php" class="btn btn-secondary">
<i class="fas fa-times me-2"></i>Batal
</a>
<?php endif; ?>
</form>
</div>
</div>
</div>
<!-- Testimonials List -->
<div class="col-md-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">Daftar Testimonial</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Foto</th>
<th>Nama</th>
<th>Jabatan</th>
<th>Testimoni</th>
<th>Rating</th>
<th>Status</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php while($testi = $testimonials->fetch_assoc()): ?>
<tr>
<td>
<?php if($testi['photo']): ?>
<img src="../<?php echo $testi['photo']; ?>" style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%;">
<?php else: ?>
<i class="fas fa-user-circle fa-2x text-muted"></i>
<?php endif; ?>
</td>
<td><strong><?php echo $testi['name']; ?></strong></td>
<td><?php echo $testi['position']; ?><?php echo $testi['company'] ? ' di ' . $testi['company'] : ''; ?></td>
<td><?php echo substr($testi['content'], 0, 50) . '...'; ?></td>
<td>
<?php for($i = 1; $i <= 5; $i++): ?>
<i class="fas fa-star <?php echo $i <= $testi['rating'] ? 'text-warning' : 'text-muted'; ?>"></i>
<?php endfor; ?>
</td>
<td>
<span class="badge bg-<?php echo $testi['status'] == 'published' ? 'success' : 'secondary'; ?>">
<?php echo $testi['status']; ?>
</span>
</td>
<td>
<a href="?edit=<?php echo $testi['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-edit"></i>
</a>
<a href="?delete=<?php echo $testi['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>