url_generator = $URLGenerator; $this->em = $em; } /** * Generates a tree for the given Element. The given element is the top node, all children are child nodes. * @param StructuralDBElement $element The element for which the tree should be generated. * @param string $href_type The type of the links that should be used for the links. Set to null, to disable links. * See EntityURLGenerator::getURL for possible types. * @return TreeViewNode The Node for the given Element. */ public function elementToTreeNode(StructuralDBElement $element, string $href_type = 'list_parts') : TreeViewNode { $children = $element->getSubelements(); $children_nodes = null; foreach ($children as $child) { $children_nodes[] = $this->elementToTreeNode($child); } //Check if we need to generate a href type $href = null; if (!empty($href_type)) { $href = $this->url_generator->getURL($element, $href_type); } return new TreeViewNode($element->getName(), $href, $children_nodes); } /** * Generates a tree for all elements of the given type * @param StructuralDBElement $class_name The class name of the StructuralDBElement class for which the tree should * be generated. * @param string $href_type The type of the links that should be used for the links. Set to null, to disable links. * See EntityURLGenerator::getURL for possible types. * @return TreeViewNode[] Returns an array, containing all nodes. It is empty if the given class has no elements. */ public function typeToTree(StructuralDBElement $class_name, string $href_type = 'list_parts') : array { /** * @var $repo StructuralDBElementRepository */ $repo = $this->em->getRepository($class_name); $root_nodes = $repo->findRootNodes(); $array = array(); foreach ($root_nodes as $node) { $array = $this->elementToTreeNode($node, $href_type); } return $array; } }