<?php
function importAction () {
if ($_POST) {
$table = '`catalog_test`';
require_once ADMIN_PATH.'/plagin/excel2/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('UTF8');
$data->setUTFEncoder('mb');
$data->read($_FILES['file1']['tmp_name']);
error_reporting(E_ALL ^ E_NOTICE);
$lines = array();
$artikul = array();
$artikul2 = array();
foreach ( $data->sheets[0]['cells'] AS $key=>$value) {
if ($key == 1)
continue;
$lines[$key] = array(
'artikul' => $value[1],
'name' => $value[2],
'sklad' => $value[3],
'cost' => $value[4],
'id_parent' => $value[8],
'brand' => $value[11],
'text_short' => $value[12],
'artikb' => $value[13],
);
$artikul[] = "'".$value[13]."'";
$artikul2[$key] = $value[13];
}
$update = array();
$_sql="
SELECT
id, artikb
FROM
$table
WHERE
`artikb` IN (".implode(',', $artikul).")
";
$result = mysql_query($_sql);
if (mysql_num_rows($result) > 0) {
while($value = mysql_fetch_assoc($result) ) {
$key = array_search($value['artikb'], $artikul2);
$update[] = array (
'artikb'=> $value['artikb'],
'sklad' => $lines[$key]['sklad'],
'cost' => $lines[$key]['cost'],
);
unset( $lines[$key] );
}
}
$max = 1000;
if (count($update)){
for($i=0;$i <= count($update); $i += $max) {
$up1 = '';
$up2 = '';
$art_array = array();
for ($j=$i;$j < $i+$max; $j++) {
if (!isset($update[$j]))
continue;
$value = $this->mysql_real_escape_array($update[$j]);
$up1 .= "WHEN '".$value['artikb']."' THEN '".$value['sklad']."' ";
$up2 .= "WHEN '".$value['artikb']."' THEN '".$value['cost']."' ";
$art_array[] = " '".$value['artikb']."' ";
}
if (!empty($up1)) {
$up = "UPDATE $table SET ";
$up .= " sklad = CASE `artikb` ". $up1;
$up .= " END ,";
$up .= " cost = CASE `artikb` ". $up2;
$up .= " END ";
$up .= " WHERE `artikb` IN ( ".implode(',', $art_array)." )";
mysql_query($up) or die( '<br>Error UPDATE Mysql:'.mysql_error() );
echo '<br>Update='.count($art_array);
}
}
}
if (count($lines)){
$i = 0;
$lines = $this->mysql_real_escape_array($lines);
$_POST = $this->mysql_real_escape_array($_POST);
foreach ($lines AS $value) {
$i++;
$add[] = "(
'".$value['artikul']."',
'".$value['name']."',
'".$value['cost']."',
'".$value['id_parent']."',
'".$_POST['type_import2']."',
NOW(),
'".$value['sklad']."',
'".$_POST['type_import4']."',
'".$value['brand']."',
'".$value['text_short']."',
'".$value['artikb']."'
)";
if ($i/$max==intval($i/$max)) {
$this->add_mysql_count($table, $add);
$add = array();
}
}
if (count($add)){
$this->add_mysql_count($table, $add);
}
}
}
return system::show_tpl(
array(
'msg'=>$this->msg,
'select'=>dbh::get_catolog_tree(),
'tpl_folder'=>$this->tpl_folder
),$this->tpl_folder.'/import.php');
}
private function add_mysql_count($table, $add){
$sql = "
INSERT INTO
$table
(`artikul`, `name`, `cost`, `id_parent`, `status`, `created_at`, `sklad`, `supplier`, `brand`, `text_short`, `artikb`)
VALUES
". implode(',', $add) ."
";
mysql_query($sql) or die( '<br>Error ADD Mysql:'.mysql_error() );
echo '<br>Insert='.count($add);
}
?>