Show a treeview in the admin menus, to select between the different elements.

This commit is contained in:
Jan Böhmer 2019-04-05 17:49:02 +02:00
parent 928b574d8c
commit 650b388a1d
16 changed files with 269 additions and 25 deletions

View file

@ -67,7 +67,7 @@ class AttachmentTypeController extends AbstractController
}
/**
* @Route("/new")
* @Route("/new", name="attachment_type_new")
*
* @return \Symfony\Component\HttpFoundation\Response
*/

View file

@ -91,7 +91,7 @@ class Attachment extends NamedDBElement
*
* @return DBElement The associated Element.
*/
public function getElement(): AttachmentContainingDBElement
public function getElement(): ?AttachmentContainingDBElement
{
return $this->element;
}

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
@ -33,9 +34,9 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
/**
* @var
* //TODO
* //@ORM\OneToMany(targetEntity="Attachment", mappedBy="element")
* @ORM\OneToMany(targetEntity="Attachment", mappedBy="element")
*/
protected $attachment;
protected $attachments;
//TODO
protected $attachmentTypes;
@ -54,7 +55,7 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
*
* @throws Exception if there was an error
*/
public function getAttachmentTypes(): array
public function getAttachmentTypes(): ?array
{
return $this->attachmentTypes;
}
@ -70,7 +71,7 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
*
* @throws Exception if there was an error
*/
public function getAttachments($type_id = null, bool $only_table_attachements = false): array
public function getAttachments($type_id = null, bool $only_table_attachements = false) : Collection
{
if ($only_table_attachements || $type_id) {
$attachements = $this->attachments;

View file

@ -25,6 +25,7 @@ namespace App\Entity;
use App\Validator\Constraints\NoneOfItsChildren;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
@ -58,7 +59,7 @@ class AttachmentType extends StructuralDBElement
* @return Attachment[] all attachements with this type, as a one-dimensional array of Attachement-objects
* (sorted by their names)
*/
public function getAttachementsForType(): ArrayCollection
public function getAttachementsForType(): Collection
{
// the attribute $this->attachements is used from class "AttachementsContainingDBELement"
if (null === $this->attachments) {

View file

@ -40,6 +40,8 @@ class TreeViewNode
protected $href;
protected $nodes;
protected $state;
/**
* Creates a new TreeView node with the given parameters.
* @param string $text The text that is shown in the node. (e.g. the name of the node)
@ -53,6 +55,8 @@ class TreeViewNode
$this->text = $text;
$this->href = $href;
$this->nodes = $nodes;
$this->state = new TreeViewNodeState();
}
/**
@ -115,4 +119,29 @@ class TreeViewNode
return $this;
}
public function getState() : TreeViewNodeState
{
return $this->state;
}
public function setState(TreeViewNodeState $state) : self
{
$this->state = $state;
return $this;
}
public function setDisabled(?bool $disabled) : self
{
$this->state->setDisabled($disabled);
return $this;
}
public function setSelected(?bool $selected) : self
{
$this->state->setSelected($selected);
return $this;
}
}

View file

@ -0,0 +1,98 @@
<?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\Helpers;
class TreeViewNodeState
{
/** @var null|bool */
protected $checked = null;
/** @var null|bool */
protected $disabled = null;
/** @var null|bool */
protected $expanded = null;
/** @var null|bool */
protected $selected = null;
/**
* @return bool|null
*/
public function getDisabled(): ?bool
{
return $this->disabled;
}
/**
* @param bool|null $disabled
*/
public function setDisabled(?bool $disabled): void
{
$this->disabled = $disabled;
}
/**
* @return bool|null
*/
public function getExpanded(): ?bool
{
return $this->expanded;
}
/**
* @param bool|null $expanded
*/
public function setExpanded(?bool $expanded): void
{
$this->expanded = $expanded;
}
/**
* @return bool|null
*/
public function getSelected(): ?bool
{
return $this->selected;
}
/**
* @param bool|null $selected
*/
public function setSelected(?bool $selected): void
{
$this->selected = $selected;
}
}

View file

@ -29,6 +29,7 @@
namespace App\Services;
use App\Entity\AttachmentType;
use App\Entity\Category;
use App\Entity\NamedDBElement;
use App\Entity\Part;
@ -107,6 +108,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]);
}
if($entity instanceof AttachmentType) {
return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]);
}
//Otherwise throw an error
throw new EntityNotSupported('The given entity is not supported yet!');
}
@ -124,6 +129,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('part_new');
}
if($entity instanceof AttachmentType) {
return $this->urlGenerator->generate('attachment_type_new');
}
throw new EntityNotSupported('The given entity is not supported yet!');
}

View file

@ -31,10 +31,12 @@
namespace App\Services;
use App\Entity\DBElement;
use App\Entity\StructuralDBElement;
use App\Helpers\TreeViewNode;
use App\Repository\StructuralDBElementRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This service gives you multiple possibilities to generate trees.
@ -45,11 +47,13 @@ class TreeBuilder
{
protected $url_generator;
protected $em;
protected $translator;
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em)
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->url_generator = $URLGenerator;
$this->em = $em;
$this->translator = $translator;
}
/**
@ -57,15 +61,18 @@ class TreeBuilder
* @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(StructuralDBElement $element, ?string $href_type = 'list_parts') : TreeViewNode
public function elementToTreeNode(StructuralDBElement $element, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : TreeViewNode
{
$children = $element->getSubelements();
$children_nodes = null;
foreach ($children as $child) {
$children_nodes[] = $this->elementToTreeNode($child, $href_type);
$children_nodes[] = $this->elementToTreeNode($child, $href_type, $selectedElement);
}
//Check if we need to generate a href type
@ -75,7 +82,14 @@ class TreeBuilder
$href = $this->url_generator->getURL($element, $href_type);
}
return new TreeViewNode($element->getName(), $href, $children_nodes);
$tree_node = new TreeViewNode($element->getName(), $href, $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;
}
/**
@ -84,9 +98,12 @@ class TreeBuilder
* 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') : array
public function typeToTree(string $class_name, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : array
{
/**
* @var $repo StructuralDBElementRepository
@ -95,8 +112,27 @@ class TreeBuilder
$root_nodes = $repo->findRootNodes();
$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);
$array[] = $this->elementToTreeNode($node, $href_type, $selectedElement);
}
return $array;

View file

@ -29,22 +29,31 @@
namespace App\Twig;
use App\Entity\Attachment;
use App\Entity\DBElement;
use App\Services\EntityURLGenerator;
use App\Services\TreeBuilder;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
protected $entityURLGenerator;
protected $cache;
protected $serializer;
protected $treeBuilder;
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache)
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
SerializerInterface $serializer, TreeBuilder $treeBuilder)
{
$this->entityURLGenerator = $entityURLGenerator;
$this->cache = $cache;
$this->serializer = $serializer;
$this->treeBuilder = $treeBuilder;
}
public function getFilters()
@ -55,6 +64,19 @@ class AppExtension extends AbstractExtension
];
}
public function getFunctions()
{
return [
new TwigFunction('generateTreeData', [$this, 'treeData'])
];
}
public function treeData(DBElement $element, string $type = 'newEdit') : string
{
$tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element);
return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]);
}
public function generateEntityURL(DBElement $entity, string $method = 'info'): string
{
return $this->entityURLGenerator->getURL($entity, $method);