From 1bc4b377eab26e4011db56d9be6d6d0e622e163a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 4 Dec 2017 13:25:03 +0100 Subject: [PATCH] Prevent circular node references refs #149 --- application/forms/AddNodeForm.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index bda7d4d..c684acb 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -327,9 +327,12 @@ class AddNodeForm extends QuickForm { $list = array(); + $parents = array(); + $this->collectAllParents($this->parent, $parents); + $parents[$this->parent->getName()] = $this->parent; + foreach ($this->bp->getNodes() as $node) { - if ($node instanceof BpNode) { - // TODO: Blacklist parents + if ($node instanceof BpNode && ! isset($parents[$node->getName()])) { $list[(string) $node] = (string) $node; // display name? } } @@ -338,6 +341,20 @@ class AddNodeForm extends QuickForm return $list; } + /** + * Collect the given node's parents recursively into the given array by their names + * + * @param BpNode $node + * @param BpNode[] $parents + */ + protected function collectAllParents(BpNode $node, array & $parents) + { + foreach ($node->getParents() as $parent) { + $parents[$parent->getName()] = $parent; + $this->collectAllParents($parent, $parents); + } + } + protected function fetchObjectList() { $this->objectList = array();