From bc8748e6f4563e0b200392cbcc3f83a5298062df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 14 Mar 2020 22:12:19 +0100 Subject: [PATCH] Show parameters on info page & improved parameter editing. --- src/Entity/Parameters/AbstractParameter.php | 98 +- src/Form/ParameterType.php | 42 +- src/Migrations/Version20200311204104.php | 2 +- .../Parts/edit/_specifications.html.twig | 2 +- .../Parts/edit/edit_form_styles.html.twig | 2 +- .../Parts/info/_specifications.html.twig | 18 + templates/Parts/info/show_part_info.html.twig | 10 + tests/Entity/Parameters/PartParameterTest.php | 115 ++ translations/messages.de.xlf | 44 +- translations/messages.en.xlf | 1598 +++++++++-------- translations/validators.en.xlf | 18 + 11 files changed, 1176 insertions(+), 773 deletions(-) create mode 100644 templates/Parts/info/_specifications.html.twig create mode 100644 tests/Entity/Parameters/PartParameterTest.php diff --git a/src/Entity/Parameters/AbstractParameter.php b/src/Entity/Parameters/AbstractParameter.php index 9c12521d..e5f62424 100644 --- a/src/Entity/Parameters/AbstractParameter.php +++ b/src/Entity/Parameters/AbstractParameter.php @@ -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 diff --git a/src/Form/ParameterType.php b/src/Form/ParameterType.php index f3eabe56..860aa0ba 100644 --- a/src/Form/ParameterType.php +++ b/src/Form/ParameterType.php @@ -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;' + ] ]); } diff --git a/src/Migrations/Version20200311204104.php b/src/Migrations/Version20200311204104.php index 16195f23..83c65d20 100644 --- a/src/Migrations/Version20200311204104.php +++ b/src/Migrations/Version20200311204104.php @@ -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'); } diff --git a/templates/Parts/edit/_specifications.html.twig b/templates/Parts/edit/_specifications.html.twig index 331f5c23..0e49569d 100644 --- a/templates/Parts/edit/_specifications.html.twig +++ b/templates/Parts/edit/_specifications.html.twig @@ -1,6 +1,6 @@ {% form_theme form with ['Parts/edit/edit_form_styles.html.twig', "bootstrap_4_layout.html.twig"] %} - +
diff --git a/templates/Parts/edit/edit_form_styles.html.twig b/templates/Parts/edit/edit_form_styles.html.twig index 6c897987..27f53d67 100644 --- a/templates/Parts/edit/edit_form_styles.html.twig +++ b/templates/Parts/edit/edit_form_styles.html.twig @@ -74,7 +74,7 @@
{% trans %}specifications.property{% endtrans %}{{ form_widget(form.unit) }}{{ form_errors(form.unit) }} {{ form_widget(form.value_text) }}{{ form_errors(form.value_text) }} - {{ form_errors(form) }} diff --git a/templates/Parts/info/_specifications.html.twig b/templates/Parts/info/_specifications.html.twig new file mode 100644 index 00000000..f6fafc70 --- /dev/null +++ b/templates/Parts/info/_specifications.html.twig @@ -0,0 +1,18 @@ +{# var \App\Entity\Parts\Part part #} + + + + + + + + + + {% for param in part.parameters %} + + + + + {% endfor %} + +
{% trans %}specifications.property{% endtrans %}{% trans %}specifications.value{% endtrans %}
{{ param.name }}{{ param.formattedValue }}
\ No newline at end of file diff --git a/templates/Parts/info/show_part_info.html.twig b/templates/Parts/info/show_part_info.html.twig index 9f319529..771a941e 100644 --- a/templates/Parts/info/show_part_info.html.twig +++ b/templates/Parts/info/show_part_info.html.twig @@ -57,6 +57,12 @@ {% endif %} +