Spaces:
Sleeping
Sleeping
| // Include database connection | |
| require_once '../includes/db_connect.php'; | |
| require_once '../includes/auth_check.php'; | |
| // Ensure only faculty can access this endpoint | |
| if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'faculty') { | |
| header('Content-Type: application/json'); | |
| echo json_encode(['error' => 'Unauthorized access']); | |
| exit; | |
| } | |
| // Set content type to JSON | |
| header('Content-Type: application/json'); | |
| // Check if domain parameter is provided | |
| if (!isset($_GET['domain']) || !is_numeric($_GET['domain'])) { | |
| echo json_encode(['error' => 'Invalid domain parameter']); | |
| exit; | |
| } | |
| $domainNumber = intval($_GET['domain']); | |
| $domainTable = "domain_" . $domainNumber; | |
| // Check if table exists | |
| $tableCheckQuery = "SHOW TABLES LIKE '$domainTable'"; | |
| $tableExists = $conn->query($tableCheckQuery)->num_rows > 0; | |
| if (!$tableExists) { | |
| echo json_encode(['error' => 'Domain table does not exist']); | |
| exit; | |
| } | |
| // Get columns of the domain table | |
| $columnsQuery = "SHOW COLUMNS FROM $domainTable"; | |
| $columnsResult = $conn->query($columnsQuery); | |
| $fields = []; | |
| if ($columnsResult) { | |
| // Process existing fields | |
| while ($column = $columnsResult->fetch_assoc()) { | |
| $fieldName = $column['Field']; | |
| // Skip common fields like Project_ID | |
| if (in_array($fieldName, ['Project_ID', 'Domain_ID'])) { | |
| continue; | |
| } | |
| // Process field type and attributes | |
| $fieldType = $column['Type']; | |
| $isRequired = strtoupper($column['Null']) === 'NO'; | |
| $defaultValue = $column['Default']; | |
| // Determine field type for form input | |
| $inputType = 'text'; // Default | |
| if (strpos($fieldType, 'text') !== false) { | |
| $inputType = 'textarea'; | |
| } else if (strpos($fieldType, 'enum') !== false) { | |
| $inputType = 'select'; | |
| // Extract options from enum | |
| preg_match_all("/'(.*?)'/", $fieldType, $matches); | |
| $options = $matches[1] ?? []; | |
| } | |
| // Add field to the response | |
| $fields[] = [ | |
| 'name' => $fieldName, | |
| 'type' => $inputType, | |
| 'options' => isset($options) ? $options : null, | |
| 'required' => $isRequired, | |
| 'default' => $defaultValue | |
| ]; | |
| // Clear options for next field | |
| unset($options); | |
| } | |
| echo json_encode(['success' => true, 'fields' => $fields]); | |
| } else { | |
| echo json_encode(['error' => 'Could not retrieve domain fields']); | |
| } | |
| // Close connection | |
| $conn->close(); | |