This repository has been archived by the owner on Sep 25, 2021. It is now read-only.
/
class_MPTTcategories.php
511 lines (467 loc) · 15.4 KB
/
class_MPTTcategories.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/***************************************************************************
* copyright : (C) 2008 - 2014 WeBid
* site : http://www.webidsupport.com/
***************************************************************************/
/***************************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. Although none of the code may be
* sold. If you have been sold this script, get a refund.
***************************************************************************/
if (!defined('InWeBid')) exit('Access denied');
class MPTTcategories
{
// Add an element to the tree as a child of $parent and as $child_num'th child. If $data is not supplied the insert id will be returned.
function add($parent_id, $child_num = 0, $misc_data = false)
{
global $system, $DBPrefix, $db;
if(!is_numeric($parent_id) || $parent_id < 0)
{
return false;
}
if($parent_id != 0)
{
$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = :parent_id";
$params = array();
$params[] = array(':parent_id', $parent_id, 'int');
$db->query($query, $params);
if($db->numrows() != 1)
{ // Row must exist.
return false;
}
$parent = $db->result();
}
else
{
// Virtual root element as parent.
$parent = $this->get_virtual_root();
}
$children = $this->get_children($parent['left_id'], $parent['right_id'], $parent['level']);
if(count($children) == 0)
{
$child_num = 0;
}
if($child_num == 0 || (count($children) - $child_num) <= 0 || (count($children) + $child_num + 1) < 0)
{
$boundry = array('left_id', 'right_id', $parent['left_id']);
}
elseif($child_num != 0)
{
// Some other child.
if($child_num < 0)
{
$child_num = count($children) + $child_num + 1;
}
if($child_num > count($children))
{
$child_num = count($children);
}
$boundry = array('right_id', 'left_id', $children[$child_num - 1]['right_id']);
}
else
{
return false;
}
// Make a hole for the new element.
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id + 2 WHERE " . $boundry[0] . " > " . $boundry[2] . " AND " . $boundry[1] . " > " . $boundry[2];
$db->direct_query($query);
$query = "UPDATE " . $DBPrefix . "categories SET right_id = right_id + 2 WHERE " . $boundry[1] . " > " . $boundry[2];
$db->direct_query($query);
// Insert the new element.
$data = array(
'left_id' => $boundry[2] + 1,
'right_id' => $boundry[2] + 2,
'level' => $parent['level'] + 1,
'parent_id' => $parent_id
);
if($misc_data && is_array($misc_data))
{
$data = array_merge($misc_data, $data);
}
$data = $this->build_sql($data);
$query = "INSERT INTO " . $DBPrefix . "categories SET " . $data;
$db->direct_query($query);
if(!$misc_data)
{
return $db->lastInsertId();
}
return true;
}
// Deletes element $id with or without children. If children should be kept they will become children of $id's parent.
function delete($id, $keep_children = false)
{
global $system, $DBPrefix, $db;
if(!is_numeric($id) || $id <= 0 || !is_bool($keep_children))
{
return false;
}
$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = :cat_id";
$params = array();
$params[] = array(':cat_id', $id, 'int');
$db->query($query, $params);
if($db->numrows() != 1)
{ // Row must exist.
return false;
}
$a = $db->result();
if(!$keep_children)
{
// Delete the element with children.
$query = "DELETE FROM " . $DBPrefix . "categories WHERE left_id >= " . $a['left_id'] . " AND right_id <= " . $a['right_id'];
$db->direct_query($query);
// Remove the hole.
$diff = $a['right_id'] - $a['left_id'] + 1;
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id - " . $diff . " WHERE right_id > " . $a['right_id'] . " AND left_id > " . $a['right_id'];
$db->direct_query($query);
$query = "UPDATE " . $DBPrefix . "categories SET right_id = right_id - " . $diff . " WHERE right_id > " . $a['right_id'];
$db->direct_query($query);
// No level cahnges needed.
}
else
{
// Delete ONLY the element.
$query = "DELETE FROM " . $DBPrefix . "categories WHERE cat_id = :cat_id";
$params = array();
$params[] = array(':cat_id', $id, 'int');
$db->query($query, $params);
// Fix children.
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id - 1, right_id = right_id - 1, level = level - 1 WHERE left_id >= " . $a['left_id'] . " AND right_id <= " . $a['right_id'];
$db->direct_query($query);
// Remove hole.
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id - 2 WHERE right_id > " . ($a['right_id'] - 1) . " AND left_id > " . ($a['right_id'] - 1);
$db->direct_query($query);
$query = "UPDATE " . $DBPrefix . "categories SET right_id = right_id - 2 WHERE right_id > " . ($a['right_id'] - 1);
$db->direct_query($query);
}
}
// Move an element (with children) $id, under element $target_id as the $child_num'th child of that element
function move($id, $target_id, $child_num = 0)
{
global $system, $DBPrefix, $db;
if(!is_numeric($id) || !is_numeric($target_id) || !is_numeric($child_num))
{
return false;
}
if($target_id != 0)
{
$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = :cat_id OR cat_id = :target_id";
// I want the to be returned in order.
$query .= ' ORDER BY cat_id ' . (($id < $target_id) ? 'ASC' : 'DESC');
$params = array();
$params[] = array(':cat_id', $id, 'int');
$params[] = array(':target_id', $target_id, 'int');
$db->query($query, $params);
if($db->numrows() != 2)
{ // Both rows must exist.
return false;
}
$data = $db->fetchall();
$a = $data[0]; // This is being moved.
$b = $data[1]; // This is the target.
}
else
{
$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = :cat_id";
$params = array();
$params[] = array(':cat_id', $id, 'int');
$db->query($query, $params);
if($db->numrows() != 1)
{ // Row must exist.
return false;
}
$a = $db->result(); // This is being moved.
// Virtual root element.
$b = $this->get_virtual_root();
}
// We need to get the children.
$children = $this->get_children($b['left_id'], $b['right_id'], $b['level']);
if(count($children) == 0)
{
$child_num = 0;
}
if($child_num == 0 || (count($children) - $child_num) <= 0 || (count($children) + $child_num + 1) < 0)
{
// First child.
$boundry = array('left_id', 'right_id', 'right_id', $b['left_id']);
}
elseif($child_num != 0)
{
// Some other child.
if($child_num < 0)
{
$child_num = count($children) + $child_num + 1;
}
if($child_num > count($children))
{
$child_num = count($children);
}
$boundry = array('right_id', 'left_id', 'right_id', $children[$child_num - 1]['right_id']);
}
else
{
return false;
}
// Math.
$diff = $a['right_id'] - $a['left_id'] + 1; // The "size" of the tree.
if($a['left_id'] < $boundry[3])
{
$size = $boundry[3] - $diff;
$dist = $boundry[3] - $diff - $a['left_id'] + 1;
}
else
{
$size = $boundry[3];
$dist = $boundry[3] - $a['left_id'] + 1;
}
// Level math.
$ldiff = ($a['level'] - $b['level'] - 1) * -1;
// We have all what we need.
$query = array();
// Give the needed rows negative id's.
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id * -1, right_id = right_id * -1 WHERE left_id >= " . $a['left_id'] . " AND right_id <= " . $a['right_id'];
$db->direct_query($query);
// Remove the hole.
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id - " . $diff . " WHERE right_id > " . $a['right_id'] . " AND left_id > " . $a['right_id'];
$db->direct_query($query);
$query = "UPDATE " . $DBPrefix . "categories SET right_id = right_id - " . $diff . " WHERE right_id > " . $a['right_id'];
$db->direct_query($query);
// Add hole
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id + " . $diff . " WHERE " . $boundry[0] . " > " . $size . " AND " . $boundry[1] . " > " . $size;
$db->direct_query($query);
$query = "UPDATE " . $DBPrefix . "categories SET right_id = right_id + " . $diff . " WHERE " . $boundry[2] . " > " . $size;
$db->direct_query($query);
// Fill hole & update rows & multiply by -1
$query = "UPDATE " . $DBPrefix . "categories SET left_id = (left_id - (" . $dist . ")) * -1, right_id = (right_id - (" . $dist . ")) * -1, level = level + (" . $ldiff . ") WHERE left_id < 0";
$db->direct_query($query);
return true;
}
// Copies element $id (with children) to $parent as the $child_mun'th child.
function copy($id, $parent, $child_num = 0)
{
global $system, $DBPrefix, $db;
if(!is_numeric($id) || $id < 0 ||!is_numeric($parent) || $parent < 0)
{
return false;
}
// Get branch left & right id's.
$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = :cat_id";
$params = array();
$params[] = array(':cat_id', $id, 'int');
$db->query($query, $params);
if($db->numrows() != 1)
{ // Row must Exist.
return false;
}
$a = $db->result();
// Get child data.
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE left_id >= " . $a['left_id'] . " AND right_id <= " . $a['right_id'];
$db->direct_query($query);
while($row = $db->fetch())
{
$data[] = $row;
}
if($parent != 0)
{
$query = "SELECT left_id, right_id, level FROM " . $DBPrefix . "categories WHERE cat_id = :parent_id";
$params = array();
$params[] = array(':parent_id', $parent, 'int');
$db->query($query, $params);
if($db->numrows() != 1)
{ // Row must exist.
return false;
}
$b = $db->result();
}
else
{
$b = $this->get_virtual_root();
}
// Get target's children.
$children = $this->get_children($b['left_id'], $b['right_id'], $b['level']);
if(count($children) == 0)
{
$child_num = 0;
}
if($child_num == 0 || (count($children) - $child_num) <= 0 || (count($children) + $child_num + 1) < 0)
{
// First child.
$boundry = array('left_id', 'right_id', 'right_id', $b['left_id']);
}
elseif($child_num != 0)
{
// Some other child.
if($child_num < 0)
{
$child_num = count($children) + $child_num + 1;
}
if($child_num > count($children))
{
$child_num = count($children);
}
$boundry = array('right_id', 'left_id', 'right_id', $children[$child_num - 1]['right_id']);
}
else
{
return false;
}
// Math.
$diff = $a['right_id'] - $a['left_id'] + 1;
$dist = $boundry[3] - $a['left_id'] + 1;
// Level math.
$ldiff = ($a['level'] - $b['level'] - 1);
// Add hole.
$query = "UPDATE " . $DBPrefix . "categories SET left_id = left_id + " . $diff . " WHERE " . $boundry[0] . " > " . $boundry[3] . " AND " . $boundry[1] . " > " . $boundry[3];
$db->direct_query($query);
$query = "UPDATE " . $DBPrefix . "categories SET right_id = right_id + " . $diff . " WHERE " . $boundry[2] . " > " . $boundry[3];
$db->direct_query($query);
// Now we have to insert all the new elements.
for($i = 0, $n = count($data); $i< $n; $i++)
{
// We need a new key.
unset($data[$i][FIELD_KEY]);
// This fields need new values.
$data[$i]['left_id'] += $dist;
$data[$i]['right_id'] += $dist;
$data[$i]['level'] -= $ldiff;
$data[$i] = $this->build_sql($data[$i]);
$query = "INSERT INTO " . $DBPrefix . "categories SET " . $data[$i];
$db->direct_query($query);
}
return true;
}
// get a nodes children
function get_children($left_id, $right_id, $level)
{
global $system, $DBPrefix, $db;
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE left_id > :left_id AND right_id < :right_id AND level = :level ORDER BY cat_name";
$params = array();
$params[] = array(':left_id', $left_id, 'int');
$params[] = array(':right_id', $right_id, 'int');
$params[] = array(':level', ($level + 1), 'int');
$db->query($query, $params);
$children = array();
while($child = $db->fetch())
{
$children[] = $child;
}
return $children;
}
// return a list of every child node of a given parent node
function get_children_list($left_id, $right_id, $return = 'cat_id')
{
global $system, $DBPrefix, $db;
if (empty($left_id) || empty($right_id))
{
return array();
}
$query = "SELECT " . $return . " FROM " . $DBPrefix . "categories WHERE left_id > :left_id AND right_id < :right_id";
$params = array();
$params[] = array(':left_id', $left_id, 'int');
$params[] = array(':right_id', $right_id, 'int');
$db->query($query, $params);
$children = array();
while($child = $db->fetch())
{
$children[] = $child;
}
return $children;
}
//returns an ordered list of categories
function display_tree($left_id, $right_id, $indent = "\t")
{
global $system, $DBPrefix, $db;
// start with an empty $right stack
$right = array();
$return = array();
// now, retrieve all descendants of the $root node
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE left_id > :left_id AND right_id < :right_id ORDER BY left_id ASC";
$params = array();
$params[] = array(':left_id', $left_id, 'int');
$params[] = array(':right_id', $right_id, 'int');
$db->query($query, $params);
// display each row
while ($row = $db->fetch())
{
// only check stack if there is one
if (count($right) > 0)
{
// check if we should remove a node from the stack
while (isset($right[count($right) - 1]) && $right[count($right) - 1] < $row['right_id'])
{
array_pop($right);
}
}
// display indented node title
$return[$row['cat_id']] = str_repeat($indent, count($right)) . $row['cat_name'];
// add this node to the stack
$right[] = $row['right_id'];
}
return $return;
}
// Return the left_id, right_id and level for the virtual root node.
function get_virtual_root()
{
global $system, $DBPrefix, $db;
// Virtual root element as parent.
$query = "SELECT right_id FROM " . $DBPrefix . "categories ORDER BY right_id DESC LIMIT 1";
$db->direct_query($query);
$row = $db->result();
$root = array('left_id' => 1, 'right_id' => $row['right_id'], 'level' => -1);
return $root;
}
function get_bread_crumbs($left_id, $right_id)
{
global $system, $DBPrefix, $db;
if (empty($left_id) || empty($right_id))
{
return array();
}
// return an array of all parent nodes
$query = "SELECT cat_name, cat_id FROM " . $DBPrefix . "categories WHERE left_id <= :left_id AND right_id >= :right_id ORDER BY left_id ASC";
$params = array();
$params[] = array(':left_id', $left_id, 'int');
$params[] = array(':right_id', $right_id, 'int');
$db->query($query, $params);
$array = array();
while ($row = $db->fetch())
{
$array[] = $row;
}
return $array;
}
// Build INSERT statement
function build_sql($data)
{
foreach($data as $k => $v)
{
if(is_numeric($v))
{
$data[$k] = '`' . $k . '` = ' . $v . '';
}
else
{
$data[$k] = '`' . $k . '` = \'' . $v . '\'';
}
}
return implode(', ', $data);
}
function check_category($id)
{
global $system, $DBPrefix, $db;
$query = "SELECT cat_id FROM " . $DBPrefix . "categories WHERE cat_id = :cat_id LIMIT 1";
$params = array();
$params[] = array(':cat_id', $id, 'int');
$db->query($query, $params);
if ($db->numrows() > 0)
{
return true;
}
else
{
return false;
}
}
}
?>