|
@@ -5,29 +5,29 @@
|
5
|
5
|
Organization : Centro de Desarrollo y Consultoria Computacional
|
6
|
6
|
Project : OPASO Material Registry
|
7
|
7
|
File : extract.php
|
8
|
|
- Description : Extract and insert data from tsv. */
|
|
8
|
+ Description : Extract and insert data from tsv */
|
9
|
9
|
|
10
|
|
- /* Import database connection & display errors */
|
11
|
|
- include_once "config.php";
|
|
10
|
+ /* database connection */
|
|
11
|
+ include_once "../config/database.php";
|
12
|
12
|
error_reporting(E_ALL);
|
13
|
13
|
ini_set("display_errors",1);
|
14
|
|
- /* For each file: open and explode each row by tabs */
|
15
|
|
- foreach (glob("../ndata/*.txt") as $file){
|
16
|
|
- /* Initiate laboratory/personnel/register dictionaries. */
|
|
14
|
+
|
|
15
|
+ /* for each file: open and explode each row by tabs */
|
|
16
|
+ foreach (glob("") as $file){
|
17
|
17
|
$laboratory = array();
|
18
|
18
|
$pi = array();
|
19
|
19
|
$cho = array();
|
20
|
20
|
$register = array();
|
21
|
|
- /* Initiate some values. */
|
22
|
21
|
$success = 0;
|
23
|
22
|
$null = "n/a";
|
24
|
|
- $row = 1; /* Start at row 1. */
|
|
23
|
+ $row = 1;
|
|
24
|
+
|
25
|
25
|
/* Parse tsv file */
|
26
|
26
|
if(($fhandle = fopen($file,"r")) !== FALSE){
|
27
|
27
|
while(($data = fgetcsv($fhandle,1000,"\t")) !== FALSE){
|
28
|
|
- /* Extract department and personnel in columns B(1) and I(8) */
|
|
28
|
+ /* extract department and personnel in columns B(1) and I(8) */
|
29
|
29
|
if($row > 6 and $row < 12){
|
30
|
|
- /* Avoid null keys. */
|
|
30
|
+ /* avoid null keys */
|
31
|
31
|
if($data[0]){
|
32
|
32
|
$laboratory[$data[0]] = $data[1];
|
33
|
33
|
}
|
|
@@ -38,67 +38,67 @@
|
38
|
38
|
$cho[$data[7]] = $data[8];
|
39
|
39
|
}
|
40
|
40
|
}
|
41
|
|
- /* Extract cho phone. */
|
|
41
|
+
|
|
42
|
+ /* cho phone */
|
42
|
43
|
elseif($row == 12){
|
43
|
44
|
$cho[$data[7]] = $data[8];
|
44
|
45
|
}
|
45
|
|
- /* Insert laboratory/personnel info and generate register keys. */
|
|
46
|
+
|
|
47
|
+ /* generate register keys */
|
46
|
48
|
elseif($row == 14){
|
47
|
49
|
for($index=0; $index < count($data); $index++){
|
48
|
|
- /* Initiate keys with null values. */
|
49
|
50
|
if($data[$index]){
|
50
|
51
|
$register[$data[$index]] = "";
|
51
|
52
|
}
|
52
|
53
|
}
|
|
54
|
+
|
53
|
55
|
foreach($pi as $key=>&$value){
|
54
|
56
|
if(!$value){
|
55
|
57
|
$value = $null;
|
56
|
58
|
}
|
57
|
59
|
}
|
|
60
|
+
|
58
|
61
|
foreach($cho as $key=>&$value){
|
59
|
62
|
if(!$value){
|
60
|
63
|
$value = $null;
|
61
|
64
|
}
|
62
|
65
|
}
|
63
|
|
- /* person entries.
|
64
|
|
- Bind and insert pi data. */
|
|
66
|
+
|
|
67
|
+ /* person entries */
|
65
|
68
|
$person = $db->stmt_init();
|
66
|
69
|
if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
|
67
|
|
- $person->bind_param("sssss",$pi['pi'],$pi['piemail'],$null,$pi['piphone'],$null);
|
|
70
|
+ $person->bind_param("sssss",$pi["pi"],$pi["piemail"],$null,$pi["piphone"],$null);
|
68
|
71
|
if($person->execute()){
|
69
|
|
- $pi = $db->insert_id; /* Extract generated pid. */
|
|
72
|
+ $pi = $db->insert_id;
|
70
|
73
|
}
|
71
|
|
- /* In case of duplicate entry. */
|
|
74
|
+ /* duplicate entry */
|
72
|
75
|
else{
|
73
|
76
|
$email = $pi['piemail'];
|
74
|
|
- /* Extract id by email. */
|
75
|
77
|
if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
|
76
|
|
- /* Bind selector and execute. */
|
77
|
78
|
$person->bind_param("s",$email);
|
78
|
79
|
if($person->execute()){
|
79
|
|
- /* Bind result variables and fetch. */
|
80
|
80
|
$person->bind_result($pi);
|
81
|
81
|
$person->fetch();
|
82
|
82
|
}
|
83
|
83
|
}
|
84
|
84
|
}
|
85
|
85
|
}
|
86
|
|
- /* Bind and insert cho data. */
|
|
86
|
+
|
|
87
|
+ /* cho */
|
87
|
88
|
$person = $db->stmt_init();
|
88
|
89
|
if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
|
89
|
|
- $person->bind_param("sssss",$cho['cho'],$cho['choemail'],$null,$cho['chophone'],$null);
|
|
90
|
+ $person->bind_param("sssss",$cho["cho"],$cho["choemail"],$null,$cho["chophone"],$null);
|
|
91
|
+
|
90
|
92
|
if($person->execute()){
|
91
|
|
- $cho = $db->insert_id; /* Extract generated pid. */
|
|
93
|
+ $cho = $db->insert_id;
|
92
|
94
|
}
|
93
|
|
- /* In case of duplicate entry. */
|
|
95
|
+
|
|
96
|
+ /* duplicate entry */
|
94
|
97
|
else{
|
95
|
98
|
$email = $cho['choemail'];
|
96
|
|
- /* Extract id by email. */
|
97
|
99
|
if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
|
98
|
|
- /* Bind selector and execute. */
|
99
|
100
|
$person->bind_param("s",$email);
|
100
|
101
|
if($person->execute()){
|
101
|
|
- /* Bind result variables and fetch. */
|
102
|
102
|
$person->bind_result($cho);
|
103
|
103
|
$person->fetch();
|
104
|
104
|
}
|
|
@@ -106,40 +106,45 @@
|
106
|
106
|
}
|
107
|
107
|
}
|
108
|
108
|
$person->close();
|
109
|
|
- /* lab entry
|
110
|
|
- Bind and insert lab data. */
|
|
109
|
+
|
|
110
|
+ /* lab entry */
|
111
|
111
|
$lab = $db->stmt_init();
|
112
|
112
|
if($lab = $db->prepare("INSERT INTO Laboratory(lname,lab,department,building,extension,pi,cho) VALUES (?,?,?,?,?,?,?)")){
|
113
|
|
- $lab->bind_param("sssssii",$laboratory['lname'],$laboratory['lab'],$laboratory['department'],$laboratory['building'],$laboratory['extension'],$pi,$cho);
|
|
113
|
+ $lab->bind_param("sssssii",$laboratory["lname"],$laboratory["lab"],$laboratory["department"],$laboratory["building"],$laboratory["extension"],$pi,$cho);
|
114
|
114
|
$lab->execute();
|
115
|
115
|
$lab->close();
|
116
|
|
- $lab = $db->insert_id; /* Extract generated lid. */
|
|
116
|
+ /* extract generated lab_id */
|
|
117
|
+ $lab_id = $db->insert_id;
|
117
|
118
|
}
|
118
|
119
|
}
|
119
|
|
- /* Extract register data by row. */
|
|
120
|
+
|
|
121
|
+ /* extract register data by row */
|
120
|
122
|
elseif($row > 15){
|
121
|
|
- $d = 0; /* Reset index. */
|
|
123
|
+ /* reset index */
|
|
124
|
+ $d = 0;
|
122
|
125
|
foreach($register as $key=>&$value){
|
123
|
|
- /* Replace null entries with identifiable string. */
|
124
|
126
|
if($data[$d]){
|
125
|
127
|
$value = $data[$d];
|
126
|
128
|
}
|
127
|
129
|
else{
|
128
|
130
|
$value = $null;
|
129
|
131
|
}
|
130
|
|
- $d++;
|
131
|
|
- /* Avoid segmentation fault. */
|
|
132
|
+
|
|
133
|
+ $d += 1;
|
|
134
|
+
|
|
135
|
+ /* avoid seg fault */
|
132
|
136
|
if($d >= count($register)){
|
133
|
137
|
break;
|
134
|
138
|
}
|
135
|
139
|
}
|
136
|
|
- /* inventory entries
|
137
|
|
- Bind and insert inventory data. */
|
|
140
|
+
|
|
141
|
+ /* inventory entries */
|
138
|
142
|
$inventory = $db->stmt_init();
|
139
|
143
|
if($register['chemical'] != "null entry"){
|
140
|
144
|
if($inventory = $db->prepare("INSERT INTO Inventory(chemical,manufacturer,sds,cas,state,hazard,type,amount,quantity,total,location,ghs,lid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")){
|
141
|
|
- $inventory->bind_param("ssssssssssssi",$register['chemical'],$register['manufacturer'],$register['sds'],$register['cas'],$register['state'],$register['hazard'],$register['type'],$register['amount'],$register['quantity'],$register['total'],$register['location'],$register['ghs'],$lab);
|
142
|
|
- /* Query failed, display error. */
|
|
145
|
+ $inventory->bind_param("ssssssssssssi",$register["chemical"],$register["manufacturer"],$register["sds"],$register["cas"],$register["state"],$register["hazard"],$register["type"],$register["amount"],$register["quantity"],$register["total"],$register["location"],$register["ghs"],$lab_id);
|
|
146
|
+
|
|
147
|
+ /* query failed */
|
143
|
148
|
if(!($inventory->execute())){
|
144
|
149
|
echo "Entry error:";
|
145
|
150
|
echo "<br>";
|
|
@@ -149,27 +154,30 @@
|
149
|
154
|
echo "<br>";
|
150
|
155
|
echo "PI id: " . $pi . "<br>";
|
151
|
156
|
echo "CHO id: " . $cho . "<br>";
|
152
|
|
- echo "LAB id: " . $lab . "<br>";
|
|
157
|
+ echo "LAB id: " . $lab_id . "<br>";
|
153
|
158
|
}
|
154
|
|
- /* Track number of successful entries. */
|
|
159
|
+
|
|
160
|
+ /* successful entries */
|
155
|
161
|
else{
|
156
|
|
- $success++;
|
|
162
|
+ $success += 1;
|
157
|
163
|
$inventory->close();
|
158
|
164
|
}
|
159
|
165
|
}
|
160
|
166
|
}
|
161
|
167
|
}
|
162
|
|
- $row++; /* Update row. */
|
|
168
|
+
|
|
169
|
+ /* update row */
|
|
170
|
+ $row += 1;
|
163
|
171
|
}
|
164
|
|
- /* Close all remaining connections/streams. */
|
|
172
|
+
|
165
|
173
|
fclose($fhandle);
|
166
|
174
|
echo "<br>FILE: " . $file . "<br>";
|
167
|
175
|
echo "PI id: " . $pi . "<br>";
|
168
|
176
|
echo "CHO id: " . $cho . "<br>";
|
169
|
|
- echo "LAB id: " . $lab . "<br>";
|
|
177
|
+ echo "LAB id: " . $lab_id . "<br>";
|
170
|
178
|
echo $success . " successful entries." . "<br><br>";
|
171
|
179
|
}
|
172
|
180
|
}
|
173
|
|
- /* Close db connection. */
|
|
181
|
+
|
174
|
182
|
$db->close();
|
175
|
|
-?>
|
|
183
|
+?>
|