Added admin page for measurement unit.

This commit is contained in:
Jan Böhmer 2019-08-14 18:31:46 +02:00
parent 06c6483fd1
commit fd4b474dbd
8 changed files with 218 additions and 15 deletions

View file

@ -0,0 +1,110 @@
<?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\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\MeasurementUnit;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\MeasurementUnitAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/measurement_unit")
* @package App\Controller
*/
class MeasurementUnitController extends BaseAdminController
{
protected $entity_class = MeasurementUnit::class;
protected $twig_template = 'AdminPages/MeasurementUnitAdmin.html.twig';
protected $form_class = MeasurementUnitAdminForm::class;
protected $route_base = "measurement_unit";
/**
* @Route("/{id}/edit", requirements={"id"="\d+"}, name="measurement_unit_edit")
* @Route("/{id}/", requirements={"id"="\d+"})
*/
public function edit(MeasurementUnit $entity, Request $request, EntityManagerInterface $em)
{
return $this->_edit($entity, $request, $em);
}
/**
* @Route("/new", name="measurement_unit_new")
* @Route("/")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer)
{
return $this->_new($request, $em, $importer);
}
/**
* @Route("/{id}", name="measurement_unit_delete", methods={"DELETE"})
*/
public function delete(Request $request, MeasurementUnit $entity, StructuralElementRecursionHelper $recursionHelper)
{
return $this->_delete($request, $entity, $recursionHelper);
}
/**
* @Route("/export", name="measurement_unit_export_all")
* @param Request $request
* @param SerializerInterface $serializer
* @param EntityManagerInterface $em
* @return Response
*/
public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request)
{
return $this->_exportAll($em, $exporter, $request);
}
/**
* @Route("/{id}/export", name="measurement_unit_export")
* @param Request $request
* @param AttachmentType $entity
*/
public function exportEntity(AttachmentType $entity, EntityExporter $exporter, Request $request)
{
return $this->_exportEntity($entity, $exporter, $request);
}
}

View file

