-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategories-ws.php
More file actions
141 lines (119 loc) · 3.36 KB
/
Copy pathcategories-ws.php
File metadata and controls
141 lines (119 loc) · 3.36 KB
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
<?php
//categories webservice
require('db_connection.php');
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
if($_GET == null){
get_all();
}
break;
case 'POST':
$data = json_decode(file_get_contents("php://input"));
if ($data->name != null){
add($data->name, $data->id_parent);
}
break;
case 'PUT':
$data = json_decode(file_get_contents("php://input"));
if ($data->id != null && $data->name != null){
modify($data->id, $data->name, $data->id_parent);
}
break;
case 'DELETE':
if (array_key_exists("id", $_GET)){
delete_by_id($_GET['id'], $_GET['id_parent']);
}
break;
default:
handle_error($request);
break;
}
function modify($id, $name, $id_parent){
if ($id_parent == 'null') {
$id_parent = null;
}
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("UPDATE categorie SET name=:name, id_parent=:id_parent WHERE id=:id;");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':id_parent', $id_parent);
$stmt->execute();
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function add($name, $id_parent){
if ($id_parent == 'null') {
$id_parent = null;
}
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("INSERT INTO categorie (name, id_parent) VALUES (:name, :id_parent)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':id_parent', $id_parent);
$stmt->execute();
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function delete_by_id($id, $id_parent){
if ($id_parent == 'null') {
$id_parent = null;
}
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("DELETE FROM note_categorie WHERE id_categorie = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$stmt = $conn->prepare("UPDATE categorie SET id_parent=:id_parent WHERE id_parent=:id;");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':id_parent', $id_parent);
$stmt->execute();
$stmt = $conn->prepare("DELETE FROM categorie WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
function get_all(){
try {
$dbconn = DbConnection::getConnection();
$conn = $dbconn->_pdo;
$stmt = $conn->prepare("SELECT * FROM categorie ORDER BY id_parent DESC");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
for ($i=0; $i < count($result); $i++) {
for ($j=0; $j < count($result); $j++) {
if ($result[$i]['id_parent'] == $result[$j]['id']){
if(!isset($result[$j]['children'])){
$result[$j]['children'][0] = $result[$i];
}else{
$result[$j]['children'][count($result[$j]['children'])] = $result[$i];
}
}
}
}
$res = array();
$j = 0;
for ($i=0; $i < count($result); $i++) {
if ($result[$i]['id_parent'] == null){
$res[$j]=$result[$i];
$j++;
}
}
echo json_encode($res);
$conn = null;
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
?>