Show parameters on info page & improved parameter editing.

This commit is contained in:
Jan Böhmer 2020-03-14 22:12:19 +01:00
parent 719e21c0df
commit bc8748e6f4
11 changed files with 1176 additions and 773 deletions

View file

@ -54,15 +54,9 @@ abstract class AbstractParameter extends AbstractNamedDBElement
*/
public const ALLOWED_ELEMENT_CLASS = '';
/**
* @var string The name of the specification (e.g. "Collector-Base Voltage"). Required!
* @Assert\NotBlank()
*/
protected $name = "";
/**
* @var string The mathematical symbol for this specification. Can be rendered pretty later. Should be short
* @Assert\Length(max=10)
* @Assert\Length(max=20)
* @ORM\Column(type="string", nullable=false)
*/
protected $symbol = "";
@ -70,8 +64,8 @@ abstract class AbstractParameter extends AbstractNamedDBElement
/**
* @var float|null The guaranteed minimum value of this property.
* @Assert\Type({"float","null"})
* @Assert\LessThanOrEqual(propertyPath="value_typical")
* @Assert\LessThan(propertyPath="value_max")
* @Assert\LessThanOrEqual(propertyPath="value_typical", message="parameters.validator.min_lesser_typical")
* @Assert\LessThan(propertyPath="value_max", message="parameters.validator.min_lesser_max")
* @ORM\Column(type="float", nullable=true)
*/
protected $value_min;
@ -86,7 +80,7 @@ abstract class AbstractParameter extends AbstractNamedDBElement
/**
* @var float|null The maximum value of this property.
* @Assert\Type({"float", "null"})
* @Assert\GreaterThanOrEqual(propertyPath="value_typical")
* @Assert\GreaterThanOrEqual(propertyPath="value_typical", message="parameters.validator.max_greater_typical")
* @ORM\Column(type="float", nullable=true)
*/
protected $value_max;
@ -141,6 +135,48 @@ abstract class AbstractParameter extends AbstractNamedDBElement
return $this->element;
}
/**
* Return a formatted string version of the values of the string.
* Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text]
* @return string
*/
public function getFormattedValue(): string
{
//If we just only have text value, return early
if ($this->value_typical === null && $this->value_min === null && $this->value_max === null) {
return $this->value_text;
}
$str = '';
$bracket_opened = false;
if ($this->value_typical) {
$str .= $this->getValueTypicalWithUnit();
if ($this->value_min || $this->value_max) {
$bracket_opened = true;
$str .= ' (';
}
}
if ($this->value_max && $this->value_min) {
$str .= $this->getValueMinWithUnit() . ' ... ' . $this->getValueMaxWithUnit();
} elseif ($this->value_max) {
$str .= 'max. ' . $this->getValueMaxWithUnit();
} elseif ($this->value_min) {
$str .= 'min. ' . $this->getValueMinWithUnit();
}
//Add closing bracket
if ($bracket_opened) {
$str .= ')';
}
if ($this->value_text) {
$str .= ' [' . $this->value_text . ']';
}
return $str;
}
/**
* Sets the element to which this parameter belongs to.
* @param AbstractDBElement $element
@ -216,6 +252,48 @@ abstract class AbstractParameter extends AbstractNamedDBElement
return $this->value_typical;
}
/**
* Return a string representation and (if possible) with its unit.
* @param float $value
* @param string $format
* @return string
*/
protected function formatWithUnit(float $value, string $format = "%g"): string
{
$str = \sprintf($format, $value);
if (!empty($this->unit)) {
return $str . ' ' . $this->unit;
}
return $str;
}
/**
* Return a formatted version with the minimum value with the unit of this parameter
* @return string
*/
public function getValueTypicalWithUnit(): string
{
return $this->formatWithUnit($this->value_typical);
}
/**
* Return a formatted version with the maximum value with the unit of this parameter
* @return string
*/
public function getValueMaxWithUnit(): string
{
return $this->formatWithUnit($this->value_max);
}
/**
* Return a formatted version with the typical value with the unit of this parameter
* @return string
*/
public function getValueMinWithUnit(): string
{
return $this->formatWithUnit($this->value_min);
}
/**
* Sets the typical value of this property
* @param float $value_typical

View file

@ -21,7 +21,6 @@
namespace App\Form;
use App\Entity\Parameters\AbstractParameter;
use App\Entity\Parameters\Parameter;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
@ -34,28 +33,67 @@ class ParameterType extends AbstractType
{
$builder->add('name', TextType::class,[
'empty_data' => '',
'attr' => [
'placeholder' => 'parameters.name.placeholder',
'class' => 'form-control-sm'
]
]);
$builder->add('symbol', TextType::class, [
'required' => false,
'empty_data' => '',
'attr' => [
'placeholder' => 'parameters.symbol.placeholder',
'class' => 'form-control-sm',
'style' => 'max-width: 15ch;'
]
]);
$builder->add('value_text', TextType::class, [
'required' => false,
'empty_data' => '',
'attr' => [
'placeholder' => 'parameters.text.placeholder',
'class' => 'form-control-sm',
]
]);
$builder->add('value_max', NumberType::class, [
'required' => false,
'html5' => true,
'attr' => [
'step' => 'any',
'placeholder' => 'parameters.max.placeholder',
'class' => 'form-control-sm',
'style' => 'max-width: 15ch;'
],
]);
$builder->add('value_min', NumberType::class, [
'required' => false,
'html5' => true,
'attr' => [
'step' => 'any',
'placeholder' => 'parameters.min.placeholder',
'class' => 'form-control-sm',
'style' => 'max-width: 15ch;'
],
]);
$builder->add('value_typical', NumberType::class, [
'required' => false
'required' => false,
'html5' => true,
'attr' => [
'step' => 'any',
'placeholder' => 'parameters.typical.placeholder',
'class' => 'form-control-sm',
'style' => 'max-width: 15ch;'
]
]);
$builder->add('unit', TextType::class, [
'required' => false,
'empty_data' => '',
'attr' => [
'placeholder' => 'parameters.unit.placeholder',
'class' => 'form-control-sm',
'style' => 'max-width: 8ch;'
]
]);
}

View file

@ -22,7 +22,7 @@ final class Version20200311204104 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 parameters (id INT AUTO_INCREMENT NOT NULL, symbol VARCHAR(255) NOT NULL, value_min DOUBLE PRECISION DEFAULT NULL, value_typical DOUBLE PRECISION DEFAULT NULL, value_max DOUBLE PRECISION DEFAULT NULL, unit VARCHAR(255) NOT NULL, value_text VARCHAR(255) NOT NULL, param_group VARCHAR(255) NOT NULL, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, type SMALLINT NOT NULL, element_id INT NOT NULL, INDEX IDX_69348FE1F1F2A24 (element_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE parameters (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, symbol VARCHAR(255) NOT NULL, value_min DOUBLE PRECISION DEFAULT NULL, value_typical DOUBLE PRECISION DEFAULT NULL, value_max DOUBLE PRECISION DEFAULT NULL, unit VARCHAR(255) NOT NULL, value_text VARCHAR(255) NOT NULL, param_group VARCHAR(255) NOT NULL, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, type SMALLINT NOT NULL, element_id INT NOT NULL, INDEX IDX_69348FE1F1F2A24 (element_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE log CHANGE level level TINYINT');
}

View file

@ -1,6 +1,6 @@
{% form_theme form with ['Parts/edit/edit_form_styles.html.twig', "bootstrap_4_layout.html.twig"] %}
<table class="table table-striped table-sm table-responsive-md" id="specifications_table" data-prototype="{% if form.parameters.vars.prototype is defined %}{{ form_widget(form.parameters.vars.prototype)|e('html_attr') }}{% endif %}">
<table class="table table-striped table-sm table-bordered table-responsive-md" id="specifications_table" data-prototype="{% if form.parameters.vars.prototype is defined %}{{ form_widget(form.parameters.vars.prototype)|e('html_attr') }}{% endif %}">
<thead>
<tr>
<th>{% trans %}specifications.property{% endtrans %}</th>

View file

@ -74,7 +74,7 @@
<td>{{ form_widget(form.unit) }}{{ form_errors(form.unit) }}</td>
<td>{{ form_widget(form.value_text) }}{{ form_errors(form.value_text) }}</td>
<td>
<button type="button" class="btn btn-danger order_btn_delete" onclick="delete_specification_entry(this);" title="{% trans %}orderdetail.delete{% endtrans %}">
<button type="button" class="btn btn-danger btn-sm order_btn_delete" onclick="delete_specification_entry(this);" title="{% trans %}orderdetail.delete{% endtrans %}">
<i class="fas fa-trash-alt fa-fw"></i>
</button>
{{ form_errors(form) }}

View file

@ -0,0 +1,18 @@
{# var \App\Entity\Parts\Part part #}
<table class="table table-hover table-striped table-sm">
<thead>
<tr>
<th>{% trans %}specifications.property{% endtrans %}</th>
<th>{% trans %}specifications.value{% endtrans %}</th>
</tr>
</thead>
<tbody>
{% for param in part.parameters %}
<tr>
<td>{{ param.name }}</td>
<td>{{ param.formattedValue }}</td>
</tr>
{% endfor %}
</tbody>
</table>

View file

@ -57,6 +57,12 @@
</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" data-toggle="tab" role="tab" href="#specifications">
<i class="fas fa-atlas fa-fw"></i>
{% trans %}part.info.specifications{% endtrans %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="attachment-tab" data-toggle="tab"
href="#attachments" role="tab">
@ -121,6 +127,10 @@
{% include "Parts/info/_tools.html.twig" %}
</div>
<div class="tab-pane fade" id="specifications" role="tabpanel" aria-labelledby="tools-tab">
{% include "Parts/info/_specifications.html.twig" %}
</div>
<div class="tab-pane fade" id="extended_info" role="tabpanel" aria-labelledby="extended_info-tab">
{% include "Parts/info/_extended_infos.html.twig" %}

View file

@ -0,0 +1,115 @@
<?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\Tests\Entity\Parameters;
use App\Entity\Parameters\PartParameter;
use PHPUnit\Framework\TestCase;
class PartParameterTest extends TestCase
{
public function valueWithUnitDataProvider(): array
{
return [
['1', 1.0, ''],
['1 V', 1.0, 'V'],
['1.23', 1.23, ''],
['1.23 V', 1.23, 'V'],
];
}
public function formattedValueDataProvider(): array
{
return [
['Text Test', null, null, null, 'V', 'Text Test'],
['10.23 V', null, 10.23, null, 'V', ''],
['10.23 V [Text]', null, 10.23, null, 'V', 'Text'],
['max. 10.23 V', null, null, 10.23, 'V', ''],
['max. 10.23 [Text]', null, null, 10.23, '', 'Text'],
['min. 10.23 V', 10.23, null, null, 'V', ''],
['10.23 V ... 11 V', 10.23, null, 11, 'V', ''],
['10.23 V (9 V ... 11 V)', 9, 10.23, 11, 'V', ''],
['10.23 V (9 V ... 11 V) [Test]', 9, 10.23, 11, 'V', 'Test'],
];
}
/**
* @dataProvider valueWithUnitDataProvider
* @param string $expected
* @param float $value
* @param string $unit
*/
public function testGetValueMinWithUnit(string $expected, float $value, string $unit): void
{
$param = new PartParameter();
$param->setUnit($unit);
$param->setValueMin($value);
$this->assertSame($expected, $param->getValueMinWithUnit());
}
/**
* @dataProvider valueWithUnitDataProvider
* @param string $expected
* @param float $value
* @param string $unit
*/
public function testGetValueMaxWithUnit(string $expected, float $value, string $unit): void
{
$param = new PartParameter();
$param->setUnit($unit);
$param->setValueMax($value);
$this->assertSame($expected, $param->getValueMaxWithUnit());
}
/**
* @dataProvider valueWithUnitDataProvider
* @param string $expected
* @param float $value
* @param string $unit
*/
public function testGetValueTypicalWithUnit(string $expected, float $value, string $unit): void
{
$param = new PartParameter();
$param->setUnit($unit);
$param->setValueTypical($value);
$this->assertSame($expected, $param->getValueTypicalWithUnit());
}
/**
* @dataProvider formattedValueDataProvider
* @param string $expected
* @param float $min
* @param float $typical
* @param float $max
* @param string $unit
* @param string $text
*/
public function testGetFormattedValue(string $expected, ?float $min, ?float $typical, ?float $max, string $unit, string $text): void
{
$param = new PartParameter();
$param->setUnit($unit);
$param->setValueMin($min);
$param->setValueTypical($typical);
$param->setValueMax($max);
$param->setValueText($text);
$this->assertSame($expected, $param->getFormattedValue());
}
}

