|
@@ -0,0 +1,175 @@
|
|
1
|
+<?php
|
|
2
|
+/* Authors : Carlos C. Corrada-Bravo
|
|
3
|
+ David J. Ortiz-Rivera
|
|
4
|
+
|
|
5
|
+ Organization : Centro de Desarrollo y Consultoria Computacional
|
|
6
|
+ Project : OPASO Material Registry
|
|
7
|
+ File : extract.php
|
|
8
|
+ Description : Extract and insert data from tsv. */
|
|
9
|
+
|
|
10
|
+ /* Import database connection & display errors */
|
|
11
|
+ include_once "config.php";
|
|
12
|
+ error_reporting(E_ALL);
|
|
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. */
|
|
17
|
+ $laboratory = array();
|
|
18
|
+ $pi = array();
|
|
19
|
+ $cho = array();
|
|
20
|
+ $register = array();
|
|
21
|
+ /* Initiate some values. */
|
|
22
|
+ $success = 0;
|
|
23
|
+ $null = "n/a";
|
|
24
|
+ $row = 1; /* Start at row 1. */
|
|
25
|
+ /* Parse tsv file */
|
|
26
|
+ if(($fhandle = fopen($file,"r")) !== FALSE){
|
|
27
|
+ while(($data = fgetcsv($fhandle,1000,"\t")) !== FALSE){
|
|
28
|
+ /* Extract department and personnel in columns B(1) and I(8) */
|
|
29
|
+ if($row > 6 and $row < 12){
|
|
30
|
+ /* Avoid null keys. */
|
|
31
|
+ if($data[0]){
|
|
32
|
+ $laboratory[$data[0]] = $data[1];
|
|
33
|
+ }
|
|
34
|
+ if($row < 10){
|
|
35
|
+ $pi[$data[7]] = $data[8];
|
|
36
|
+ }
|
|
37
|
+ else{
|
|
38
|
+ $cho[$data[7]] = $data[8];
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+ /* Extract cho phone. */
|
|
42
|
+ elseif($row == 12){
|
|
43
|
+ $cho[$data[7]] = $data[8];
|
|
44
|
+ }
|
|
45
|
+ /* Insert laboratory/personnel info and generate register keys. */
|
|
46
|
+ elseif($row == 14){
|
|
47
|
+ for($index=0; $index < count($data); $index++){
|
|
48
|
+ /* Initiate keys with null values. */
|
|
49
|
+ if($data[$index]){
|
|
50
|
+ $register[$data[$index]] = "";
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ foreach($pi as $key=>&$value){
|
|
54
|
+ if(!$value){
|
|
55
|
+ $value = $null;
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+ foreach($cho as $key=>&$value){
|
|
59
|
+ if(!$value){
|
|
60
|
+ $value = $null;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ /* person entries.
|
|
64
|
+ Bind and insert pi data. */
|
|
65
|
+ $person = $db->stmt_init();
|
|
66
|
+ 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);
|
|
68
|
+ if($person->execute()){
|
|
69
|
+ $pi = $db->insert_id; /* Extract generated pid. */
|
|
70
|
+ }
|
|
71
|
+ /* In case of duplicate entry. */
|
|
72
|
+ else{
|
|
73
|
+ $email = $pi['piemail'];
|
|
74
|
+ /* Extract id by email. */
|
|
75
|
+ if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
|
|
76
|
+ /* Bind selector and execute. */
|
|
77
|
+ $person->bind_param("s",$email);
|
|
78
|
+ if($person->execute()){
|
|
79
|
+ /* Bind result variables and fetch. */
|
|
80
|
+ $person->bind_result($pi);
|
|
81
|
+ $person->fetch();
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ /* Bind and insert cho data. */
|
|
87
|
+ $person = $db->stmt_init();
|
|
88
|
+ 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
|
+ if($person->execute()){
|
|
91
|
+ $cho = $db->insert_id; /* Extract generated pid. */
|
|
92
|
+ }
|
|
93
|
+ /* In case of duplicate entry. */
|
|
94
|
+ else{
|
|
95
|
+ $email = $cho['choemail'];
|
|
96
|
+ /* Extract id by email. */
|
|
97
|
+ if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
|
|
98
|
+ /* Bind selector and execute. */
|
|
99
|
+ $person->bind_param("s",$email);
|
|
100
|
+ if($person->execute()){
|
|
101
|
+ /* Bind result variables and fetch. */
|
|
102
|
+ $person->bind_result($cho);
|
|
103
|
+ $person->fetch();
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ $person->close();
|
|
109
|
+ /* lab entry
|
|
110
|
+ Bind and insert lab data. */
|
|
111
|
+ $lab = $db->stmt_init();
|
|
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);
|
|
114
|
+ $lab->execute();
|
|
115
|
+ $lab->close();
|
|
116
|
+ $lab = $db->insert_id; /* Extract generated lid. */
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ /* Extract register data by row. */
|
|
120
|
+ elseif($row > 15){
|
|
121
|
+ $d = 0; /* Reset index. */
|
|
122
|
+ foreach($register as $key=>&$value){
|
|
123
|
+ /* Replace null entries with identifiable string. */
|
|
124
|
+ if($data[$d]){
|
|
125
|
+ $value = $data[$d];
|
|
126
|
+ }
|
|
127
|
+ else{
|
|
128
|
+ $value = $null;
|
|
129
|
+ }
|
|
130
|
+ $d++;
|
|
131
|
+ /* Avoid segmentation fault. */
|
|
132
|
+ if($d >= count($register)){
|
|
133
|
+ break;
|
|
134
|
+ }
|
|
135
|
+ }
|
|
136
|
+ /* inventory entries
|
|
137
|
+ Bind and insert inventory data. */
|
|
138
|
+ $inventory = $db->stmt_init();
|
|
139
|
+ if($register['chemical'] != "null entry"){
|
|
140
|
+ 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. */
|
|
143
|
+ if(!($inventory->execute())){
|
|
144
|
+ echo "Entry error:";
|
|
145
|
+ echo "<br>";
|
|
146
|
+ print_r($inventory);
|
|
147
|
+ echo "<br>";
|
|
148
|
+ print_r($register);
|
|
149
|
+ echo "<br>";
|
|
150
|
+ echo "PI id: " . $pi . "<br>";
|
|
151
|
+ echo "CHO id: " . $cho . "<br>";
|
|
152
|
+ echo "LAB id: " . $lab . "<br>";
|
|
153
|
+ }
|
|
154
|
+ /* Track number of successful entries. */
|
|
155
|
+ else{
|
|
156
|
+ $success++;
|
|
157
|
+ $inventory->close();
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+ $row++; /* Update row. */
|
|
163
|
+ }
|
|
164
|
+ /* Close all remaining connections/streams. */
|
|
165
|
+ fclose($fhandle);
|
|
166
|
+ echo "<br>FILE: " . $file . "<br>";
|
|
167
|
+ echo "PI id: " . $pi . "<br>";
|
|
168
|
+ echo "CHO id: " . $cho . "<br>";
|
|
169
|
+ echo "LAB id: " . $lab . "<br>";
|
|
170
|
+ echo $success . " successful entries." . "<br><br>";
|
|
171
|
+ }
|
|
172
|
+ }
|
|
173
|
+ /* Close db connection. */
|
|
174
|
+ $db->close();
|
|
175
|
+?>
|