-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtreeModel.php
260 lines (232 loc) · 7.84 KB
/
treeModel.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
class Tree
{
private $server = 'localhost';
private $user = 'root';
private $pass = 'fire987';
private $db = 'familytree_db';
private function addNode($name, $parent_id, $spouse_id, $user_id)
{
$parent_id = $parent_id? $parent_id:"NULL";
$spouse_id = $spouse_id? $spouse_id:"NULL";
$user_id = $user_id? $user_id:"NULL";
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query = "INSERT into trees(name,spouse_id,parent_id, user_id) values('$name', $spouse_id, $parent_id, $user_id)";
mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$insert_id = mysql_insert_id();
mysql_close($con);
return $insert_id;
}
private function getParentId($child_id)
{
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query = "SELECT parent_id from trees where id=$child_id";
$result = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
mysql_close($con);
return $row['parent_id'];
}
public function isTopNode($node_id, &$treeName, &$treeId)
{
$parent_id = $this->getParentId($node_id);
if(is_null($parent_id))
return false;
//echo "parent id: ",$parent_id;
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query = "SELECT parent_id,name,id from trees where id=$parent_id";
$result = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
mysql_close($con);
$treeName = $row['name'];
$treeId = $row['id'];
return is_null($row['parent_id']);
}
public function getNodeInfo($node_id, $con)
{
// Performing SQL query
$query = "SELECT n.id as id, n.user_id as owner_id, n.name as name, s.name as spouse from trees as n left join trees as s on n.spouse_id = s.id where n.id=$node_id";
$result = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
return $row;
}
public function getNodeChildren($node_id, $con)
{
// Performing SQL query
$query = "SELECT id from trees where parent_id=$node_id";
$result = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$children = array();
while($row = mysql_fetch_array($result))
{
array_push($children, $row['id']);
}
return $children;
}
public function editNode($node_id, $newName, $spouse_id, $parent_id, $user_id)
{
//echo $node_id, $newName, $spouse_id, $parent_id;
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query="UPDATE trees SET";
$useComma=false;
if(!is_null($newName))
{
$query .= " name='$newName'";
$useComma=true;
}
if(!is_null($spouse_id))
{
if($useComma)
$query .= ",";
$query .= " spouse_id=$spouse_id";
$useComma=true;
}
if(!is_null($parent_id))
{
if($useComma)
$query .= ",";
$query .= " parent_id=$parent_id";
$useComma=true;
}
if(!is_null($user_id))
{
if($useComma)
$query .= ",";
$query .= " user_id=$user_id";
}
$query .= " WHERE id=$node_id";
//echo $query,"<br>";
mysql_query($query, $con) or die('Query failed: ' . mysql_error());
mysql_close($con);
}
public function addChild($name, $parent_id)
{
$this->addNode($name, $parent_id, null, null);
}
public function addSpouse($name, $spouse_id)
{
//add the spouse as a node
$insert_id = $this->addNode($name, null, $spouse_id, null);
//update the original node
$this->editNode($spouse_id, null, $insert_id, null, null);
}
public function createNewTree($name, $user_id)
{
return $this->addNode($name, null, null, $user_id);
}
public function addParent($name, $child_id, $user_id)
{
//can only be called for the top node
if(!$this->isTopNode($child_id, $treeName, $treeId))
return;
//echo "is top node","<br>";
//create new node for the tree
$newTree_id = $this->createNewTree($treeName, $user_id);
//update the old tree node with new parent
$this->editNode($treeId, $name, null, $newTree_id, "NULL");
return $newTree_id;
}
private function readNode($node_id, &$tree, $con)
{
$row = $this->getNodeInfo($node_id, $con);
$tree['name'] = $row['name'];
$tree['spouse'] = $row['spouse'];
$tree['id'] = $row['id'];
$tree['owner_id'] = $row['owner_id'];
$children = $this->getNodeChildren($node_id, $con);
$count=0;
foreach($children as $child_id)
{
$this->readNode($child_id, $tree['contents'][$count], $con);
$count++;
}
}
public function readTreeAndGenerateJson($tree_id)
{
$tree = array();
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
$this->readNode($tree_id, $tree, $con);
mysql_close($con);
return json_encode($tree);
}
public function createUser($username, $password)
{
$password = sha1($password);
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query = "INSERT into users(username,password) values('$username', '$password')";
mysql_query($query, $con) or die('Query failed: ' . mysql_error());
$user = array();
$user["id"] = mysql_insert_id();
$user["name"] = $username;
mysql_close($con);
return $user;
}
public function login($username, $password, &$user)
{
$password = sha1($password);
// Connecting, selecting database
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query = "SELECT id,username from users where username='$username' and password='$password'";
$result = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
mysql_close($con);
if($row = mysql_fetch_array($result))
{
$user['id'] = $row['id'];
$user['name'] = $row['username'];
return true;
}
return false;
}
public function getAllTrees($user_id)
{
$con = mysql_connect($this->server, $this->user, $this->pass)
or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db($this->db) or die('Could not select database');
// Performing SQL query
$query = "SELECT id,name from trees where user_id=$user_id order by created desc";
$result = mysql_query($query, $con) or die('Query failed: ' . mysql_error());
mysql_close($con);
$treeList = array();
$count=0;
while($row = mysql_fetch_array($result))
{
$treeList[$count]['id'] = $row['id'];
$treeList[$count]['name'] = $row['name'];
$count++;
}
return $treeList;
}
}
?>