From dc23b1d5ca53b5fa51b86181a761a73707c2b44d Mon Sep 17 00:00:00 2001 From: dMetzger Date: Thu, 3 May 2012 15:11:43 +0000 Subject: [PATCH] [-] BO : you cannot create 2 states with the same iso code in the same country anymore #PSCFV-53 --- classes/State.php | 10 +++++----- controllers/admin/AdminStatesController.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/classes/State.php b/classes/State.php index 2a500ab5f..9ca4fccb1 100644 --- a/classes/State.php +++ b/classes/State.php @@ -113,13 +113,13 @@ class StateCore extends ObjectModel * @param string $iso_code Iso code * @return integer state id */ - public static function getIdByIso($iso_code) + public static function getIdByIso($iso_code, $id_country = null) { return Db::getInstance()->getValue(' - SELECT `id_state` - FROM `'._DB_PREFIX_.'state` - WHERE `iso_code` = \''.pSQL($iso_code).'\'' - ); + SELECT `id_state` + FROM `'._DB_PREFIX_.'state` + WHERE `iso_code` = \''.pSQL($iso_code).'\' + '.($id_country ? 'AND `id_country` = '.(int)$id_country : '')); } /** diff --git a/controllers/admin/AdminStatesController.php b/controllers/admin/AdminStatesController.php index 93abf400d..7987cd44b 100644 --- a/controllers/admin/AdminStatesController.php +++ b/controllers/admin/AdminStatesController.php @@ -181,6 +181,18 @@ class AdminStatesControllerCore extends AdminController { if (!isset($this->table)) return false; + + if (!Tools::getValue('id_'.$this->table)) + { + if (Validate::isStateIsoCode(Tools::getValue('iso_code')) && State::getIdByIso(Tools::getValue('iso_code'), Tools::getValue('id_country'))) + $this->errors[] = Tools::displayError('This ISO code already exists, you cannot create two states with the same ISO code in the same country'); + } + else if (Validate::isStateIsoCode(Tools::getValue('iso_code'))) + { + $id_state = State::getIdByIso(Tools::getValue('iso_code'), Tools::getValue('id_country')); + if ($id_state && $id_state != Tools::getValue('id_'.$this->table)) + $this->errors[] = Tools::displayError('This ISO code already exists, you cannot create two states with the same ISO code in the same country'); + } /* Delete object */ if (isset($_GET['delete'.$this->table]))