File size: 12,801 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
<?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();

// Check if domain and project ID are provided
if (!isset($_GET['domain']) || !isset($_GET['id'])) {
    header("Location: project_manage.php");
    exit;
}

$domainNumber = intval($_GET['domain']);
$projectId = intval($_GET['id']);
$domainTable = "domain_" . $domainNumber;

// Initialize variables
$success = '';
$error = '';
$project = null;

// Check if the domain table exists
$tableCheckQuery = "SHOW TABLES LIKE '$domainTable'";
$tableExists = $conn->query($tableCheckQuery)->num_rows > 0;

if (!$tableExists) {
    $error = "Domain table does not exist.";
} else {
    // Get the correct project name field for this domain
    $projectNameField = getProjectNameField($conn, $domainTable);
    
    // Get the columns of the domain table to determine field structure
    $columnsQuery = "SHOW COLUMNS FROM $domainTable";
    $columnsResult = $conn->query($columnsQuery);
    $columns = [];
    
    while ($column = $columnsResult->fetch_assoc()) {
        $columns[$column['Field']] = $column;
    }
    
    // Process form submission
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Validate form data
        $projectName = isset($_POST['project_name']) ? trim($_POST['project_name']) : '';
        $projectType = isset($_POST['project_type']) ? trim($_POST['project_type']) : '';
        $description = isset($_POST['description']) ? trim($_POST['description']) : '';
        $email = isset($_POST['email']) ? trim($_POST['email']) : '';
        $links = isset($_POST['links']) ? trim($_POST['links']) : '';
        
        // Additional fields (if any)
        $additionalFields = [];
        foreach ($_POST as $key => $value) {
            if (strpos($key, 'additional_') === 0) {
                $fieldName = substr($key, 11); // Remove 'additional_' prefix
                $additionalFields[$fieldName] = trim($value);
            }
        }
        
        // Basic validation
        if (empty($projectName) || empty($projectType)) {
            $error = "Please provide project name and project type.";
        } else {
            // Prepare the SQL for update
            $updates = [];
            $values = [];
            $types = '';
            
            // Add project name field
            $updates[] = "`$projectNameField` = ?";
            $values[] = $projectName;
            $types .= 's'; // string for Project Name
            
            // Add project type field (H/S)
            if (isset($columns['H/S'])) {
                $updates[] = "`H/S` = ?";
                $values[] = $projectType;
                $types .= 's'; // string for H/S
            }
            
            // Add description field
            if (isset($columns['Description'])) {
                $updates[] = "`Description` = ?";
                $values[] = $description;
                $types .= 's'; // string for Description
            }
            
            // Add email field
            if (isset($columns['Email'])) {
                $updates[] = "`Email` = ?";
                $values[] = $email;
                $types .= 's'; // string for Email
            }
            
            // Add links field
            if (isset($columns['Links'])) {
                $updates[] = "`Links` = ?";
                $values[] = $links;
                $types .= 's'; // string for Links
            }
            
            // Add additional fields
            foreach ($additionalFields as $field => $value) {
                if (isset($columns[$field])) {
                    $updates[] = "`$field` = ?";
                    $values[] = $value;
                    $types .= 's'; // string for additional fields
                }
            }
            
            // Add project ID for WHERE clause
            $values[] = $projectId;
            $types .= 'i'; // integer for Project_ID
            
            // Prepare and execute the update query
            $updateQuery = "UPDATE $domainTable SET " . implode(', ', $updates) . " WHERE Project_ID = ?";
            $stmt = $conn->prepare($updateQuery);
            
            if ($stmt) {
                // Dynamically bind parameters
                $bindParams = array_merge([$types], $values);
                $ref_params = [];
                
                foreach ($bindParams as $key => $value) {
                    $ref_params[$key] = &$bindParams[$key];
                }
                
                call_user_func_array([$stmt, 'bind_param'], $ref_params);
                
                if ($stmt->execute()) {
                    $success = "Project updated successfully.";
                    
                    // Refresh project data
                    $projectQuery = "SELECT * FROM $domainTable WHERE Project_ID = ? LIMIT 1";
                    $projectStmt = $conn->prepare($projectQuery);
                    $projectStmt->bind_param('i', $projectId);
                    $projectStmt->execute();
                    $projectResult = $projectStmt->get_result();
                    $project = $projectResult->fetch_assoc();
                    $projectStmt->close();
                } else {
                    $error = "Error updating project: " . $stmt->error;
                }
                
                $stmt->close();
            } else {
                $error = "Error preparing query: " . $conn->error;
            }
        }
    } else {
        // Fetch project details
        $projectQuery = "SELECT * FROM $domainTable WHERE Project_ID = ? LIMIT 1";
        $stmt = $conn->prepare($projectQuery);
        $stmt->bind_param('i', $projectId);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result && $result->num_rows > 0) {
            $project = $result->fetch_assoc();
        } else {
            $error = "Project not found.";
        }
        
        $stmt->close();
    }
}

