forked from chk1/eggtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
149 lines (122 loc) · 4.5 KB
/
export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
include("inc/config.inc.php");
#Übergabe der Verbindungsdaten
$dbconn = pg_connect("host=". $conf["db"]["host"] .
" port=". $conf["db"]["port"] .
" dbname=". $conf["db"]["db"] .
" user=". $conf["db"]["user"] .
" password=". $conf["db"]["pass"]);
if(!$dbconn) { die('<p>Die Datenbankverbindung konnte nicht hergestellt werden, bitte versuchen Sie es später noch einmal.</p>'); }
$abfrage = "SELECT cosmid FROM eggs";
$eggs = pg_query($dbconn, $abfrage);
$i = 0;
$res = array();
while ($helper = pg_fetch_assoc($eggs)){
$res[$i] = $helper;
$i++;
}
Foreach ($res as $k => $V) {
$res2 [$k] = $V ['cosmid'];
}
//test if Egg was chosen
if (empty($_POST["CosmID"])){
print "Bitte wählen Sie ein Ei aus, von dem Sie Daten exportieren möchten.";
print '</br>Zurück zum <a href="/eggtracker?action=export_form">Exportformular</a>.';
die();}
//test if measuring parameter was chosen
if(empty($_POST["Parameter"])){
print "Bitte wählen Sie einen Messparameter aus.";
print '</br>Zurück zum <a href="/eggtracker/?action=export_form">Exportformular</a>.';
die();}
//test if datapoints were chosen
if(empty($_POST["Wert"]["id"]) AND empty($_POST["Wert"]["time"]) AND empty($_POST["Wert"]["value"]) AND empty($_POST["Wert"]["validated"]) AND empty($_POST["Wert"]["outlier"])){
print "Bitte wählen Sie mindestens einen der Datenpunkte aus.";
print '</br>Zurück zum <a href="/eggtracker/?action=export_form">Exportformular</a>.';
die();}
//test if startdate was chosen
if($_POST["von"] =="Von (YYYY-MM-TT)"){
print "Bitte wählen Sie einen Anfangszeitpunkt aus.";
print '</br>Zurück zum <a href="/eggtracker/?action=export_form">Exportformular</a>.';
die();}
//test if enddate was chosen
if($_POST["bis"] =="Bis (YYYY-MM-TT) "){
print "Bitte wählen Sie einen Endzeitpunkt aus.";
print '</br>Zurück zum <a href="/eggtracker/?action=export_form">Exportformular</a>.';
die();}
//test if a exportformat was chosen
if(empty($_POST["format"])){
print "Bitte wählen Sie ein Exportformat aus.";
print '</br>Zurück zum <a href="/eggtracker/?action=export_form">Exportformular</a>.';
die();}
if (in_array ( $_POST["CosmID"], $res2) == true){
$ei = $_POST["CosmID"];
}
$wo = "";
#Abfrage der Radiobuttons aus dem Export-Formular
if($_POST["Parameter"] == 1)
$wo .= "o3";
if($_POST["Parameter"] == 2)
$wo .= "no2";
if($_POST["Parameter"] == 3)
$wo .= "co";
if($_POST["Parameter"] == 4)
$wo .= "temperature";
if($_POST["Parameter"] == 5)
$wo .= "humidity";
$was = "";
#Abfrage der Checkboxen aus dem Export-Formular
if(isset($_POST["Wert"]["id"]) && $_POST["Wert"]["id"] == 1)
$was .= "id, ";
if(isset($_POST["Wert"]["time"]) && $_POST["Wert"]["time"] == 1)
$was .= "time, ";
if(isset($_POST["Wert"]["value"]) && $_POST["Wert"]["value"] == 1)
$was .= "$wo, ";
if(isset($_POST["Wert"]["validated"]) && $_POST["Wert"]["validated"] == 1)
$was .= "validated, ";
if(isset($_POST["Wert"]["outlier"]) && $_POST["Wert"]["outlier"] == 1)
$was .= "outlier, ";
$was = rtrim ($was, ', ');
#Abfrage der Datumsfelder aus dem Export-Formular
$von = "'".$_POST['von']."'";
$bis = "'".$_POST['bis']."'";
$query_params = array($ei, $von, $bis);
$query = "SELECT $was FROM $wo NATURAL INNER JOIN eggs WHERE cosmid = $1 AND time BETWEEN $2 AND $3";
$result = pg_query_params($dbconn, $query, $query_params);
if($_POST["format"] == "xml") {
$doc = new DomDocument("1.0");
$doc->formatOutput = true;
$root = $doc->createElement('data');
$root = $doc->appendChild($root);
while($row = pg_fetch_assoc($result)){
$node = $doc->createElement('collection');
$node = $root->appendChild($node);
foreach($row as $fieldname => $fieldvalue){
$node->appendChild($doc->createElement($fieldname, $fieldvalue));
}
}
header("Content-type: application/xml");
header('Content-Disposition: attachment; filename="Egg-Values.xml"');
echo $doc->saveXML();
} elseif($_POST["format"] == "csv") {
header("Content-type: text/plain");
header('Content-Disposition: attachment; filename="Egg-Values.csv"');
echo $was.PHP_EOL;
while($row = pg_fetch_assoc($result)){
$keys = array_keys($row);
$last_key = end($keys);
foreach($row as $fieldname => $fieldvalue){
echo $fieldvalue;
if($fieldname != $last_key) echo ", ";
}
echo PHP_EOL;
}
} elseif($_POST["format"] == "json") {
header("Content-type: text/plain");
header('Content-Disposition: attachment; filename="Egg-Values.json"');
$data = array();
while($row = pg_fetch_assoc($result)){
$data[] = $row;
}
echo json_encode($data);
}
?>