prepare("SELECT j.*, s.name as status_name, c.name as client_name FROM jobs j LEFT JOIN job_statuses s ON j.status_id = s.id LEFT JOIN clients c ON j.client_id = c.id WHERE j.id = ? AND j.company_id = ?"); $stmt->execute([$job_id, $company_id]); $job = $stmt->fetch(PDO::FETCH_ASSOC); if (!$job) { die("Job not found."); } // Ensure all required folders for this company are created for this job ensureRequiredJobFoldersExist($job_id, $company_id); // Handle POST requests for File & Folder Management if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; if ($action === 'toggle_approved') { $new_val = $job['works_approved'] ? 0 : 1; $stmt = db()->prepare("UPDATE jobs SET works_approved = ? WHERE id = ?"); $stmt->execute([$new_val, $job_id]); logActivity($job_id, 'works_approved_toggle', 'works_approved', $job['works_approved'] ? 'True' : 'False', $new_val ? 'True' : 'False'); header("Location: job_detail.php?id=" . $job_id); exit; } elseif ($action === 'add_folder') { $folder_name = trim($_POST['folder_name'] ?? ''); if (!empty($folder_name)) { $result = createJobFolder($job_id, $company_id, $folder_name, TRUE); if ($result['success']) { // Log activity (already handled inside createJobFolder via ensureRequiredJobFoldersExist) header("Location: job_detail.php?id=" . $job_id . "&message=Folder '" . urlencode($folder_name) . "' created successfully.&message_type=success"); exit; } else { $error = $result['message'] ?? 'Failed to create folder.'; } } else { $error = 'Folder name cannot be empty.'; } } elseif ($action === 'delete_folder') { $job_job_folder_id = (int)$_POST['job_job_folder_id']; $result = deleteJobFolder($job_job_folder_id, $job_id, $company_id); if ($result['success']) { header("Location: job_detail.php?id=" . $job_id . "&message=Folder deleted successfully.&message_type=success"); exit; } else { $error = $result['message'] ?? 'Failed to delete folder.'; } } elseif ($action === 'upload_file') { $job_job_folder_id = (int)$_POST['job_job_folder_id']; if (isset($_FILES['file_upload']) && $_FILES['file_upload']['error'] === UPLOAD_ERR_OK) { $file_tmp_path = $_FILES['file_upload']['tmp_name']; $file_name = basename($_FILES['file_upload']['name']); $file_size = $_FILES['file_upload']['size']; $file_type = $_FILES['file_upload']['type']; // Define upload directory: uploads/company_id/job_id/job_job_folder_id/ $upload_dir = __DIR__ . "/uploads/{$company_id}/{$job_id}/{$job_job_folder_id}/"; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0775, true); } $dest_path = $upload_dir . $file_name; if (move_uploaded_file($file_tmp_path, $dest_path)) { $result = addJobFile($job_id, $job_job_folder_id, $company_id, $user['id'], $file_name, $dest_path, $file_type, $file_size); if ($result['success']) { header("Location: job_detail.php?id=" . $job_id . "&message=File '" . urlencode($file_name) . "' uploaded successfully.&message_type=success"); exit; } else { // If DB insert fails, try to remove the uploaded file if (file_exists($dest_path)) { unlink($dest_path); } $error = 'Failed to record file in database.'; } } else { $error = 'Failed to move uploaded file.'; } } else { $error = 'File upload error or no file selected.'; } } elseif ($action === 'delete_file') { $file_id = (int)$_POST['file_id']; $result = deleteJobFile($file_id, $job_id, $company_id); if ($result['success']) { header("Location: job_detail.php?id=" . $job_id . "&message=File deleted successfully.&message_type=success"); exit; } else { $error = $result['message'] ?? 'Failed to delete file.'; } } } // Fetch Logs $logStmt = db()->prepare("SELECT * FROM activity_logs WHERE job_id = ? ORDER BY created_at DESC"); $logStmt->execute([$job_id]); $logs = $logStmt->fetchAll(PDO::FETCH_ASSOC); // Fetch all job folders (required and custom) $jobFolders = getJobFolders($job_id, $company_id); // Get messages from URL for display $message = $_GET['message'] ?? ''; $message_type = $_GET['message_type'] ?? ''; ?> Job Detail - <?php echo htmlspecialchars($job['job_ref']); ?>
Job Reference


Works Approved

Toggle this when the scope of work is confirmed.


Job Folders
( files) Required
( KB)
No files in this folder.
No folders set up for this job yet.
Activity Log
No activity yet.