File size: 14,321 Bytes
102fe5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?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();

?>