123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?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. */
-
- /* Import database connection & display errors */
- include_once "config.php";
- error_reporting(E_ALL);
- ini_set("display_errors",1);
- /* For each file: open and explode each row by tabs */
- foreach (glob("../ndata/*.txt") as $file){
- /* Initiate laboratory/personnel/register dictionaries. */
- $laboratory = array();
- $pi = array();
- $cho = array();
- $register = array();
- /* Initiate some values. */
- $success = 0;
- $null = "n/a";
- $row = 1; /* Start at 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];
- }
- }
- /* Extract cho phone. */
- elseif($row == 12){
- $cho[$data[7]] = $data[8];
- }
- /* Insert laboratory/personnel info and generate register keys. */
- elseif($row == 14){
- for($index=0; $index < count($data); $index++){
- /* Initiate keys with null values. */
- 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.
- Bind and insert pi data. */
- $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; /* Extract generated pid. */
- }
- /* In case of duplicate entry. */
- else{
- $email = $pi['piemail'];
- /* Extract id by email. */
- if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
- /* Bind selector and execute. */
- $person->bind_param("s",$email);
- if($person->execute()){
- /* Bind result variables and fetch. */
- $person->bind_result($pi);
- $person->fetch();
- }
- }
- }
- }
- /* Bind and insert cho data. */
- $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; /* Extract generated pid. */
- }
- /* In case of duplicate entry. */
- else{
- $email = $cho['choemail'];
- /* Extract id by email. */
- if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
- /* Bind selector and execute. */
- $person->bind_param("s",$email);
- if($person->execute()){
- /* Bind result variables and fetch. */
- $person->bind_result($cho);
- $person->fetch();
- }
- }
- }
- }
- $person->close();
- /* lab entry
- Bind and insert lab data. */
- $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();
- $lab = $db->insert_id; /* Extract generated lid. */
- }
- }
- /* Extract register data by row. */
- elseif($row > 15){
- $d = 0; /* Reset index. */
- foreach($register as $key=>&$value){
- /* Replace null entries with identifiable string. */
- if($data[$d]){
- $value = $data[$d];
- }
- else{
- $value = $null;
- }
- $d++;
- /* Avoid segmentation fault. */
- if($d >= count($register)){
- break;
- }
- }
- /* inventory entries
- Bind and insert inventory data. */
- $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);
- /* Query failed, display error. */
- 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 . "<br>";
- }
- /* Track number of successful entries. */
- else{
- $success++;
- $inventory->close();
- }
- }
- }
- }
- $row++; /* Update row. */
- }
- /* Close all remaining connections/streams. */
- fclose($fhandle);
- echo "<br>FILE: " . $file . "<br>";
- echo "PI id: " . $pi . "<br>";
- echo "CHO id: " . $cho . "<br>";
- echo "LAB id: " . $lab . "<br>";
- echo $success . " successful entries." . "<br><br>";
- }
- }
- /* Close db connection. */
- $db->close();
- ?>
|