Proyecto en colaboración con OPASO

extract.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* database connection */
  9. include_once "../config/database.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("") as $file){
  14. $laboratory = array();
  15. $pi = array();
  16. $cho = array();
  17. $register = array();
  18. $success = 0;
  19. $null = "n/a";
  20. $row = 1;
  21. /* Parse tsv file */
  22. if(($fhandle = fopen($file,"r")) !== FALSE){
  23. while(($data = fgetcsv($fhandle,1000,"\t")) !== FALSE){
  24. /* extract department and personnel in columns B(1) and I(8) */
  25. if($row > 6 and $row < 12){
  26. /* avoid null keys */
  27. if($data[0]){
  28. $laboratory[$data[0]] = $data[1];
  29. }
  30. if($row < 10){
  31. $pi[$data[7]] = $data[8];
  32. }
  33. else{
  34. $cho[$data[7]] = $data[8];
  35. }
  36. }
  37. /* cho phone */
  38. elseif($row == 12){
  39. $cho[$data[7]] = $data[8];
  40. }
  41. /* generate register keys */
  42. elseif($row == 14){
  43. for($index=0; $index < count($data); $index++){
  44. if($data[$index]){
  45. $register[$data[$index]] = "";
  46. }
  47. }
  48. foreach($pi as $key=>&$value){
  49. if(!$value){
  50. $value = $null;
  51. }
  52. }
  53. foreach($cho as $key=>&$value){
  54. if(!$value){
  55. $value = $null;
  56. }
  57. }
  58. /* person entries */
  59. $person = $db->stmt_init();
  60. if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
  61. $person->bind_param("sssss",$pi["pi"],$pi["piemail"],$null,$pi["piphone"],$null);
  62. if($person->execute()){
  63. $pi = $db->insert_id;
  64. }
  65. /* duplicate entry */
  66. else{
  67. $email = $pi['piemail'];
  68. if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
  69. $person->bind_param("s",$email);
  70. if($person->execute()){
  71. $person->bind_result($pi);
  72. $person->fetch();
  73. }
  74. }
  75. }
  76. }
  77. /* cho */
  78. $person = $db->stmt_init();
  79. if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
  80. $person->bind_param("sssss",$cho["cho"],$cho["choemail"],$null,$cho["chophone"],$null);
  81. if($person->execute()){
  82. $cho = $db->insert_id;
  83. }
  84. /* duplicate entry */
  85. else{
  86. $email = $cho['choemail'];
  87. if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
  88. $person->bind_param("s",$email);
  89. if($person->execute()){
  90. $person->bind_result($cho);
  91. $person->fetch();
  92. }
  93. }
  94. }
  95. }
  96. $person->close();
  97. /* lab entry */
  98. $lab = $db->stmt_init();
  99. if($lab = $db->prepare("INSERT INTO Laboratory(lname,lab,department,building,extension,pi,cho) VALUES (?,?,?,?,?,?,?)")){
  100. $lab->bind_param("sssssii",$laboratory["lname"],$laboratory["lab"],$laboratory["department"],$laboratory["building"],$laboratory["extension"],$pi,$cho);
  101. $lab->execute();
  102. $lab->close();
  103. /* extract generated lab_id */
  104. $lab_id = $db->insert_id;
  105. }
  106. }
  107. /* extract register data by row */
  108. elseif($row > 15){
  109. /* reset index */
  110. $d = 0;
  111. foreach($register as $key=>&$value){
  112. if($data[$d]){
  113. $value = $data[$d];
  114. }
  115. else{
  116. $value = $null;
  117. }
  118. $d += 1;
  119. /* avoid seg fault */
  120. if($d >= count($register)){
  121. break;
  122. }
  123. }
  124. /* inventory entries */
  125. $inventory = $db->stmt_init();
  126. if($register['chemical'] != "null entry"){
  127. if($inventory = $db->prepare("INSERT INTO Inventory(chemical,manufacturer,sds,cas,state,hazard,type,amount,quantity,total,location,ghs,lid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")){
  128. $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);
  129. /* query failed */
  130. if(!($inventory->execute())){
  131. echo "Entry error:";
  132. echo "<br>";
  133. print_r($inventory);
  134. echo "<br>";
  135. print_r($register);
  136. echo "<br>";
  137. echo "PI id: " . $pi . "<br>";
  138. echo "CHO id: " . $cho . "<br>";
  139. echo "LAB id: " . $lab_id . "<br>";
  140. }
  141. /* successful entries */
  142. else{
  143. $success += 1;
  144. $inventory->close();
  145. }
  146. }
  147. }
  148. }
  149. /* update row */
  150. $row += 1;
  151. }
  152. fclose($fhandle);
  153. echo "<br>FILE: " . $file . "<br>";
  154. echo "PI id: " . $pi . "<br>";
  155. echo "CHO id: " . $cho . "<br>";
  156. echo "LAB id: " . $lab_id . "<br>";
  157. echo $success . " successful entries." . "<br><br>";
  158. }
  159. }
  160. $db->close();
  161. ?>