Author Topic: VmConfig::get () the default is ignored  (Read 1378 times)

balai

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 1378
VmConfig::get () the default is ignored
« 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;
}

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4017
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: VmConfig::get () the default is ignored
« Reply #1 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 ?

balai

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 1378
Re: VmConfig::get () the default is ignored
« Reply #2 on: January 24, 2018, 12:15:31 pm »
Have you read my post?

This is exactly the bug i reported.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4017
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: VmConfig::get () the default is ignored
« Reply #3 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 ?

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9942
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: VmConfig::get () the default is ignored
« Reply #4 on: January 25, 2018, 10:08:17 am »
Balai, a lot configs use empty String or 0. It is completly correct that way.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

balai

  • 3rd party VirtueMart Developer
  • Full Member
  • *
  • Posts: 1378
Re: VmConfig::get () the default is ignored
« Reply #5 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.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4017
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: VmConfig::get () the default is ignored
« Reply #6 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 ...

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9942
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: VmConfig::get () the default is ignored
« Reply #7 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
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/