-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.php
183 lines (159 loc) · 5.83 KB
/
controller.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Created by JetBrains PhpStorm.
* User: Peter Wegrzynek
* Date: 20.11.13
* Time: 15:19
* To change this template use File | Settings | File Templates.
*/
class controller {
// Hilfsfunktion für CRUD Operation, wird in CharManager.php verwendet
public static function createCategory($category) {
$null = NULL;
$sql = "INSERT INTO categories (
Category
) VALUES (
:category
)";
try{
$db = dbConnect::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(":category", $category);
$stmt->execute();
echo "update gut";
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
public static function createGroup($groupName) {
//$arr = array('ba' => $groupName);
//$arr = json_encode($groupName);
$anzahlElements = (count($groupName['group']));
$gruppenName = $groupName['group']['groupName'];
for($i = 0; $i < $anzahlElements - 1; $i++) {
$catName = ($groupName['group'][$i]);
$catId = controller::getIdFromCategoryName($catName);
$null = NULL;
$sql = "INSERT INTO categorygroup ( groupname , categoryname, category_id)
SELECT * FROM (SELECT :groupName, :catName, :catId) AS tmp
WHERE NOT EXISTS (
SELECT groupname , categoryname FROM categorygroup WHERE groupname = :groupName AND categoryname = :catName
) LIMIT 1;";
try{
$db = dbConnect::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(":groupName", $gruppenName);
$stmt->bindParam(":catName", $catName);
$stmt->bindParam(":catId", $catId);
$stmt->execute();
echo "update gut";
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
}
public static function getAllGroups() {
$sql = "SELECT * FROM categorygroup GROUP BY groupname";
try{
$dbh = dbConnect::getConnection();
$stmt = $dbh->query($sql);
$msg = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
echo '{"groups":'.json_encode($msg).'}';
} catch(PDOException $e) {
echo $e;
}
}
public static function showCategories(){
$sql = "SELECT * FROM categories";
try{
$dbh = dbConnect::getConnection();
$stmt = $dbh->query($sql);
$msg = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
echo '{"categories":'.json_encode($msg).'}';
} catch(PDOException $e) {
echo $e;
}
}
public static function getNewItemsFromJSON($json_object_input){
$json_object = json_decode( $json_object_input, true);
$anzahlItems = sizeof($json_object['entries']);
for($i = 0; $i<$anzahlItems; $i++) {
$amount = $json_object['entries'][$i]['value'];
$categoryName = $json_object['entries'][$i]['categoryName'];
$datum = $json_object['entries'][$i]['date'];
$user_id = 0;
if(!controller::isCategoryExistend($categoryName)){
controller::createCategory($categoryName);
}
$categoryId = controller::getIdFromCategoryName($categoryName);
$sql = "INSERT INTO entry (
category_id,
amount,
datum,
user_id
) VALUES (
:categoryId,
:amount,
:datum,
:userId
)";
try{
$db = dbConnect::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(":categoryId", $categoryId);
$stmt->bindParam(":amount", $amount);
$stmt->bindParam(":datum", $datum);
$stmt->bindParam(":userId", $user_id);
$stmt->execute();
echo "update gut";
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
}
public static function checkJsonError($json) {
json_decode($json);
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
/**
* @param $category
* returns true if categroy already in Database
* hilfsfunktion
*/
public static function isCategoryExistend($categoryName){
$sql = "SELECT * FROM categories WHERE Category LIKE '".$categoryName."'";
try{
$dbh = dbConnect::getConnection();
$stmt = $dbh->query($sql);
$msg = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
if(sizeof($msg) > 0)
return true;
else
return false;
} catch(PDOException $e) {
echo $e;
}
}
//Hilfsfunktion
public static function getIdFromCategoryName($categoryName){
$sql = "SELECT Id FROM categories WHERE Category LIKE '".$categoryName."'";
try{
$dbh = dbConnect::getConnection();
$stmt = $dbh->query($sql);
$msg = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
return $msg[0]->Id;
} catch(PDOException $e) {
echo $e;
}
}
}