// Get domain descriptions
$domainDescriptions = getDomainDescriptions($conn);
$domainDescription = isset($domainDescriptions[$domainNumber]) ? $domainDescriptions[$domainNumber] : "Domain $domainNumber";

// 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"><a href="project_manage.php">Manage Projects</a></li>

                <li class="breadcrumb-item active">Edit Project</li>

            </ol>

        </nav>

        <div class="d-flex justify-content-between align-items-center">

            <h2><i class="fas fa-edit me-2"></i> Edit Project</h2>

            <div>

                <a href="project_details.php?domain=<?php echo urlencode($domainNumber); ?>&id=<?php echo urlencode($projectId); ?>" class="btn btn-info">

                    <i class="fas fa-eye me-1"></i> View Project

                </a>

                <a href="project_manage.php" class="btn btn-secondary">

                    <i class="fas fa-arrow-left me-1"></i> Back to List

                </a>

            </div>

        </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; ?>



<?php if ($project): ?>

<div class="row">

    <div class="col-md-12">

        <div class="card shadow-sm">

            <div class="card-header bg-primary text-white">

                <h5 class="mb-0"><i class="fas fa-edit me-2"></i> Project Details</h5>

            </div>

            <div class="card-body">

                <form method="post" action="project_edit.php?domain=<?php echo urlencode($domainNumber); ?>&id=<?php echo urlencode($projectId); ?>" id="projectForm">

                    <div class="row">

                        <div class="col-md-6 mb-3">

                            <label class="form-label">Domain</label>

                            <input type="text" class="form-control" value="Domain <?php echo htmlspecialchars($domainNumber); ?>: <?php echo htmlspecialchars($domainDescription); ?>" disabled>

                        </div>

                        

                        <div class="col-md-6 mb-3">

                            <label for="project_type" class="form-label">Project Type *</label>

                            <select class="form-select" id="project_type" name="project_type" required>

                                <option value="">Select Type</option>

                                <option value="Hardware" <?php echo (isset($project['H/S']) && $project['H/S'] === 'Hardware') ? 'selected' : ''; ?>>Hardware</option>

                                <option value="Software" <?php echo (isset($project['H/S']) && $project['H/S'] === 'Software') ? 'selected' : ''; ?>>Software</option>

                            </select>

                        </div>

                    </div>

                    

                    <div class="mb-3">

                        <label for="project_name" class="form-label">Project Name *</label>

                        <input type="text" class="form-control" id="project_name" name="project_name" value="<?php echo htmlspecialchars($project[$projectNameField]); ?>" required>

                    </div>

                    

                    <div class="mb-3">

                        <label for="description" class="form-label">Description</label>

                        <textarea class="form-control" id="description" name="description" rows="4"><?php echo htmlspecialchars($project['Description'] ?? ''); ?></textarea>

                    </div>

                    

                    <div class="row">

                        <div class="col-md-6 mb-3">

                            <label for="email" class="form-label">Contact Email</label>

                            <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($project['Email'] ?? ''); ?>">

                        </div>

                        

                        <div class="col-md-6 mb-3">

                            <label for="links" class="form-label">Links (Comma separated)</label>

                            <input type="text" class="form-control" id="links" name="links" value="<?php echo htmlspecialchars($project['Links'] ?? ''); ?>" placeholder="https://example1.com, https://example2.com">

                        </div>

                    </div>

                    

                    <div id="additional_fields">

                        <?php foreach ($project as $field => $value): ?>

                            <?php if (!in_array($field, ['Project_ID', 'Domain_ID', $projectNameField, 'H/S', 'Description', 'Email', 'Links']) && !empty($field)): ?>

                                <div class="mb-3">

                                    <label for="additional_<?php echo htmlspecialchars($field); ?>" class="form-label"><?php echo htmlspecialchars($field); ?></label>

                                    <input type="text" class="form-control" id="additional_<?php echo htmlspecialchars($field); ?>" name="additional_<?php echo htmlspecialchars($field); ?>" value="<?php echo htmlspecialchars($value); ?>">

                                </div>

                            <?php endif; ?>

                        <?php endforeach; ?>

                    </div>

                    

                    <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">

                        <a href="project_manage.php" class="btn btn-secondary">

                            <i class="fas fa-times me-1"></i> Cancel

                        </a>

                        <button type="submit" class="btn btn-primary">

                            <i class="fas fa-save me-1"></i> Save Changes

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>

<?php endif; ?>



<?php

// Include footer

include 'includes/footer.php';



// Close connection

$conn->close();

?>