Project_Bank / project_manage.php
Kshitij2604's picture
Upload 30 files
102fe5c verified
<?php
// Include database connection and authentication
require_once 'includes/db_connect.php';
require_once 'includes/domain_utils.php';
require_once 'includes/auth_check.php';
// Ensure only faculty can access this page
requireFaculty();
// Initialize variables
$error = '';
$success = '';
$domain_tables = getDomainTables($conn);
// Check if project was deleted
if (isset($_GET['deleted']) && $_GET['deleted'] === '1' && isset($_GET['project'])) {
$success = "Project '" . htmlspecialchars($_GET['project']) . "' has been deleted successfully.";
}
// Handle delete request
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['domain']) && isset($_GET['id'])) {
$domainNumber = intval($_GET['domain']);
$projectId = intval($_GET['id']);
// Redirect to the delete confirmation page
header("Location: project_delete.php?domain=" . urlencode($domainNumber) . "&id=" . urlencode($projectId));
exit;
}
// Search and filtering
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$domain_filter = isset($_GET['domain']) ? intval($_GET['domain']) : 0;
$type_filter = isset($_GET['type']) ? trim($_GET['type']) : '';
// Pagination
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$per_page = 10;
$offset = ($page - 1) * $per_page;
// Get all projects across domains
$projects = [];
$total_count = 0;
foreach ($domain_tables as $domain_id => $domain_table) {
// Skip if domain filter is set and doesn't match
if ($domain_filter > 0 && $domain_id != $domain_filter) {
continue;
}
// Get project name field for this domain
$projectNameField = getProjectNameField($conn, $domain_table);
// Build the query
$query = "SELECT *, '$domain_id' as domain_number FROM $domain_table";
$where_clauses = [];
$params = [];
$types = '';
// Add search condition if search term is provided
if (!empty($search)) {
$where_clauses[] = "(`$projectNameField` LIKE ? OR `Description` LIKE ?)";
$search_param = "%$search%";
$params[] = $search_param;
$params[] = $search_param;
$types .= 'ss';
}
// Add type filter if provided
if (!empty($type_filter)) {
$where_clauses[] = "`H/S` = ?";
$params[] = $type_filter;
$types .= 's';
}
// Finalize the query
if (!empty($where_clauses)) {
$query .= " WHERE " . implode(' AND ', $where_clauses);
}
// First, get the total count for this domain
$count_query = str_replace("SELECT *, '$domain_id' as domain_number", "SELECT COUNT(*)", $query);
$count_stmt = $conn->prepare($count_query);
if ($count_stmt) {
if (!empty($params)) {
$count_stmt->bind_param($types, ...$params);
}
$count_stmt->execute();
$count_result = $count_stmt->get_result();
$count_row = $count_result->fetch_row();
$total_count += $count_row[0];
$count_stmt->close();
}
// Now get the actual projects
$query .= " ORDER BY Project_ID DESC";
$stmt = $conn->prepare($query);
if ($stmt) {
if (!empty($params)) {
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$row['domain_table'] = $domain_table;
$row['projectNameField'] = $projectNameField;
$projects[] = $row;
}
$stmt->close();
}
}
// If domain filter is active, we need to adjust pagination
$total_pages = ceil($total_count / $per_page);
// Sort projects by most recent (assuming Project_ID increases with newer projects)
usort($projects, function($a, $b) {
return $b['Project_ID'] - $a['Project_ID'];
});
// Apply pagination
$paginated_projects = array_slice($projects, $offset, $per_page);
// Get domain descriptions
$domainDescriptions = getDomainDescriptions($conn);
// Include header
include 'includes/header.php';
?>
<div class="row mb-4">
<div class="col-md-12">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.php">Home</a></li>
<li class="breadcrumb-item active">Manage Projects</li>
</ol>
</nav>
<div class="d-flex justify-content-between align-items-center">
<h2><i class="fas fa-tasks me-2"></i> Manage Projects</h2>
<a href="project_add.php" class="btn btn-primary">
<i class="fas fa-plus-circle me-1"></i> Add New Project
</a>
</div>
</div>
</div>
<?php if (!empty($success)): ?>
<div class="alert alert-success">
<i class="fas fa-check-circle me-2"></i> <?php echo $success; ?>
</div>
<?php endif; ?>
<?php if (!empty($error)): ?>
<div class="alert alert-danger">
<i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>
</div>
<?php endif; ?>
<div class="card shadow-sm mb-4">
<div class="card-header bg-light">
<h5 class="mb-0"><i class="fas fa-filter me-2"></i> Search and Filter</h5>
</div>
<div class="card-body">
<form method="get" action="project_manage.php" class="row g-3">
<div class="col-md-4">
<label for="search" class="form-label">Search</label>
<div class="input-group">
<input type="text" class="form-control" id="search" name="search"
placeholder="Search by name or description" value="<?php echo htmlspecialchars($search); ?>">
<button class="btn btn-outline-secondary" type="submit">
<i class="fas fa-search"></i>
</button>
</div>
</div>
<div class="col-md-4">
<label for="domain" class="form-label">Domain</label>
<select class="form-select" id="domain" name="domain">
<option value="0">All Domains</option>
<?php foreach ($domain_tables as $id => $table): ?>
<?php $desc = isset($domainDescriptions[$id]) ? $domainDescriptions[$id] : "Domain $id"; ?>
<option value="<?php echo $id; ?>" <?php echo ($domain_filter == $id) ? 'selected' : ''; ?>>
Domain <?php echo $id; ?>: <?php echo htmlspecialchars($desc); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<label for="type" class="form-label">Project Type</label>
<select class="form-select" id="type" name="type">
<option value="">All Types</option>
<option value="Hardware" <?php echo ($type_filter === 'Hardware') ? 'selected' : ''; ?>>Hardware</option>
<option value="Software" <?php echo ($type_filter === 'Software') ? 'selected' : ''; ?>>Software</option>
</select>
</div>
<div class="col-md-1 d-flex align-items-end">
<?php if (!empty($search) || $domain_filter > 0 || !empty($type_filter)): ?>
<a href="project_manage.php" class="btn btn-outline-secondary">
<i class="fas fa-times"></i> Clear
</a>
<?php endif; ?>
</div>
</form>
</div>
</div>
<div class="card shadow-sm">
<div class="card-header bg-light d-flex justify-content-between align-items-center">
<h5 class="mb-0"><i class="fas fa-clipboard-list me-2"></i> Projects</h5>
<span class="badge bg-primary rounded-pill"><?php echo $total_count; ?> Projects</span>
</div>
<div class="card-body">
<?php if (empty($paginated_projects)): ?>
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i> No projects found matching your criteria. Try different search parameters or <a href="project_add.php" class="alert-link">add a new project</a>.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover">
<thead class="table-light">
<tr>
<th>ID</th>
<th>Domain</th>
<th>Project Name</th>
<th>Type</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($paginated_projects as $project): ?>
<?php
$domain_id = $project['domain_number'];
$projectName = $project[$project['projectNameField']];
$domainDesc = isset($domainDescriptions[$domain_id]) ? $domainDescriptions[$domain_id] : "Domain $domain_id";
?>
<tr>
<td><?php echo $project['Project_ID']; ?></td>
<td>
<span class="badge bg-secondary">Domain <?php echo $domain_id; ?></span>
<small class="d-block text-muted"><?php echo htmlspecialchars($domainDesc); ?></small>
</td>
<td><?php echo htmlspecialchars($projectName); ?></td>
<td>
<?php if (isset($project['H/S'])): ?>
<?php if ($project['H/S'] === 'Hardware'): ?>
<span class="badge bg-danger">Hardware</span>
<?php elseif ($project['H/S'] === 'Software'): ?>
<span class="badge bg-success">Software</span>
<?php else: ?>
<span class="badge bg-secondary"><?php echo htmlspecialchars($project['H/S']); ?></span>
<?php endif; ?>
<?php else: ?>
<span class="badge bg-secondary">N/A</span>
<?php endif; ?>
</td>
<td>
<div class="btn-group btn-group-sm" role="group">
<a href="project_details.php?domain=<?php echo urlencode($domain_id); ?>&id=<?php echo urlencode($project['Project_ID']); ?>" class="btn btn-outline-primary" title="View Project">
<i class="fas fa-eye"></i>
</a>
<a href="project_edit.php?domain=<?php echo urlencode($domain_id); ?>&id=<?php echo urlencode($project['Project_ID']); ?>" class="btn btn-outline-success" title="Edit Project">
<i class="fas fa-edit"></i>
</a>
<a href="project_delete.php?domain=<?php echo urlencode($domain_id); ?>&id=<?php echo urlencode($project['Project_ID']); ?>" class="btn btn-outline-danger" title="Delete Project">
<i class="fas fa-trash-alt"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation" class="mt-4">
<ul class="pagination justify-content-center">
<li class="page-item <?php echo ($page <= 1) ? 'disabled' : ''; ?>">
<a class="page-link" href="<?php echo ($page <= 1) ? '#' : "project_manage.php?page=" . ($page - 1) . "&search=" . urlencode($search) . "&domain=" . urlencode($domain_filter) . "&type=" . urlencode($type_filter); ?>">
<i class="fas fa-chevron-left"></i> Previous
</a>
</li>
<?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
<li class="page-item <?php echo ($i == $page) ? 'active' : ''; ?>">
<a class="page-link" href="project_manage.php?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>&domain=<?php echo urlencode($domain_filter); ?>&type=<?php echo urlencode($type_filter); ?>">
<?php echo $i; ?>
</a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo ($page >= $total_pages) ? 'disabled' : ''; ?>">
<a class="page-link" href="<?php echo ($page >= $total_pages) ? '#' : "project_manage.php?page=" . ($page + 1) . "&search=" . urlencode($search) . "&domain=" . urlencode($domain_filter) . "&type=" . urlencode($type_filter); ?>">
Next <i class="fas fa-chevron-right"></i>
</a>
</li>
</ul>
</nav>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
<?php
// Include footer
include 'includes/footer.php';
// Close connection
$conn->close();
?>