:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/public_html/crm/**
:: Editing File: portfolio_edit.php
<?php ob_start(); include 'header.php'; // ✅ Page setup for breadcrumb $page_parent = "Portfolio CRM"; $page_parent_link = "portfolio.php"; $page_title = "Edit Portfolio CRM"; include 'breadcrumb.php'; include 'menu.php'; $message = ""; // ✅ Get record by ID if (!isset($_GET['id']) || empty($_GET['id'])) { header("Location: portfolio.php?msg=Invalid Request"); exit; } $id = intval($_GET['id']); $result = mysqli_query($conn, "SELECT * FROM portfolio_photos WHERE id='$id'"); $portfolio = mysqli_fetch_assoc($result); if (!$portfolio) { header("Location: portfolio.php?msg=Portfolio not found"); exit; } // ✅ Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $work_section_id = mysqli_real_escape_string($conn, $_POST['work_section_id']); $status = mysqli_real_escape_string($conn, $_POST['status']); $old_image = mysqli_real_escape_string($conn, $_POST['old_image']); $video_link = isset($_POST['video_link']) ? mysqli_real_escape_string($conn, $_POST['video_link']) : ''; $website_link = isset($_POST['website_link']) ? mysqli_real_escape_string($conn, $_POST['website_link']) : ''; // ✅ Fetch section name for folder $sectionQuery = mysqli_query($conn, "SELECT section_name FROM work_sections WHERE id='$work_section_id'"); $sectionRow = mysqli_fetch_assoc($sectionQuery); $section_name = strtolower(trim($sectionRow['section_name'])); $folder_name = preg_replace('/[^a-z0-9]+/', '_', $section_name); // ✅ Define upload directory $uploadDir = "uploads/portfolio/$folder_name/"; if (!file_exists($uploadDir)) { mkdir($uploadDir, 0777, true); } $imageName = $old_image; // ✅ If not video production, handle image upload if ($section_name !== 'video production') { if (!empty($_FILES['image']['name'])) { $imageName = time() . "_" . basename($_FILES['image']['name']); $targetFile = $uploadDir . $imageName; if (move_uploaded_file($_FILES['image']['tmp_name'], $targetFile)) { // Delete old image file if exists $oldFilePath = "uploads/portfolio/$folder_name/$old_image"; if (file_exists($oldFilePath) && !empty($old_image)) { unlink($oldFilePath); } } else { $message = "<div class='alert alert-danger'>Error uploading image file.</div>"; } } $sql = "UPDATE portfolio_photos SET work_section_id='$work_section_id', status='$status', img_name='$imageName', video_link=NULL, website_link='$website_link' WHERE id='$id'"; } else { // ✅ For video production, update video link $sql = "UPDATE portfolio_photos SET work_section_id='$work_section_id', status='$status', video_link='$video_link', img_name=NULL, website_link='$website_link' WHERE id='$id'"; } if (mysqli_query($conn, $sql)) { $message = "<div class='alert alert-success'>Portfolio updated successfully!</div>"; $result = mysqli_query($conn, "SELECT * FROM portfolio_photos WHERE id='$id'"); $portfolio = mysqli_fetch_assoc($result); } else { $message = "<div class='alert alert-danger'>Database Error: " . mysqli_error($conn) . "</div>"; } } ?> <div class="page-body"> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <div class="card"> <div class="card-header"> <h2>Edit Portfolio</h2> </div> <div class="card-body"> <?= $message; ?> <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="old_image" value="<?= htmlspecialchars($portfolio['img_name']); ?>"> <div class="row"> <!-- Work Section --> <div class="col-md-6"> <label class="form-label">Work Section</label> <select name="work_section_id" id="work_section_id" class="form-select form-control" required> <option value="">-- Select Work Section --</option> <?php $sectionQuery = mysqli_query($conn, "SELECT * FROM work_sections ORDER BY section_name ASC"); while ($row = mysqli_fetch_assoc($sectionQuery)) { $selected = ($portfolio['work_section_id'] == $row['id']) ? 'selected' : ''; echo "<option value='{$row['id']}' $selected>{$row['section_name']}</option>"; } ?> </select> </div> <!-- Status --> <div class="col-md-6"> <label class="form-label">Status</label> <select name="status" class="form-select"> <option value="Active" <?= ($portfolio['status'] == 'Active') ? 'selected' : ''; ?>>Active</option> <option value="Inactive" <?= ($portfolio['status'] == 'Inactive') ? 'selected' : ''; ?>>Inactive</option> </select> </div> <!-- Website Link --> <div class="col-md-6"> <label class="form-label">Website Link</label> <input type="url" name="website_link" class="form-control" value="<?= htmlspecialchars($portfolio['website_link']); ?>" placeholder="https://example.com"> </div> <!-- Image Upload --> <div class="col-md-6" id="image_upload_section"> <label class="form-label">Image</label> <input type="file" name="image" id="image" class="form-control"> <?php $workSectionId = (int)$portfolio['work_section_id']; $sectionQuery = mysqli_query($conn, "SELECT section_name FROM work_sections WHERE id='$workSectionId'"); $sectionRow = mysqli_fetch_assoc($sectionQuery); $section_name = strtolower(trim($sectionRow['section_name'])); $folder_name = preg_replace('/[^a-z0-9]+/', '_', $section_name); $imagePath = "uploads/portfolio/$folder_name/" . $portfolio['img_name']; $serverImagePath = __DIR__ . "/uploads/portfolio/$folder_name/" . $portfolio['img_name']; ?> <?php if (!empty($portfolio['img_name']) && file_exists($serverImagePath)) { ?> <div class="mt-2"> <img src="<?= $imagePath; ?>" alt="Portfolio Image" width="120" style="border-radius:6px;"> </div> <?php } ?> </div> <!-- Video Link --> <div class="col-md-6" id="video_link_section" style="display: none;"> <label class="form-label">Video Link (YouTube/Vimeo)</label> <input type="text" name="video_link" id="video_link" class="form-control" value="<?= htmlspecialchars($portfolio['video_link']); ?>" placeholder="<iframe>...</iframe>"> <?php $iframe_html = $portfolio['video_link']; if (preg_match('/src="([^"]+)"/', $iframe_html, $matches)) { $video_url = $matches[1]; ?> <div class="mt-2"> <iframe src="<?= htmlspecialchars($video_url); ?>" width="560" height="315" frameborder="0" allowfullscreen></iframe> </div> <?php } ?> </div> </div> <br> <div class="text-start"> <button type="submit" class="btn btn-primary">Update Portfolio</button> <a href="portfolio.php" class="btn btn-secondary">Cancel</a> </div> </form> </div> </div> </div> </div> </div> </div> <!-- ✅ JS to toggle image/video input --> <script> function toggleFields() { var select = document.getElementById('work_section_id'); var selectedText = select.options[select.selectedIndex].text.toLowerCase(); if (selectedText.includes('video production')) { document.getElementById('image_upload_section').style.display = 'none'; document.getElementById('video_link_section').style.display = 'block'; document.getElementById('image').removeAttribute('required'); document.getElementById('video_link').setAttribute('required', 'required'); } else { document.getElementById('image_upload_section').style.display = 'block'; document.getElementById('video_link_section').style.display = 'none'; document.getElementById('image').removeAttribute('required'); document.getElementById('video_link').removeAttribute('required'); } } document.getElementById('work_section_id').addEventListener('change', toggleFields); window.onload = toggleFields; </script> <?php include 'footer.php'; ob_end_flush(); ?>