diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 60f5a54eeba..4048ec62f8a 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -197,13 +197,64 @@ class Setup extends DolibarrApi * @throws RestException */ public function getCountryByID($id, $lang = '') + { + return $this->_fetchCcountry($id, '', '', $lang); + } + + /** + * Get country by Code. + * + * @param string $code Code of country + * @param string $lang Code of the language the name of the + * country must be translated to + * @return array Array of cleaned object properties + * + * @url GET dictionary/countries/byCode/{code} + * + * @throws RestException + */ + public function getCountryByCode($code, $lang = '') + { + return $this->_fetchCcountry('', $code, '', $lang); + } + + /** + * Get country by Iso. + * + * @param string $iso ISO of country + * @param string $lang Code of the language the name of the + * country must be translated to + * @return array Array of cleaned object properties + * + * @url GET dictionary/countries/byISO/{iso} + * + * @throws RestException + */ + public function getCountryByISO($iso, $lang = '') + { + return $this->_fetchCcountry('', '', $iso, $lang); + } + + /** + * Get country. + * + * @param int $id ID of country + * @param string $code Code of country + * @param string $iso ISO of country + * @param string $lang Code of the language the name of the + * country must be translated to + * @return array Array of cleaned object properties + * + * @throws RestException + */ + private function _fetchCcountry($id, $code = '', $iso = '', $lang = '') { $country = new Ccountry($this->db); - if ($country->fetch($id) < 0) { + $result = $country->fetch($id, $code, $iso); + if ($result < 0) { throw new RestException(503, 'Error when retrieving country : '.$country->error); - } - elseif ($country->fetch($id) == 0) { + } elseif ($result == 0) { throw new RestException(404, 'country not found'); } diff --git a/htdocs/core/class/ccountry.class.php b/htdocs/core/class/ccountry.class.php index 84defbfd163..edbceec50fd 100644 --- a/htdocs/core/class/ccountry.class.php +++ b/htdocs/core/class/ccountry.class.php @@ -163,22 +163,24 @@ class Ccountry // extends CommonObject /** * Load object in memory from database * - * @param int $id Id object - * @param string $code Code + * @param int $id Id object + * @param string $code Code + * @param string $code_iso Code ISO * @return int >0 if OK, 0 if not found, <0 if KO */ - public function fetch($id, $code = '') + public function fetch($id, $code = '', $code_iso = '') { global $langs; - $sql = "SELECT"; - $sql.= " t.rowid,"; - $sql.= " t.code,"; - $sql.= " t.code_iso,"; - $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)."'"; + $sql = "SELECT"; + $sql.= " t.rowid,"; + $sql.= " t.code,"; + $sql.= " t.code_iso,"; + $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)."'"; dol_syslog(get_class($this)."::fetch", LOG_DEBUG); $resql=$this->db->query($sql);