forked from NYCPlanning/edm-metadata-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdd_upload.php
More file actions
113 lines (90 loc) · 3.49 KB
/
dd_upload.php
File metadata and controls
113 lines (90 loc) · 3.49 KB
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
<?php
// Data Dictionary File Upload
// Append button is pressed
if (isset($_POST['dd_submit_append'])) {
// Get the file
$tbname = $_GET['tbname'];
$id = $_GET['id'];
$file = $_FILES["file"]["tmp_name"];
// Get contents of the file
$file_contents = file_get_contents($file);
// Get the file extension
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// XML file upload
if($ext === 'xml') {
$xml = simplexml_load_file($file);
$results = $xml->xpath("//attr");
$order = 1;
foreach ($results as $result) {
$attrlabl = $result->attrlabl. " ";
$attrdef = $result->attrdef. " " .$result->attrdomv->udom. " ";
foreach($result->attrdomv->edom as $e) {
$edom .= $e->edomv . " - " . $e->edomvd . ", ";
}
$query = "INSERT INTO ".$tbname."(orders, field_name, description, expected_allowed_values) VALUES ('$order','$attrlabl','$attrdef','$edom')";
pg_query($query);
$order += 1;
$edom ="";
}
}
// CSV file upload
else if($ext === 'csv') {
if ($_FILES["file"]["size"] > 0) {
$handle = fopen($file,"r");
// reads the first line (the header) but doesn't output anything
fgets($handle);
while (($data = fgetcsv($handle,10000,",")) !== FALSE) {
if($flag) { $flag = false; continue; }
$query2 = "INSERT INTO ".$tbname."(orders, field_name, longform_name, description, geocoded, required, data_type, expected_allowed_values, last_modified_date, no_longer_in_use, notes) VALUES ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]')";
$res2 = pg_query($query2);
}
fclose($handle);
}
}
}
// Overwrite button is pressed
else if (isset($_POST['dd_submit_overwrite'])) {
$tbname = $_GET['tbname'];
$id = $_GET['id'];
$file = $_FILES["file"]["tmp_name"];
// Get contents of the file
$file_contents = file_get_contents($file);
// Get the file extension
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// XML file upload
if($ext === 'xml') {
$query2 = "DELETE FROM " .$tbname;
pg_query($query2);
$xml = simplexml_load_file($file);
$results = $xml->xpath("//attr");
$order = 1;
foreach ($results as $result) {
$attrlabl = $result->attrlabl. " ";
$attrdef = $result->attrdef. " " .$result->attrdomv->udom. " ";
foreach($result->attrdomv->edom as $e) {
$edom .= $e->edomv . " - " . $e->edomvd . ", ";
}
$query = "INSERT INTO ".$tbname."(orders, field_name, description, expected_allowed_values) VALUES ('$order','$attrlabl','$attrdef','$edom')";
pg_query($query);
$order += 1;
$edom ="";
}
}
// CSV file upload
else if($ext === 'csv') {
$query3 = "DELETE FROM " .$tbname;
pg_query($query3);
if ($_FILES["file"]["size"] > 0) {
$handle = fopen($file,"r");
// reads the first line (the header) but doesn't output anything
fgets($handle);
while (($data = fgetcsv($handle,10000,",")) !== FALSE) {
if($flag) { $flag = false; continue; }
$query4 = "INSERT INTO ".$tbname."(orders, field_name, longform_name, description, geocoded, required, data_type, expected_allowed_values, last_modified_date, no_longer_in_use, notes) VALUES ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]')";
$res4 = pg_query($query4);
}
fclose($handle);
}
}
}
?>