-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNestedSet.cfc
More file actions
executable file
·853 lines (692 loc) · 37.4 KB
/
Copy pathNestedSet.cfc
File metadata and controls
executable file
·853 lines (692 loc) · 37.4 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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
<cfcomponent output="false" displayname="Nested Sets for CF Wheels" mixin="model">
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
Title: Nested Sets Plugin for CF Wheels (http://cfwheels.org)
Source: http://github.com/liferealized/cfwheels-nested-set
Authors: James Gibson (me@iamjamesgibson.com)
Andy Bellenie (andybellenie@gmail.com)
Notes: Ported from Awesome Nested Sets for Rails
http://github.com/collectiveidea/awesome_nested_set
Usage: Use nestedSet() in your model init to setup for the methods below
defaults
- idColumn = '' (defaults to the primary key during validation)
- parentColumn = 'parentId'
- leftColumn = 'lft'
- rightColumn = 'rgt'
- scope = ''
- instantiateOnDelete = false
- idsAreNullable = true
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="init" access="public" output="false" returntype="any">
<cfset this.version = "1.1" />
<cfreturn this />
</cffunction>
<cffunction name="nestedSet" returntype="void" access="public" output="false" mixin="model">
<cfargument name="idColumn" type="string" default="">
<cfargument name="parentColumn" type="string" default="parentId">
<cfargument name="leftColumn" type="string" default="lft">
<cfargument name="rightColumn" type="string" default="rgt">
<cfargument name="scope" type="string" default="">
<cfargument name="instantiateOnDelete" type="boolean" default="false">
<cfargument name="idsAreNullable" type="boolean" default="true">
<cfscript>
// add the nested set configuration into the model
variables.wheels.class.nestedSet = Duplicate(arguments);
variables.wheels.class.nestedSet.scope = $listClean(variables.wheels.class.nestedSet.scope);
variables.wheels.class.nestedSet.isValidated = false;
// set callbacks
beforeValidationOnCreate(methods="$setDefaultLeftAndRight");
beforeSave(methods="$checkForNewParent");
afterSave(methods="$moveToNewParent");
beforeDelete(methods="$deleteDescendants");
// add in a calculated property for the leaf value
property(name="isLeaf", sql="CASE WHEN (#arguments.rightColumn# - #arguments.leftColumn#) = 1 THEN 1 ELSE 0 END");
// allow for the two new types of callbacks
variables.wheels.class.callbacks.beforeMove = ArrayNew(1);
variables.wheels.class.callbacks.afterMove = ArrayNew(1);
</cfscript>
</cffunction>
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
private accessors for our nested set values
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="$getIdColumn" returntype="string" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.idColumn />
</cffunction>
<cffunction name="$getIdType" returntype="string" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.properties[variables.wheels.class.nestedSet.idColumn].type />
</cffunction>
<cffunction name="$getParentColumn" returntype="string" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.parentColumn />
</cffunction>
<cffunction name="$getLeftColumn" returntype="string" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.leftColumn />
</cffunction>
<cffunction name="$getRightColumn" returntype="string" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.rightColumn />
</cffunction>
<cffunction name="$getScope" returntype="string" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.scope />
</cffunction>
<cffunction name="$getInstantiateOnDelete" returntype="boolean" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.instantiateOnDelete />
</cffunction>
<cffunction name="$idsAreNullable" returntype="boolean" access="public" output="false">
<cfset $validateNestedSet()>
<cfreturn variables.wheels.class.nestedSet.idsAreNullable />
</cffunction>
<!---
plugin validation
--->
<cffunction name="$validateNestedSet" returntype="void" access="public" output="false">
<cfscript>
var loc = {};
// check hasNestedSet() has been run
if (not StructKeyExists(variables.wheels.class,"nestedSet"))
$throw(type="Wheels.Plugins.NestedSet.SetupNotComplete",message="You must call nestedSet() from your model's init() before you can use NestedSet methods.");
// skip validation if it has already run
if (not variables.wheels.class.nestedSet.isValidated)
{
// check for custom id column, otherwise use the primary key from the model
if (not Len(variables.wheels.class.nestedSet.idColumn))
variables.wheels.class.nestedSet.idColumn = primaryKey();
// check id types match
if (CompareNoCase(variables.wheels.class.properties[variables.wheels.class.nestedset.idColumn].type,variables.wheels.class.properties[variables.wheels.class.nestedset.parentColumn].type) neq 0)
$throw(type="Wheels.Plugins.NestedSet.KeyTypeMismatch",message="The cf_sql_type of the idColumn and parentColumn must be identical.");
// check scope fields are present in the object
for (loc.i=1; loc.i lte ListLen(variables.wheels.class.nestedSet.scope); loc.i++)
{
if (not StructKeyExists(variables.wheels.class.properties,ListGetAt(variables.wheels.class.nestedSet.scope,loc.i)))
$throw(type="Wheels.Plugins.NestedSet.ScopePropertyMissing",message="The property '#ListGetAt(variables.wheels.class.nestedSet.scope,loc.i)#' required in the scope argument is not present in the model.");
}
variables.wheels.class.nestedSet.isValidated = true;
}
</cfscript>
</cffunction>
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
id validation methods
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="$propertyIsInteger" returntype="boolean" access="public" output="false">
<cfargument name="property" type="string" required="true">
<cfreturn ListFindNoCase("cf_sql_integer,cf_sql_bigint,cf_sql_tinyint,cf_sql_smallint",variables.wheels.class.properties[arguments.property].type)>
</cffunction>
<cffunction name="$idIsValid" returntype="boolean" access="public" output="false">
<cfargument name="id" type="string" required="true">
<cfif $propertyIsInteger($getIdColumn())>
<cfreturn IsNumeric(arguments.id)>
</cfif>
<cfreturn not IsBoolean(arguments.id) and Len(arguments.id) gt 0>
</cffunction>
<cffunction name="$formatIdForQuery" returntype="string" access="public" output="false">
<cfargument name="id" type="string" required="true">
<cfargument name="match" type="boolean" default="true">
<cfset arguments.id = Trim(arguments.id)>
<cfif $idsAreNullable() and arguments.id eq "">
<cfif arguments.match>
<cfreturn "IS NULL">
</cfif>
<cfreturn "IS NOT NULL">
</cfif>
<cfif $propertyIsInteger($getIdColumn())>
<cfif arguments.match>
<cfreturn "= #arguments.id#">
</cfif>
<cfreturn "!= #arguments.id#">
</cfif>
<cfif arguments.match>
<cfreturn "= '#arguments.id#'">
</cfif>
<cfreturn "!= '#arguments.id#'">
</cffunction>
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
add in new callback types of beforeMove and afterMove for nested sets
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="beforeMove" returntype="void" access="public" output="false">
<cfargument name="methods" type="string" required="false" default="" />
<cfset $registerCallback(type="beforeMove", argumentCollection=arguments) />
</cffunction>
<cffunction name="afterMove" returntype="void" access="public" output="false">
<cfargument name="methods" type="string" required="false" default="" />
<cfset $registerCallback(type="afterMove", argumentCollection=arguments) />
</cffunction>
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
class level methods
e.g. model("user").allRoots();
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="firstRoot" returntype="any" access="public" output="false" hint="I return the first root object.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $appendWhereClause(arguments.where,"#$getParentColumn()# IS NULL")>
<cfreturn findOne(argumentcollection=arguments) />
</cffunction>
<cffunction name="allRoots" returntype="any" access="public" output="false" hint="I return all root objects.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $appendWhereClause(arguments.where,"#$getParentColumn()# IS NULL")>
<cfreturn findAll(argumentcollection=arguments) />
</cffunction>
<cffunction name="isTreeValid" returntype="boolean" access="public" output="false">
<cfreturn (leftAndRightIsValid() and noDuplicatesForColumns() and allRootsValid())>
</cffunction>
<cffunction name="leftAndRightIsValid" returntype="boolean" access="public" output="false">
<cfscript>
var loc = {};
loc.queryArgs = $getQueryArguments();
</cfscript>
<cfquery name="loc.query" attributeCollection="#loc.queryArgs#">
SELECT COUNT(*) AS leftRightCount
FROM #tableName()# AS nodes
LEFT JOIN #tableName()# AS parent ON nodes.#$getParentColumn()# = parent.#$getIdColumn()#
WHERE (
nodes.#$getLeftColumn()# IS NULL
OR nodes.#$getRightColumn()# IS NULL
OR nodes.#$getLeftColumn()# >= nodes.#$getRightColumn()#
OR (
nodes.#$getParentColumn()# IS NOT NULL
AND (
nodes.#$getLeftColumn()# <= parent.#$getLeftColumn()#
OR nodes.#$getRightColumn()# >= parent.#$getRightColumn()#
)
)
)
</cfquery>
<cfreturn (loc.query.leftRightCount eq 0) />
</cffunction>
<cffunction name="noDuplicatesForColumns" returntype="boolean" access="public" output="false">
<cfscript>
var loc = {
select = $getScope()
, columns = ListAppend($getLeftColumn(), $getRightColumn())
, queryArgs = $getQueryArguments()
, queries = {}
, returnValue = true
};
loc.iEnd = ListLen(loc.columns);
</cfscript>
<cfloop index="loc.i" from="1" to="#loc.iEnd#">
<cfset loc.column = ListGetAt(loc.columns, loc.i) />
<cfset loc.queryArgs.name = "loc.queries.#loc.column#" />
<cfquery name="loc.query" attributeCollection="#loc.queryArgs#">
SELECT #ListAppend(loc.select, loc.column)#, COUNT(#loc.column#)
FROM #tableName()#
GROUP BY #ListAppend(loc.select, loc.column)#
HAVING COUNT(#loc.column#) > 1
</cfquery>
</cfloop>
<cfscript>
for (loc.item in loc.queries)
{
if (loc.queries[loc.item].RecordCount gt 0)
{
loc.returnValue = false;
break;
}
}
</cfscript>
<cfreturn loc.returnValue />
</cffunction>
<cffunction name="allRootsValid" returntype="boolean" access="public" output="false">
<cfreturn this.eachRootValid(this.allRoots(argumentCollection=arguments)) />
</cffunction>
<cffunction name="eachRootValid" returntype="boolean" access="public" output="false">
<cfargument name="roots" type="query" required="true" />
<cfscript>
var loc = {
lft = 0
, rgt = 0
, iEnd = arguments.roots.RecordCount
, valid = true
};
for (loc.i=1; loc.i lte loc.iEnd; loc.i++) {
if (arguments.roots[$getLeftColumn()][loc.i] gt loc.lft and arguments.roots[$getRightColumn()][loc.i] gt loc.rgt) {
loc.lft = arguments.roots[$getLeftColumn()][loc.i];
loc.rgt = arguments.roots[$getRightColumn()][loc.i];
} else {
loc.valid = false;
break;
}
}
</cfscript>
<cfreturn loc.valid />
</cffunction>
<cffunction name="rebuild" returntype="boolean" access="public" output="false">
<cfscript>
var loc = {};
if (this.isTreeValid())
return true;
// find all root nodes
loc.roots = this.allRoots(returnAs="objects");
loc.iEnd = ArrayLen(loc.roots);
loc.lft = 1;
</cfscript>
<cftransaction action="begin">
<cfscript>
for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
loc.lft = $rebuildTree(loc.roots[loc.i], loc.lft);
</cfscript>
<cftransaction action="commit" />
</cftransaction>
<cfreturn true />
</cffunction>
<cffunction name="$rebuildTree" returntype="numeric" access="public" output="false">
<cfargument name="node" type="any" required="true" />
<cfargument name="lft" type="numeric" required="true" />
<cfscript>
var loc = {};
arguments.rgt = arguments.lft + 1;
loc.children = arguments.node.children(returnAs="objects");
loc.iEnd = ArrayLen(loc.children);
for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
arguments.rgt = $rebuildTree(loc.children[loc.i], arguments.rgt);
arguments.node[$getLeftColumn()] = arguments.lft;
arguments.node[$getRightColumn()] = arguments.rgt;
arguments.node.$update(parameterize=true, reload=false); // pass all callbacks and validations
</cfscript>
<cfreturn arguments.rgt + 1 />
</cffunction>
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
instance level methods
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="isRoot" returntype="boolean" access="public" output="false" hint="I return true if the current node is a root node.">
<cfreturn not $idIsValid(this[$getParentColumn()]) />
</cffunction>
<cffunction name="isChild" returntype="boolean" access="public" output="false" hint="I return true if the current node is a child node.">
<cfreturn $idIsValid(this[$getParentColumn()]) />
</cffunction>
<cffunction name="isLeaf" returntype="boolean" access="public" output="false" hint="I return true if the current node is a leaf node (i.e. has no children).">
<cfreturn !isNew() and (this[$getRightColumn()] - this[$getLeftColumn()] eq 1) />
</cffunction>
<cffunction name="root" returntype="any" access="public" output="false" hint="I return the root object for the current node's branch.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# <= #this[$getLeftColumn()]# AND #$getRightColumn()# >= #this[$getRightColumn()]# AND #$getParentColumn()# IS NULL")>
<cfreturn findOne(argumentcollection=arguments) />
</cffunction>
<cffunction name="parent" returntype="any" access="public" output="false" hint="I return the parent of the current node.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getIdColumn()# #$formatIdForQuery(this[$getParentColumn()])#")>
<cfreturn findOne(argumentCollection=arguments)>
</cffunction>
<cffunction name="selfAndAncestors" returntype="any" access="public" output="false" hint="I return the current node and all of its parents down to the root.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# DESC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# <= #this[$getLeftColumn()]# AND #$getRightColumn()# >= #this[$getRightColumn()]#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="ancestors" returntype="any" access="public" output="false" hint="I return all of the current nodes's parents down to the root.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# DESC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# < #this[$getLeftColumn()]# AND #$getRightColumn()# > #this[$getRightColumn()]#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="selfAndSiblings" returntype="any" access="public" output="false" hint="I return the current node and its siblings.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getParentColumn()# #$formatIdForQuery(this[$getParentColumn()])#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="siblings" returntype="any" access="public" output="false" hint="I return the current node's siblings.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getParentColumn()# #$formatIdForQuery(this[$getParentColumn()])# AND #$getIdColumn()# != #$formatIdForQuery(this[$getIdColumn()])#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="leaves" returntype="any" access="public" output="false" hint="I return all children of the current node that do not have children themselves.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# > #this[$getLeftColumn()]# AND #$getRightColumn()# < #this[$getRightColumn()]# AND leaf = 1")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="selfAndDescendants" returntype="any" access="public" output="false" hint="I return the current node and its descendants.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# >= #this[$getLeftColumn()]# AND #$getRightColumn()# <= #this[$getRightColumn()]#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="descendants" returntype="any" access="public" output="false" hint="I return the current node's descendants.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# > #this[$getLeftColumn()]# AND #$getRightColumn()# < #this[$getRightColumn()]#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="isDescendantOf" returntype="boolean" access="public" output="false" hint="I return true if the current node is a descendant of the supplied node.">
<cfargument name="target" type="any" required="true" hint="I am either the id of a node or the node itself.">
<cfset arguments.target = $getObject(arguments.target)>
<cfif IsObject(arguments.target) and arguments.target[$getLeftColumn()] lt this[$getLeftColumn()] and this[$getLeftColumn()] lt arguments.target[$getRightColumn()] and isSameScope(arguments.target)>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="isAncestorOf" returntype="boolean" access="public" output="false" hint="I return true if the current node is an ancestor of the supplied node.">
<cfargument name="target" type="any" required="true" hint="I am either the id of a node or the node itself.">
<cfset arguments.target = $getObject(arguments.target)>
<cfif IsObject(arguments.target) and this[$getLeftColumn()] lt arguments.target[$getLeftColumn()] and arguments.target[$getLeftColumn()] lt this[$getRightColumn()] and isSameScope(arguments.target)>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="level" returntype="numeric" access="public" output="false" hint="I return the current level of this node as expressed from the root.">
<cfif not IsNumeric(this[$getParentColumn()])>
<cfreturn 1>
</cfif>
<cfreturn selfAndAncestors(returnAs="query").RecordCount>
</cffunction>
<cffunction name="selfAndChildren" returntype="any" access="public" output="false" hint="I return the current node and its immediate children.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"(#$getParentColumn()# #$formatIdForQuery(this[$getIdColumn()])# OR #$getIdColumn()# #$formatIdForQuery(this[$getIdColumn()])#)")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="children" returntype="any" access="public" output="false" hint="I return the current node's immediate children.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getParentColumn()# #$formatIdForQuery(this[$getIdColumn()])#")>
<cfreturn findAll(argumentCollection=arguments) />
</cffunction>
<cffunction name="isSameScope" returntype="boolean" access="public" output="false" hint="I return true if the supplied target is of the same scope as the current node.">
<cfargument name="target" type="any" required="true" hint="I am either the id of a node or the node itself.">
<cfscript>
var loc = {};
// check target object exists
arguments.target = $getObject(arguments.target);
if (not IsObject(arguments.target))
return false;
// no scoping defined, true by default
if (not Len($getScope()))
return true;
for (loc.i=1; loc.i lte ListLen($getScope()); loc.i++)
if (this[ListGetAt($getScope(), loc.i)] != arguments.target[ListGetAt($getScope(), loc.i)])
return false;
</cfscript>
<cfreturn true />
</cffunction>
<cffunction name="leftSibling" returntype="any" access="public" output="false" hint="I return the nearest sibling to the left of the current node.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getRightColumn()# = #this[$getLeftColumn()] - 1# AND #$getParentColumn()# #$formatIdForQuery(this[$getParentColumn()])#")>
<cfreturn findOne(argumentCollection=arguments) />
</cffunction>
<cffunction name="rightSibling" returntype="any" access="public" output="false" hint="I return the nearest sibling to the right of the current node.">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="order" type="string" required="false" default="#$getLeftColumn()# ASC">
<cfset arguments.where = $createScopedWhere(arguments.where,"#$getLeftColumn()# = #this[$getRightColumn()] + 1# AND #$getParentColumn()# #$formatIdForQuery(this[$getParentColumn()])#")>
<cfreturn findOne(argumentCollection=arguments) />
</cffunction>
<cffunction name="moveLeft" returntype="boolean" access="public" output="false" hint="I exchange position with the nearest sibling to the left of the current node.">
<cfreturn moveToLeftOf(leftSibling())>
</cffunction>
<cffunction name="moveRight" returntype="boolean" access="public" output="false" hint="I exchange position with the nearest sibling to the right of the current node.">
<cfreturn moveToRightOf(rightSibling())>
</cffunction>
<cffunction name="moveToLeftOf" returntype="boolean" access="public" output="false" mixin="model" hint="I move the current node to the left of the target node.">
<cfargument name="target" type="any" required="true" hint="I am either the id of a node or the node itself.">
<cfset arguments.target = $getObject(arguments.target)>
<cfif IsObject(arguments.target)>
<cfreturn $moveTo(arguments.target,"left")>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="moveToRightOf" returntype="boolean" access="public" output="false" mixin="model" hint="I move the current node to the right of the target node.">
<cfargument name="target" type="any" required="true" hint="I am either the id of a node or the node itself.">
<cfset arguments.target = $getObject(arguments.target)>
<cfif IsObject(arguments.target)>
<cfreturn $moveTo(arguments.target, "right")>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="moveToChildOf" returntype="boolean" access="public" output="false" mixin="model" hint="I move the current node underneath the target node.">
<cfargument name="target" type="any" required="true" hint="I am either the id of a node or the node itself.">
<cfset arguments.target = $getObject(arguments.target)>
<cfif IsObject(arguments.target)>
<cfreturn $moveTo(arguments.target, "child")>
</cfif>
<cfreturn false>
</cffunction>
<cffunction name="moveToRoot" returntype="boolean" access="public" output="false" hint="I move the current node to the first root position.">
<cfreturn $moveTo("", "root") />
</cffunction>
<cffunction name="isMovePossible" returntype="boolean" access="public" output="false" hint="I check to see that the requested move is possible.">
<cfargument name="target" type="component" required="true" />
<cfscript>
if (this[$getIdColumn()] == arguments.target[$getIdColumn()] or not isSameScope(arguments.target))
return false;
if (((this[$getLeftColumn()] lte arguments.target[$getLeftColumn()] and this[$getRightColumn()] gte arguments.target[$getLeftColumn()])
or (this[$getLeftColumn()] lte arguments.target[$getRightColumn()] and this[$getRightColumn()] gte arguments.target[$getRightColumn()])))
return false;
return true;
</cfscript>
</cffunction>
<cffunction name="toText" returntype="string" access="public" output="false">
<cfthrow type="Wheels.Plugins.NestedSet.NotImplemented" message="This method has not been implemented yet." />
</cffunction>
<!-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
private methods
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------>
<cffunction name="$checkForNewParent" returntype="boolean" access="public" output="false">
<cfset variables.moveToNewParent = hasChanged($getParentColumn()) />
<cfreturn true />
</cffunction>
<cffunction name="$moveToNewParent" returntype="boolean" access="public" output="false">
<cfscript>
var parent = false;
if (StructKeyExists(this, $getParentColumn()))
parent = $getObject(this[$getParentColumn()]);
if (IsObject(parent))
{
if (!isSameScope(parent))
$throw(type="Wheels.Plugins.NestedSet.ScopeMismatch",message="The supplied parent is not within the same scope as the item you are trying to insert.");
else if (StructKeyExists(variables, "moveToNewParent") && IsBoolean(variables.moveToNewParent) && variables.moveToNewParent)
moveToChildOf(parent);
}
// delete the instance variable
StructDelete(variables, "moveToNewParent", false);
</cfscript>
<cfreturn true />
</cffunction>
<cffunction name="$setDefaultLeftAndRight" returntype="void" access="public" output="false">
<cfset this[$getLeftColumn()] = Val(this.maximum(where=$createScopedWhere("", ""), property=$getRightColumn(), reload=true)) + 1>
<cfset this[$getRightColumn()] = this[$getLeftColumn()] + 1>
</cffunction>
<!---
removes all descendants of itself before being deleted
if you would like callbacks to run for each object deleted, simply
pass the argument instanciateOnDelete=true into nestedSet()
--->
<cffunction name="$deleteDescendants" returntype="boolean" access="public" output="false">
<cfscript>
var loc = {};
// make sure this method can only run on the original object
if (StructKeyExists(request, "deleteDescendantsCalled"))
return true;
if (not IsNumeric(this[$getRightColumn()]) or not IsNumeric(this[$getLeftColumn()]))
return true;
arguments.where = $createScopedWhere(where="", append="#$getLeftColumn()# > #this[$getLeftColumn()]# AND #$getRightColumn()# < #this[$getRightColumn()]#");
request.deleteDescendantsCalled = true;
deleteAll(argumentCollection=arguments, instantiate=$getInstantiateOnDelete());
loc.diff = this[$getRightColumn()] - this[$getLeftColumn()] + 1;
</cfscript>
<cfquery name="loc.query" attributeCollection="#$getQueryArguments()#">
UPDATE #tableName()#
SET #$getLeftColumn()# = #$getLeftColumn()# - <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.diff#">
WHERE #$getLeftColumn()# > <cfqueryparam cfsqltype="cf_sql_integer" value="#this[$getRightColumn()]#">
<cfloop list="#$getScope()#" index="loc.i">
AND #loc.i# = <cfqueryparam cfsqltype="#variables.wheels.class.properties[loc.i].type#" value="#this[loc.i]#">
</cfloop>
;
UPDATE #tableName()#
SET #$getRightColumn()# = #$getRightColumn()# - <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.diff#">
WHERE #$getRightColumn()# > <cfqueryparam cfsqltype="cf_sql_integer" value="#this[$getRightColumn()]#">
<cfloop list="#$getScope()#" index="loc.i">
AND #loc.i# = <cfqueryparam cfsqltype="#variables.wheels.class.properties[loc.i].type#" value="#this[loc.i]#">
</cfloop>
</cfquery>
<cfreturn true />
</cffunction>
<!---
core private method used to move items around in the tree
--->
<cffunction name="$moveTo" returntype="any" access="public" output="false">
<cfargument name="target" type="any" required="true" />
<cfargument name="position" type="string" required="true" hint="may be one of 'child, left, right, root'" />
<cfscript>
var loc = {};
if (isNew())
$updatePersistedProperties();
if (!$callback("beforeMove", true))
return false;
// reload objects so we have the freshest data
arguments.target.reload();
this.reload();
// make sure we can do the move
if (arguments.position != "root" and !isMovePossible(arguments.target))
$throw(type="Wheels.Plugins.NestedSet.MoveNotAllowed", message="Impossible move, target node cannot be inside moved tree.");
switch (arguments.position)
{
case "child": { loc.bound = arguments.target[$getRightColumn()]; break; }
case "left": { loc.bound = arguments.target[$getLeftColumn()]; break; }
case "right": { loc.bound = arguments.target[$getRightColumn()] + 1; break; }
case "root": { loc.bound = 1; break; }
default: {
$throw(type="Wheels.Plugins.NestedSet.IncorrectArgumentValue", message="Position should be 'child', 'left', 'right' or 'root' ('#arguments.position#' received).");
}
}
if (loc.bound gt this[$getRightColumn()])
{
loc.bound--;
loc.otherBound = this[$getRightColumn()] + 1;
}
else
{
loc.otherBound = this[$getLeftColumn()] - 1;
}
if (loc.bound == this[$getRightColumn()] or loc.bound == this[$getLeftColumn()])
return true;
loc.sortArray = [this[$getLeftColumn()], this[$getRightColumn()], loc.bound, loc.otherBound];
ArraySort(loc.sortArray, "numeric");
loc.a = loc.sortArray[1];
loc.b = loc.sortArray[2];
loc.c = loc.sortArray[3];
loc.d = loc.sortArray[4];
switch (arguments.position)
{
case "child": { loc.newParent = target[$getIdColumn()]; break; }
case "root": { loc.newParent = "NULL"; break; }
default: { loc.newParent = target[$getParentColumn()]; break; }
}
</cfscript>
<cfquery name="loc.update" attributeCollection="#$getQueryArguments()#">
UPDATE #tableName()#
SET #$getLeftColumn()# =
CASE
WHEN #$getLeftColumn()# BETWEEN <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.a#"> AND <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.b#">
THEN #$getLeftColumn()# + <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.d#"> - <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.b#">
WHEN #$getLeftColumn()# BETWEEN <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.c#"> AND <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.d#">
THEN #$getLeftColumn()# + <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.a#"> - <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.c#">
ELSE #$getLeftColumn()#
END,
#$getRightColumn()# =
CASE
WHEN #$getRightColumn()# BETWEEN <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.a#"> AND <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.b#">
THEN #$getRightColumn()# + <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.d#"> - <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.b#">
WHEN #$getRightColumn()# BETWEEN <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.c#"> AND <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.d#">
THEN #$getRightColumn()# + <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.a#"> - <cfqueryparam cfsqltype="cf_sql_integer" value="#loc.c#">
ELSE #$getRightColumn()#
END,
#$getParentColumn()# =
CASE
WHEN #$getIdColumn()# = <cfqueryparam cfsqltype="#$getIdType()#" value="#this[$getIdColumn()]#">
THEN <cfif arguments.position eq "root" or ($idsAreNullable() and not Len(loc.newParent))>
NULL
<cfelse>
<cfqueryparam cfsqltype="#$getIdType()#" value="#loc.newParent#">
</cfif>
ELSE #$getParentColumn()#
END
WHERE 1 = 1
<cfloop list="#$getScope()#" index="loc.i">
AND #loc.i# = <cfqueryparam cfsqltype="#variables.wheels.class.properties[loc.i].type#" value="#this[loc.i]#">
</cfloop>
</cfquery>
<cfreturn $callback("afterMove", true) />
</cffunction>
<!---
all instance queries should scope their where clauses with the scope parameter
passed into hasNestedSet()
--->
<cffunction name="$createScopedWhere" returntype="string" access="public" output="false">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="append" type="string" required="false" default="">
<cfset var loc = {}>
<cfset arguments.where = $appendWhereClause(argumentCollection=arguments)>
<!--- loop over the list of scopes and add each one in turn --->
<cfloop list="#$getScope()#" index="loc.property">
<cfscript>
loc.value = this[loc.property];
if (!$propertyIsInteger(loc.property))
loc.value = "'#loc.value#'";
if (Len(arguments.where))
arguments.where = arguments.where & " AND ";
arguments.where = arguments.where & "#loc.property#=#loc.value#";
</cfscript>
</cfloop>
<cfreturn arguments.where>
</cffunction>
<cffunction name="$appendWhereClause" returntype="string" access="public" output="false">
<cfargument name="where" type="string" required="false" default="">
<cfargument name="append" type="string" required="false" default="">
<cfscript>
arguments.where = Trim(arguments.where);
arguments.append = Trim(arguments.append);
if (Len(arguments.append))
if (Len(arguments.where))
return arguments.where & " AND " & arguments.append;
return arguments.append;
return arguments.where;
</cfscript>
</cffunction>
<!---
developers should be able to pass in an object or key and we get the object
--->
<cffunction name="$getObject" returntype="any" access="public" output="false">
<cfargument name="identifier" type="any" required="true" hint="An id or an object." />
<cfscript>
if (IsObject(arguments.identifier))
return arguments.identifier;
else if ($idIsValid(arguments.identifier))
return findOne(where="#$getIdColumn()# = #$formatIdForQuery(arguments.identifier)#");
else
return false;
</cfscript>
</cffunction>
<cffunction name="$getQueryArguments" returntype="struct" access="public" output="false">
<cfscript>
var loc = {};
loc.datasource = variables.wheels.class.connection.datasource;
if (Len(variables.wheels.class.connection.username))
loc.username = variables.wheels.class.connection.username;
if (Len(variables.wheels.class.connection.password))
loc.password = variables.wheels.class.connection.password;
</cfscript>
<cfreturn loc>
</cffunction>
</cfcomponent>