Index: branches/5.2.x/core/units/helpers/country_states_helper.php =================================================================== diff -u -N -r15145 -r16040 --- branches/5.2.x/core/units/helpers/country_states_helper.php (.../country_states_helper.php) (revision 15145) +++ branches/5.2.x/core/units/helpers/country_states_helper.php (.../country_states_helper.php) (revision 16040) @@ -1,6 +1,6 @@ currentLanguage = $this->Application->Phrases->LanguageId; + $this->primaryLanguage = $this->Application->GetDefaultLanguageId(); + } + + /** * Returns countries, that have states * * @return Array @@ -58,32 +85,18 @@ */ 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 ( !$country_iso ) { + return; } - if (!array_key_exists($country_iso, $cache)) { - $states = $this->getStates($country_iso); - - if ( !$states ) { - 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); } @@ -101,16 +114,20 @@ 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%]:PL=' . $this->primaryLanguage . ':CL=' . $this->currentLanguage; + $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->getUnitOption('country-state', 'TableName') . ' 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 +146,6 @@ $table_name = $this->Application->getUnitOption('country-state', 'TableName'); $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 +154,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); } @@ -191,11 +204,19 @@ */ function getCountryStateId($iso_code, $type) { - $sql = 'SELECT ' . $this->Application->getUnitOption('country-state', 'IDField') . ' - FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . ' - WHERE (Type = ' . $type . ') AND (IsoCode = ' . $this->Conn->qstr($iso_code) . ')'; + $cache_key = 'country_state_id[%CountryStateSerial%]:ISO=' . $iso_code . ';Type=' . $type; + $id = $this->Application->getCache($cache_key); - return (int)$this->Conn->GetOne($sql); + if ( $id === false ) { + $sql = 'SELECT ' . $this->Application->getUnitOption('country-state', 'IDField') . ' + FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . ' + WHERE (Type = ' . $type . ') AND (IsoCode = ' . $this->Conn->qstr($iso_code) . ')'; + $id = (int)$this->Conn->GetOne($sql); + + $this->Application->setCache($cache_key, $id); + } + + return $id; } /** @@ -220,4 +241,4 @@ return $this->Conn->GetOne($sql); } - } \ No newline at end of file + }