mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-08 12:09:30 +00:00
Added working treeviews and buttons.
This commit is contained in:
parent
82761a3454
commit
afd45d464c
17 changed files with 547 additions and 74 deletions
|
|
@ -1,104 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\StructuralDBElement;
|
||||
use App\Helpers\TreeViewNode;
|
||||
use App\Repository\StructuralDBElementRepository;
|
||||
use App\Services\EntityURLGenerator;
|
||||
use App\Services\ToolsTreeBuilder;
|
||||
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.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -30,9 +30,16 @@
|
|||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Category;
|
||||
use App\Entity\Device;
|
||||
use App\Entity\Footprint;
|
||||
use App\Entity\Manufacturer;
|
||||
use App\Entity\Storelocation;
|
||||
use App\Entity\Supplier;
|
||||
use App\Helpers\TreeViewNode;
|
||||
use App\Services\ToolsTreeBuilder;
|
||||
use App\Services\TreeBuilder;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
/**
|
||||
|
|
@ -55,10 +62,11 @@ class TreeController extends AbstractController
|
|||
|
||||
/**
|
||||
* @Route("/tree/category/{id}", name="tree_category")
|
||||
* @Route("/tree/categories")
|
||||
*/
|
||||
public function categoryTree(TreeBuilder $builder, Category $category = null)
|
||||
{
|
||||
if($category != null) {
|
||||
if ($category != null) {
|
||||
$tree[] = $builder->elementToTreeNode($category);
|
||||
} else {
|
||||
$tree = $builder->typeToTree(Category::class);
|
||||
|
|
@ -68,7 +76,85 @@ class TreeController extends AbstractController
|
|||
return $this->json($tree, 200, [], ['skip_null_values' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/tree/footprint/{id}", name="tree_footprint")
|
||||
* @Route("/tree/footprints")
|
||||
*/
|
||||
public function footprintTree(TreeBuilder $builder, Footprint $footprint = null)
|
||||
{
|
||||
if ($footprint != null) {
|
||||
$tree[] = $builder->elementToTreeNode($footprint);
|
||||
} else {
|
||||
$tree = $builder->typeToTree(Footprint::class, null);
|
||||
}
|
||||
|
||||
|
||||
return $this->json($tree, 200, [], ['skip_null_values' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/tree/location/{id}", name="tree_location")
|
||||
* @Route("/tree/locations")
|
||||
*/
|
||||
public function locationTree(TreeBuilder $builder, Storelocation $location = null)
|
||||
{
|
||||
if ($location != null) {
|
||||
$tree[] = $builder->elementToTreeNode($location);
|
||||
} else {
|
||||
$tree = $builder->typeToTree(Storelocation::class, null);
|
||||
}
|
||||
|
||||
|
||||
return $this->json($tree, 200, [], ['skip_null_values' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/tree/manufacturer/{id}", name="tree_manufacturer")
|
||||
* @Route("/tree/manufacturers")
|
||||
*/
|
||||
public function manufacturerTree(TreeBuilder $builder, Manufacturer $manufacturer = null)
|
||||
{
|
||||
if ($manufacturer != null) {
|
||||
$tree[] = $builder->elementToTreeNode($manufacturer);
|
||||
} else {
|
||||
$tree = $builder->typeToTree(Manufacturer::class, null);
|
||||
}
|
||||
|
||||
|
||||
return $this->json($tree, 200, [], ['skip_null_values' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/tree/supplier/{id}", name="tree_supplier")
|
||||
* @Route("/tree/suppliers")
|
||||
*/
|
||||
public function supplierTree(TreeBuilder $builder, Supplier $supplier = null)
|
||||
{
|
||||
if ($supplier != null) {
|
||||
$tree[] = $builder->elementToTreeNode($supplier);
|
||||
} else {
|
||||
$tree = $builder->typeToTree(Supplier::class, null);
|
||||
}
|
||||
|
||||
|
||||
return $this->json($tree, 200, [], ['skip_null_values' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/tree/device/{id}", name="tree_device")
|
||||
* @Route("/tree/devices")
|
||||
*/
|
||||
public function deviceTree(TreeBuilder $builder, Device $device = null)
|
||||
{
|
||||
if ($device != null) {
|
||||
$tree[] = $builder->elementToTreeNode($device);
|
||||
} else {
|
||||
$tree = $builder->typeToTree(Device::class, null);
|
||||
}
|
||||
|
||||
|
||||
return $this->json($tree, 200, [], ['skip_null_values' => true]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue