1- <?php namespace Kalnoy \Nestedset ;
1+ <?php
2+
3+ namespace Kalnoy \Nestedset ;
24
35use \Illuminate \Database \Eloquent \Model as Eloquent ;
46use \Illuminate \Database \Query \Builder ;
@@ -41,6 +43,27 @@ class Node extends Eloquent {
4143 */
4244 const AFTER = 'after ' ;
4345
46+ /**
47+ * Whether model uses soft delete.
48+ *
49+ * @var bool
50+ *
51+ * @since 1.1
52+ */
53+ static protected $ softDelete ;
54+
55+ /**
56+ * {@inheritdoc}
57+ */
58+ protected static function boot ()
59+ {
60+ parent ::boot ();
61+
62+ $ instance = new static ;
63+
64+ static ::$ softDelete = method_exists ($ instance , 'withTrashed ' );
65+ }
66+
4467 /**
4568 * Get the root node.
4669 *
@@ -300,7 +323,7 @@ protected function beforeOrAfter(Node $node, $dir)
300323 */
301324 protected function checkTarget (Node $ node )
302325 {
303- if (! $ node ->exists || $ node ->isDirty (static ::LFT ))
326+ if ( ! $ node ->exists || $ node ->isDirty (static ::LFT ))
304327 {
305328 throw new Exception ("Target node is updated but not saved. " );
306329 }
@@ -361,19 +384,19 @@ public function fireModelEvent($event, $halt = true)
361384 {
362385 if ($ this ->exists )
363386 {
364- if ($ this ->isDirty (static ::LFT ) && !$ this ->updateTree ())
387+ if ($ this ->isDirty (static ::LFT ) && ! $ this ->updateTree ())
365388 {
366389 return false ;
367390 }
368391 }
369392 else
370393 {
371- if (! isset ($ this ->attributes [static ::LFT ]))
394+ if ( ! isset ($ this ->attributes [static ::LFT ]))
372395 {
373396 throw new Exception ("Cannot save node until it is inserted. " );
374397 }
375398
376- if (! $ this ->updateTree ()) return false ;
399+ if ( ! $ this ->updateTree ()) return false ;
377400 }
378401 }
379402
@@ -382,7 +405,7 @@ public function fireModelEvent($event, $halt = true)
382405 throw new Exception ("Cannot delete root node. " );
383406 }
384407
385- if ($ event === 'deleted ' && !$ this -> softDelete ) $ this ->deleteNode ();
408+ if ($ event === 'deleted ' && ! static :: $ softDelete ) $ this ->deleteNode ();
386409
387410 return parent ::fireModelEvent ($ event , $ halt );
388411 }
@@ -591,6 +614,16 @@ protected function newBaseQueryBuilder()
591614 return new QueryBuilder ($ conn , $ grammar , $ conn ->getPostProcessor (), $ this );
592615 }
593616
617+ /**
618+ * Get a new query including deleted nodes.
619+ *
620+ * @since 1.1
621+ */
622+ protected function newQueryWithDeleted ()
623+ {
624+ return static ::$ softDelete ? $ this ->withTrashed () : $ this ->newQuery ();
625+ }
626+
594627 /**
595628 * Create a new NestedSet Collection instance.
596629 *
@@ -610,7 +643,7 @@ public function newCollection(array $models = array())
610643 */
611644 public function getNodeHeight ()
612645 {
613- if (! $ this ->exists ) return 2 ;
646+ if ( ! $ this ->exists ) return 2 ;
614647
615648 return $ this ->attributes [static ::RGT ] - $ this ->attributes [static ::LFT ] + 1 ;
616649 }
@@ -635,7 +668,7 @@ public function getDescendantCount()
635668 */
636669 public function setParentIdAttribute ($ value )
637670 {
638- if (! isset ($ this ->attributes [static ::PARENT_ID ]) || $ this ->attributes [static ::PARENT_ID ] != $ value )
671+ if ( ! isset ($ this ->attributes [static ::PARENT_ID ]) || $ this ->attributes [static ::PARENT_ID ] != $ value )
639672 {
640673 $ this ->appendTo (static ::findOrFail ($ value ));
641674 }
0 commit comments