Index: trunk/core/units/languages/import_xml.php =================================================================== diff -u -N -r1566 -r1623 --- trunk/core/units/languages/import_xml.php (.../import_xml.php) (revision 1566) +++ trunk/core/units/languages/import_xml.php (.../import_xml.php) (revision 1623) @@ -1,280 +1,280 @@ -Conn =& $this->Application->GetADODBConnection(); - - $this->Application->SetVar('lang_mode', 't'); - - $this->tables['lang'] = $this->prepareTempTable('lang'); - $this->Application->setUnitOption('lang','AutoLoad',false); - $this->lang_object =& $this->Application->recallObject('lang.imp'); - - $this->tables['phrases'] = $this->prepareTempTable('phrases'); - $this->tables['emailmessages'] = $this->prepareTempTable('emailmessages'); - - $sql = 'SELECT EventId, CONCAT(Event,"_",Type) AS EventMix FROM '.TABLE_PREFIX.'Events'; - $this->events_hash = $this->Conn->GetCol($sql, 'EventMix'); - - $this->ip_address = getenv('HTTP_X_FORWARDED_FOR') ? getenv('HTTP_X_FORWARDED_FOR') : getenv('REMOTE_ADDR'); - } - - /** - * Create temp table for prefix, if table already exists, then delete it and create again - * - * @param string $prefix - */ - function prepareTempTable($prefix) - { - $idfield = $this->Application->getUnitOption($prefix, 'IDField'); - $table = $this->Application->getUnitOption($prefix,'TableName'); - $temp_table = kTempTablesHandler::GetTempName($table); - - $sql = 'DROP TABLE IF EXISTS %s'; - $this->Conn->Query( sprintf($sql, $temp_table) ); - - $sql = 'CREATE TABLE %s SELECT * FROM %s WHERE 0'; - $this->Conn->Query( sprintf($sql, $temp_table, $table) ); - - $sql = 'ALTER TABLE %1$s CHANGE %2$s %2$s INT(11) NOT NULL'; - $this->Conn->Query( sprintf($sql, $temp_table, $idfield) ); - - return $temp_table; - } - - function Parse($filename, $phrase_types, $module_ids) - { - // define the XML parsing routines/functions to call based on the handler path - if( !file_exists($filename) ) return false; - - $this->phrase_types_allowed = array_flip($phrase_types); - - //if (in_array('In-Portal',) - - $xml_parser = xml_parser_create(); - xml_set_element_handler( $xml_parser, Array(&$this, 'startElement'), Array(&$this, 'endElement') ); - xml_set_character_data_handler( $xml_parser, Array(&$this, 'characterData') ); - - $fdata = file_get_contents($filename); - - $ret = xml_parse($xml_parser, $fdata); - xml_parser_free($xml_parser); // clean up the parser object - - $this->Application->SetVar('lang_mode', ''); - return $ret; - } - - function startElement(&$parser, $element, $attributes) - { - array_push($this->path, $element); - $path = implode(' ',$this->path); - //check what path we are in - - switch($path) - { - case 'LANGUAGES LANGUAGE': - $this->current_language = Array('PackName' => $attributes['PACKNAME'], 'LocalName' => $attributes['PACKNAME']); - break; - - case 'LANGUAGES LANGUAGE PHRASES': - case 'LANGUAGES LANGUAGE EVENTS': - if( !getArrayValue($this->current_language,'LanguageId') ) - { - if( !getArrayValue($this->current_language,'Charset') ) $this->current_language['Charset'] = 'iso-8859-1'; - $this->lang_object->SetFieldsFromHash($this->current_language); - if( $this->lang_object->Create() ) $this->current_language['LanguageId'] = $this->lang_object->GetID(); - } - break; - - case 'LANGUAGES LANGUAGE PHRASES PHRASE': - $phrase_module = getArrayValue($attributes,'MODULE'); - if(!$phrase_module) $phrase_module = 'In-Portal'; - - $this->current_phrase = Array( 'LanguageId' => $this->current_language['LanguageId'], - 'Phrase' => $attributes['LABEL'], - 'PhraseType' => $attributes['TYPE'], - 'Module' => $phrase_module, - 'LastChanged' => time(), - 'LastChangeIP' => $this->ip_address ); - break; - - case 'LANGUAGES LANGUAGE EVENTS EVENT': - $this->current_event = Array( 'LanguageId' => $this->current_language['LanguageId'], - 'EventId' => $this->events_hash[ $attributes['EVENT'].'_'.$attributes['TYPE'] ], - 'MessageType' => $attributes['MESSAGETYPE']); - break; - - - } - - // if($path == 'SHIPMENT PACKAGE') - } - - function characterData(&$parser, $line) - { - $line = trim($line); - if(!$line) return ; - - $path = join (' ',$this->path); - - $language_nodes = Array('DATEFORMAT','TIMEFORMAT','DECIMAL','THOUSANDS','CHARSET'); - - - $node_field_map = Array('LANGUAGES LANGUAGE DATEFORMAT' => 'DateFormat', - 'LANGUAGES LANGUAGE TIMEFORMAT' => 'TimeFormat', - 'LANGUAGES LANGUAGE DECIMAL' => 'DecimalPoint', - 'LANGUAGES LANGUAGE THOUSANDS' => 'ThousandSep', - 'LANGUAGES LANGUAGE CHARSET' => 'Charset'); - - if( in_array( end($this->path), $language_nodes) ) - { - $this->current_language[ $node_field_map[$path] ] = $line; - } - else - { - switch($path) - { - case 'LANGUAGES LANGUAGE PHRASES PHRASE': - if( isset($this->phrase_types_allowed[ $this->current_phrase['PhraseType'] ]) ) - { - $this->current_phrase['Translation'] = base64_decode($line); - $this->insertRecord($this->tables['phrases'], $this->current_phrase); - } - break; - - case 'LANGUAGES LANGUAGE EVENTS EVENT': - $this->current_event['Template'] = base64_decode($line); - $this->insertRecord($this->tables['emailmessages'],$this->current_event); - break; - } - } - } - - function endElement(&$parser, $element) - { - $path = implode(' ',$this->path); - array_pop($this->path); - } - - function insertRecord($table, $fields_hash) - { - $fields = ''; - $values = ''; - - foreach($fields_hash as $field_name => $field_value) - { - $fields .= '`'.$field_name.'`,'; - $values .= $this->Conn->qstr($field_value).','; - } - - $fields = preg_replace('/(.*),$/', '\\1', $fields); - $values = preg_replace('/(.*),$/', '\\1', $values); - - $sql = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES ('.$values.')'; - $this->Conn->Query($sql); - -// return $this->Conn->getInsertID(); // no need because of temp table without auto_increment column at all - } - - /** - * Creates XML file with exported language data - * - * @param string $filename filename to export into - * @param Array $phrase_types phrases types to export from modules passed in $module_ids - * @param Array $language_ids IDs of languages to export - * @param Array $module_ids IDs of modules to export phrases from - */ - function Create($filename, $phrase_types, $language_ids, $module_ids) - { - $fp = fopen($filename,'w'); - if(!$fp || !$phrase_types || !$module_ids || !$language_ids) return false; - - $this->events_hash = array_flip($this->events_hash); - - $lang_table = $this->Application->getUnitOption('lang','TableName'); - $phrases_table = $this->Application->getUnitOption('phrases','TableName'); - $emailevents_table = $this->Application->getUnitOption('emailmessages','TableName'); - $mainevents_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"; - foreach($language_ids as $language_id) - { - // languages - $row = $this->Conn->GetRow( sprintf($sql, $lang_table, $language_id) ); - $ret .= "\t".''.$row['DateFormat'].''; - $ret .= ''.$row['TimeFormat'].''.$row['DecimalPoint'].''; - $ret .= ''.$row['ThousandSep'].''.$row['Charset'].''."\n"; - - // phrases - $ret .= "\t\t".''."\n"; - $phrases_sql = 'SELECT * FROM '.$phrases_table.' WHERE LanguageId = %s AND PhraseType IN (%s) AND Module IN (%s)'; - 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).'\'' ) ); - foreach($rows as $row) - { - $ret .= sprintf($phrase_tpl, $row['Phrase'], $row['PhraseType'], base64_encode($row['Translation']) ); - } - $ret .= "\t\t".''."\n"; - - // email events - $ret .= "\t\t".''."\n"; - if( in_array('In-Portal',$module_ids) ) unset( $module_ids[array_search('',$module_ids)] ); // for old language packs - $module_sql = preg_replace('/(.*) OR $/', '\\1', preg_replace('/(.*),/U', 'INSTR(Module,\'\\1\') OR ', implode(',', $module_ids).',' ) ); - - $sql = 'SELECT EventId FROM '.$mainevents_table.' WHERE '.$module_sql; - $event_ids = $this->Conn->GetCol($sql); - - $event_sql = 'SELECT * FROM '.$emailevents_table.' WHERE LanguageId = %s AND EventId IN (%s)'; - $rows = $this->Conn->Query( sprintf($event_sql,$language_id, $event_ids ? implode(',',$event_ids) : '' ) ); - foreach($rows as $row) - { - list($event_name, $event_type) = explode('_', $this->events_hash[ $row['EventId'] ] ); - $ret .= sprintf($event_tpl, $row['MessageType'], $event_name, $event_type, base64_encode($row['Template']) ); - } - $ret .= "\t\t".''."\n"; - $ret .= "\t".''."\n"; - } - - $ret .= ''; - fwrite($fp, $ret); - fclose($fp); - return true; - } - } - +Conn =& $this->Application->GetADODBConnection(); + + $this->Application->SetVar('lang_mode', 't'); + + $this->tables['lang'] = $this->prepareTempTable('lang'); + $this->Application->setUnitOption('lang','AutoLoad',false); + $this->lang_object =& $this->Application->recallObject('lang.imp'); + + $this->tables['phrases'] = $this->prepareTempTable('phrases'); + $this->tables['emailmessages'] = $this->prepareTempTable('emailmessages'); + + $sql = 'SELECT EventId, CONCAT(Event,"_",Type) AS EventMix FROM '.TABLE_PREFIX.'Events'; + $this->events_hash = $this->Conn->GetCol($sql, 'EventMix'); + + $this->ip_address = getenv('HTTP_X_FORWARDED_FOR') ? getenv('HTTP_X_FORWARDED_FOR') : getenv('REMOTE_ADDR'); + } + + /** + * Create temp table for prefix, if table already exists, then delete it and create again + * + * @param string $prefix + */ + function prepareTempTable($prefix) + { + $idfield = $this->Application->getUnitOption($prefix, 'IDField'); + $table = $this->Application->getUnitOption($prefix,'TableName'); + $temp_table = kTempTablesHandler::GetTempName($table); + + $sql = 'DROP TABLE IF EXISTS %s'; + $this->Conn->Query( sprintf($sql, $temp_table) ); + + $sql = 'CREATE TABLE %s SELECT * FROM %s WHERE 0'; + $this->Conn->Query( sprintf($sql, $temp_table, $table) ); + + $sql = 'ALTER TABLE %1$s CHANGE %2$s %2$s INT(11) NOT NULL'; + $this->Conn->Query( sprintf($sql, $temp_table, $idfield) ); + + return $temp_table; + } + + function Parse($filename, $phrase_types, $module_ids) + { + // define the XML parsing routines/functions to call based on the handler path + if( !file_exists($filename) ) return false; + + $this->phrase_types_allowed = array_flip($phrase_types); + + //if (in_array('In-Portal',) + + $xml_parser = xml_parser_create(); + xml_set_element_handler( $xml_parser, Array(&$this, 'startElement'), Array(&$this, 'endElement') ); + xml_set_character_data_handler( $xml_parser, Array(&$this, 'characterData') ); + + $fdata = file_get_contents($filename); + + $ret = xml_parse($xml_parser, $fdata); + xml_parser_free($xml_parser); // clean up the parser object + + $this->Application->SetVar('lang_mode', ''); + return $ret; + } + + function startElement(&$parser, $element, $attributes) + { + array_push($this->path, $element); + $path = implode(' ',$this->path); + //check what path we are in + + switch($path) + { + case 'LANGUAGES LANGUAGE': + $this->current_language = Array('PackName' => $attributes['PACKNAME'], 'LocalName' => $attributes['PACKNAME']); + break; + + case 'LANGUAGES LANGUAGE PHRASES': + case 'LANGUAGES LANGUAGE EVENTS': + if( !getArrayValue($this->current_language,'LanguageId') ) + { + if( !getArrayValue($this->current_language,'Charset') ) $this->current_language['Charset'] = 'iso-8859-1'; + $this->lang_object->SetFieldsFromHash($this->current_language); + if( $this->lang_object->Create() ) $this->current_language['LanguageId'] = $this->lang_object->GetID(); + } + break; + + case 'LANGUAGES LANGUAGE PHRASES PHRASE': + $phrase_module = getArrayValue($attributes,'MODULE'); + if(!$phrase_module) $phrase_module = 'In-Portal'; + + $this->current_phrase = Array( 'LanguageId' => $this->current_language['LanguageId'], + 'Phrase' => $attributes['LABEL'], + 'PhraseType' => $attributes['TYPE'], + 'Module' => $phrase_module, + 'LastChanged' => time(), + 'LastChangeIP' => $this->ip_address ); + break; + + case 'LANGUAGES LANGUAGE EVENTS EVENT': + $this->current_event = Array( 'LanguageId' => $this->current_language['LanguageId'], + 'EventId' => $this->events_hash[ $attributes['EVENT'].'_'.$attributes['TYPE'] ], + 'MessageType' => $attributes['MESSAGETYPE']); + break; + + + } + + // if($path == 'SHIPMENT PACKAGE') + } + + function characterData(&$parser, $line) + { + $line = trim($line); + if(!$line) return ; + + $path = join (' ',$this->path); + + $language_nodes = Array('DATEFORMAT','TIMEFORMAT','DECIMAL','THOUSANDS','CHARSET'); + + + $node_field_map = Array('LANGUAGES LANGUAGE DATEFORMAT' => 'DateFormat', + 'LANGUAGES LANGUAGE TIMEFORMAT' => 'TimeFormat', + 'LANGUAGES LANGUAGE DECIMAL' => 'DecimalPoint', + 'LANGUAGES LANGUAGE THOUSANDS' => 'ThousandSep', + 'LANGUAGES LANGUAGE CHARSET' => 'Charset'); + + if( in_array( end($this->path), $language_nodes) ) + { + $this->current_language[ $node_field_map[$path] ] = $line; + } + else + { + switch($path) + { + case 'LANGUAGES LANGUAGE PHRASES PHRASE': + if( isset($this->phrase_types_allowed[ $this->current_phrase['PhraseType'] ]) ) + { + $this->current_phrase['Translation'] = base64_decode($line); + $this->insertRecord($this->tables['phrases'], $this->current_phrase); + } + break; + + case 'LANGUAGES LANGUAGE EVENTS EVENT': + $this->current_event['Template'] = base64_decode($line); + $this->insertRecord($this->tables['emailmessages'],$this->current_event); + break; + } + } + } + + function endElement(&$parser, $element) + { + $path = implode(' ',$this->path); + array_pop($this->path); + } + + function insertRecord($table, $fields_hash) + { + $fields = ''; + $values = ''; + + foreach($fields_hash as $field_name => $field_value) + { + $fields .= '`'.$field_name.'`,'; + $values .= $this->Conn->qstr($field_value).','; + } + + $fields = preg_replace('/(.*),$/', '\\1', $fields); + $values = preg_replace('/(.*),$/', '\\1', $values); + + $sql = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES ('.$values.')'; + $this->Conn->Query($sql); + +// return $this->Conn->getInsertID(); // no need because of temp table without auto_increment column at all + } + + /** + * Creates XML file with exported language data + * + * @param string $filename filename to export into + * @param Array $phrase_types phrases types to export from modules passed in $module_ids + * @param Array $language_ids IDs of languages to export + * @param Array $module_ids IDs of modules to export phrases from + */ + function Create($filename, $phrase_types, $language_ids, $module_ids) + { + $fp = fopen($filename,'w'); + if(!$fp || !$phrase_types || !$module_ids || !$language_ids) return false; + + $this->events_hash = array_flip($this->events_hash); + + $lang_table = $this->Application->getUnitOption('lang','TableName'); + $phrases_table = $this->Application->getUnitOption('phrases','TableName'); + $emailevents_table = $this->Application->getUnitOption('emailmessages','TableName'); + $mainevents_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"; + foreach($language_ids as $language_id) + { + // languages + $row = $this->Conn->GetRow( sprintf($sql, $lang_table, $language_id) ); + $ret .= "\t".''.$row['DateFormat'].''; + $ret .= ''.$row['TimeFormat'].''.$row['DecimalPoint'].''; + $ret .= ''.$row['ThousandSep'].''.$row['Charset'].''."\n"; + + // phrases + $ret .= "\t\t".''."\n"; + $phrases_sql = 'SELECT * FROM '.$phrases_table.' WHERE LanguageId = %s AND PhraseType IN (%s) AND Module IN (%s)'; + 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).'\'' ) ); + foreach($rows as $row) + { + $ret .= sprintf($phrase_tpl, $row['Phrase'], $row['Module'], $row['PhraseType'], base64_encode($row['Translation']) ); + } + $ret .= "\t\t".''."\n"; + + // email events + $ret .= "\t\t".''."\n"; + if( in_array('In-Portal',$module_ids) ) unset( $module_ids[array_search('',$module_ids)] ); // for old language packs + $module_sql = preg_replace('/(.*) OR $/', '\\1', preg_replace('/(.*),/U', 'INSTR(Module,\'\\1\') OR ', implode(',', $module_ids).',' ) ); + + $sql = 'SELECT EventId FROM '.$mainevents_table.' WHERE '.$module_sql; + $event_ids = $this->Conn->GetCol($sql); + + $event_sql = 'SELECT * FROM '.$emailevents_table.' WHERE LanguageId = %s AND EventId IN (%s)'; + $rows = $this->Conn->Query( sprintf($event_sql,$language_id, $event_ids ? implode(',',$event_ids) : '' ) ); + foreach($rows as $row) + { + list($event_name, $event_type) = explode('_', $this->events_hash[ $row['EventId'] ] ); + $ret .= sprintf($event_tpl, $row['MessageType'], $event_name, $event_type, base64_encode($row['Template']) ); + } + $ret .= "\t\t".''."\n"; + $ret .= "\t".''."\n"; + } + + $ret .= ''; + fwrite($fp, $ret); + fclose($fp); + return true; + } + } + ?> \ No newline at end of file