Он не универсальный чисто для себя писался.
<?php
class Translit {
var $cyr=array(
"Щ", "Ш", "Ч", "Ц","Ю", "Я", "Ж", "А","Б","В","Г","Д","Е","Ё","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х", "Ь","Ы","Ъ","Э","Є","Ї",
"щ", "ш", "ч", "ц","ю", "я", "ж", "а","б","в","г","д","е","ё","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х", "ь","ы","ъ","э","є","ї","."," ","№");
var $lat=array(
"Shh","Sh","Ch","C","Ju","Ja","Zh","A","B","V","G","D","Je","Jo","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","Kh","_","Y","_","E","Je","Ji",
"shh","sh","ch","c","ju","ja","zh","a","b","v","g","d","je","jo","z","i","j","k","l","m","n","o","p","r","s","t","u","f","kh","_","y","_","e","je","ji","_","_","N"
);
function Transliterate($str, $encIn, $encOut){
//$str = iconv($encIn, "utf-8", $str);
for($i=0; $i<count($this->cyr); $i++){
$c_cyr = $this->cyr[$i];
$c_lat = $this->lat[$i];
$str = str_replace($c_cyr, $c_lat, $str);
}
$str = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM]+)[jJ]e/", "\${1}e", $str);
$str = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM]+)[jJ]/", "\${1}", $str);
$str = preg_replace("/([eyuioaEYUIOA]+)[Kk]h/", "\${1}h", $str);
$str = preg_replace("/^kh/", "h", $str);
$str = preg_replace("/^Kh/", "H", $str);
//$str = iconv("utf-8", $encOut, $str);
return $str;
}
}
class import{
var $table='modx'; //
var $file; //
var $debug = 1; //
var $conn; //
var $orders;
var $t_tovar=7; //tovar templeate
var $t_catalog=8; //catalog templeat
var $published=1;
var $delete=0;
var $hidemenu=0;
var $isfolder=1;
var $count=0;
var $Translit;
var $test = 1;
var $table_prefix = 'aqs';
function import(){
$this->db_connect();
$this->date = time();
$this->t_tovar = 7; //tovar templeate
$this->t_catalog = 8; //catalog templeat
$this->Translit = new Translit();
//$this->delete("all");
}
function set_table($table,$file,$type){
}
function set_file($file,$type){
}
function set_type(){
}
function drop(){
}
function db_connect()
{
$hosh="localhost";
$login="root";
$pass="";
$name_baze="aqua_shop";
$this->conn = @mysql_pconnect("$hosh", "$login", "$pass");
if (!$this->conn )
$this->debug('connection failed','ERROR');
if (!@mysql_select_db("$name_baze"))
$this->debug('can not find DB','ERROR');
}
function select($type){
$t_start = date("Y-m-d H:i:s", time());
//$type=='tovar';
$sql ='';
//$this->delete();
// for properties
if($type=='svoistva'){
print "Загрузка характеристик - начали: $t_start<br>";
$w = "tovar/svoistva.txt";
$orders= file("$w");
$number_of_orders = count($orders);
print "кол-во эл-тов - $number_of_orders<br>";
if ($orders){
//print "Удаляем <br>";
//$this->delete();
}
for ($i=0; $i<$number_of_orders; $i++){
$line = explode( "|", $orders[$i]);
if ($line[0]==6){
$type = 'svoistva';
$sql = $this->query($type,$line);
$this->exec($sql);
}
if ($line[0]==6){
$type = 'templates';
$sql = $this->query($type,$line);
$this->exec($sql);
}
if ($line[0]==8){
$type = 'svoistva_val';
$sql = $this->query($type,$line);
$this->exec($sql);
}
}
}
if($type=='tovar'){
print "Загрузка - закончена: $t_start<br>";
$w = "tovar/1sbtrans.txt";
$orders= file("$w");
$number_of_orders = count($orders);
print "кол-во эл-тов - $number_of_orders<br>";
if ($orders){
//print "Удаляем <br>";
//$this->delete();
}
for ($i=0; $i<$number_of_orders; $i++){
$line = explode( "|", $orders[$i]);
//print $i."<br>";
//print $line[0]."<br>";
if(strlen($line[0])>1) $line[0] = 9;
if ($line[0]<=5 and $line[0]>0){
$type = 'cat';
$sql = $this->query($type,$line);
//print "sql ".$sql;
$this->exec($sql);
}
if ($line[0]==0){
$type = 'tovar';
$sql = $this->query($type,$line);
//print "sql ".$sql;
//print $i."<br>";
$this->exec($sql);
$type = 'tovar_detail';
$sql = $this->query($type,$line);
}
}
}
if($type=='opis'){
print "Загрузка описаний - начали: $t_start<br>";
$w = "tovar/opis.txt";
$orders= file("$w");
$number_of_orders = count($orders);
print "кол-во эл-тов - $number_of_orders<br>";
if ($orders){
//print "Удаляем <br>";
//$this->delete();
}
for ($i=0; $i<$number_of_orders; $i++){
$line = explode( "|", $orders[$i]);
//print $i."<br>";
//print $line[2]."<br>";
if ($line[0]==6){
$type = 'opis';
$sql = $this->query($type,$line);
//print "sql ".$sql;
$this->exec($sql);
}
}
}
$t_end = date("Y-m-d H:i:s", time());
print "<br> Загрузка - законена: $t_end итого".($t_end-$t_start)."<br>";
}
function query($type='',$line){
// tv type
$query = "";
// заносим список характеристик
if($type == 'svoistva'){
$caption = $this->conv(eregi_replace('<разделитель строк>','<br />', $line[2]));
$name = $this->conv($this->Translit->Transliterate( $caption ,'windows-1251','windows-1251'));
//$caption
$tvid = intval(eregi_replace('_','', $line[1]));
//print $tvid." - ".$caption." - ".$name."<br>";
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvars` ( `id` , `name`, `type`,`caption` )VALUES ('$tvid', '$name', 'text','$caption');";
}
//tv 2 template
if($type == 'templates'){
//$name = eregi_replace('<разделитель строк>','<br />', $line[2]);
$tvid = intval(eregi_replace('_','', $line[1]));
$templateid = 7;
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_templates` (`tmplvarid`, `templateid`, `rank`) VALUES ('$tvid', '$templateid', '0');";
//print $query." <br> ";
}
// template
//tv value
// заносим характеристики товаров
if($type == 'svoistva_val'){
$content_id = $this->prepare_t($line[1]);
if($content_id<=1000){return "";} //hack
$tvid = intval(eregi_replace('_','', $line[2])); //
$content = $this->conv(ereg_replace("'","’" , $line[3]));
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
}
// заносим товары
if($type == 'template'){
//$name = eregi_replace('<разделитель строк>','<br />', $line[2]);
$tvid = intval(eregi_replace('_','', $line[1]));
$templateid = 7;
$query = "INSERT INTO `".$this->table_prefix."_site_templates` (`id`, `content`, `name`) VALUES ('$tvid', '$templateid', '0');";
//print $query." <br> ";
}
if($type == 'cat'){
//$content_id = $this->prepare(; // to get categary id
$pagetitle = $this->conv($line[6]);
$longtitle = $this->conv($line[1]);
$alias = "";
$description = '';
$introtext = '';
$published = $this->published;
$delete = $this->delete;
$hidemenu = $this->hidemenu;
$parent = 2;
$content = '';
$template = $this->t_catalog;
$isfolder = 1;
$description = $line[1];
$menutitle = $line[6];
$content_id = 0;
$content='';
//print $line[1]."<br />";
$description = $this->prepare_s($line[15],$line[16]); // определения сезана
if($line[0]<=5 and $line[0]>1) $parent = $this->prepare($line[2]);
$content_id = $this->prepare($line[1]);
//////
if (($line[7]<>0) or ($line[8]<>0) or ($line[9]<>0) or ($line[10]<>0) or ($line[11]<>0) or ($line[12]<>0) or ($line[13]<>0) or ($line[14]<>0))
{
$tvids = array(7,8,9,10,11,12,13,14);
foreach( $tvids as $tvid){
$content = $this->conv($line[$tvid]);
$sub_query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id ', '$content');\n";
//print $sub_query."<br>";
}
}
//////
//print "<li>".$content_id. " ".$parent."</li>";
$query = "INSERT INTO ".$this->table_prefix."_site_content (id, pagetitle,longtitle,description,introtext,template,published, deleted,hidemenu,parent,content,createdon, editedon, publishedon,isfolder, menutitle) VALUES ('".$content_id."', '".$pagetitle."','".$longtitle."','".$description."','".$introtext."','".$template."','".$published."','".$delete."','".$hidemenu."','".$parent."','".$content."', '".$this->date."','".$this->date."','".$this->date."','".$isfolder."','".$menutitle."')";
//$this->debug($query,'');
}
/**/
if($type == 'tovar'){
//print $line[1] . $line[2] .$line[3] . $line[4] .$line[5] . $line[6] .$line[7] . $line[8] .$line[9] . $line[10] ."<br />";
//$content_id = intval(eregi_replace('gr','99', $line[1]));
$pagetitle = ereg_replace("'","’" , $line[8]);
$pagetitle = $this->conv(ereg_replace("\"","”" , $pagetitle));
$longtitle = $this->conv($line[1]);
$introtext = $line[7]; // артикуль
$description = $this->prepare_s($line[14],$line[15]); // определения сезана
$delete = $this->delete;
$published = $this->published;
$hidemenu = 1;
$isfolder = 0;
//$parent = 1;
$content = 'content';
$template = $this->t_tovar;
$content_id = 0;
$content='';
$content_id = $this->prepare_t($line[1]);
if($content_id<=1000){return "";} //hack
$parent = $this->prepare($line[2]);
$query = "INSERT INTO
".$this->table_prefix."_site_content (id, pagetitle, longtitle, description, introtext, template, published, deleted, hidemenu, parent, content, createdon, editedon, publishedon, isfolder)
VALUES ('".$content_id."', '".$pagetitle."','".$longtitle."','".$description."','".$introtext."','".$template."','".$published."','".$delete."','".$hidemenu."','".$parent."','".$content."', '".$this->date."','".$this->date."','".$this->date."','".$isfolder."')";
//$this->debug($query,'');
}
//content заносим описания
if($type == 'opis'){
$content = eregi_replace('<разделитель строк>','<br>', $line[2]); //
$content = ereg_replace("'","’" , $content);
$content = ereg_replace("\"","”" , $content);
$content = $this->conv($content);
//$tvid=intval(eregi_replace('_','', $line[1]));
$content_id = $this->prepare_t($line[1]);
if($content_id<=1000){return "";} //hack
//$query = "INSERT INTO `".$this->table_prefix."_site_content` ( `id`, `content`)VALUES ('$id', '$content');";
$query = "UPDATE `".$this->table_prefix."_site_content` SET `content` = '".$content."' WHERE `id` = '".$content_id."';";
}
if($type == 'tovar_detail'){
$content_id = $this->prepare_t($line[1]);
if($content_id>=1000){ //hack
$content = $line[10];
$content = $this->conv($content);
$tvid = 1; // price 1
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
$this->exec($query);
$content = $line[11];
$content = $this->conv($content);
$tvid = 2; // price 2
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
$this->exec($query);
$content = $line[12];
$content = $this->conv($content);
$tvid = 3; // price 3
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
$this->exec($query);
$content = $line[13];
$content = $this->conv($content);
$tvid = 4; // price 4
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
$this->exec($query);
$content = $line[9];
$content = $this->conv($content);
$tvid = 5; // kol-vo
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
$this->exec($query);
$content = $line[7];
$content = $this->conv($content);
$tvid = 6; //apt
$query = "INSERT INTO `".$this->table_prefix."_site_tmplvar_contentvalues` ( `tmplvarid`, `contentid` , `value` )VALUES ('$tvid', '$content_id', '$content');\n";
$this->exec($query);
}
}
return $query;
}
//cat id converter
function prepare($var){
$var = intval(eregi_replace('gr','', $var))+100;
return $var;
}
//prepare сезон
function prepare_s($var1,$var2){
if($var1==1 and $var2==0){
$var = 1;
}
elseif($var1==0 and $var2==1){
$var = 2;
}
else{
$var = 0;
}
return $var;
}
//prepare tovar
function prepare_t($var){
if(is_numeric($var)){
$var = intval($var);
}
else $var=0;
return $var;
}
//Удаляем всё
function exec($sql){
if (strlen($sql)>10){
if (!$result = @mysql_query($sql, $this->conn)){
$this->debug($sql,'Execution of a query to the database failed','exec error');
}
else{
//$this->debug($sql,'ok');
}
}
}
function delete($type){
//$sql = 'delete from ".$this->table_prefix."_site_tmplvars where id > 1000; delete from ".$this->table_prefix."_site_tmplvar_contentvalues where id >1000;'
if($type = "all"){
$query = " delete from ".$this->table_prefix."_site_tmplvars where id > 100;";
$this->exec($query);
$query = " delete from ".$this->table_prefix."_site_tmplvar_contentvalues where id > 100;";
$this->exec($query);
$query = "delete from ".$this->table_prefix."_site_content where id > 100;";
$this->exec($query);
$query = "delete from ".$this->table_prefix."_site_tmplvar_templates where tmplvarid > 10;";
$this->exec($query);
print "<br /> Удаление завершено<br />";
}
//$result = @mysql_query($query);
//$this->exec($query);
}
function conv($str){
//return iconv("WINDOWS-1251","UTF-8",$str);
return $str;
}
function debug($str, $head){
if($this->debug == 1){
if($head) print "<h4>".$head."</h4>";
print "<pre>".$str."</pre>";
}
}
};
?>