Fix API to get country

This commit is contained in:
Laurent Destailleur 2020-12-19 22:34:33 +01:00
parent 98cf15ba0f
commit 6066a92b9d
2 changed files with 16 additions and 13 deletions

View File

@ -440,7 +440,7 @@ class Setup extends DolibarrApi
/**
* Get country by Code.
*
* @param string $code Code of country
* @param string $code Code of country (2 characters)
* @param string $lang Code of the language the name of the
* country must be translated to
* @return array Array of cleaned object properties
@ -457,7 +457,7 @@ class Setup extends DolibarrApi
/**
* Get country by Iso.
*
* @param string $iso ISO of country
* @param string $iso ISO of country (3 characters)
* @param string $lang Code of the language the name of the
* country must be translated to
* @return array Array of cleaned object properties
@ -498,8 +498,8 @@ class Setup extends DolibarrApi
* Get country.
*
* @param int $id ID of country
* @param string $code Code of country
* @param string $iso ISO of country
* @param string $code Code of country (2 characters)
* @param string $iso ISO of country (3 characters)
* @param string $lang Code of the language the name of the
* country must be translated to
* @return array Array of cleaned object properties
@ -511,10 +511,11 @@ class Setup extends DolibarrApi
$country = new Ccountry($this->db);
$result = $country->fetch($id, $code, $iso);
if ($result < 0) {
throw new RestException(503, 'Error when retrieving country : '.$country->error);
} elseif ($result == 0) {
throw new RestException(404, 'country not found');
throw new RestException(404, 'Country not found');
}
$this->translateLabel($country, $lang, 'Country');

View File

@ -162,9 +162,9 @@ class Ccountry // extends CommonObject
$sql .= " t.label,";
$sql .= " t.active";
$sql .= " FROM ".MAIN_DB_PREFIX."c_country as t";
if ($id) $sql .= " WHERE t.rowid = ".$id;
elseif ($code) $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
elseif ($code_iso) $sql .= " WHERE t.code_iso = '".$this->db->escape($code_iso)."'";
if ($id) $sql .= " WHERE t.rowid = ".((int) $id);
elseif ($code) $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
elseif ($code_iso) $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql = $this->db->query($sql);
@ -174,11 +174,13 @@ class Ccountry // extends CommonObject
{
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->code = $obj->code;
$this->code_iso = $obj->code_iso;
$this->label = $obj->label;
$this->active = $obj->active;
if ($obj) {
$this->id = $obj->rowid;
$this->code = $obj->code;
$this->code_iso = $obj->code_iso;
$this->label = $obj->label;
$this->active = $obj->active;
}
$this->db->free($resql);
return 1;