Added an table to show all defined attachments.

This commit is contained in:
Jan Böhmer 2019-10-04 18:06:37 +02:00
parent 2f0dc600e2
commit f53cc08f52
8 changed files with 437 additions and 17 deletions

View file

@ -0,0 +1,132 @@
<?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\Services;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\DBElement;
use App\Entity\Base\NamedDBElement;
use App\Entity\Devices\Device;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation;
use App\Entity\PriceInformations\Currency;
use App\Entity\PriceInformations\Orderdetail;
use App\Entity\PriceInformations\Pricedetail;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Exceptions\EntityNotSupportedException;
use Proxies\__CG__\App\Entity\Parts\Supplier;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Contracts\Translation\TranslatorInterface;
class ElementTypeNameGenerator
{
protected $translator;
protected $mapping;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
//Child classes has to become before parent classes
$this->mapping = [
Attachment::class => $this->translator->trans('attachment.label'),
Category::class => $this->translator->trans('category.label'),
AttachmentType::class => $this->translator->trans('attachment_type.label'),
Device::class => $this->translator->trans('device.label'),
Footprint::class => $this->translator->trans('footprint.label'),
Manufacturer::class => $this->translator->trans('manufacturer.label'),
MeasurementUnit::class => $this->translator->trans('measurement_unit.label'),
Part::class => $this->translator->trans('part.label'),
PartLot::class => $this->translator->trans('part_lot.label'),
Storelocation::class => $this->translator->trans('storelocation.label'),
Supplier::class => $this->translator->trans('supplier.label'),
Currency::class => $this->translator->trans('currency.label'),
Orderdetail::class => $this->translator->trans('orderdetail.label'),
Pricedetail::class => $this->translator->trans('pricedetail.label'),
Group::class => $this->translator->trans('group.label'),
User::class => $this->translator->trans('user.label'),
];
}
/**
* Gets an localized label for the type of the entity.
* A part element becomes "Part" ("Bauteil" in german) and a category object becomes "Category".
* Useful when the type should be shown to user.
* Throws an exception if the class is not supported.
* @param DBElement $entity The element for which the label should be generated
* @return string The locatlized label for the entity type.
* @throws EntityNotSupportedException When the passed entity is not supported.
*/
public function getLocalizedTypeLabel(DBElement $entity) : string
{
//Check if we have an direct array entry for our entity class, then we can use it
if (isset($this->mapping[get_class($entity)])) {
return $this->mapping[get_class($entity)];
}
//Otherwise iterate over array and check for inheritance (needed when the proxy element from doctrine are passed)
foreach ($this->mapping as $class => $translation) {
if ($entity instanceof $class) {
return $translation;
}
}
//When nothing was found throw an exception
throw new EntityNotSupportedException(
sprintf('No localized label for the element with type %s was found!', get_class($entity))
);
}
/**
* Returns a string like in the format ElementType: ElementName.
* For example this could be something like: "Part: BC547".
* It uses getLocalizedLabel to determine the type.
* @param NamedDBElement $entity The entity for which the string should be generated.
* @param bool $use_html If set to true, a html string is returned, where the type is set italic
* @return string The localized string
*/
public function getTypeNameCombination(NamedDBElement $entity, bool $use_html = false) : string
{
$type = $this->getLocalizedTypeLabel($entity);
if ($use_html) {
return '<i>' . $type . ':</i> ' . $entity->getName();
}
return $type . ": " . htmlspecialchars($entity->getName());
}
}

View file

