This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__API.php
150 lines (117 loc) · 2.92 KB
/
__API.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
149
150
<?php
include_once("notorm/NotORM.php");
///API.php
// $SQL =
// 'CREATE TABLE IF NOT EXISTS "dataset" ( '
// . 'id INTEGER PRIMARY KEY AUTOINCREMENT, '
// . 'name TEXT NOT NULL, '
// . 'CREATE TABLE IF NOT EXISTS "attribute" ( '
// . 'id INTEGER PRIMARY KEY AUTOINCREMENT, '
// . 'name TEXT NOT NULL, '
// . 'CREATE TABLE IF NOT EXISTS "data" ( '
// . 'id INTEGER PRIMARY KEY AUTOINCREMENT, '
// . 'dataset_id INTEGER NOT NULL, '
// . 'attribute_id INTEGER NOT NULL, '
// . 'value TEXT NOT NULL, '
// . 'time INTEGER NOT NULL); '
// ;
$pdo = new PDO("mysql:host=localhost;dbname=di", "root", "root");
$db = new NotORM($pdo);
$dataset = $db->{"dataset"};
$attribute = $db->{"attribute"};
$data = $db->{"data"};
$request = $_POST["request"];
$args = json_decode($_POST["args"]);
$result = array();
$result["data"] = array();
$result["status"] = "Unknown Condition";
switch ($request) {
case 'write':
write();
break;
case 'read':
read();
break;
}
echo json_encode($result);
function read()
{
// example args:
// {"set":31}
$set = $dataset[$args["set"]];
$data = $dataset->$data();
foreach ($data as $key => $value) {
$name = (string) $value->$attribute["name"];
$result["data"][] = array($name => $value[value]);
}
$result["status"] = "Data available";
}
function write(){
//JSON for example like
// {
// "one": "54",
// "two": "12",
// "three": "73",
// "four": "30",
// "five": "20"
// }
// or
// {"set": 1, "data":[
// "one": "54",
// "two": "12",
// "three": "73",
// "four": "30",
// "five": "20"]
// }
$newData = array();
if(isset($args["set"])){
$id = $args["set"];
$newData = $args["data"];
$setId = $dataset[$id];
}else {
$newData = $args;
$set = $dataset->insert_update();
$setId = (string) $set;
}
foreach ($newData as $key => $value) {
$attributeRow = $attribute->where("name", $key);
if(count($attributeRow) == 0){
$attributeRow = $attribute->insert_update(
array('name' => $key)
);
}else{
$attributeRow->fetch();
}
$attributeId = (string) $attributeRow;
$dataRow = $data->where(
array('dataset_id' => $setId,
'attribute_id' => $attributeId)
)->fetch();
$dataRow["value"] = $value;
$dataRow->update();
}
$result["status"] = "Data written";
}
// var api = "API.php";
// //for example:
// // apiRequest("read", {"set":1});
// function apiRequest (request, args) {
// var requestAddress = "";
// requestAddress = api_adrress
// + "?requestedApi=" + api
// + "&request=" + request
// var type = "post";
// var args = (typeof args != "undefined") ? args : "";
// console.debug("Ask[" + type + "]: " + requestAddress);
// $ajax = $.ajax({
// url : requestAddress,
// type : type,
// dataType: json,
// data : {"args": args}
// });
// $ajax.fail(function (args) {
// console.debug("request failed");
// console.debug(args);
// })
// return $ajax;
// }