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');
}