Index: branches/5.1.x/core/units/helpers/language_import_helper.php
===================================================================
diff -u -N -r13140 -r13151
--- branches/5.1.x/core/units/helpers/language_import_helper.php (.../language_import_helper.php) (revision 13140)
+++ branches/5.1.x/core/units/helpers/language_import_helper.php (.../language_import_helper.php) (revision 13151)
@@ -1,6 +1,6 @@
_languages as $language_id) {
- $this->_performUpgrade($language_id, 'phrases', 'PhraseKey');
- $this->_performUpgrade_v2($language_id, 'emailevents', 'EventId', Array ('l%s_Subject', 'Headers', 'l%s_Body'));
+ $this->_performUpgrade($language_id, 'phrases', 'PhraseKey', Array ('l%s_Translation'));
+ $this->_performUpgrade($language_id, 'emailevents', 'EventId', Array ('l%s_Subject', 'Headers', 'l%s_Body'));
}
$this->_initImportTables(true);
@@ -192,84 +192,97 @@
$phrase_types = explode('|', substr($phrase_types, 1, -1) );
$module_ids = explode('|', substr($module_ids, 1, -1) );
- $this->events_hash = array_flip($this->events_hash);
-
- $lang_table = $this->Application->getUnitOption('lang','TableName');
- $phrases_table = $this->Application->getUnitOption('phrases','TableName');
- $events_table = $this->Application->getUnitOption('emailevents','TableName');
-
$phrase_tpl = "\t\t\t".'%s'."\n";
$event_tpl = "\t\t\t".'%s'."\n";
- $sql = 'SELECT * FROM %s WHERE LanguageId = %s';
$ret = ''."\n";
$export_fields = $this->_getExportFields();
$email_message_helper =& $this->Application->recallObject('EmailMessageHelper');
/* @var $email_message_helper EmailMessageHelper */
- foreach ($language_ids as $language_id) {
- // languages
- $row = $this->Conn->GetRow( sprintf($sql, $lang_table, $language_id) );
+ // get languages
+ $sql = 'SELECT *
+ FROM ' . $this->Application->getUnitOption('lang','TableName') . '
+ WHERE LanguageId IN (' . implode(',', $language_ids) . ')';
+ $languages = $this->Conn->Query($sql, 'LanguageId');
+
+ // get phrases
+ $phrase_modules = $module_ids;
+ array_push($phrase_modules, ''); // for old language packs without module
+
+ $phrase_modules = array_map(Array (&$this->Conn, 'qstr'), $phrase_modules);
+
+ $sql = 'SELECT *
+ FROM ' . $this->Application->getUnitOption('phrases','TableName') . '
+ WHERE PhraseType IN (' . implode(',', $phrase_types) . ') AND Module IN (' . implode(',', $phrase_modules) . ')
+ ORDER BY Phrase';
+ $phrases = $this->Conn->Query($sql, 'PhraseId');
+
+ // email events
+ $module_sql = preg_replace('/(.*),/U', 'INSTR(Module,\'\\1\') OR ', implode(',', $module_ids) . ',');
+
+ $sql = 'SELECT *
+ FROM ' . $this->Application->getUnitOption('emailevents','TableName') . '
+ WHERE ' . substr($module_sql, 0, -4) . '
+ ORDER BY `Event`, `Type`';
+ $events = $this->Conn->Query($sql, 'EventId');
+
+ foreach ($languages as $language_id => $language_info) {
+ // language
$ret .= "\t" . '_exportEncoding == 'base64' ? base64_encode($row['FilenameReplacements']) : '';
+ // filename replacements
+ $ret .= "\t\t" . '';
+ $replacements = $language_info['FilenameReplacements'];
+ $ret .= $this->_exportEncoding == 'base64' ? base64_encode($replacements) : '';
$ret .= '' . "\n";
// phrases
- $phrases_sql = 'SELECT * FROM '.$phrases_table.' WHERE LanguageId = %s AND PhraseType IN (%s) AND Module IN (%s) ORDER BY Phrase';
- if( in_array('In-Portal',$module_ids) ) array_push($module_ids, ''); // for old language packs
- $rows = $this->Conn->Query( sprintf($phrases_sql,$language_id, implode(',',$phrase_types), '\''.implode('\',\'',$module_ids).'\'' ) );
- if($rows)
- {
- $ret .= "\t\t".''."\n";
- foreach($rows as $row)
- {
- $data = $this->_exportEncoding == 'base64' ? base64_encode($row['Translation']) : '';
- $ret .= sprintf($phrase_tpl, $row['Phrase'], $row['Module'], $row['PhraseType'], $data );
+ if ($phrases) {
+ $ret .= "\t\t" . '' . "\n";
+ foreach ($phrases as $phrase_id => $phrase) {
+ $translation = $phrase['l' . $language_id . '_Translation'];
+
+ if (!$translation) {
+ // phrase is not translated on given language
+ continue;
+ }
+
+ $data = $this->_exportEncoding == 'base64' ? base64_encode($translation) : '';
+ $ret .= sprintf($phrase_tpl, $phrase['Phrase'], $phrase['Module'], $phrase['PhraseType'], $data);
}
- $ret .= "\t\t".''."\n";
+ $ret .= "\t\t" . '' . "\n";
}
// email events
- if ( in_array('In-Portal',$module_ids) ) {
- unset( $module_ids[array_search('',$module_ids)] ); // for old language packs
- }
+ $ret .= "\t\t" . '' . "\n";
- $module_sql = preg_replace('/(.*),/U', 'INSTR(Module,\'\\1\') OR ', implode(',', $module_ids).',');
- $module_sql = substr($module_sql, 0, -4);
-
- $ret .= "\t\t".''."\n";
-
- $event_sql = ' SELECT *
- FROM ' . $events_table . '
- WHERE ' . $module_sql . '
- ORDER BY `Event`, `Type`';
- $rows = $this->Conn->Query($event_sql);
-
- foreach ($rows as $row) {
+ foreach ($events as $event_id => $event) {
$fields_hash = Array (
- 'Headers' => $row['Headers'],
- 'Subject' => $row['l' . $language_id . '_Subject'],
- 'Body' => $row['l' . $language_id . '_Body'],
+ 'Headers' => $event['Headers'],
+ 'Subject' => $event['l' . $language_id . '_Subject'],
+ 'Body' => $event['l' . $language_id . '_Body'],
);
$template = $email_message_helper->buildTemplate($fields_hash);
- list($event_name, $event_type) = explode('_', $this->events_hash[ $row['EventId'] ] );
+ if (!$template) {
+ // email event is not translated on given language
+ continue;
+ }
+
$data = $this->_exportEncoding == 'base64' ? base64_encode($template) : '';
- $ret .= sprintf($event_tpl, $row['MessageType'], $event_name, $event_type, $data);
+ $ret .= sprintf($event_tpl, $event['MessageType'], $event['Event'], $event['Type'], $data);
}
- $ret .= "\t\t".''."\n";
-
- $ret .= "\t".''."\n";
+ $ret .= "\t\t" . '' . "\n";
+ $ret .= "\t" . '' . "\n";
}
$ret .= '';
@@ -295,59 +308,12 @@
* @param int $language_id
* @param string $prefix
* @param string $unique_field
- */
- function _performUpgrade($language_id, $prefix, $unique_field)
- {
- $live_records = $this->_getTableData($language_id, $prefix, $unique_field, false);
- $temp_records = $this->_getTableData($language_id, $prefix, $unique_field, true);
-
- if ($this->import_mode == LANG_OVERWRITE_EXISTING) {
- // remove existing records before copy
- $common_records = array_intersect($temp_records, $live_records);
- if ($common_records) {
- $live_records = array_diff($live_records, $common_records); // remove overlaping records
- $common_records = array_map(Array(&$this->Conn, 'qstr'), $common_records);
-
- $sql = 'DELETE FROM ' . $this->Application->getUnitOption($prefix, 'TableName') . '
- WHERE (LanguageId = ' . $language_id . ') AND (' . $unique_field . ' IN (' . implode(',', $common_records) . '))';
- $this->Conn->Query($sql);
- }
- }
-
- $temp_records = array_diff($temp_records, $live_records);
-
- if (!$temp_records) {
- // no new records found in temp table while comparing it to live table
- return ;
- }
-
- $temp_records = array_map(Array(&$this->Conn, 'qstr'), $temp_records);
-
- $sql = 'INSERT INTO ' . $this->Application->getUnitOption($prefix, 'TableName') . '
- SELECT *
- FROM ' . $this->_tables[$prefix] . '
- WHERE (LanguageId = ' . $language_id . ')';
-
- if ($live_records) {
- // subsctract live records from temp table during coping
- $sql .= ' AND (' . $unique_field . ' IN (' . implode(',', $temp_records) . '))';
- }
-
- $this->Conn->Query($sql);
- }
-
- /**
- * Performs upgrade of given language pack part
- *
- * @param int $language_id
- * @param string $prefix
- * @param string $unique_field
* @param string $data_fields
*/
- function _performUpgrade_v2($language_id, $prefix, $unique_field, $data_fields)
+ function _performUpgrade($language_id, $prefix, $unique_field, $data_fields)
{
- $live_records = $this->_getTableData_v2($language_id, $prefix, $unique_field, $data_fields[0], false);
- $temp_records = $this->_getTableData_v2($language_id, $prefix, $unique_field, $data_fields[0], true);
+ $live_records = $this->_getTableData($language_id, $prefix, $unique_field, $data_fields[0], false);
+ $temp_records = $this->_getTableData($language_id, $prefix, $unique_field, $data_fields[0], true);
if (!$temp_records) {
// no data for given language
@@ -416,31 +382,8 @@
* @param bool $temp_mode
* @return Array
*/
- function _getTableData($language_id, $prefix, $unique_field, $temp_mode = false)
+ function _getTableData($language_id, $prefix, $unique_field, $data_field, $temp_mode = false)
{
- $table_name = $this->Application->getUnitOption($prefix, 'TableName');
-
- if ($temp_mode) {
- $table_name = $this->Application->GetTempName($table_name, 'prefix:' . $prefix);
- }
-
- $sql = 'SELECT ' . $unique_field . '
- FROM ' . $table_name . '
- WHERE LanguageId = ' . $language_id;
- return $this->Conn->GetCol($sql);
- }
-
- /**
- * Returns data from given table used for language pack upgrade
- *
- * @param int $language_id
- * @param string $prefix
- * @param string $unique_field
- * @param bool $temp_mode
- * @return Array
- */
- function _getTableData_v2($language_id, $prefix, $unique_field, $data_field, $temp_mode = false)
- {
$data_field = sprintf($data_field, $language_id);
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
@@ -514,11 +457,15 @@
$this->Conn->Query( sprintf($sql, $temp_table) );
if (!$drop_only) {
- $sql = 'CREATE TABLE %s SELECT * FROM %s WHERE 0';
- $this->Conn->Query( sprintf($sql, $temp_table, $table) );
+ $sql = 'CREATE TABLE ' . $temp_table . ' SELECT * FROM ' . $table . ' WHERE 0';
+ $this->Conn->Query($sql);
$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';
+ $sql = 'ALTER TABLE ' . $temp_table . ' ADD UNIQUE (' . $unique_field . ')';
+ $this->Conn->Query($sql);
}
return $temp_table;
@@ -669,28 +616,50 @@
*/
function _processPhrases(&$phrase_node, $language_id, $language_encoding)
{
+ static $other_translations = Array ();
+
+ if ($this->Application->isDebugMode()) {
+ $this->Application->Debugger->profileStart('L[' . $language_id . ']P', 'Language: ' . $language_id . '; Phrases Import');
+ }
+
do {
+ $phrase_key = mb_strtoupper($phrase_node->Attributes['LABEL']);
+
$fields_hash = Array (
- 'LanguageId' => $language_id,
'Phrase' => $phrase_node->Attributes['LABEL'],
- 'PhraseKey' => mb_strtoupper($phrase_node->Attributes['LABEL']),
+ 'PhraseKey' => $phrase_key,
'PhraseType' => $phrase_node->Attributes['TYPE'],
'Module' => array_key_exists('MODULE', $phrase_node->Attributes) ? $phrase_node->Attributes['MODULE'] : 'Core',
'LastChanged' => adodb_mktime(),
'LastChangeIP' => $this->ip_address,
- 'Translation' => $phrase_node->Data,
);
+ $translation = $phrase_node->Data;
+
if (array_key_exists($fields_hash['PhraseType'], $this->phrase_types_allowed)) {
if ($language_encoding != 'plain') {
- $fields_hash['Translation'] = base64_decode($fields_hash['Translation']);
+ $translation = base64_decode($translation);
}
- $this->Conn->doInsert($fields_hash, $this->_tables['phrases'], 'INSERT', false);
+ if (array_key_exists($phrase_key, $other_translations)) {
+ $other_translations[$phrase_key]['l' . $language_id . '_Translation'] = $translation;
+ }
+ else {
+ $other_translations[$phrase_key] = Array (
+ 'l' . $language_id . '_Translation' => $translation
+ );
+ }
+
+ $fields_hash = array_merge($fields_hash, $other_translations[$phrase_key]);
+ $this->Conn->doInsert($fields_hash, $this->_tables['phrases'], 'REPLACE', false);
}
} while (($phrase_node =& $phrase_node->NextSibling()));
- $this->Conn->doInsert($fields_hash, $this->_tables['phrases'], 'INSERT');
+ if ($this->Application->isDebugMode()) {
+ $this->Application->Debugger->profileFinish('L[' . $language_id . ']P', 'Language: ' . $language_id . '; Phrases Import');
+ }
+
+ $this->Conn->doInsert($fields_hash, $this->_tables['phrases'], 'REPLACE');
}
/**
@@ -702,8 +671,12 @@
*/
function _processEvents(&$event_node, $language_id, $language_encoding)
{
- static $events_added = Array ();
+ static $other_translations = Array ();
+ if ($this->Application->isDebugMode()) {
+ $this->Application->Debugger->profileStart('L[' . $language_id . ']E', 'Language: ' . $language_id . '; Events Import');
+ }
+
$email_message_helper =& $this->Application->recallObject('EmailMessageHelper');
/* @var $email_message_helper EmailMessageHelper */
@@ -724,23 +697,36 @@
'Event' => $event_node->Attributes['EVENT'],
'Type' => $event_node->Attributes['TYPE'],
'MessageType' => $event_node->Attributes['MESSAGETYPE'],
- 'l' . $language_id . '_Subject' => $parsed['Subject'],
- 'l' . $language_id . '_Body' => $parsed['Body'],
);
- if ($parsed['Headers']) {
- $fields_hash['Headers'] = $parsed['Headers'];
+ if (array_key_exists($event_id, $other_translations)) {
+ $other_translations[$event_id]['l' . $language_id . '_Subject'] = $parsed['Subject'];
+ $other_translations[$event_id]['l' . $language_id . '_Body'] = $parsed['Body'];
}
+ else {
+ $other_translations[$event_id] = Array (
+ 'l' . $language_id . '_Subject' => $parsed['Subject'],
+ 'l' . $language_id . '_Body' => $parsed['Body'],
+ );
+ }
- if (!in_array($event_id, $events_added)) {
- $events_added[] = $event_id;
- $this->Conn->doInsert($fields_hash, $this->_tables['emailevents']);
+ if ($parsed['Headers']) {
+ $other_translations[$event_id]['Headers'] = $parsed['Headers'];
}
- else {
- $this->Conn->doUpdate($fields_hash, $this->_tables['emailevents'], 'EventId = ' . $event_id);
+ elseif (!$parsed['Headers'] && !array_key_exists('Headers', $other_translations[$event_id])) {
+ $other_translations[$event_id]['Headers'] = $parsed['Headers'];
}
+
+ $fields_hash = array_merge($fields_hash, $other_translations[$event_id]);
+ $this->Conn->doInsert($fields_hash, $this->_tables['emailevents'], 'REPLACE', false);
}
} while (($event_node =& $event_node->NextSibling()));
+
+ if ($this->Application->isDebugMode()) {
+ $this->Application->Debugger->profileFinish('L[' . $language_id . ']E', 'Language: ' . $language_id . '; Events Import');
+ }
+
+ $this->Conn->doInsert($fields_hash, $this->_tables['emailevents'], 'REPLACE');
}
/**