forked from BernhardZuba/d3js-orgchart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPositions.php
More file actions
63 lines (53 loc) · 1.77 KB
/
getPositions.php
File metadata and controls
63 lines (53 loc) · 1.77 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
function db() {
// TODO: Replace these variables
$dsn = 'mysql:host=localhost;dbname=ORGANIGRAMM';
$username = 'ORGANIGRAMM_USER';
$password = 'PLEASE_FILL_ME_IN';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
return new PDO($dsn, $username, $password, $options);
}
function getChilds($dbconn, $id) {
$stmt = $dbconn->prepare("SELECT `id`, `desc` FROM `position` WHERE `parent_id`=?");
$stmt->execute(array($id));
return $stmt;
}
$dbconn = db();
$id = filter_input(INPUT_GET,"id", FILTER_VALIDATE_INT);
$stmt = getChilds($dbconn, $id);
if($id == 0) {
// Initial load: Get the first children too
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt2 = getChilds($dbconn, $row['id']);
$i_j = 0;
$childs = array();
while($childrow = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$stmt3 = getChilds($dbconn, $childrow['id']);
$hasChildRow = $stmt3->rowCount();
if($hasChildRow > 0) {
$hasChild = true;
} else {
$hasChild = false;
}
$childs[$i_j] = array("id" => $childrow['id'], "desc" => $childrow['desc'], "hasChild" => $hasChild);
$i_j++;
}
echo json_encode(array("id" => $row['id'], "desc" => $row['desc'], "children" => $childs));
} else {
// Just check if there are children
$i_i = 0;
$childs = array();
while($childrow = $stmt->fetch(PDO::FETCH_ASSOC)) {
$stmt3 = getChilds($dbconn, $childrow['id']);
$hasChildRow = $stmt3->rowCount();
if($hasChildRow > 0) {
$hasChild = true;
} else {
$hasChild = false;
}
$childs[$i_i] = array("id" => $childrow['id'], "desc" => $childrow['desc'], "hasChild" => $hasChild);
$i_i++;
}
echo json_encode(array("result" => $childs));
}