Added a field for edit the mass of a part.

This commit is contained in:
Jan Böhmer 2019-08-24 12:55:47 +02:00
parent 016c2889ba
commit e7cc53f046
9 changed files with 231 additions and 30 deletions

View file

@ -10,7 +10,6 @@ fos_ck_editor:
complex_config: complex_config:
extraPlugins: ["bbcode"] extraPlugins: ["bbcode"]
toolbar: standard toolbar: standard
height: 100
simple_config: simple_config:
extraPlugins: "bbcode" extraPlugins: "bbcode"
toolbar: basic toolbar: basic

View file

@ -37,6 +37,7 @@ use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation; use App\Entity\Parts\Storelocation;
use App\Form\Type\SIUnitType;
use App\Form\Type\StructuralEntityType; use App\Form\Type\StructuralEntityType;
use Doctrine\DBAL\Types\FloatType; use Doctrine\DBAL\Types\FloatType;
use FOS\CKEditorBundle\Form\Type\CKEditorType; use FOS\CKEditorBundle\Form\Type\CKEditorType;
@ -68,12 +69,8 @@ class PartBaseType extends AbstractType
/** @var Part $part */ /** @var Part $part */
$part = $options['data']; $part = $options['data'];
//Select the amount value type based on the $part_unit_name = $part->getPartUnit() !== null ? $part->getPartUnit()->getUnit() : "";
if($part->useFloatAmount()) { $use_si_prefix = $part->getPartUnit() !== null ? $part->getPartUnit()->isUseSIPrefix() : false;
$amount_class = NumberType::class;
} else {
$amount_class = IntegerType::class;
}
//Common section //Common section
@ -84,11 +81,10 @@ class PartBaseType extends AbstractType
->add('description', CKEditorType::class, ['required' => false, 'empty_data' => '', ->add('description', CKEditorType::class, ['required' => false, 'empty_data' => '',
'label' => 'description.label', 'help' => 'bbcode.hint', 'config_name' => 'description_config', 'label' => 'description.label', 'help' => 'bbcode.hint', 'config_name' => 'description_config',
'attr' => ['placeholder' => 'part.description.placeholder', 'rows' => 2], 'attr' => ['placeholder' => 'part.description.placeholder', 'rows' => 2],
'disabled' => !$this->security->isGranted('description.edit', $part), 'empty_data' => '' ]) 'disabled' => !$this->security->isGranted('description.edit', $part) ])
->add('partUnit', StructuralEntityType::class, ['class'=> MeasurementUnit::class, ->add('minAmount', SIUnitType::class,
'required' => false, 'disable_not_selectable' => true, 'label' => 'part.partUnit'])
->add('minAmount', $amount_class,
['attr' => ['min' => 0, 'placeholder' => 'part.mininstock.placeholder'], 'label' => 'mininstock.label', ['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), ]) 'disabled' => !$this->security->isGranted('mininstock.edit', $part), ])
->add('category', StructuralEntityType::class, ['class' => Category::class, ->add('category', StructuralEntityType::class, ['class' => Category::class,
'label' => 'category.label', 'disable_not_selectable' => true, 'label' => 'category.label', 'disable_not_selectable' => true,
@ -98,10 +94,7 @@ class PartBaseType extends AbstractType
'disabled' => !$this->security->isGranted('move', $part), ]) 'disabled' => !$this->security->isGranted('move', $part), ])
->add('tags', TextType::class, ['required' => false, 'label' => 'part.tags', 'empty_data' => "", ->add('tags', TextType::class, ['required' => false, 'label' => 'part.tags', 'empty_data' => "",
'attr' => ['data-role' => 'tagsinput'], 'attr' => ['data-role' => 'tagsinput'],
'disabled' => !$this->security->isGranted('edit', $part) ]) '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' => '']);
//Manufacturer section //Manufacturer section
$builder->add('manufacturer', StructuralEntityType::class, ['class' => Manufacturer::class, $builder->add('manufacturer', StructuralEntityType::class, ['class' => Manufacturer::class,
@ -114,11 +107,21 @@ class PartBaseType extends AbstractType
'empty_data' => '', 'label' => 'part.mpn', 'empty_data' => '', 'label' => 'part.mpn',
'disabled' => !$this->security->isGranted('manufacturer.edit', $part)]); 'disabled' => !$this->security->isGranted('manufacturer.edit', $part)]);
//Options section //Advanced section
$builder->add('needsReview', CheckboxType::class, ['label_attr'=> ['class' => 'checkbox-custom'], $builder->add('needsReview', CheckboxType::class, ['label_attr'=> ['class' => 'checkbox-custom'],
'required' => false, 'label' => 'part.edit.needs_review']) 'required' => false, 'label' => 'part.edit.needs_review'])
->add('favorite', CheckboxType::class, ['label_attr'=> ['class' => 'checkbox-custom'], ->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 //Part Lots section
$builder->add('partLots', CollectionType::class, [ $builder->add('partLots', CollectionType::class, [

View 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;
}
}
}

View file

@ -75,12 +75,32 @@ class SIFormatter
} }
/** /**
*
* @param float $value * @param float $value
* @param string $unit * @return array
* @param int $decimals */
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 * @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)); [$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value));
$value /= $divisor; $value /= $divisor;

View file

@ -29,4 +29,18 @@
{% endif %} {% endif %}
{%- endif -%} {%- endif -%}
{% endfor %} {% endfor %}
{%- endblock choice_widget_options -%} {%- endblock choice_widget_options -%}
{% block si_unit_widget %}
<div class="input-group">
{{ form_widget(form.value) }}
<div class="input-group-append">
{% if form.prefix is defined %}
{{ form_widget(form.prefix, {'attr': {'class': 'custom-select btn'}}) }}
{% endif %}
{% if unit is not empty %}
<label class="input-group-text">{{ unit }}</label>
{% endif %}
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,4 @@
{{ form_row(form.needsReview) }}
{{ form_row(form.favorite) }}
{{ form_row(form.mass) }}
{{ form_row(form.partUnit) }}

View file

@ -3,9 +3,6 @@
{{ form_row(form.category) }} {{ form_row(form.category) }}
{{ form_row(form.tags) }} {{ form_row(form.tags) }}
{{ form_row(form.minAmount) }} {{ form_row(form.minAmount) }}
{{ form_row(form.partUnit)}}
{{ form_row(form.footprint) }} {{ form_row(form.footprint) }}
{{ form_row(form.comment) }}

View file

@ -1,2 +0,0 @@
{{ form_row(form.needsReview) }}
{{ form_row(form.favorite) }}

View file

@ -29,9 +29,9 @@
</a> </a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" data-toggle="tab" role="tab" href="#options"> <a class="nav-link" data-toggle="tab" role="tab" href="#advanced">
<i class="fas fa-shapes fa-fw"></i> <i class="fas fa-shapes fa-fw"></i>
{% trans %}part.edit.tab.options{% endtrans %} {% trans %}part.edit.tab.advanced{% endtrans %}
</a> </a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
@ -40,6 +40,12 @@
{% trans %}part.edit.tab.part_lots{% endtrans %} {% trans %}part.edit.tab.part_lots{% endtrans %}
</a> </a>
</li> </li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" role="tab" href="#comment">
<i class="fas fa-sticky-note fa-fw"></i>
{% trans %}part.edit.tab.comment{% endtrans %}
</a>
</li>
</ul> </ul>
{{ form_start(form) }} {{ form_start(form) }}
@ -51,13 +57,17 @@
<div class="tab-pane fade p-2" id="manufacturer" role="tabpanel"> <div class="tab-pane fade p-2" id="manufacturer" role="tabpanel">
{% include "Parts/edit/_manufacturer.html.twig" %} {% include "Parts/edit/_manufacturer.html.twig" %}
</div> </div>
<div class="tab-pane fade p-2" id="options" role="tabpanel"> <div class="tab-pane fade p-2" id="advanced" role="tabpanel">
{% include "Parts/edit/_options.html.twig" %} {% include "Parts/edit/_advanced.html.twig" %}
</div> </div>
<div class="tab-pane fade p-2" id="part_lots" role="tabpanel"> <div class="tab-pane fade p-2" id="part_lots" role="tabpanel">
{% include "Parts/edit/_lots.html.twig" %} {% include "Parts/edit/_lots.html.twig" %}
</div> </div>
<div class="tab-pane fade p-2" id="comment" role="tabpanel">
{{ form_widget(form.comment)}}
</div>
</div> </div>