url_generator = $URLGenerator; $this->em = $em; $this->translator = $translator; $this->security = $security; $this->cache = $treeCache; } /** * 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. * @param DBElement|null $selectedElement When a element is given here, its tree node will be marked as selected in * the resulting tree. When $selectedElement is not existing in the tree, then nothing happens. * @return TreeViewNode The Node for the given Element. * @throws \App\Exceptions\EntityNotSupported */ public function elementToTreeNode(NamedDBElement $element, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : TreeViewNode { $children_nodes = null; if ($element instanceof StructuralDBElement) { $children = $element->getSubelements(); foreach ($children as $child) { $children_nodes[] = $this->elementToTreeNode($child, $href_type, $selectedElement); } } //Check if we need to generate a href type $href = null; if (!empty($href_type)) { $href = $this->url_generator->getURL($element, $href_type); } $tree_node = new TreeViewNode($element->__toString(), $href, $children_nodes); if($children_nodes != null) { $tree_node->addTag((string) count($children_nodes)); } //Check if we need to select the current part if ($selectedElement !== null && $element->getID() === $selectedElement->getID()) { $tree_node->setSelected(true); } return $tree_node; } /** * 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. * @param DBElement|null $selectedElement When a element is given here, its tree node will be marked as selected in * the resulting tree. When $selectedElement is not existing in the tree, then nothing happens. * @return TreeViewNode[] Returns an array, containing all nodes. It is empty if the given class has no elements. * @throws \App\Exceptions\EntityNotSupported */ public function typeToTree(string $class_name, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : array { /** * @var $repo StructuralDBElementRepository */ $repo = $this->em->getRepository($class_name); if (new $class_name() instanceof StructuralDBElement) { $root_nodes = $repo->findRootNodes(); } else { $root_nodes = $repo->findAll(); } $array = array(); //When we use the newEdit type, add the New Element node. if ($href_type === 'newEdit') { //Generate the url for the new node $href = $this->url_generator->createURL(new $class_name()); $new_node = new TreeViewNode($this->translator->trans('entity.tree.new'), $href); //When the id of the selected element is null, then we have a new element, and we need to select "new" node if ($selectedElement != null && $selectedElement->getID() == null) { $new_node->setSelected(true); } $array[] = $new_node; //Add spacing $array[] = (new TreeViewNode(''))->setDisabled(true); //Every other treeNode will be used for edit $href_type = "edit"; } foreach ($root_nodes as $node) { $array[] = $this->elementToTreeNode($node, $href_type, $selectedElement); } return $array; } /** * Gets a flattened hierachical tree. Useful for generating option lists. * In difference to the Repository Function, the results here are cached. * @param string $class_name The class name of the entity you want to retrieve. * @param StructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root * @return StructuralDBElement[] A flattened list containing the tree elements. * @throws \Psr\Cache\InvalidArgumentException */ public function typeToNodesList(string $class_name, ?StructuralDBElement $parent = null): array { $username = $this->security->getUser()->getUsername(); $parent_id = $parent != null ? $parent->getID() : "0"; // Backslashes are not allowed in cache keys $secure_class_name = str_replace("\\", '_', $class_name); $key = "list_" . $username . "_" . $secure_class_name . $parent_id; $ret = $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name, $username) { // Invalidate when groups, a element with the class or the user changes $item->tag(['groups', 'tree_list', 'user_' . $username, $secure_class_name]); /** * @var $repo StructuralDBElementRepository */ $repo = $this->em->getRepository($class_name); return $repo->toNodesList($parent); }); return $ret; } }