@@ -80,30 +80,45 @@ public function __invoke(Request $request)
8080 ],
8181 'levels_up ' => 'nullable|integer|min:0 ' ,
8282 'levels_down ' => 'nullable|integer|min:0 ' ,
83+ 'ancestor_resources ' => 'nullable|boolean '
8384 ]);
8485
8586 // now, retrieve the input
8687 $ node_id = $ request ->input ('id ' );
8788 $ node_id = is_null ($ node_id ) ? 0 : $ node_id ;
8889 $ up = $ request ->input ('levels_up ' );
8990 $ down = $ request ->input ('levels_down ' );
91+ $ ancestor_resources = $ request ->input ('ancestor_resources ' );
92+ $ ancestor_resources = is_null ($ ancestor_resources ) ? false : $ ancestor_resources ;
9093
91- return $ this ->show ($ node_id , $ up , $ down );
94+ return $ this ->show ($ node_id , $ up , $ down, $ ancestor_resources );
9295 }
9396
9497 /**
9598 * converts a portion of the tree to JSON for traversal by the JavaScript team
96- * @param integer $node_id the id of the current node in the tree; defaults to the root of the tree, which has an id of 0
97- * @param int|null $levels_up the number of ancestor levels of the tree to return; defaults to infinity
98- * @param int|null $levels_down the number of descendant levels of the tree to return; defaults to infinity
99+ * @param integer $node_id the id of the current node in the tree; defaults to the root of the tree, which has an id of 0
100+ * @param int|null $levels_up the number of ancestor levels of the tree to return; defaults to infinity
101+ * @param int|null $levels_down the number of descendant levels of the tree to return; defaults to infinity
102+ * @param bool|null $ancestor_resources whether to return the resources of the ancestors; defaults to true
99103 * @return Collection the nodes and connections of the target portion of the tree
100104 */
101- public function show ($ node_id = 0 , int $ levels_up = null , int $ levels_down = null )
105+ public function show ($ node_id = 0 , int $ levels_up = null , int $ levels_down = null , bool $ ancestor_resources = false )
102106 {
103- // first, get the required data
104- $ this ->getNodes ($ node_id , $ levels_up , $ levels_down );
107+ // first, get the required descendants (and the current node)
108+ $ this ->getDescendantNodes ($ node_id , $ levels_down );
109+ // now, get the node_ids, since we'll need to get resources for them
110+ $ node_ids = $ this ->tree ->pluck ('id ' );
111+ // next, get the ancestor nodes
112+ $ this ->getAncestorNodes ($ node_id , $ levels_up );
105113 // then, convert the data to the nodes/connections format
106- $ node_ids = $ this ->convertFormat ();
114+ if ($ ancestor_resources )
115+ {
116+ $ node_ids = $ this ->convertFormat ();
117+ }
118+ else
119+ {
120+ $ this ->convertFormat ();
121+ }
107122
108123 // get the converted resources of each node in the tree
109124 $ resources = $ this ->getResourceNodes ($ node_ids );
@@ -122,18 +137,34 @@ public function show($node_id = 0, int $levels_up = null, int $levels_down = nul
122137 * @param int|null $levels_up the number of ancestor levels of the tree to return; defaults to infinity
123138 * @param int|null $levels_down the number of descendant levels of the tree to return; defaults to infinity
124139 */
125- private function getNodes ($ node_id , $ levels_up , $ levels_down )
140+ private function getAncestorNodes ($ node_id , $ levels_up )
141+ {
142+ $ root = $ this ->model_name ::getRoot ();
143+ $ node = null ;
144+ if ($ node_id != 0 )
145+ {
146+ $ node = $ this ->model_name ::find ($ node_id );
147+ }
148+ // get ancestors
149+ $ this ->tree = (new $ this ->repo )->ancestors ($ node , $ levels_up , $ root )->merge ($ this ->tree );
150+ }
151+
152+ /**
153+ * get the raw nodes and connections data
154+ * @param integer $node_id the id of the current node in the tree; defaults to the root of the tree, which has an id of 0
155+ * @param int|null $levels_up the number of ancestor levels of the tree to return; defaults to infinity
156+ * @param int|null $levels_down the number of descendant levels of the tree to return; defaults to infinity
157+ */
158+ private function getDescendantNodes ($ node_id , $ levels_down )
126159 {
127160 $ root = $ this ->model_name ::getRoot ();
128161 $ node = null ;
129162 if ($ node_id != 0 )
130163 {
131164 $ node = $ this ->model_name ::find ($ node_id );
132165 }
133- // get the ancestors and descendants of this node in a flat collection
134- $ this ->tree = (new $ this ->repo )->ancestors ($ node , $ levels_up , $ root )->merge (
135- (new $ this ->repo )->descendants ($ node , $ levels_down , $ root )
136- );
166+ // get the descendants
167+ $ this ->tree = (new $ this ->repo )->descendants ($ node , $ levels_down , $ root );
137168 // add the current node to the data
138169 // but use the root if the current node is null
139170 $ this ->tree ->prepend (collect (is_null ($ node ) ? $ root : $ node ));
0 commit comments