Part-DB.Part-DB-server/src/Controller/TreeController.php

152 lines
4.4 KiB
PHP
Raw Normal View History

2019-03-24 15:25:40 +01:00
<?php
/**
2019-11-01 13:40:30 +01:00
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony)
2019-03-24 15:25:40 +01:00
*
2019-11-01 13:40:30 +01:00
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
2019-03-24 15:25:40 +01:00
*
* 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-11-01 13:40:30 +01:00
*
2019-03-24 15:25:40 +01:00
*/
namespace App\Controller;
use App\Entity\Devices\Device;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
2019-03-24 15:25:40 +01:00
use App\Services\ToolsTreeBuilder;
2019-03-25 12:44:44 +01:00
use App\Services\TreeBuilder;
2019-03-24 15:25:40 +01:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
/**
* This controller has the purpose to provide the data for all treeviews.
*
* @package App\Controller
*/
class TreeController extends AbstractController
{
/**
* @Route("/tree/tools", name="tree_tools")
2019-03-24 15:25:40 +01:00
*/
public function tools(ToolsTreeBuilder $builder)
{
$tree = $builder->getTree();
//Ignore null values, to save data
return $this->json($tree, 200, [], ['skip_null_values' => true]);
}
/**
* @Route("/tree/category/{id}", name="tree_category")
2019-03-25 12:44:44 +01:00
* @Route("/tree/categories")
2019-03-24 15:25:40 +01:00
*/
public function categoryTree(TreeBuilder $builder, Category $category = null)
{
2019-08-20 18:39:57 +02:00
if ($category !== null) {
2019-03-24 15:25:40 +01:00
$tree[] = $builder->elementToTreeNode($category);
} else {
$tree = $builder->typeToTree(Category::class);
}
return $this->json($tree, 200, [], ['skip_null_values' => true]);
}
2019-03-25 12:44:44 +01:00
/**
* @Route("/tree/footprint/{id}", name="tree_footprint")
* @Route("/tree/footprints")
*/
public function footprintTree(TreeBuilder $builder, Footprint $footprint = null)
{
2019-08-20 18:39:57 +02:00
if ($footprint !== null) {
2019-03-25 12:44:44 +01:00
$tree[] = $builder->elementToTreeNode($footprint);
} else {
$tree = $builder->typeToTree(Footprint::class);
2019-03-25 12:44:44 +01:00
}
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)
{
2019-08-20 18:39:57 +02:00
if ($location !== null) {
2019-03-25 12:44:44 +01:00
$tree[] = $builder->elementToTreeNode($location);
} else {
$tree = $builder->typeToTree(Storelocation::class);
2019-03-25 12:44:44 +01:00
}
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)
{
2019-08-20 18:39:57 +02:00
if ($manufacturer !== null) {
2019-03-25 12:44:44 +01:00
$tree[] = $builder->elementToTreeNode($manufacturer);
} else {
$tree = $builder->typeToTree(Manufacturer::class);
2019-03-25 12:44:44 +01:00
}
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)
{
2019-08-20 18:39:57 +02:00
if ($supplier !== null) {
2019-03-25 12:44:44 +01:00
$tree[] = $builder->elementToTreeNode($supplier);
} else {
$tree = $builder->typeToTree(Supplier::class);
2019-03-25 12:44:44 +01:00
}
2019-03-24 15:25:40 +01:00
2019-03-25 12:44:44 +01:00
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)
{
2019-08-20 18:39:57 +02:00
if ($device !== null) {
2019-03-25 12:44:44 +01:00
$tree[] = $builder->elementToTreeNode($device);
} else {
$tree = $builder->typeToTree(Device::class, null);
}
return $this->json($tree, 200, [], ['skip_null_values' => true]);
}
2019-03-24 15:25:40 +01:00
}