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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
<?LassoScript
// -----------------------------------------------------------
/*
VERSION 2007-06-02
NOTE:
- MUST be used ONLY with MySQL 4.1+ as it requires subqueries
- $gv_sql must be defined appropriately
- -cattable will be required to be passed in all operations
DESCRIPTION
addSibling
Adds an entry AFTER specified item, at the same depth
addChild
Adds a child to the specified item.
Usually employed when there is no existing child.
addRoot
Adds a node at the root - at the end
deleteNode
Self-descriptive... DELETES the node and all it's child nodes.
moveNode
Self-descriptive... MOVES the node and all it's child nodes in the specified direction.
moveNodeTo
Moves given node + nested nodes to inside a given node's id.
fullCatSQL
Returns the SQL required to extract the full tree.
subTreeSQL
Returns the SQL required to extract the tree branching from a specified node.
showPathSQL
Returns the SQL that will extract the linear path from the root to the node.
getParent
Returns a map, id and parentname
getURLpath
Returns the url page like /hello/world/ from an id
USAGE:
xs_cat->(addSibling(-cattable='category',-txt='Hello World',-id=10));
xs_cat->(addChild(-cattable='category',-txt='Hello World',-id=10));
xs_cat->(deleteNode(-cattable='category',-id=10));
xs_cat->(moveNode(-cattable='category',-id=10));
xs_cat->(fullCatSQL(-cattable='category',-xtraReturn=',column1, column2',-xtraWhere='SQL statement here'));
xs_cat->(subTreeSQL(-cattable='category',-depth=2,-relative=true,-xtraReturn=',column1, column2',-xtraWhere='SQL statement here'));
xs_cat->(showPathSQL(-cattable='category',-xtraReturn=',column1, column2',-xtraWhere='SQL statement here'));
CHANGES:
2006-06-17
Added order to fullcatSQL
*/
// -----------------------------------------------------------
if:!(Lasso_TagExists:'xs_cat');
define_type('cat',
-Priority='replace',
-namespace='xs_',
-prototype);
define_tag('addSibling',
-Required='cattable',
-Required='txt',
-Optional='othersmap',
-Required='id'
);
// ADDS ENTRY AFTER ANOTHER CHILD, SAME LEVEL
local('xtraFields' = string);
local('xtraValues' = string);
if(local_defined('othersmap') && local('othersmap')->(IsA('Map')));
iterate(#othersmap,local('temp'));
#xtraFields += ','+#temp->name;
(#temp->value == 'NOW()')? #xtraValues += ',NOW()' | #xtraValues += ',"'+#temp->value+'"';
/iterate;
/if;
local('uniqueSeed' = lasso_uniqueid);
local('sSQL' = '
LOCK TABLE '+#cattable+' WRITE;
SELECT @myRight := rgt FROM '+#cattable+' WHERE id = '+#id+';
UPDATE '+#cattable+' SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE '+#cattable+' SET lft = lft + 2 WHERE lft > @myRight;
INSERT INTO '+#cattable+'(name, lft, rgt'+#xtraFields+',uniqueSeed) VALUES("'+(encode_sql(#txt))+'", @myRight + 1, @myRight + 2'+#xtraValues+',"'#uniqueSeed'");
UNLOCK TABLES;
');
inline($gv_sql,-SQL=#sSQL);
xs_iserror;
inline($gv_sql,-SQL='SELECT id FROM '#cattable' WHERE uniqueSeed = "'#uniqueSeed'" LIMIT 1');
records;
return(field('id'));
/records;
/inline;
/inline;
/define_tag;
define_tag('addChild',
-Required='cattable',
-Required='txt',
-Optional='othersmap',
-Required='id'
);
// ADDS ENTRY NESTED INSIDE CAT WHERE NO CHILD EXISTS
local('xtraFields' = string);
local('xtraValues' = string);
if(local_defined('othersmap') && local('othersmap')->(IsA('Map')));
iterate(#othersmap,local('temp'));
#xtraFields += ','+#temp->name;
(#temp->value == 'NOW()')? #xtraValues += ',NOW()' | #xtraValues += ',"'+#temp->value+'"';
/iterate;
/if;
local('uniqueSeed' = lasso_uniqueid);
local('sSQL' = '
LOCK TABLE '+#cattable+' WRITE;
SELECT @myLeft := lft FROM '+#cattable+' WHERE id = '+#id+';
UPDATE '+#cattable+' SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE '+#cattable+' SET lft = lft + 2 WHERE lft > @myLeft;
INSERT INTO '+#cattable+'(name, lft, rgt'+#xtraFields+',uniqueSeed) VALUES("'+(encode_sql(#txt))+'", @myLeft + 1, @myLeft + 2'+#xtraValues+',"'#uniqueSeed'");
UNLOCK TABLES;
');
inline($gv_sql,-SQL=#sSQL);
xs_iserror;
inline($gv_sql,-SQL='SELECT id FROM '#cattable' WHERE uniqueSeed = "'#uniqueSeed'" LIMIT 1');
records;
return(field('id'));
/records;
/inline;
/inline;
/define_tag;
define_tag('addRoot',
-Required='cattable',
-Required='txt',
-Optional='othersmap'
);
// ADDS ROOT NODE AT END
local('xtraFields' = string);
local('xtraValues' = string);
if(local_defined('othersmap') && local('othersmap')->(IsA('Map')));
iterate(#othersmap,local('temp'));
#xtraFields += ','+#temp->name;
(#temp->value == 'NOW()')? #xtraValues += ',NOW()' | #xtraValues += ',"'+#temp->value+'"';
/iterate;
/if;
local('uniqueSeed' = lasso_uniqueid);
local('sSQL' = '
LOCK TABLE '+#cattable+' WRITE;
SELECT @myRight := rgt FROM '+#cattable+' ORDER BY rgt DESC LIMIT 1;
INSERT INTO '+#cattable+'(name, lft, rgt'+#xtraFields+',uniqueSeed) VALUES("'+(encode_sql(#txt))+'", @myRight + 1, @myRight + 2'+#xtraValues+',"'#uniqueSeed'");
UNLOCK TABLES;
');
inline($gv_sql,-SQL=#sSQL);
xs_iserror;
inline($gv_sql,-SQL='SELECT id FROM '#cattable' WHERE uniqueSeed = "'#uniqueSeed'" LIMIT 1');
records;
return(field('id'));
/records;
/inline;
/inline;
/define_tag;
define_tag('deleteNode',
-Required='cattable',
-Required='id'
);
// DELETE A NODE
local('sSQL' = '
LOCK TABLE '+#cattable+' WRITE;
SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1
FROM '+#cattable+'
WHERE id = '+#id+';
DELETE FROM '+#cattable+' WHERE lft BETWEEN @myLeft AND @myRight;
UPDATE '+#cattable+' SET rgt = rgt - @myWidth WHERE rgt > @myRight;
UPDATE '+#cattable+' SET lft = lft - @myWidth WHERE lft > @myRight;
UNLOCK TABLES;
');
// $gv_feedback += #sSQL;
inline($gv_sql,-SQL=#sSQL);
xs_iserror;
/inline;
/define_tag;
define_tag('moveNode',
-Required='cattable',
-Required='id'
);
/*
1 Calculate the branch width of the branch you want to move.
2 Update all lft and rgt values on the branch you want to move by multiplying them by -1.
3 Update all values on the tree as though the branch you just negated was deleted.
4 Update all lft values that are greater than the rgt value of the node you want to move the branch to by adding the branch width + 2 to them.
5 Update all rgt values that are greater than or equal to the rgt value of the node you want to move the branch to by adding the branch width + 2 to them.
6 Find the greatest negative lft value in the table,
which will be the top of the branch that you are moving, multiply it by -1 and call it x.
7 Find the lft value of the node that you want to attach the branch to, and call it y.
8 Calculate (y - x + 1) = z . Update all negative values in the table by subtracting z from them.
9 Update all negative values in the tree by multiplying them by -1.
*/
local('id2' = 0);
// get immediate prior sibling
local('sSQL' = (
'
SELECT node.id, node.name, (COUNT(parent.id) - (sub_tree.depth + 1)) AS depth, node.lft, node.rgt
FROM '+#cattable+' AS node,
'+#cattable+' AS parent,
'+#cattable+' AS sub_parent,
(
SELECT node.name, (COUNT(parent.id) - 1) AS depth
FROM '+#cattable+' AS node,
'+#cattable+' AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id =
(
SELECT parent.id
FROM '+#cattable+' AS node,
'+#cattable+' AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = '+#id+' AND parent.id != '+#id+'
ORDER BY parent.lft DESC LIMIT 1
)
GROUP BY node.name
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.id
HAVING depth = 1
ORDER BY node.lft ASC;'
));
inline($gv_sql,-SQL=#sSQL);
records;
if(integer(field('id')) != #id);
#id2 = integer(field('id'));
else;
loop_abort;
/if;
/records;
/inline;
if(#id2 == 0);
// here we are trying to ascertain if it's a root node!
#sSQL = '
SELECT node.id, node.name, (COUNT(parent.id) - 1) AS depth
FROM '+#cattable+' AS node,
'+#cattable+' AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = '+#id+'
GROUP BY node.id
ORDER BY node.lft';
inline($gv_sql,-SQL=#sSQL);
records;
if(integer(field('depth')) == 0);
// yay, it's a root node!!!
#sSQL = '
SELECT node.id, node.name, (COUNT(parent.id) - 1) AS depth
FROM '+#cattable+' AS node,
'+#cattable+' AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.id
HAVING depth = 0
ORDER BY node.lft
';
inline($gv_sql,-SQL=#sSQL);
records;
if(integer(field('id')) != #id);
#id2 = integer(field('id'));
else;
loop_abort;
/if;
/records;
/inline;
/if;
/records;
/inline;
/if;
if(#id2 > 0);
#sSQL = ('
LOCK TABLE '+#cattable+' WRITE;
-- 1
SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1 FROM '+#cattable+' WHERE id = '+#id2+';
-- 2
UPDATE '+#cattable+' SET rgt = (rgt*-1), lft = (lft*-1) WHERE lft BETWEEN @myLeft AND @myRight;
-- 3
UPDATE '+#cattable+' SET rgt = rgt - @myWidth WHERE rgt > @myRight;
UPDATE '+#cattable+' SET lft = lft - @myWidth WHERE lft > @myRight;
-- 4a
SELECT @myLeft2 := lft, @myRight2 := rgt, @myWidth2 := rgt - lft + 1 FROM '+#cattable+' WHERE id = '+#id+';
-- 4 & 5
UPDATE '+#cattable+' SET rgt = rgt + @myWidth WHERE rgt > @myRight2;
UPDATE '+#cattable+' SET lft = lft + @myWidth WHERE lft > @myRight2;
-- 6
-- SELECT @x := (@myRight2 + 1) - (lft * -1) FROM '+#cattable+' WHERE id = '+#id+';
SELECT @x := lft FROM '+#cattable+' WHERE id = '+#id+';
SELECT @y := rgt FROM '+#cattable+' WHERE id = '+#id+';
-- 8
UPDATE '+#cattable+' SET rgt = (rgt - (@y - @x + 1)) WHERE rgt < 0;
UPDATE '+#cattable+' SET lft = (lft - (@y - @x + 1)) WHERE lft < 0;
UPDATE '+#cattable+' SET rgt = rgt * -1 WHERE rgt < 0;
UPDATE '+#cattable+' SET lft = lft * -1 WHERE lft < 0;
UNLOCK TABLES;
');
inline($gv_sql,-SQL=#sSQL);
/inline;
/if;
/define_tag;
define_tag('moveNodeTo',
-Required='cattable',
-Required='id',
-Required='newparent'
);
local('sSQL' = string);
if(#newparent > 0 && #id > 0);
#sSQL = ('
LOCK TABLE '+#cattable+' WRITE;
-- 1 , get boundaries of chunk to move
SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1 FROM '+#cattable+' WHERE id = '+#id+';
-- 2 , make chunk negative (ie move outta da way)
UPDATE '+#cattable+' SET rgt = ((rgt-@myRight)-1), lft = ((lft-@myRight)-1) WHERE lft BETWEEN @myLeft AND @myRight;
-- 3 , collapse
UPDATE '+#cattable+' SET rgt = rgt - @myWidth WHERE rgt > @myRight;
UPDATE '+#cattable+' SET lft = lft - @myWidth WHERE lft > @myRight;
-- 4, get boundaries of new parent
SELECT @myRight2a := rgt FROM '+#cattable+' WHERE id = '+#newparent+';
-- 5 , expand new parent + others to hold content
UPDATE '+#cattable+' SET rgt = rgt + @myWidth WHERE rgt >= @myRight2a; -- note >= is new addition
UPDATE '+#cattable+' SET lft = lft + @myWidth WHERE lft > @myRight2a;
-- 6, get boundaries of new parent
SELECT @myLeft2 := lft, @myRight2 := rgt, @myWidth2 := rgt - lft + 1 FROM '+#cattable+' WHERE id = '+#newparent+';
-- 8, take all less than 0 and ad myRight2, puts it automagically in teh right pos
UPDATE '+#cattable+' SET rgt = (rgt + @myRight2) WHERE rgt < 0;
UPDATE '+#cattable+' SET lft = (lft + @myRight2) WHERE lft < 0;
UNLOCK TABLES;
');
inline($gv_sql,-SQL=#sSQL);
xs_iserror;
/inline;
/if;
/define_tag;
define_tag('fullCatSQL',
-Required='cattable',
-Optional='xtraReturn',
-Optional='xtraWhere',
-Optional='orderby',
-Optional='depth'
);
(!(local_defined('xtraReturn'))) ? local('xtraReturn' = string);
(!(local_defined('xtraWhere'))) ? local('xtraWhere' = string);
if(!(local_defined('depth')));
local('depthComp' = string);
else(string(#depth) != '');
local('depthComp' = 'HAVING depth <= '+#depth);
else;
local('depthComp' = string);
/if;
if(!(local_defined('orderby')));
local('orderComp' = 'node.lft');
else;
local('orderComp' = #orderby);
/if;
/*
Returns full category list incl depth.
To specify additional columns returned use xtraReturn parameter
To specify additional restrictions in WHERE clause, use xtraWhere parameter
An example of xtraReturn is as follows:
',
(
SELECT COUNT(*)
FROM asset, category AS subc
WHERE asset.category_id = subc.id
AND subc.lft BETWEEN node.lft AND node.rgt
)AS chqty,
(
SELECT COUNT(asset.name) FROM asset WHERE asset.category_id = node.id
)AS qty,
(
SELECT COUNT(*) - 1
FROM category AS nnode
WHERE nnode.lft BETWEEN node.lft AND node.rgt
)AS nchild'
*/
return('
SELECT
node.id, node.name, (COUNT(parent.id) - 1) AS depth '+#xtraReturn+'
FROM '+#cattable+' AS node,
'+#cattable+' AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt '+#xtraWhere+'
GROUP BY node.id
'+#depthComp+'
ORDER BY '+#orderComp);
/define_tag;
define_tag('subTreeSQL',
-Required='cattable',
-Required='id',
-Optional='depth',
-Optional='relative',
-Optional='xtraReturn',
-Optional='xtraWhere',
-Optional='usage'
);
(!(local_defined('usage'))) ? local('usage' = string);
(!(local_defined('xtraReturn'))) ? local('xtraReturn' = string);
(!(local_defined('xtraWhere'))) ? local('xtraWhere' = string);
(!(local_defined('relative')) || (local('relative') == false)) ? local('relative' = '1') | local('relative' = '(sub_tree.depth + 1)');
if(!(local_defined('depth')));
local('depthComp' = string);
else(integer(#depth) > 0);
local('depthComp' = 'HAVING depth <= '+#depth);
else;
local('depthComp' = string);
/if;
//(sub_tree.depth + 1) - makes the depth relative to the one requested
//HAVING depth <= 1 - limits how many subs it pulls in
/*
================================================================================
From Pier 23/05/2006 16:26
Added node.id to the nested SELECT so that we can replace
[...] sub_parent.name = sub_tree.name [...]
with
[...] sub_parent.id = sub_tree.id [...]
================================================================================
*/
local('out' = 'SELECT node.id');
#usage != 'in' ? #out += ', node.name, (COUNT(parent.id) - '+#relative+') AS depth '+#xtraReturn;
#out += ' FROM '+#cattable+' AS node,
'+#cattable+' AS parent,
'+#cattable+' AS sub_parent,
(
SELECT node.id, node.name, (COUNT(parent.id) - 1) AS depth
FROM '+#cattable+' AS node,
'+#cattable+' AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = '+#id+'
GROUP BY node.name
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.id = sub_tree.id '+#xtraWhere+'
GROUP BY node.id
'+#depthComp;
#usage != 'in' ? #out += 'ORDER BY node.lft;';
return(#out);
/define_tag;
define_tag('showPathSQL',
-Required='cattable',
-Required='id',
-Optional='xtraReturn',
-Optional='xtraWhere'
);
(!(local_defined('xtraReturn'))) ? local('xtraReturn' = string);
(!(local_defined('xtraWhere'))) ? local('xtraWhere' = string);
local('out' = 'SELECT node.id, node.name, (COUNT(parent.id) - 1) AS depth '+#xtraReturn+'
FROM '+#cattable+' AS node,
'+#cattable+' AS parent,
(
SELECT sub.lft AS sublft, sub.rgt AS subrgt
FROM '+#cattable+' AS sub
WHERE sub.id = '+#id+'
) AS sub
WHERE
node.lft <= sub.sublft
AND node.rgt >= sub.subrgt
AND node.lft BETWEEN parent.lft AND parent.rgt '+#xtraWhere+'
GROUP BY node.id
ORDER BY node.lft
;');
return(#out);
/define_tag;
define_tag('getParent',
-Required='cattable',
-Required='id',
-Optional='xtraWhere'
);
// -Optional='xtraReturn',
(!(local_defined('xtraReturn'))) ? local('xtraReturn' = string);
(!(local_defined('xtraWhere'))) ? local('xtraWhere' = string);
local('out' = map);
local('sSQL' = 'SELECT
parent.id,parent.name '+#xtraReturn+'
FROM
'+#cattable+' AS node,
'+#cattable+' AS parent
WHERE
node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = '+#id+'
AND parent.id != node.id
'+#xtraWhere+'
ORDER BY
parent.lft DESC
LIMIT 1');
inline($gv_sql,-SQL=#sSQL);
records;
#out->insert('id'=field('id'),'parentname'=field('name'));
/records;
/inline;
return(#out);
/define_tag;
define_tag('getURLpath',
-Required='cattable',
-Required='id',
-Description='Returns the url page like /hello/world/ from an id'
);
local('out' = '/');
local('sSQL' = '
SELECT node.id, node.page_url, (COUNT(parent.id) - 1) AS depth
FROM '+#cattable+' AS node,
'+#cattable+' AS parent,
(
SELECT sub.lft AS sublft, sub.rgt AS subrgt
FROM '+#cattable+' AS sub
WHERE sub.id = '+#id+'
) AS sub
WHERE
node.lft <= sub.sublft
AND node.rgt >= sub.subrgt
AND node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.id
ORDER BY node.lft');
inline($gv_sql,-SQL=#sSQL);
records;
#out += field('page_url') + '/';
/records;
/inline;
return(#out);
/define_tag;
/define_type;
/if;
?>
|