Basket: introduce constants

This commit is contained in:
Thomas Gelf 2023-03-07 11:41:14 +01:00
parent 7c8edfd467
commit f1add44beb
2 changed files with 14 additions and 15 deletions

View File

@ -52,9 +52,9 @@ class BasketForm extends DirectorObjectForm
$types = $this->getAvailableTypes(); $types = $this->getAvailableTypes();
$options = [ $options = [
'IGNORE' => $this->translate('Ignore'), Basket::SELECTION_NONE => $this->translate('Ignore'),
'ALL' => $this->translate('All of them'), Basket::SELECTION_ALL => $this->translate('All of them'),
'[]' => $this->translate('Custom Selection'), Basket::SELECTION_CUSTOM => $this->translate('Custom Selection'),
]; ];
$this->addHtmlHint($this->translate( $this->addHtmlHint($this->translate(
@ -92,13 +92,13 @@ class BasketForm extends DirectorObjectForm
/** @var Basket $object */ /** @var Basket $object */
$values = []; $values = [];
foreach ($this->getAvailableTypes() as $type => $label) { foreach ($this->getAvailableTypes() as $type => $label) {
$values[$type] = 'IGNORE'; $values[$type] = Basket::SELECTION_NONE;
} }
foreach ($object->getChosenObjects() as $type => $selection) { foreach ($object->getChosenObjects() as $type => $selection) {
if ($selection === true) { if ($selection === true) {
$values[$type] = 'ALL'; $values[$type] = Basket::SELECTION_ALL;
} elseif (is_array($selection)) { } elseif (is_array($selection)) {
$values[$type] = '[]'; $values[$type] = Basket::SELECTION_CUSTOM;
} }
} }

View File

@ -15,8 +15,9 @@ use Icinga\Module\Director\Exception\DuplicateKeyException;
*/ */
class Basket extends DbObject implements ExportInterface class Basket extends DbObject implements ExportInterface
{ {
const SELECTION_ALL = true; const SELECTION_ALL = 'ALL';
const SELECTION_NONE = false; const SELECTION_NONE = 'IGNORE';
const SELECTION_CUSTOM = '[]';
protected $table = 'director_basket'; protected $table = 'director_basket';
@ -141,11 +142,11 @@ class Basket extends DbObject implements ExportInterface
{ {
BasketSnapshot::assertValidType($type); BasketSnapshot::assertValidType($type);
// '1' -> from Form! // '1' -> from Form!
if ($objects === 'ALL') { if ($objects === self::SELECTION_ALL) {
$objects = true; $objects = true;
} elseif ($objects === null || $objects === 'IGNORE') { } elseif ($objects === null || $objects === self::SELECTION_NONE) {
return; return;
} elseif ($objects === '[]' || is_array($objects)) { } elseif ($objects === self::SELECTION_CUSTOM || is_array($objects)) {
if (! isset($this->chosenObjects[$type]) || ! is_array($this->chosenObjects[$type])) { if (! isset($this->chosenObjects[$type]) || ! is_array($this->chosenObjects[$type])) {
$this->chosenObjects[$type] = []; $this->chosenObjects[$type] = [];
} }
@ -157,16 +158,14 @@ class Basket extends DbObject implements ExportInterface
} }
} }
if ($objects === '[]') { if ($objects === self::SELECTION_CUSTOM) {
$objects = []; $objects = [];
} }
} }
if ($objects === true) { if ($objects === true) {
$this->chosenObjects[$type] = true; $this->chosenObjects[$type] = true;
} elseif ($objects === '0') { } elseif ($objects !== '0') { // TODO: what would generate '0'?
// nothing
} else {
foreach ($objects as $object) { foreach ($objects as $object) {
$this->addObject($type, $object); $this->addObject($type, $object);
} }