File size: 2,666 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
<?php
// 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();
?>