View file

@ -7942,7 +7942,7 @@ Element 3</target>
<unit id="mRpl2vJ" name="specifications.unit">
<segment>
<source>specifications.unit</source>
<target>Unit</target>
<target>Einheit</target>
</segment>
</unit>
<unit id="05vM0Vp" name="specifications.text">
@ -7951,5 +7951,47 @@ Element 3</target>
<target>Text</target>
</segment>
</unit>
<unit id="o.P_V00" name="parameters.name.placeholder">
<segment>
<source>parameters.name.placeholder</source>
<target>z.B. DC Current Gain</target>
</segment>
</unit>
<unit id="3LWIDYM" name="parameters.symbol.placeholder">
<segment>
<source>parameters.symbol.placeholder</source>
<target>z.B. h_{FE}</target>
</segment>
</unit>
<unit id="BypUOrf" name="parameters.min.placeholder">
<segment>
<source>parameters.min.placeholder</source>
<target>z.B. 100</target>
</segment>
</unit>
<unit id="VmYIvYF" name="parameters.typical.placeholder">
<segment>
<source>parameters.typical.placeholder</source>
<target>z.B. 200</target>
</segment>
</unit>
<unit id="6utToZ6" name="parameters.max.placeholder">
<segment>
<source>parameters.max.placeholder</source>
<target>z.B. 350</target>
</segment>
</unit>
<unit id="kvhGoo0" name="parameters.unit.placeholder">
<segment>
<source>parameters.unit.placeholder</source>
<target>z.B. V</target>
</segment>
</unit>
<unit id="w7bfLKj" name="parameters.text.placeholder">
<segment>
<source>parameters.text.placeholder</source>
<target>z.B. Test Specifications</target>
</segment>
</unit>
</file>
</xliff>

File diff suppressed because it is too large Load diff

View file

@ -1135,5 +1135,23 @@
<target>The storage location was marked as "single part", so you can not add a new part to it.</target>
</segment>
</unit>
<unit id="jDBA_WW" name="parameters.validator.min_lesser_max">
<segment>
<source>parameters.validator.min_lesser_max</source>
<target>Value must be lesser than the maximum value ({{ compared_value }})</target>
</segment>
</unit>
<unit id="3ODUtpU" name="parameters.validator.min_lesser_typical">
<segment>
<source>parameters.validator.min_lesser_typical</source>
<target>Value must be lesser or equal the the typical value ({{ compared_value }})</target>
</segment>
</unit>
<unit id="ygK_e_X" name="parameters.validator.max_greater_typical">
<segment>
<source>parameters.validator.max_greater_typical</source>
<target>Value must be greater or equal than the typical value ({{ compared_value }})</target>
</segment>
</unit>
</file>
</xliff>