Index: branches/5.3.x/core/units/helpers/country_states_helper.php =================================================================== diff -u -N -r15698 -r16111 --- branches/5.3.x/core/units/helpers/country_states_helper.php (.../country_states_helper.php) (revision 15698) +++ branches/5.3.x/core/units/helpers/country_states_helper.php (.../country_states_helper.php) (revision 16111) @@ -1,6 +1,6 @@ currentLanguage = $this->Application->Phrases->LanguageId; + $this->primaryLanguage = $this->Application->GetDefaultLanguageId(); + } + + /** * Returns countries, that have states * * @return Array @@ -58,33 +85,19 @@ */ function PopulateStates($event, $state_field, $country_field) { - static $cache = Array (); - $object = $event->getObject(); /* @var $object kDBItem */ $country_iso = $object->GetDBField($country_field); - if (!$country_iso) { - return ; - } - - if (!array_key_exists($country_iso, $cache)) { - $states = $this->getStates($country_iso); - - if ( !$states ) { + if ( !$country_iso ) { return; } - $cache[$country_iso] = $states; - } - $field_options = $object->GetFieldOptions($state_field); - - $field_options['options'] = $cache[$country_iso]; + $field_options['options'] = $this->getStates($country_iso); $field_options['options'][''] = ''; - - $object->SetFieldOptions($state_field, $field_options); + $object->SetFieldOptions($state_field, $field_options, $object->isVirtualField($state_field)); } /** @@ -101,16 +114,21 @@ return Array (); } - // don't use GetVar('m_lang') since it's always equals to default language on editing form in admin - $current_language = $this->Application->Phrases->LanguageId; - $primary_language = $this->Application->GetDefaultLanguageId(); + $cache_key = 'country_states[%CountryStateSerial%]'; + $cache_key .= ':PL=' . $this->primaryLanguage . ':CL=' . $this->currentLanguage . ':ISO=' . $country_iso; + $states = $this->Application->getCache($cache_key); - $sql = 'SELECT IF(l' . $current_language . '_Name = "", l' . $primary_language . '_Name, l' . $current_language . '_Name) AS Name, IsoCode + if ( $states === false ) { + $sql = 'SELECT IF(l' . $this->currentLanguage . '_Name = "", l' . $this->primaryLanguage . '_Name, l' . $this->currentLanguage . '_Name) AS Name, IsoCode FROM ' . $this->Application->getUnitConfig('country-state')->getTableName() . ' WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = ' . $country_id . ') ORDER BY Name ASC'; + $states = $this->Conn->GetCol($sql, 'IsoCode'); - return $this->Conn->GetCol($sql, 'IsoCode'); + $this->Application->setCache($cache_key, $states); + } + + return $states; } /** @@ -129,10 +147,6 @@ $table_name = $this->Application->getUnitConfig('country-state')->getTableName(); $country_id = $this->getCountryStateId($country_iso, DESTINATION_TYPE_COUNTRY); - // don't use GetVar('m_lang') since it's always equals to default language on editing form in admin - $current_language = $this->Application->Phrases->LanguageId; - $primary_language = $this->Application->GetDefaultLanguageId(); - $sql = 'SELECT IsoCode FROM ' . $table_name . ' WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = %1$s) AND @@ -141,7 +155,7 @@ )'; $state_name = trim(mb_strtoupper($state_name)); - $sql = sprintf($sql, $country_id, $this->Conn->qstr($state_name), $current_language, $primary_language); + $sql = sprintf($sql, $country_id, $this->Conn->qstr($state_name), $this->currentLanguage, $this->primaryLanguage); return $this->Conn->GetOne($sql); } @@ -192,12 +206,19 @@ function getCountryStateId($iso_code, $type) { $config = $this->Application->getUnitConfig('country-state'); + $cache_key = 'country_state_id[%CountryStateSerial%]:ISO=' . $iso_code . ';Type=' . $type; + $id = $this->Application->getCache($cache_key); - $sql = 'SELECT ' . $config->getIDField() . ' - FROM ' . $config->getTableName() . ' - WHERE (Type = ' . $type . ') AND (IsoCode = ' . $this->Conn->qstr($iso_code) . ')'; + if ( $id === false ) { + $sql = 'SELECT ' . $config->getIDField() . ' + FROM ' . $config->getTableName() . ' + WHERE (Type = ' . $type . ') AND (IsoCode = ' . $this->Conn->qstr($iso_code) . ')'; + $id = (int)$this->Conn->GetOne($sql); - return (int)$this->Conn->GetOne($sql); + $this->Application->setCache($cache_key, $id); + } + + return $id; } /** @@ -222,4 +243,4 @@ return $this->Conn->GetOne($sql); } - } \ No newline at end of file + }