@ -32,9 +32,11 @@
namespace App\Entity\Parts;
use App\Entity\Base\PartsContainingDBElement;
use App\Entity\Base\StructuralDBElement;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This unit represents the unit in which the amount of parts in stock are measured.
@ -45,13 +47,14 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
* @ORM\Table(name="`measurement_units`")
* @UniqueEntity("unit")
*/
class MeasurementUnit extends StructuralDBElement
class MeasurementUnit extends PartsContainingDBElement
{
/**
* @var string The unit symbol that should be used for the Unit. This could be something like "", g (for gramms)
* or m (for meters).
* @ORM\Column(type="string", name="unit")
* @ORM\Column(type="string", name="unit", nullable=true)
* @Assert\Length(max=10)
*/
protected $unit;
@ -60,14 +63,14 @@ class MeasurementUnit extends StructuralDBElement
* Set to false, to measure continuous sizes likes masses or lengthes.
* @ORM\Column(type="boolean", name="is_integer")
*/
protected $isInteger;
protected $is_integer = false;
/**
* @var bool Determines if the unit can be used with SI Prefixes (kilo, giga, milli, etc.).
* Useful for sizes like meters.
* @ORM\Column(type="boolean", name="use_si_prefix")
*/
protected $usesSIPrefixes;
protected $use_si_prefix = false;
/**
* @ORM\OneToMany(targetEntity="MeasurementUnit", mappedBy="parent", cascade={"persist"})
@ -95,7 +98,7 @@ class MeasurementUnit extends StructuralDBElement
/**
* @return string
*/
public function getUnit(): string
public function getUnit(): ?string
{
return $this->unit;
}
@ -104,7 +107,7 @@ class MeasurementUnit extends StructuralDBElement
* @param string $unit
* @return MeasurementUnit
*/
public function setUnit(string $unit): MeasurementUnit
public function setUnit(?string $unit): MeasurementUnit
{
$this->unit = $unit;
return $this;
@ -115,7 +118,7 @@ class MeasurementUnit extends StructuralDBElement
*/
public function isInteger(): bool
{
return $this->isInteger;
return $this->is_integer;
}
/**
@ -131,21 +134,18 @@ class MeasurementUnit extends StructuralDBElement
/**
* @return bool
*/
public function isUsesSIPrefixes(): bool
public function isUseSIPrefix(): bool
{
return $this->usesSIPrefixes;
return $this->use_si_prefix;
}
/**
* @param bool $usesSIPrefixes
* @return MeasurementUnit
*/
public function setUsesSIPrefixes(bool $usesSIPrefixes): MeasurementUnit
public function setUseSIPrefix(bool $usesSIPrefixes): MeasurementUnit
{
$this->usesSIPrefixes = $usesSIPrefixes;
$this->useSIPrefixes = $usesSIPrefixes;
return $this;
}
}

View file

@ -0,0 +1,62 @@
<?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\Form\AdminPages;
use App\Entity\Base\NamedDBElement;
use App\Form\AdminPages\BaseEntityAdminForm;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class MeasurementUnitAdminForm extends BaseEntityAdminForm
{
protected function additionalFormElements(FormBuilderInterface $builder, array $options, NamedDBElement $entity)
{
$is_new = $entity->getID() === null;
$builder->add('is_integer', CheckboxType::class, ['required' => false,
'label' => 'measurement_unit.is_integer.label', 'help' => 'measurement_unit.is_integer.help',
'label_attr'=> ['class' => 'checkbox-custom'],
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
$builder->add('use_si_prefix', CheckboxType::class, ['required' => false,
'label' => 'measurement_unit.use_si_prefix.label', 'help' => 'measurement_unit.use_si_prefix.help',
'label_attr'=> ['class' => 'checkbox-custom'],
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
$builder->add('unit', TextType::class, ['required' => false,
'label' => 'measurement_unit.unit.label', 'attr' => ['placeholder' => 'measurement_unit.unit.placeholder'],
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
}
}

View file

@ -22,7 +22,7 @@ final class Version20190812154222 extends AbstractMigration
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE `measurement_units` (id INT AUTO_INCREMENT NOT NULL, unit VARCHAR(255) NOT NULL, is_integer TINYINT(1) NOT NULL, use_si_prefix TINYINT(1) NOT NULL, comment LONGTEXT NOT NULL, parent_id INT DEFAULT NULL, not_selectable TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE `measurement_units` (id INT AUTO_INCREMENT NOT NULL, unit VARCHAR(255) DEFAULT NULL, is_integer TINYINT(1) NOT NULL, use_si_prefix TINYINT(1) NOT NULL, comment LONGTEXT NOT NULL, parent_id INT DEFAULT NULL, not_selectable TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE part_lots (id INT AUTO_INCREMENT NOT NULL, id_store_location INT DEFAULT NULL, id_part INT DEFAULT NULL, description LONGTEXT NOT NULL, comment LONGTEXT NOT NULL, expiration_date DATETIME DEFAULT NULL, instock_unknown TINYINT(1) NOT NULL, instock INT DEFAULT NULL, amount DOUBLE PRECISION DEFAULT NULL, needs_refill TINYINT(1) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, INDEX IDX_EBC8F9435D8F4B37 (id_store_location), INDEX IDX_EBC8F943C22F6CC4 (id_part), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE currencies (id INT AUTO_INCREMENT NOT NULL, iso_code VARCHAR(255) NOT NULL, exchange_rate NUMERIC(11, 5) DEFAULT NULL, comment LONGTEXT NOT NULL, parent_id INT DEFAULT NULL, not_selectable TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE part_lots ADD CONSTRAINT FK_EBC8F9435D8F4B37 FOREIGN KEY (id_store_location) REFERENCES `storelocations` (id)');

View file

@ -37,6 +37,7 @@ 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\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
@ -87,6 +88,9 @@ class StructureVoter extends ExtendedVoter
case Currency::class:
//TODO: Implement own permission
return 'suppliers';
case MeasurementUnit::class:
//TODO: Implement own permission
return 'suppliers';
}
//When the class is not supported by this class return null
return null;

View file

@ -36,6 +36,7 @@ use App\Entity\Devices\Device;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Base\NamedDBElement;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
@ -185,6 +186,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('currency_edit', ['id' => $entity->getID()]);
}
if ($entity instanceof MeasurementUnit) {
return $this->urlGenerator->generate('measurement_unit_edit', ['id' => $entity->getID()]);
}
//Otherwise throw an error
throw new EntityNotSupported('The given entity is not supported yet!');
}
@ -238,6 +243,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('currency_new');
}
if ($entity instanceof MeasurementUnit) {
return $this->urlGenerator->generate('measurement_unit_new');
}
throw new EntityNotSupported('The given entity is not supported yet!');
}
@ -312,6 +321,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('currency_delete', ['id' => $entity->getID()]);
}
if ($entity instanceof MeasurementUnit) {
return $this->urlGenerator->generate('measurement_unit_delete', ['id' => $entity->getID()]);
}
throw new EntityNotSupported('The given entity is not supported yet!');
}

View file

@ -79,6 +79,9 @@ class ToolsTreeBuilder
$nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.currency'),
$this->urlGenerator->generate('currency_new'));
$nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.measurement_unit'),
$this->urlGenerator->generate('measurement_unit_new'));
$nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.part'),
$this->urlGenerator->generate('part_new'));

View file

@ -0,0 +1,11 @@
{% extends "AdminPages/EntityAdminBase.html.twig" %}
{% block card_title %}
<i class="fas fa-balance-scale fa-fw"></i> {% trans %}measurement_unit.caption{% endtrans %}
{% endblock %}
{% block additional_controls %}
{{ form_row(form.unit) }}
{{ form_row(form.is_integer) }}
{{ form_row(form.use_si_prefix)}}
{% endblock %}