123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /* Authors : Carlos C. Corrada-Bravo
- David J. Ortiz-Rivera
-
- Organization : Centro de Desarrollo y Consultoria Computacional
- Project : OPASO Material Registry
- File : extract.php
- Description : Extract and insert data from tsv */
-
- /* database connection */
- include_once "../config/database.php";
- error_reporting(E_ALL);
- ini_set("display_errors",1);
-
- /* for each file: open and explode each row by tabs */
- foreach (glob("") as $file){
- $laboratory = array();
- $pi = array();
- $cho = array();
- $register = array();
- $success = 0;
- $null = "n/a";
- $row = 1;
-
- /* Parse tsv file */
- if(($fhandle = fopen($file,"r")) !== FALSE){
- while(($data = fgetcsv($fhandle,1000,"\t")) !== FALSE){
- /* extract department and personnel in columns B(1) and I(8) */
- if($row > 6 and $row < 12){
- /* avoid null keys */
- if($data[0]){
- $laboratory[$data[0]] = $data[1];
- }
- if($row < 10){
- $pi[$data[7]] = $data[8];
- }
- else{
- $cho[$data[7]] = $data[8];
- }
- }
-
- /* cho phone */
- elseif($row == 12){
- $cho[$data[7]] = $data[8];
- }
-
- /* generate register keys */
- elseif($row == 14){
- for($index=0; $index < count($data); $index++){
- if($data[$index]){
- $register[$data[$index]] = "";
- }
- }
-
- foreach($pi as $key=>&$value){
- if(!$value){
- $value = $null;
- }
- }
-
- foreach($cho as $key=>&$value){
- if(!$value){
- $value = $null;
- }
- }
-
- /* person entries */
- $person = $db->stmt_init();
- if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
- $person->bind_param("sssss",$pi["pi"],$pi["piemail"],$null,$pi["piphone"],$null);
- if($person->execute()){
- $pi = $db->insert_id;
- }
- /* duplicate entry */
- else{
- $email = $pi['piemail'];
- if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
- $person->bind_param("s",$email);
- if($person->execute()){
- $person->bind_result($pi);
- $person->fetch();
- }
- }
- }
- }
-
- /* cho */
- $person = $db->stmt_init();
- if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
- $person->bind_param("sssss",$cho["cho"],$cho["choemail"],$null,$cho["chophone"],$null);
-
- if($person->execute()){
- $cho = $db->insert_id;
- }
-
- /* duplicate entry */
- else{
- $email = $cho['choemail'];
- if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
- $person->bind_param("s",$email);
- if($person->execute()){
- $person->bind_result($cho);
- $person->fetch();
- }
- }
- }
- }
- $person->close();
-
- /* lab entry */
- $lab = $db->stmt_init();
- if($lab = $db->prepare("INSERT INTO Laboratory(lname,lab,department,building,extension,pi,cho) VALUES (?,?,?,?,?,?,?)")){
- $lab->bind_param("sssssii",$laboratory["lname"],$laboratory["lab"],$laboratory["department"],$laboratory["building"],$laboratory["extension"],$pi,$cho);
- $lab->execute();
- $lab->close();
- /* extract generated lab_id */
- $lab_id = $db->insert_id;
- }
- }
-
- /* extract register data by row */
- elseif($row > 15){
- /* reset index */
- $d = 0;
- foreach($register as $key=>&$value){
- if($data[$d]){
- $value = $data[$d];
- }
- else{
- $value = $null;
- }
-
- $d += 1;
-
- /* avoid seg fault */
- if($d >= count($register)){
- break;
- }
- }
-
- /* inventory entries */
- $inventory = $db->stmt_init();
- if($register['chemical'] != "null entry"){
- if($inventory = $db->prepare("INSERT INTO Inventory(chemical,manufacturer,sds,cas,state,hazard,type,amount,quantity,total,location,ghs,lid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")){
- $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);
-
- /* query failed */
- if(!($inventory->execute())){
- echo "Entry error:";
- echo "<br>";
- print_r($inventory);
- echo "<br>";
- print_r($register);
- echo "<br>";
- echo "PI id: " . $pi . "<br>";
- echo "CHO id: " . $cho . "<br>";
- echo "LAB id: " . $lab_id . "<br>";
- }
-
- /* successful entries */
- else{
- $success += 1;
- $inventory->close();
- }
- }
- }
- }
-
- /* update row */
- $row += 1;
- }
-
- fclose($fhandle);
- echo "<br>FILE: " . $file . "<br>";
- echo "PI id: " . $pi . "<br>";
- echo "CHO id: " . $cho . "<br>";
- echo "LAB id: " . $lab_id . "<br>";
- echo $success . " successful entries." . "<br><br>";
- }
- }
-
- $db->close();
- ?>
|