Skip to content

Commit

Permalink
added postgres db connector
Browse files Browse the repository at this point in the history
  • Loading branch information
MIKEINTOSHSYSTEMS committed Aug 15, 2024
1 parent 786e499 commit bedd043
Show file tree
Hide file tree
Showing 10 changed files with 803 additions and 6 deletions.
97 changes: 97 additions & 0 deletions api/surveycto/ethprism.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

function fetchJSONData($url, $username, $password) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
if (curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}

function alterTable($conn, $tblName, $columns) {
foreach ($columns as $column) {
$alterQuery = "ALTER TABLE `$tblName` ADD COLUMN IF NOT EXISTS `$column` TEXT";
if ($conn->query($alterQuery) !== TRUE) {
echo "Error altering table: " . $conn->error . "<br>";
}
}
}

function JSON_to_table($json_var, $tblName = 'ethprism01') {
$j_obj = json_decode($json_var, true);

if (!$j_obj || !is_array($j_obj) || count($j_obj) === 0) {
die("Invalid JSON input or empty array");
}

// Establish a new connection using mysqli
$conn = new mysqli('localhost', 'surveyctoethprism', 'surveyctoethprism', 'surveyctoethprism');

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Extract column names from the first row of the JSON data
$firstRow = $j_obj[0];
$columns = array_keys($firstRow);
$columnDefinitions = implode(", ", array_map(function($col) {
return "`$col` TEXT";
}, $columns));

// Create table query if it doesn't exist
$createTableQuery = "CREATE TABLE IF NOT EXISTS `$tblName` ($columnDefinitions)";

if ($conn->query($createTableQuery) !== TRUE) {
die("Error creating table: " . $conn->error);
}

// Iterate over the JSON data to insert rows and handle new columns
foreach ($j_obj as $row) {
$keys = array_keys($row);

// Check for new columns and alter the table if needed
alterTable($conn, $tblName, $keys);

$columns = implode(", ", array_map(function($key) {
return "`$key`";
}, $keys));
$escaped_values = array_map(array($conn, 'real_escape_string'), array_values($row));
$values = "'" . implode("', '", $escaped_values) . "'";

// Use prepared statements for better security
$placeholders = implode(", ", array_fill(0, count($keys), '?'));
$stmt = $conn->prepare("INSERT INTO `$tblName` ($columns) VALUES ($placeholders)");

if ($stmt === false) {
echo "Error preparing statement: " . $conn->error;
continue;
}

$stmt->bind_param(str_repeat('s', count($escaped_values)), ...$escaped_values);

if (!$stmt->execute()) {
echo "Error executing statement: " . $stmt->error;
}

$stmt->close();
}

$conn->close();
}

// Example usage
$url = 'https://ethiopiaprism.surveycto.com/api/v2/forms/data/wide/json/dup_t_module_v_facilityoffice_checklist?date=2024&r=approved|rejected';
$username = '[email protected]';
$password = 'merq123';

$json_data = fetchJSONData($url, $username, $password);
JSON_to_table($json_data);

?>
87 changes: 87 additions & 0 deletions api/surveycto/ethprismcsv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

function fetchCSVData($url, $username, $password) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
if (curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}

function createTable($conn, $tblName, $columns) {
$columns = array_unique($columns); // Ensure column names are unique
$columnDefinitions = implode(", ", array_map(function($col) {
return "`$col` TEXT";
}, $columns));
$createTableQuery = "CREATE TABLE IF NOT EXISTS `$tblName` ($columnDefinitions) ENGINE=InnoDB ROW_FORMAT=DYNAMIC";
if ($conn->query($createTableQuery) !== TRUE) {
die("Error creating table: " . $conn->error);
}
}

function CSV_to_table($csv_data, $tblName = 'ethprismcsv') {
$lines = explode("\n", $csv_data);
$header = str_getcsv(array_shift($lines));

if (empty($header)) {
die("Invalid CSV input");
}

// Ensure column names are unique
$header = array_unique($header);

// Establish a new connection using mysqli
$conn = new mysqli('localhost', 'surveyctoethprism', 'surveyctoethprism', 'surveyctoethprism');

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create table with all columns as TEXT
createTable($conn, $tblName, $header);

// Iterate over the CSV data to insert rows
foreach ($lines as $line) {
if (empty(trim($line))) {
continue; // Skip empty lines
}

$row = str_getcsv($line);
$escaped_values = array_map(array($conn, 'real_escape_string'), $row);

$columns = implode(", ", array_map(function($col) {
return "`$col`";
}, $header));
$values = "'" . implode("', '", $escaped_values) . "'";

$sql = "INSERT INTO `$tblName` ($columns) VALUES ($values)";

if ($conn->query($sql) !== TRUE) {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();
}


// $url = 'https://ethiopiaprism.surveycto.com/api/v1/forms/data/wide/csv/dup_t_module_v_facilityoffice_checklist';
// $url = 'https://ethiopiaprism.surveycto.com/api/v1/forms/data/csv/dup_t_module_v_facilityoffice_checklist?date=2024&r=approved|rejected';
//$url = 'https://ethiopiaprism.surveycto.com/api/v1/forms/data/csv/dup_t_module_v_facilityoffice_checklist';
$url = 'https://ethiopiaprism.surveycto.com/api/v1/forms/data/csv/dup_t_module_v_facilityoffice_checklist';
$username = '[email protected]';
$password = 'merq123';


$csv_data = fetchCSVData($url, $username, $password);
CSV_to_table($csv_data);

?>

3 changes: 3 additions & 0 deletions api/surveycto/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
echo phpinfo();
?>
12 changes: 12 additions & 0 deletions app/connections/apis.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ protected function _setConnectionsData()
$data["password"] = "district";

$connectionsData["devdhis2hispmdathispmdmerqcons"] = $data;
$data = array();
$data["connId"] = "surveyctohispmdathispmdmerqcon";
$data["connName"] = "SURVEYCTO_ETHIOPIA_PRISM_API";

$this->_connectionsIdByName["SURVEYCTO_ETHIOPIA_PRISM_API"] = "surveyctohispmdathispmdmerqcon";

$data["url"] = "https://ethiopiaprism.surveycto.com";
$data["authType"] = "basic";
$data["username"] = "fasil.merqconsultancy.org";
$data["password"] = "merq123";

$connectionsData["surveyctohispmdathispmdmerqcon"] = $data;
$this->_connectionsData = &$connectionsData;
}

Expand Down
4 changes: 2 additions & 2 deletions app/include/appsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@
$strLastSQL = "";
$showCustomMarkerOnPrint = false;

$projectBuildKey = "468_1723305847";
$projectBuildKey = "469_1723305847";
$wizardBuildKey = "39558";
$projectBuildNumber = "468";
$projectBuildNumber = "469";

$mlang_messages = array();
$mlang_charsets = array();
Expand Down
2 changes: 1 addition & 1 deletion app/include/pages/.global_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@
<div class="col-lg-6 d-flex flex-column justify-content-center aos-init aos-animate" data-aos="fade-in">
<h1>Welcome to <span>HISPMD</span></h1>
<p>Health Information Management Information Systems Dashboard</p>
<p>Health Information Systems Performance Monitoring Dashboard</p>
<div class="d-flex">
Expand Down
2 changes: 1 addition & 1 deletion app/pdf/.global_menu.json

Large diffs are not rendered by default.

Loading

0 comments on commit bedd043

Please sign in to comment.