Fullstack Portal Created by the HCMR for the Marine Strategy Framework Directive Program in order to cover demands and aspects considering extendability and maintainability
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.8 KiB

2 years ago
<?php
if (isset($_GET['db'])) {
$postgres_host = 'localhost';
$postgres_dbname = 'gisdata';
$postgres_dbtable = 'data_domains';
$postgres_user = 'postgres';
$postgres_password = 'postgres';
$sqlite_dbname = 'fielddomains.db';
$sqlite_table = 'data_domains';
if ($_GET['db'] == 'postgres') {
$dbconn = pg_connect("host=" . $postgres_host . " dbname=" . $postgres_dbname . " user=" . $postgres_user . " password=" . $postgres_password) or die('Could not connect: ' . pg_last_error());
$query = 'SELECT * FROM ' . $postgres_dbtable . ' ORDER BY domain_name ASC ';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$objects0 = json_encode(pg_fetch_all($result));
$objects = json_decode($objects0);
$grouped = array();
// Loop JSON objects
foreach ($objects as $object) {
if (!array_key_exists($object->domain_id, $grouped)) { // a new ID...
$newObject = new stdClass();
// Copy the ID/ID_NAME, and create an ITEMS placeholder
$newObject->_domain_id = $object->domain_id;
$newObject->_service_url = $object->service_url;
$newObject->_layer_name = $object->layer_name;
$newObject->_field_name = $object->field_name;
$newObject->_field_domain = array();
// Save this new object
$grouped[$object->domain_id] = $newObject;
}
$taskObject = new stdClass();
// Copy the TASK/TASK_NAME
$taskObject->name = $object->domain_name;
$taskObject->value = $object->domain_value;
// Append this new task to the ITEMS array
$grouped[$object->domain_id]->_field_domain[] = $taskObject;
}
// We use array_values() to remove the keys used to identify similar objects
// And then re-encode this data :)
$grouped = array_values($grouped);
$json = json_encode($grouped);
echo $json;
pg_free_result($result);
pg_close($dbconn);
}
if ($_GET['db'] == 'sqlite') {
$sqlite_db = new PDO('sqlite:' . $sqlite_dbname);
$objects = $sqlite_db->query("SELECT * FROM " . $sqlite_table . ' ORDER BY domain_name ASC ');
$grouped = array();
// Loop JSON objects
foreach ($objects as $object) {
if (!array_key_exists($object['domain_id'], $grouped)) { // a new ID...
$newObject = new stdClass();
// Copy the ID/ID_NAME, and create an ITEMS placeholder
$newObject->_domain_id = $object['domain_id'];
$newObject->_service_url = $object['service_url'];
$newObject->_layer_name = $object['layer_name'];
$newObject->_field_name = $object['field_name'];
$newObject->_field_domain = array();
// Save this new object
$grouped[$object['domain_id']] = $newObject;
}
$taskObject = new stdClass();
// Copy the TASK/TASK_NAME
$taskObject->name = $object['domain_name'];
$taskObject->value = $object['domain_value'];
// Append this new task to the ITEMS array
$grouped[$object['domain_id']]->_field_domain[] = $taskObject;
}
// We use array_values() to remove the keys used to identify similar objects
// And then re-encode this data :)
$grouped = array_values($grouped);
$json = json_encode($grouped);
echo $json;
// Close file db connection
$sqlite_db = null;
}
}