MVC: refactor JsonKeyValueStoreField type, skip unit test since it depends on configd. for https://github.com/opnsense/core/issues/3252

This commit is contained in:
Ad Schellevis 2019-11-08 16:04:00 +01:00
parent 5324f35ceb
commit e1883f34ac

View File

@ -1,7 +1,7 @@
<?php
/**
* Copyright (C) 2015 Deciso B.V.
* Copyright (C) 2015-2019 Deciso B.V.
*
* All rights reserved.
*
@ -29,41 +29,14 @@
*/
namespace OPNsense\Base\FieldTypes;
use Phalcon\Validation\Validator\InclusionIn;
use OPNsense\Base\Validators\CsvListValidator;
use OPNsense\Core\Backend;
/**
* Class JsonKeyValueStoreField, use a json encoded file as selection list
* @package OPNsense\Base\FieldTypes
*/
class JsonKeyValueStoreField extends BaseField
class JsonKeyValueStoreField extends BaseListField
{
/**
* @var bool marks if this is a data node or a container
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "option not in list";
/**
* @var bool field may contain multiple servers at once
*/
private $internalMultiSelect = false;
/**
* @var string default description for empty item
*/
private $internalEmptyDescription = "none";
/**
* @var array valid options for this list
*/
private $internalOptionList = array();
/**
* @var null source field
*/
@ -94,15 +67,6 @@ class JsonKeyValueStoreField extends BaseField
*/
private $internalSortByValue = false;
/**
* set descriptive text for empty value
* @param string $value description
*/
public function setBlankDesc($value)
{
$this->internalEmptyDescription = $value;
}
/**
* @param string $value source field, pattern for source file
*/
@ -191,19 +155,6 @@ class JsonKeyValueStoreField extends BaseField
}
}
/**
* select if multiple authentication servers may be selected at once
* @param $value boolean value Y/N
*/
public function setMultiple($value)
{
if (trim(strtoupper($value)) == "Y") {
$this->internalMultiSelect = true;
} else {
$this->internalMultiSelect = false;
}
}
/**
* change default sorting order (value vs key)
* @param $value boolean value Y/N
@ -223,49 +174,13 @@ class JsonKeyValueStoreField extends BaseField
*/
public function getNodeData()
{
$result = array ();
// if relation is not required and single, add empty option
if (!$this->internalIsRequired && !$this->internalMultiSelect) {
$result[""] = array("value"=>$this->internalEmptyDescription, "selected" => 0);
}
$options = explode(',', $this->internalValue);
// set sorting by key (default) or value
if ($this->internalSortByValue) {
natcasesort($this->internalOptionList);
} else {
ksort($this->internalOptionList);
}
foreach ($this->internalOptionList as $optKey => $optValue) {
if (in_array($optKey, $options)) {
$selected = 1;
} else {
$selected = 0;
}
$result[$optKey] = array("value"=>$optValue, "selected" => $selected);
}
return $result;
return parent::getNodeData();
}
/**
* retrieve field validators for this field type
* @return array returns InclusionIn validator
*/
public function getValidators()
{
$validators = parent::getValidators();
if ($this->internalValue != null) {
if ($this->internalMultiSelect) {
// field may contain more than one value
$validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage,
'domain'=>array_keys($this->internalOptionList)));
} else {
// single value selection
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys($this->internalOptionList)));
}
}
return $validators;
}
}