Proyecto en colaboración con OPASO

extract.php 5.8KB

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