modifica script
Inviato: 26/12/2008, 16:51
salve, spero sia la sezione giusta...
allora ho un script che mi importa dei prodotti da uno csv, questo script è impostato in maniera che ogni volta che viene eseguito, importa tutti i prodotti, ma i prodotti che non trova nel csv li elimina dal catalogo online.
ora ho necessita di mporta piu di un csv in totale 4, quindi dovrei eliminare questa funzione, appunto che se nn trova il prodotto sul csv lo deve lascire cmq sul catalogo online...
questo è il codice dell script
allora ho un script che mi importa dei prodotti da uno csv, questo script è impostato in maniera che ogni volta che viene eseguito, importa tutti i prodotti, ma i prodotti che non trova nel csv li elimina dal catalogo online.
ora ho necessita di mporta piu di un csv in totale 4, quindi dovrei eliminare questa funzione, appunto che se nn trova il prodotto sul csv lo deve lascire cmq sul catalogo online...
questo è il codice dell script
Codice: Seleziona tutto
*/
ini_set ( 'memory_limit', '64M' );
require('includes/application_top.php');
tep_set_time_limit(0);
$action = (isset($_GET['action']) ? $_GET['action'] : '');
if ($action == 'upload') {
if ($upl_file = new upload('upl_file')) {
$upl_file->set_destination( DIR_FS_ADMIN.'temp/');
if ($upl_file->parse() && $upl_file->save()) {}
}
}
function tep_output_generated_category_path_excel($id, $from = 'category') {
$calculated_category_path_string = '';
$calculated_category_path = tep_generate_category_path($id, $from);
for ($i=0, $n=sizeof($calculated_category_path); $i<$n; $i++) {
for ($j=sizeof($calculated_category_path[$i])-1; $j>=0; ($j=$j-1)) {
$calculated_category_path_string .= $calculated_category_path[$i][$j]['text'] . ' => ';
}
}
$calculated_category_path_string = substr($calculated_category_path_string, 0, -4);
if (strlen($calculated_category_path_string) < 1) $calculated_category_path_string = TEXT_TOP;
return $calculated_category_path_string;
}
function tep_get_category_id_from_path($str, $parent_id) {
global $languages_id;
$parent_id_ar = tep_db_fetch_array(tep_db_query('select c.categories_id, cd.categories_name from '.TABLE_CATEGORIES.'
c, '.TABLE_CATEGORIES_DESCRIPTION.' cd where c.categories_id=cd.categories_id and c.parent_id='.$parent_id.' and
cd.categories_name=\''.tep_db_input($str).'\' and cd.language_id=1'));
$categories_id = (int)$parent_id_ar['categories_id'];
// echo 'select c.categories_id, cd.categories_name from '.TABLE_CATEGORIES.' c, '.TABLE_CATEGORIES_DESCRIPTION.' cd
where c.categories_id=cd.categories_id and c.parent_id='.$parent_id.' and cd.categories_name=\''.tep_db_input($str).'\' and
cd.language_id=1<br>'.$categories_id.'<br>
//<br>
//<br>
//';
return $categories_id;
}
function tep_check_product_db($str) {
$parent_id_ar = tep_db_fetch_array(tep_db_query('select products_id from '.TABLE_PRODUCTS.' where
products_ref_code=\''.$str.'\''));
$products_id = (int)$parent_id_ar['products_id'];
return $products_id;
}
function tep_check_manufacturers_db($str) {
$parent_id_ar = tep_db_fetch_array(tep_db_query('select manufacturers_id from '.TABLE_MANUFACTURERS.' where
manufacturers_name=\''.tep_db_input($str).'\''));
$products_id = (int)$parent_id_ar['manufacturers_id'];
return $products_id;
}
// languages definition --------------------------------------
$languages_query = tep_db_query("select languages_id, name, code, image, directory from " . TABLE_LANGUAGES . " order by
languages_id");
$first_languages_id = 0;
while ($languages = tep_db_fetch_array($languages_query)) {
$languages_array[$languages['languages_id']] = array(
'name' => $languages['name'],
'code' => $languages['code'],
'languages_id' => $languages['languages_id'],
'image' => $languages['image'],
'directory' => $languages['directory']);
if ($first_languages_id == -1) $first_languages_id = $languages['languages_id'];
}
//var_dump($languages_array);
//var_dump($languages_array_keys);
$languages_array_keys = array_keys($languages_array);
// $handle = fopen("http://www.example.com/", "r");
$filename = "FILE CSV";
$handle = fopen($filename, "r");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
$st_ar = explode("\n",$contents);
$i = round(sizeof($st_ar)/2*((int)$_GET['thepage']));
$nnn = round(sizeof($st_ar)/2*((int)$_GET['thepage']+1));
$ref_codes = array();
if (!tep_session_is_registered('prod_ar_delete')) {
tep_session_register('prod_ar_delete');
$prod_ar_delete = array();
$dop_qu = tep_db_query("select products_id from " . TABLE_PRODUCTS . " where products_ref_code<>''" );
while ($dop_ar = tep_db_fetch_array($dop_qu)) {
$prod_ar_delete[$dop_ar['products_id']] = $dop_ar['products_id'];
}
}
while ($i<=$nnn) {
$dop_ar = explode(';',$st_ar[$i]);
if (tep_not_null($dop_ar[3]))$ref_codes[] = tep_db_input($dop_ar[3]);
$parent_id = 0;
for ($k=0;$k<3;$k++) {
if (trim($dop_ar[$k])<>'') {
$dop_ar[$k] = trim($dop_ar[$k]);
$categories_id = tep_get_category_id_from_path($dop_ar[$k],$parent_id);
if ((int)$categories_id == 0) {
$sql_data_array = array();
$sql_data_array['parent_id'] = $parent_id;
tep_db_perform(TABLE_CATEGORIES, $sql_data_array);
$categories_id = tep_db_insert_id();
$sql_data_array = array();
$sql_data_array['categories_id'] = $categories_id;
$sql_data_array['language_id'] = 1;
$sql_data_array['categories_name'] = tep_db_input($dop_ar[$k]);
tep_db_perform(TABLE_CATEGORIES_DESCRIPTION, $sql_data_array);
}
$parent_id = $categories_id;
}
}
$dop_ar[4] = trim($dop_ar[4]);
$manufacturers_id = 0;
if ($dop_ar[4]<>'') {
$manufacturers_id = tep_check_manufacturers_db($dop_ar[4]);
if ((int)$manufacturers_id == 0) {
$sql_data_array = array();
$sql_data_array['manufacturers_name'] = tep_db_input($dop_ar[4]);
tep_db_perform(TABLE_MANUFACTURERS, $sql_data_array);
$manufacturers_id = tep_db_insert_id();
$sql_data_array = array();
$sql_data_array['manufacturers_id'] = $manufacturers_id;
$sql_data_array['languages_id'] = 1;
tep_db_perform(TABLE_MANUFACTURERS_INFO, $sql_data_array);
}
}
$sql_data_array = array();
$sql_data_desc_array = array();
$sql_data_array['manufacturers_id'] = $manufacturers_id;
$sql_data_array['products_ref_code'] = tep_db_input($dop_ar[3]);
$sql_data_array['products_status'] = 1;
$sql_data_array['products_tax_class_id'] = 0;
$sql_data_array['products_price'] = (trim($dop_ar[7]))*1.05;
$sql_data_array['products_image'] = tep_db_input($dop_ar[10]);
//if ($sql_data_array['products_date_available']<>'Disponibile') $sql_data_array['products_date_available'] =
tep_db_prepare_input($dop_ar[11]);
if ($dop_ar[11] == 'Non disponibile') {
$sql_data_array['products_status'] = 1;
$sql_data_array['products_quantity'] = 0;
} elseif ($dop_ar[11] == 'Disponibile') {
$sql_data_array['products_status'] = 1;
$sql_data_array['products_quantity'] = 10;
} elseif ($dop_ar[11] == 'Momentaneamente indisponibile') {
$sql_data_array['products_status'] = 0;
$sql_data_array['products_quantity'] = 0;
} else {
$sql_data_array['products_status'] = 1;
$sql_data_array['products_quantity'] = 0;
$anno = substr($dop_ar[11], 6, 4);
$mese = substr($dop_ar[11], 3, 2);
$giorno = substr($dop_ar[11], 0, 2);
$datadisponibilita = $anno.'-'.$mese.'-'.$giorno;
$sql_data_array['products_date_available'] = tep_db_prepare_input($datadisponibilita);
}
$sql_data_array['products_weight'] = tep_db_input($dop_ar[13]);
$sql_data_array['products_vol_weight'] = tep_db_input($dop_ar[12]);
$sql_data_desc_array['products_name'] = $dop_ar[4].' '.tep_db_input($dop_ar[5]);
//$sql_data_desc_array['products_head_title_tag'] = $dop_ar[4].' '.tep_db_input($dop_ar[5]);
$sql_data_desc_array['products_description'] = tep_db_input($dop_ar[6]);
if (trim($sql_data_desc_array['products_name']) <> '') {
$products_id = tep_check_product_db($dop_ar[3]);
if ((int)$products_id > 0) {
tep_db_perform(TABLE_PRODUCTS, $sql_data_array, 'update', "products_id = '" . $products_id . "'");
tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_desc_array, 'update', "products_id = '" .
$products_id . "'");
$action = 'update';
} else {
tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
$products_id = tep_db_insert_id();
$sql_data_desc_array['products_id'] = $products_id;
tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_desc_array);
$action = 'insert';
}
$sql_data_array = array();
$sql_data_array['products_id'] = $products_id;
$sql_data_array['categories_id'] = $categories_id;
tep_db_query('delete from '.TABLE_PRODUCTS_TO_CATEGORIES.' where products_id='.$products_id);
tep_db_perform(TABLE_PRODUCTS_TO_CATEGORIES, $sql_data_array);
}
unset($prod_ar_delete[$products_id]);
$i++;
}
if ((int)$_GET['thepage'] <> 1) tep_redirect(tep_href_link('cvs_impr.php','thepage=1')); else {
$prod_ar_delete_key = array_keys($prod_ar_delete);
for ($i = 0; $i<=sizeof($prod_ar_delete_key); $i++ ) {
if ($prod_ar_delete_key[$i]>0) {
//echo $prod_ar_delete_key[$i].'<br>';
tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" .
(int)$prod_ar_delete_key[$i] . "'");
tep_remove_product($prod_ar_delete_key[$i]);
}
}
tep_session_unregister('prod_ar_delete');
unset($prod_ar_delete);
unset($prod_ar_delete_key);
}
?>
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0"
leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="init()">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td width="<?php echo BOX_WIDTH; ?>" valign="top">
<table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1"
cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
</table>
</td>
<!-- body_text //-->
<td width="100%" valign="top">
<table width="100%" border="0">
<tr>
<td><?php
echo 'Done with products update';
?></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<!-- body_text_eof //-->
</tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>