Skip to content

Commit

Permalink
Merge pull request #17069 from atomiix/webservice-i18n
Browse files Browse the repository at this point in the history
Fix Currency i18n fields issue in webservice
  • Loading branch information
Progi1984 committed Jan 10, 2020
2 parents ef148c6 + 16e2a80 commit 90ee5fb
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions classes/Currency.php
Expand Up @@ -36,6 +36,13 @@ class CurrencyCore extends ObjectModel
*/
public $name;

/**
* Localized names of the currency
*
* @var string[]
*/
protected $localizedNames;

/**
* Alphabetic ISO 4217 code of this currency.
*
Expand Down Expand Up @@ -93,6 +100,13 @@ class CurrencyCore extends ObjectModel
*/
public $symbol;

/**
* Localized Currency's symbol.
*
* @var string[]
*/
private $localizedSymbol;

/**
* CLDR price formatting pattern
* e.g.: In french (fr-FR), price formatting pattern is : #,##0.00 ¤.
Expand Down Expand Up @@ -149,6 +163,24 @@ class CurrencyCore extends ObjectModel

protected $webserviceParameters = array(
'objectsNodeName' => 'currencies',
'fields' => array(
'name' => array(
'setter' => false,
'getter' => 'getName',
'modifier' => array(
'http_method' => WebserviceRequest::HTTP_POST | WebserviceRequest::HTTP_PUT,
'modifier' => 'setNameForWebservice',
),
),
'symbol' => array(
'setter' => false,
'getter' => 'getSymbol',
'modifier' => array(
'http_method' => WebserviceRequest::HTTP_POST | WebserviceRequest::HTTP_PUT,
'modifier' => 'setSymbolForWebservice',
),
),
),
);

/**
Expand Down Expand Up @@ -183,14 +215,18 @@ public function __construct($id = null, $idLang = null, $idShop = null)
$idLang = Context::getContext()->language->id;
}
if (is_array($this->symbol)) {
$this->localizedSymbol = $this->symbol;
$this->sign = $this->symbol = $this->symbol[$idLang];
} else {
$this->localizedSymbol = [$idLang => $this->symbol];
$this->sign = $this->symbol;
}

if (is_array($this->name)) {
$this->localizedNames = $this->name;
$this->name = Tools::ucfirst($this->name[$idLang]);
} else {
$this->localizedNames = [$idLang => $this->name];
$this->name = Tools::ucfirst($this->name);
}

Expand All @@ -205,6 +241,29 @@ public function __construct($id = null, $idLang = null, $idShop = null)
}
}

public function getWebserviceParameters($ws_params_attribute_name = null)
{
$parameters = parent::getWebserviceParameters($ws_params_attribute_name);
// name & symbol are i18n fields but casted to single string in the constructor
// so we need to force the webservice to consider those fields as non-i18n fields.
// Also, in 1.7.5 the field symbol didn't exists and name wasn't an i18n field so in order
// to keep 1.7.6 backward compatible we need to make those fields non-i18n.
$parameters['fields']['name']['i18n'] = false;
$parameters['fields']['symbol']['i18n'] = false;

return $parameters;
}

public function setNameForWebservice()
{
$this->name = $this->localizedNames;
}

public function setSymbolForWebservice()
{
$this->symbol = $this->localizedSymbol;
}

/**
* reset static cache (eg unit testing purpose).
*/
Expand Down Expand Up @@ -379,6 +438,20 @@ public function getName()
return Tools::ucfirst($this->name[$id_lang]);
}

public function getSymbol()
{
if (is_string($this->symbol)) {
return $this->symbol;
}

$id_lang = $this->id_lang;
if (null === $id_lang) {
$id_lang = Configuration::get('PS_LANG_DEFAULT');
}

return Tools::ucfirst($this->symbol[$id_lang]);
}

/**
* Return available currencies.
*
Expand Down

0 comments on commit 90ee5fb

Please sign in to comment.