Part-DB.Part-DB-server/src/Services/TreeBuilder.php

105 lines
3.6 KiB
PHP
Raw Normal View History

2019-03-24 15:25:40 +01:00
<?php
/**
2019-03-25 12:44:44 +01:00
*
2019-03-24 15:25:40 +01:00
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
2019-03-25 12:44:44 +01:00
* http://www.cl-projects.de/
2019-03-24 15:25:40 +01:00
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2019-03-25 12:44:44 +01:00
*
2019-03-24 15:25:40 +01:00
*/
2019-03-25 12:44:44 +01:00
namespace App\Services;
2019-03-24 15:25:40 +01:00
use App\Entity\StructuralDBElement;
use App\Helpers\TreeViewNode;
use App\Repository\StructuralDBElementRepository;
use Doctrine\ORM\EntityManagerInterface;
/**
* This service gives you multiple possibilities to generate trees.
*
* @package App\Controller
*/
class TreeBuilder
{
protected $url_generator;
protected $em;
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em)
{
$this->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.
*/
2019-03-25 12:44:44 +01:00
public function elementToTreeNode(StructuralDBElement $element, ?string $href_type = 'list_parts') : TreeViewNode
2019-03-24 15:25:40 +01:00
{
$children = $element->getSubelements();
$children_nodes = null;
foreach ($children as $child) {
2019-03-25 12:44:44 +01:00
$children_nodes[] = $this->elementToTreeNode($child, $href_type);
2019-03-24 15:25:40 +01:00
}
//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.
*/
2019-03-25 12:44:44 +01:00
public function typeToTree(string $class_name, ?string $href_type = 'list_parts') : array
2019-03-24 15:25:40 +01:00
{
/**
* @var $repo StructuralDBElementRepository
*/
$repo = $this->em->getRepository($class_name);
$root_nodes = $repo->findRootNodes();
$array = array();
foreach ($root_nodes as $node) {
2019-03-25 12:44:44 +01:00
$array[] = $this->elementToTreeNode($node, $href_type);
2019-03-24 15:25:40 +01:00
}
return $array;
}
}