Index: branches/5.1.x/core/units/helpers/language_import_helper.php
===================================================================
diff -u -N -r13453 -r13470
--- branches/5.1.x/core/units/helpers/language_import_helper.php (.../language_import_helper.php) (revision 13453)
+++ branches/5.1.x/core/units/helpers/language_import_helper.php (.../language_import_helper.php) (revision 13470)
@@ -1,6 +1,6 @@
_languages as $language_id) {
$this->_performUpgrade($language_id, 'phrases', 'PhraseKey', Array ('l%s_Translation'));
$this->_performUpgrade($language_id, 'emailevents', 'EventId', Array ('l%s_Subject', 'Headers', 'MessageType', 'l%s_Body'));
+ $this->_performUpgrade($language_id, 'country-state', 'CountryStateId', Array ('l%s_Name'));
}
$this->_initImportTables(true);
@@ -214,9 +215,7 @@
$phrase_types = explode('|', substr($phrase_types, 1, -1) );
$module_ids = explode('|', substr($module_ids, 1, -1) );
- $phrase_tpl = "\t\t\t".'%s'."\n";
- $event_tpl = "\t\t\t".'%s'."\n";
- $ret = ''."\n";
+ $ret = '' . "\n";
$export_fields = $this->_getExportFields();
@@ -263,11 +262,35 @@
}
$sql = 'SELECT *
- FROM ' . $this->Application->getUnitOption('emailevents','TableName') . '
+ FROM ' . $this->Application->getUnitOption('emailevents', 'TableName') . '
WHERE ' . substr($module_sql, 0, -4) . ' AND ' . $limit_where . '
ORDER BY `Event`, `Type`';
$events = $this->Conn->Query($sql, 'EventId');
+ // countries
+ $sql = 'SELECT *
+ FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
+ WHERE Type = ' . DESTINATION_TYPE_COUNTRY . '
+ ORDER BY `IsoCode`';
+ $countries = $this->Conn->Query($sql, 'CountryStateId');
+
+ // states
+ $sql = 'SELECT *
+ FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
+ WHERE Type = ' . DESTINATION_TYPE_STATE . '
+ ORDER BY `IsoCode`';
+ $states = $this->Conn->Query($sql, 'CountryStateId');
+
+ foreach ($states as $state_id => $state_data) {
+ $country_id = $state_data['StateCountryId'];
+
+ if (!array_key_exists('States', $countries[$country_id])) {
+ $countries[$country_id]['States'] = Array ();
+ }
+
+ $countries[$country_id]['States'][] = $state_id;
+ }
+
foreach ($languages as $language_id => $language_info) {
// language
$ret .= "\t" . '';
$replacements = $language_info['FilenameReplacements'];
- $ret .= $this->_exportEncoding == 'base64' ? base64_encode($replacements) : '';
- $ret .= '' . "\n";
+ if ($replacements) {
+ $ret .= "\t\t" . '';
+ $ret .= $this->_exportEncoding == 'base64' ? base64_encode($replacements) : '';
+ $ret .= '' . "\n";
+ }
+
// phrases
if ($phrases) {
$ret .= "\t\t" . '' . "\n";
@@ -296,8 +322,9 @@
}
$data = $this->_exportEncoding == 'base64' ? base64_encode($translation) : '';
- $ret .= sprintf($phrase_tpl, $phrase['Phrase'], $phrase['Module'], $phrase['PhraseType'], $data);
+ $ret .= "\t\t\t" . '' . $data . '' . "\n";
}
+
$ret .= "\t\t" . '' . "\n";
}
@@ -320,12 +347,49 @@
}
$data = $this->_exportEncoding == 'base64' ? base64_encode($template) : '';
- $ret .= sprintf($event_tpl, $event['MessageType'], $event['Event'], $event['Type'], $data);
+ $ret .= "\t\t\t" . '' . $data . ''."\n";
}
$ret .= "\t\t" . '' . "\n";
}
+ if ($countries) {
+ $ret .= "\t\t" . '' . "\n";
+ foreach ($countries as $country_id => $country_data) {
+ $translation = $country_data['l' . $language_id . '_Name'];
+
+ if (!$translation) {
+ // country is not translated on given language
+ continue;
+ }
+
+ $data = $this->_exportEncoding == 'base64' ? base64_encode($translation) : $translation;
+
+ if (array_key_exists('States', $country_data)) {
+ $ret .= "\t\t\t" . '' . "\n";
+
+ foreach ($country_data['States'] as $state_id) {
+ $translation = $states[$state_id]['l' . $language_id . '_Name'];
+
+ if (!$translation) {
+ // state is not translated on given language
+ continue;
+ }
+
+ $data = $this->_exportEncoding == 'base64' ? base64_encode($translation) : $translation;
+ $ret .= "\t\t\t\t" . '' . "\n";
+ }
+
+ $ret .= "\t\t\t" . '' . "\n";
+ }
+ else {
+ $ret .= "\t\t\t" . '' . "\n";
+ }
+ }
+
+ $ret .= "\t\t" . '' . "\n";
+ }
+
$ret .= "\t" . '' . "\n";
}
@@ -515,6 +579,7 @@
{
$this->_tables['phrases'] = $this->_prepareTempTable('phrases', $drop_only);
$this->_tables['emailevents'] = $this->_prepareTempTable('emailevents', $drop_only);
+ $this->_tables['country-state'] = $this->_prepareTempTable('country-state', $drop_only);
}
/**
@@ -538,7 +603,24 @@
$sql = 'ALTER TABLE %1$s CHANGE %2$s %2$s INT(11) NOT NULL DEFAULT "0"';
$this->Conn->Query( sprintf($sql, $temp_table, $idfield) );
- $unique_field = $prefix == 'phrases' ? 'PhraseKey' : 'EventId';
+ switch ($prefix) {
+ case 'phrases':
+ $unique_field = 'PhraseKey';
+ break;
+
+ case 'emailevents':
+ $unique_field = 'EventId';
+ break;
+
+ case 'country-state':
+ $unique_field = 'CountryStateId';
+ break;
+
+ default:
+ trigger_error('Unknown prefix "' . $prefix . '" during language pack import', E_USER_ERROR);
+ break;
+ }
+
$sql = 'ALTER TABLE ' . $temp_table . ' ADD UNIQUE (' . $unique_field . ')';
$this->Conn->Query($sql);
}
@@ -661,6 +743,16 @@
}
break;
+ case 'COUNTRIES':
+ if ($sub_node->Children) {
+ if (!$language_id) {
+ $language_id = $this->_processLanguage($fields_hash);
+ }
+
+ $this->_processCountries($sub_node->firstChild, $language_id, $fields_hash['Encoding']);
+ }
+ break;
+
case 'REPLACEMENTS':
// added since v2
$replacements = $sub_node->Data;
@@ -805,6 +897,58 @@
}
/**
+ * Performs country_state translation import
+ *
+ * @param kXMLNode $country_state_node
+ * @param int $language_id
+ * @param string $language_encoding
+ */
+ function _processCountries(&$country_state_node, $language_id, $language_encoding, $process_states = false)
+ {
+ static $other_translations = Array ();
+
+ do {
+ if ($process_states) {
+ $country_state_id = $this->_getStateId($country_state_node->Parent->Attributes['ISO'], $country_state_node->Attributes['ISO']);
+ }
+ else {
+ $country_state_id = $this->_getCountryId($country_state_node->Attributes['ISO']);
+ }
+
+ if ($country_state_id) {
+ if ($language_encoding == 'plain') {
+ $translation = rtrim($country_state_node->Attributes['TRANSLATION']);
+ }
+ else {
+ $translation = base64_decode($country_state_node->Attributes['TRANSLATION']);
+ }
+
+ $fields_hash = Array (
+ 'CountryStateId' => $country_state_id,
+ );
+
+ if (array_key_exists($country_state_id, $other_translations)) {
+ $other_translations[$country_state_id]['l' . $language_id . '_Name'] = $translation;
+ }
+ else {
+ $other_translations[$country_state_id] = Array (
+ 'l' . $language_id . '_Name' => $translation,
+ );
+ }
+
+ $fields_hash = array_merge($fields_hash, $other_translations[$country_state_id]);
+ $this->Conn->doInsert($fields_hash, $this->_tables['country-state'], 'REPLACE', false);
+
+ if (!$process_states && $country_state_node->Children) {
+ $this->_processCountries($country_state_node->firstChild, $language_id, $language_encoding, true);
+ }
+ }
+ } while (($country_state_node =& $country_state_node->NextSibling()));
+
+ $this->Conn->doInsert($fields_hash, $this->_tables['country-state'], 'REPLACE');
+ }
+
+ /**
* Creates/updates language based on given fields and returns it's id
*
* @param Array $fields_hash
@@ -864,4 +1008,47 @@
return array_key_exists($cache_key, $this->events_hash) ? $this->events_hash[$cache_key] : 0;
}
+
+ /**
+ * Returns country id based on it's 3letter ISO code
+ *
+ * @param string $iso
+ * @return int
+ */
+ function _getCountryId($iso)
+ {
+ static $cache = null;
+
+ if (!isset($cache)) {
+ $sql = 'SELECT CountryStateId, IsoCode
+ FROM ' . TABLE_PREFIX . 'CountryStates
+ WHERE Type = ' . DESTINATION_TYPE_COUNTRY;
+ $cache = $this->Conn->GetCol($sql, 'IsoCode');
+ }
+
+ return array_key_exists($iso, $cache) ? $cache[$iso] : false;
+ }
+
+ /**
+ * Returns state id based on 3letter country ISO code and 2letter state ISO code
+ *
+ * @param string $country_iso
+ * @param string $state_iso
+ * @return int
+ */
+ function _getStateId($country_iso, $state_iso)
+ {
+ static $cache = null;
+
+ if (!isset($cache)) {
+ $sql = 'SELECT CountryStateId, CONCAT(StateCountryId, "-", IsoCode) AS IsoCode
+ FROM ' . TABLE_PREFIX . 'CountryStates
+ WHERE Type = ' . DESTINATION_TYPE_STATE;
+ $cache = $this->Conn->GetCol($sql, 'IsoCode');
+ }
+
+ $country_id = $this->_getCountryId($country_iso);
+
+ return array_key_exists($country_id . '-' . $state_iso, $cache) ? $cache[$country_id . '-' . $state_iso] : false;
+ }
}
\ No newline at end of file