isLoggedIn()) {
redirectTo('login.php');
}
$currentUser = $auth->getCurrentUser();
$db = Database::getInstance();
$error = '';
$success = '';
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
$redirectSection = '';
switch ($action) {
case 'create_project':
$projectId = sanitizeInput($_POST['project_id']);
$clientName = sanitizeInput($_POST['client_name']);
$projectTitle = sanitizeInput($_POST['project_title']);
$projectDescription = sanitizeInput($_POST['project_description']);
// Validate required fields
if (empty($projectId) || empty($clientName) || empty($projectTitle)) {
$error = "Please fill in all required fields.";
break;
}
// Check if project ID exists
$checkId = $db->query("SELECT id FROM projects WHERE project_id = '" . $db->escape($projectId) . "'");
if ($checkId && $checkId->num_rows > 0) {
$error = "Project ID already exists. Please use a different ID.";
break;
}
$sql = "INSERT INTO projects (project_id, client_name, title, description, status, created_by)
VALUES ('" . $db->escape($projectId) . "',
'" . $db->escape($clientName) . "',
'" . $db->escape($projectTitle) . "',
'" . $db->escape($projectDescription) . "',
'active',
" . (int)$_SESSION['user_id'] . ")";
if ($db->query($sql)) {
$success = "Project created successfully.";
} else {
$error = "Failed to create project: " . $db->getLastError();
}
break;
case 'update_project':
$id = (int)$_POST['id'];
$projectId = sanitizeInput($_POST['project_id']);
$clientName = sanitizeInput($_POST['client_name']);
$projectTitle = sanitizeInput($_POST['project_title']);
$projectDescription = sanitizeInput($_POST['project_description']);
// Validate required fields
if (empty($projectId) || empty($clientName) || empty($projectTitle)) {
$error = "Please fill in all required fields.";
break;
}
// Check if project ID exists for other projects
$checkId = $db->query("SELECT id FROM projects WHERE project_id = '" . $db->escape($projectId) . "' AND id != $id");
if ($checkId && $checkId->num_rows > 0) {
$error = "Project ID already exists. Please use a different ID.";
break;
}
$sql = "UPDATE projects SET
project_id = '" . $db->escape($projectId) . "',
client_name = '" . $db->escape($clientName) . "',
title = '" . $db->escape($projectTitle) . "',
description = '" . $db->escape($projectDescription) . "'
WHERE id = $id";
if ($db->query($sql)) {
$success = "Project updated successfully.";
} else {
$error = "Failed to update project: " . $db->getLastError();
}
break;
case 'delete_project':
$id = (int)$_POST['id'];
// Delete the project
if ($db->query("DELETE FROM projects WHERE id = $id")) {
$success = "Project deleted successfully.";
} else {
$error = "Failed to delete project: " . $db->getLastError();
}
break;
}
}
// Get all projects with connected surveys
$query = "SELECT p.*,
u.full_name as created_by_name,
DATE_FORMAT(p.created_at, '%b %d, %Y %H:%i') as formatted_date,
(SELECT COUNT(*) FROM project_surveys WHERE project_id = p.id) as connected_surveys
FROM projects p
JOIN users u ON p.created_by = u.id
ORDER BY p.created_at DESC";
$projects = $db->query($query);
?>
Projects |
| Project ID |
Client |
Title |
Description |
Connected Surveys |
Created By |
Created Date |
Actions |
num_rows > 0): ?>
fetch_assoc()): ?>
|
|
|
|
0) {
$survey_query = $db->query("
SELECT s.title
FROM project_surveys ps
JOIN surveys s ON ps.survey_id = s.id
WHERE ps.project_id = " . $project['id'] . "
LIMIT 1
");
if ($survey_query && $survey_query->num_rows > 0) {
$connected_survey = $survey_query->fetch_assoc()['title'];
}
}
if (!empty($connected_survey)): ?>
None
|
|
|
|
| No projects found. Click "Create Project" to add a new project. |
Connected Surveys
Available Surveys