VirtueMart Forum

VirtueMart 2 + 3 => Virtuemart Development and bug reports => Topic started by: balai on January 23, 2018, 12:30:57 pm

Title: VmConfig::get () the default is ignored
Post by: balai on January 23, 2018, 12:30:57 pm
Hi

I am checking the VmConfig::get('value', 'default');
Though even when the config value is empty the default is ignored.

Looking at the code, it justifies that. The default is taken into account only when the setting does not exist, ignoring the empty inputs.

Code: [Select]
static function get($key, $default='',$allow_load=FALSE)
{

$value = '';
if ($key) {

if (empty(self::$_jpConfig->_params) && $allow_load) {
self::loadConfig();
}

if (!empty(self::$_jpConfig->_params)) {
if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){
$value = self::$_jpConfig->_params[$key];
} else {
$value = $default;
}

} else {
$value = $default;
}

} else {
$app = JFactory::getApplication();
$app -> enqueueMessage('VmConfig get, empty key given');
}

return $value;
}
Title: Re: VmConfig::get () the default is ignored
Post by: Studio 42 on January 24, 2018, 03:00:10 am
Have you read correctly?
Code: [Select]
if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){
$value = self::$_jpConfig->_params[$key];
} else {
$value = $default;
}
isset(self::$_jpConfig->_params[$key]) mean if not exist value and not if empty $value
So $default is loaded if $key do not exist.
Have you do a test ?
Title: Re: VmConfig::get () the default is ignored
Post by: balai on January 24, 2018, 12:15:31 pm
Have you read my post?

This is exactly the bug i reported.
Title: Re: VmConfig::get () the default is ignored
Post by: Studio 42 on January 24, 2018, 14:46:14 pm
It's same as Joomla input.
If the value is set, default is not used.
so if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){ return the $value in all case if the $key exist
Else it return $default
It's why i have ask
Quote
Have you do a test ?
Title: Re: VmConfig::get () the default is ignored
Post by: Milbo on January 25, 2018, 10:08:17 am
Balai, a lot configs use empty String or 0. It is completly correct that way.
Title: Re: VmConfig::get () the default is ignored
Post by: balai on January 25, 2018, 11:53:09 am
I think 0 is acceptable, but an empty  field is not a value.
Also the ideal would be to have also some data validation like the inputs/requests.
VmConfig::get ('value', 'default', 'type');
*type > optional (defaults to string)

Accepting empty or invalid values, renders the default param almost useless.
This could generate problems to scripts using these values, since it will pass non expected values to them.

For example yesterday i confronted an issue, where a i could not get the proper image's filename, because the thumbnail's length setting was empty or a space.
As you know the thumbnails have this format: name_lengthxwidth.png
Hence i was getting an invalid filename (missing the length part). Instead i was expecting to read the default value that was there exactly for that.
Title: Re: VmConfig::get () the default is ignored
Post by: Studio 42 on January 25, 2018, 13:26:37 pm
VmConfig::get ('value', 'default', 'type');
Can be done using : (type)VmConfig::get ('value', 'default');
Where type is a valid PHP type eg. Int, String, Bool, Array ...
Title: Re: VmConfig::get () the default is ignored
Post by: Milbo on January 25, 2018, 20:26:18 pm
Balai, the empty value is the common value for "use global"! That is the reason you have "is set" and "is empty"
http://php.net/manual/en/function.isset.php and http://php.net/manual/en/function.empty.php