mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-10 10:24:31 +02:00
Added a field for edit the mass of a part.
This commit is contained in:
parent
016c2889ba
commit
e7cc53f046
9 changed files with 231 additions and 30 deletions
|
@ -37,6 +37,7 @@ use App\Entity\Parts\Manufacturer;
|
|||
use App\Entity\Parts\MeasurementUnit;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\Parts\Storelocation;
|
||||
use App\Form\Type\SIUnitType;
|
||||
use App\Form\Type\StructuralEntityType;
|
||||
use Doctrine\DBAL\Types\FloatType;
|
||||
use FOS\CKEditorBundle\Form\Type\CKEditorType;
|
||||
|
@ -68,12 +69,8 @@ class PartBaseType extends AbstractType
|
|||
/** @var Part $part */
|
||||
$part = $options['data'];
|
||||
|
||||
//Select the amount value type based on the
|
||||
if($part->useFloatAmount()) {
|
||||
$amount_class = NumberType::class;
|
||||
} else {
|
||||
$amount_class = IntegerType::class;
|
||||
}
|
||||
$part_unit_name = $part->getPartUnit() !== null ? $part->getPartUnit()->getUnit() : "";
|
||||
$use_si_prefix = $part->getPartUnit() !== null ? $part->getPartUnit()->isUseSIPrefix() : false;
|
||||
|
||||
|
||||
//Common section
|
||||
|
@ -84,11 +81,10 @@ class PartBaseType extends AbstractType
|
|||
->add('description', CKEditorType::class, ['required' => false, 'empty_data' => '',
|
||||
'label' => 'description.label', 'help' => 'bbcode.hint', 'config_name' => 'description_config',
|
||||
'attr' => ['placeholder' => 'part.description.placeholder', 'rows' => 2],
|
||||
'disabled' => !$this->security->isGranted('description.edit', $part), 'empty_data' => '' ])
|
||||
->add('partUnit', StructuralEntityType::class, ['class'=> MeasurementUnit::class,
|
||||
'required' => false, 'disable_not_selectable' => true, 'label' => 'part.partUnit'])
|
||||
->add('minAmount', $amount_class,
|
||||
'disabled' => !$this->security->isGranted('description.edit', $part) ])
|
||||
->add('minAmount', SIUnitType::class,
|
||||
['attr' => ['min' => 0, 'placeholder' => 'part.mininstock.placeholder'], 'label' => 'mininstock.label',
|
||||
'show_prefix' => $use_si_prefix, "unit" => $part_unit_name, "is_integer" => !$part->useFloatAmount(),
|
||||
'disabled' => !$this->security->isGranted('mininstock.edit', $part), ])
|
||||
->add('category', StructuralEntityType::class, ['class' => Category::class,
|
||||
'label' => 'category.label', 'disable_not_selectable' => true,
|
||||
|
@ -98,10 +94,7 @@ class PartBaseType extends AbstractType
|
|||
'disabled' => !$this->security->isGranted('move', $part), ])
|
||||
->add('tags', TextType::class, ['required' => false, 'label' => 'part.tags', 'empty_data' => "",
|
||||
'attr' => ['data-role' => 'tagsinput'],
|
||||
'disabled' => !$this->security->isGranted('edit', $part) ])
|
||||
->add('comment', CKEditorType::class, ['required' => false,
|
||||
'label' => 'comment.label', 'attr' => ['rows' => 4], 'help' => 'bbcode.hint',
|
||||
'disabled' => !$this->security->isGranted('comment.edit', $part), 'empty_data' => '']);
|
||||
'disabled' => !$this->security->isGranted('edit', $part) ]);
|
||||
|
||||
//Manufacturer section
|
||||
$builder->add('manufacturer', StructuralEntityType::class, ['class' => Manufacturer::class,
|
||||
|
@ -114,11 +107,21 @@ class PartBaseType extends AbstractType
|
|||
'empty_data' => '', 'label' => 'part.mpn',
|
||||
'disabled' => !$this->security->isGranted('manufacturer.edit', $part)]);
|
||||
|
||||
//Options section
|
||||
//Advanced section
|
||||
$builder->add('needsReview', CheckboxType::class, ['label_attr'=> ['class' => 'checkbox-custom'],
|
||||
'required' => false, 'label' => 'part.edit.needs_review'])
|
||||
->add('favorite', CheckboxType::class, ['label_attr'=> ['class' => 'checkbox-custom'],
|
||||
'required' => false, 'label' => 'part.edit.is_favorite']);
|
||||
'required' => false, 'label' => 'part.edit.is_favorite'])
|
||||
->add('mass', SIUnitType::class, ['unit' => 'g',
|
||||
'label' => 'part.mass', 'required' => false])
|
||||
->add('partUnit', StructuralEntityType::class, ['class'=> MeasurementUnit::class,
|
||||
'required' => false, 'disable_not_selectable' => true, 'label' => 'part.partUnit']);
|
||||
|
||||
|
||||
//Comment section
|
||||
$builder->add('comment', CKEditorType::class, ['required' => false,
|
||||
'label' => 'comment.label', 'attr' => ['rows' => 4], 'help' => 'bbcode.hint',
|
||||
'disabled' => !$this->security->isGranted('comment.edit', $part), 'empty_data' => '']);
|
||||
|
||||
//Part Lots section
|
||||
$builder->add('partLots', CollectionType::class, [
|
||||
|
|
156
src/Form/Type/SIUnitType.php
Normal file
156
src/Form/Type/SIUnitType.php
Normal file
|
@ -0,0 +1,156 @@
|
|||
<?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\Type;
|
||||
|
||||
|
||||
use App\Services\SIFormatter;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
use Symfony\Component\Form\Exception;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class SIUnitType extends AbstractType implements DataMapperInterface
|
||||
{
|
||||
protected $si_formatter;
|
||||
|
||||
public function __construct(SIFormatter $SIFormatter)
|
||||
{
|
||||
$this->si_formatter = $SIFormatter;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'show_prefix' => true,
|
||||
'is_integer' => false,
|
||||
]);
|
||||
|
||||
$resolver->setRequired('unit');
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('value', NumberType::class);
|
||||
|
||||
if ($options['show_prefix']) {
|
||||
$builder->add('prefix', ChoiceType::class, [
|
||||
'choices' => ['M' => 6, 'k' => 3, '' => 0, 'm' => -3 ]
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->setDataMapper($this);
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['unit'] = $options['unit'];
|
||||
parent::buildView($view, $form, $options); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the view data of a compound form to its children.
|
||||
*
|
||||
* The method is responsible for calling {@link FormInterface::setData()}
|
||||
* on the children of compound forms, defining their underlying model data.
|
||||
*
|
||||
* @param mixed $viewData View data of the compound form being initialized
|
||||
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
|
||||
*
|
||||
* @throws Exception\UnexpectedTypeException if the type of the data parameter is not supported
|
||||
*/
|
||||
public function mapDataToForms($viewData, $forms)
|
||||
{
|
||||
if ($viewData === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $this->si_formatter->convertValue($viewData);
|
||||
|
||||
$forms = iterator_to_array($forms);
|
||||
|
||||
if (isset($forms['prefix'])) {
|
||||
$forms['value']->setData($data["value"]);
|
||||
$forms['prefix']->setData($data['prefix_magnitude']);
|
||||
} else {
|
||||
$forms['value']->setData($viewData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the model data of a list of children forms into the view data of their parent.
|
||||
*
|
||||
* This is the internal cascade call of FormInterface::submit for compound forms, since they
|
||||
* cannot be bound to any input nor the request as scalar, but their children may:
|
||||
*
|
||||
* $compoundForm->submit($arrayOfChildrenViewData)
|
||||
* // inside:
|
||||
* $childForm->submit($childViewData);
|
||||
* // for each entry, do the same and/or reverse transform
|
||||
* $this->dataMapper->mapFormsToData($compoundForm, $compoundInitialViewData)
|
||||
* // then reverse transform
|
||||
*
|
||||
* When a simple form is submitted the following is happening:
|
||||
*
|
||||
* $simpleForm->submit($submittedViewData)
|
||||
* // inside:
|
||||
* $this->viewData = $submittedViewData
|
||||
* // then reverse transform
|
||||
*
|
||||
* The model data can be an array or an object, so this second argument is always passed
|
||||
* by reference.
|
||||
*
|
||||
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
|
||||
* @param mixed $viewData The compound form's view data that get mapped
|
||||
* its children model data
|
||||
*
|
||||
* @throws Exception\UnexpectedTypeException if the type of the data parameter is not supported
|
||||
*/
|
||||
public function mapFormsToData($forms, &$viewData)
|
||||
{
|
||||
//Convert both fields to a single float value.
|
||||
|
||||
$forms = iterator_to_array($forms);
|
||||
|
||||
$viewData = $forms['value']->getData();
|
||||
|
||||
if (isset($forms['prefix'])) {
|
||||
$multiplier = $forms['prefix']->getData();
|
||||
$viewData *= 10 ** $multiplier;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -75,12 +75,32 @@ class SIFormatter
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @param int $decimals
|
||||
* @return array
|
||||
*/
|
||||
public function convertValue(float $value) : array
|
||||
{
|
||||
//Choose the prefix to use
|
||||
$tmp = $this->getPrefixByMagnitude($this->getMagnitude($value));
|
||||
|
||||
$ret = array(
|
||||
'value' => $value / $tmp[0],
|
||||
'prefix_magnitude' => log10($tmp[0]),
|
||||
'prefix' => $tmp[1]
|
||||
);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given value to a string, using the given options
|
||||
* @param float $value The value that should be converted
|
||||
* @param string $unit The unit that should be appended after the prefix
|
||||
* @param int $decimals The number of decimals (after decimal dot) that should be outputed.
|
||||
* @return string
|
||||
*/
|
||||
public function format(float $value, string $unit = '', int $decimals = 2)
|
||||
public function format(float $value, string $unit = '', int $decimals = 2) : string
|
||||
{
|
||||
[$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value));
|
||||
$value /= $divisor;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue