Added preview with example elements, on LabelProfile Admin Pages.

This commit is contained in:
Jan Böhmer 2020-05-01 21:19:13 +02:00
parent 48ff81a6d1
commit 3804e2534d
3 changed files with 127 additions and 1 deletions

View file

@ -45,6 +45,8 @@ namespace App\Controller\AdminPages;
use App\DataTables\LogDataTable; use App\DataTables\LogDataTable;
use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractStructuralDBElement; use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\Parts\PartLot;
use App\Entity\UserSystem\User; use App\Entity\UserSystem\User;
use App\Events\SecurityEvent; use App\Events\SecurityEvent;
use App\Events\SecurityEvents; use App\Events\SecurityEvents;
@ -54,6 +56,8 @@ use App\Form\AdminPages\MassCreationForm;
use App\Services\Attachments\AttachmentSubmitHandler; use App\Services\Attachments\AttachmentSubmitHandler;
use App\Services\EntityExporter; use App\Services\EntityExporter;
use App\Services\EntityImporter; use App\Services\EntityImporter;
use App\Services\LabelSystem\BarcodeExampleElementsGenerator;
use App\Services\LabelSystem\LabelGenerator;
use App\Services\LogSystem\EventCommentHelper; use App\Services\LogSystem\EventCommentHelper;
use App\Services\LogSystem\HistoryHelper; use App\Services\LogSystem\HistoryHelper;
use App\Services\LogSystem\TimeTravel; use App\Services\LogSystem\TimeTravel;
@ -92,11 +96,14 @@ abstract class BaseAdminController extends AbstractController
protected $dataTableFactory; protected $dataTableFactory;
/** @var EventDispatcher */ /** @var EventDispatcher */
protected $eventDispatcher; protected $eventDispatcher;
protected $labelGenerator;
protected $barcodeExampleGenerator;
public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder, public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder,
AttachmentSubmitHandler $attachmentSubmitHandler, AttachmentSubmitHandler $attachmentSubmitHandler,
EventCommentHelper $commentHelper, HistoryHelper $historyHelper, TimeTravel $timeTravel, EventCommentHelper $commentHelper, HistoryHelper $historyHelper, TimeTravel $timeTravel,
DataTableFactory $dataTableFactory, EventDispatcherInterface $eventDispatcher) DataTableFactory $dataTableFactory, EventDispatcherInterface $eventDispatcher, BarcodeExampleElementsGenerator $barcodeExampleGenerator,
LabelGenerator $labelGenerator)
{ {
if ('' === $this->entity_class || '' === $this->form_class || '' === $this->twig_template || '' === $this->route_base) { if ('' === $this->entity_class || '' === $this->form_class || '' === $this->twig_template || '' === $this->route_base) {
throw new InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!'); throw new InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!');
@ -118,6 +125,8 @@ abstract class BaseAdminController extends AbstractController
$this->timeTravel = $timeTravel; $this->timeTravel = $timeTravel;
$this->dataTableFactory = $dataTableFactory; $this->dataTableFactory = $dataTableFactory;
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->barcodeExampleGenerator = $barcodeExampleGenerator;
$this->labelGenerator = $labelGenerator;
} }
protected function _edit(AbstractNamedDBElement $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response protected function _edit(AbstractNamedDBElement $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response
@ -210,11 +219,18 @@ abstract class BaseAdminController extends AbstractController
$this->addFlash('error', 'entity.edit_flash.invalid'); $this->addFlash('error', 'entity.edit_flash.invalid');
} }
//Show preview for LabelProfile if needed.
if ($entity instanceof LabelProfile) {
$example = $this->barcodeExampleGenerator->getElement($entity->getOptions()->getSupportedElement());
$pdf_data = $this->labelGenerator->generateLabel($entity->getOptions(), $example);
}
return $this->render($this->twig_template, [ return $this->render($this->twig_template, [
'entity' => $entity, 'entity' => $entity,
'form' => $form->createView(), 'form' => $form->createView(),
'route_base' => $this->route_base, 'route_base' => $this->route_base,
'datatable' => $table, 'datatable' => $table,
'pdf_data' => $pdf_data ?? null,
'timeTravel' => $timeTravel_timestamp, 'timeTravel' => $timeTravel_timestamp,
]); ]);
} }

View file

@ -0,0 +1,101 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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 Affero General Public License as published
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Services\LabelSystem;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation;
class BarcodeExampleElementsGenerator
{
public function getElement(string $type): object
{
switch ($type) {
case 'part':
return $this->getExamplePart();
case 'part_lot':
return $this->getExamplePartLot();
default:
throw new \InvalidArgumentException('Unknown $type.');
}
}
protected function getStructuralData(string $class): AbstractStructuralDBElement
{
if (!is_a($class, AbstractStructuralDBElement::class, true)) {
throw new \InvalidArgumentException('$class must be an child of AbstractStructuralDBElement');
}
/** @var AbstractStructuralDBElement $parent */
$parent = new $class();
$parent->setName('Example');
/** @var AbstractStructuralDBElement $child */
$child = new $class();
$child->setName((new \ReflectionClass($class))->getShortName());
$child->setParent($parent);
return $child;
}
public function getExamplePart(): Part
{
$part = new Part();
$part->setName('Example Part');
$part->setDescription('<b>Part</b> description');
$part->setComment('<i>Part</i> comment');
$part->setCategory($this->getStructuralData(Category::class));
$part->setFootprint($this->getStructuralData(Footprint::class));
$part->setManufacturer($this->getStructuralData(Manufacturer::class));
$part->setMass(123.4);
$part->setManufacturerProductNumber('CUSTOM MPN');
$part->setTags('Tag1, Tag2, Tag3');
$part->setManufacturingStatus('active');
$part->updatedTimestamps();
$part->setFavorite(true);
$part->setMinAmount(100);
$part->setNeedsReview(true);
return $part;
}
public function getExamplePartLot(): PartLot
{
$lot = new PartLot();
$lot->setPart($this->getExamplePart());
$lot->setDescription('Example Lot');
$lot->setComment('Lot comment');
$lot->setExpirationDate(new \DateTime('+1 days'));
$lot->setStorageLocation($this->getStructuralData(Storelocation::class));
$lot->setAmount(123);
return $lot;
}
}

View file

@ -13,3 +13,12 @@
{{ form_widget(form.options) }} {{ form_widget(form.options) }}
</div> </div>
{% endblock %} {% endblock %}
{% block additional_content %}
{% if pdf_data is defined and pdf_data is not empty %}
<div class="card mt-2 mb-2 p-1 border-secondary" style="resize: vertical; overflow: scroll; height: 250px;">
<object id="pdf_preview" data="{{ inlineData(pdf_data, 'application/pdf') | escape('html_attr') }}"style="height: inherit">
</object>
</div>
{% endif %}
{% endblock %}