@ -44,7 +44,7 @@ use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Exceptions\EntityNotSupported;
use App\Exceptions\EntityNotSupportedException;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@ -72,7 +72,7 @@ class EntityURLGenerator
* @param array $map The map that should be used for determing the controller
* @param $entity mixed The entity for which the controller name should be determined.
* @return string The name of the controller fitting the entity class
* @throws EntityNotSupported
* @throws EntityNotSupportedException
*/
protected function mapToController(array $map, $entity): string
{
@ -87,7 +87,7 @@ class EntityURLGenerator
}
}
throw new EntityNotSupported(sprintf(
throw new EntityNotSupportedException(sprintf(
'The given entity is not supported yet! Passed class type: %s',
get_class($entity)
));
@ -104,7 +104,7 @@ class EntityURLGenerator
* @param $entity mixed The element for which the page should be generated.
* @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts'
* @return string The link to the desired page.
* @throws EntityNotSupported Thrown if the entity is not supported for the given type.
* @throws EntityNotSupportedException Thrown if the entity is not supported for the given type.
* @throws \InvalidArgumentException Thrown if the givent type is not existing.
*/
public function getURL($entity, string $type)
@ -142,7 +142,7 @@ class EntityURLGenerator
}
//Otherwise throw an error
throw new EntityNotSupported('The given entity is not supported yet!');
throw new EntityNotSupportedException('The given entity is not supported yet!');
}
public function downloadURL($entity): string
@ -155,7 +155,7 @@ class EntityURLGenerator
}
//Otherwise throw an error
throw new EntityNotSupported(sprintf(
throw new EntityNotSupportedException(sprintf(
'The given entity is not supported yet! Passed class type: %s',
get_class($entity)
));
@ -166,12 +166,25 @@ class EntityURLGenerator
*
* @param $entity mixed The entity for which the info should be generated.
* @return string The URL to the info page
* @throws EntityNotSupported If the method is not supported for the given Entity
* @throws EntityNotSupportedException If the method is not supported for the given Entity
*/
public function infoURL(DBElement $entity): string
{
$map = [
Part::class => 'part_info'
Part::class => 'part_info',
//As long we does not have own things for it use edit page
AttachmentType::class => 'attachment_type_edit',
Category::class => 'category_edit',
Device::class => 'device_edit',
Supplier::class => 'supplier_edit',
Manufacturer::class => 'manufacturer_edit',
Storelocation::class => 'store_location_edit',
Footprint::class => 'footprint_edit',
User::class => 'user_edit',
Currency::class => 'currency_edit',
MeasurementUnit::class => 'measurement_unit_edit',
Group::class => 'group_edit'
];
return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]);
@ -182,7 +195,7 @@ class EntityURLGenerator
*
* @param $entity mixed The entity for which the edit link should be generated.
* @return string The URL to the edit page.
* @throws EntityNotSupported If the method is not supported for the given Entity
* @throws EntityNotSupportedException If the method is not supported for the given Entity
*/
public function editURL($entity): string
{
@ -209,7 +222,7 @@ class EntityURLGenerator
*
* @param $entity mixed The entity for which the link should be generated.
* @return string The URL to the page.
* @throws EntityNotSupported If the method is not supported for the given Entity
* @throws EntityNotSupportedException If the method is not supported for the given Entity
*/
public function createURL($entity): string
{
@ -237,7 +250,7 @@ class EntityURLGenerator
*
* @param $entity mixed The entity for which the link should be generated.
* @return string The URL to the page.
* @throws EntityNotSupported If the method is not supported for the given Entity
* @throws EntityNotSupportedException If the method is not supported for the given Entity
*/
public function cloneURL(DBElement $entity): string
{
@ -253,7 +266,7 @@ class EntityURLGenerator
*
* @param $entity mixed The entity for which the link should be generated.
* @return string The URL to the page.
* @throws EntityNotSupported If the method is not supported for the given Entity
* @throws EntityNotSupportedException If the method is not supported for the given Entity
*/
public function listPartsURL(DBElement $entity): string
{

View file

@ -30,6 +30,7 @@
namespace App\Services;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Devices\Device;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
@ -50,6 +51,7 @@ use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This Service generates the tree structure for the tools.
* Whenever you change something here, you has to clear the cache, because the results are cached for performance reasons.
* @package App\Services
*/
class ToolsTreeBuilder
@ -78,7 +80,7 @@ class ToolsTreeBuilder
/**
* Generates the tree for the tools menu.
* The result is cached.
* @return TreeViewNode The array containing all Nodes for the tools menu.
* @return TreeViewNode[] The array containing all Nodes for the tools menu.
*/
public function getTree() : array
{
@ -175,10 +177,18 @@ class ToolsTreeBuilder
protected function getShowNodes() : array
{
$show_nodes = array();
$show_nodes[] = new TreeViewNode($this->translator->trans('tree.tools.show.all_parts'),
$show_nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.show.all_parts'),
$this->urlGenerator->generate('parts_show_all')
);
if ($this->security->isGranted('read', new PartAttachment())) {
$show_nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.show.all_attachments'),
$this->urlGenerator->generate('attachment_list')
);
}
return $show_nodes;
}

View file

@ -80,7 +80,7 @@ class TreeBuilder
* @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
* @throws \App\Exceptions\EntityNotSupportedException
*/
public function elementToTreeNode(NamedDBElement $element, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : TreeViewNode
{
@ -123,7 +123,7 @@ class TreeBuilder
* @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
* @throws \App\Exceptions\EntityNotSupportedException
*/
public function typeToTree(string $class_name, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : array
{