PEEL Shopping
Open source ecommerce : PEEL Shopping
import_produits.php
Go to the documentation of this file.
1 <?php
2 // This file should be in UTF8 without BOM - Accents examples: éèê
3 // +----------------------------------------------------------------------+
4 // | Copyright (c) 2004-2015 Advisto SAS, service PEEL - contact@peel.fr |
5 // +----------------------------------------------------------------------+
6 // | This file is part of PEEL Shopping 8.0.0, which is subject to an |
7 // | opensource GPL license: you are allowed to customize the code |
8 // | for your own needs, but must keep your changes under GPL |
9 // | More information: https://www.peel.fr/lire/licence-gpl-70.html |
10 // +----------------------------------------------------------------------+
11 // | Author: Advisto SAS, RCS 479 205 452, France, https://www.peel.fr/ |
12 // +----------------------------------------------------------------------+
13 // $Id: import_produits.php 46935 2015-09-18 08:49:48Z gboussin $
14 define('IN_PEEL_ADMIN', true);
15 include("../configuration.inc.php");
17 necessite_priv("admin_products,admin_webmastering");
18 
19 $specific_fields_array = array($GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_LISTED_PRICE_INCLUDING_VAT'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_LISTED_PRICE_EXCLUDING_VAT'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_SIZES'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_COLORS'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_BRAND'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_ASSOCIATED_PRODUCTS'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_CATEGORY'], 'Stock', 'Categorie', 'categorie_id');
20 $GLOBALS['DOC_TITLE'] = $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_TITLE'];
21 
22 include($GLOBALS['repertoire_modele'] . "/admin_haut.php");
23 
24 $action = vb($_POST['action']);
25 // On récupère les noms des champs de la table de produits
28 
29 // Seléction des attributs, actif ou pas.
30 $q_nom_attrib = query("SELECT id, nom_" . $_SESSION['session_langue'] . "
31  FROM peel_nom_attributs
32  WHERE " . get_filter_site_cond('nom_attributs') . "
33  ORDER BY nom_" . $_SESSION['session_langue'] . "");
34 $attributs_array = array();
35 while ($attrib = fetch_assoc($q_nom_attrib)) {
36  $attributs_array[] = $attrib;
37 }
38 $columns_skipped = array();
39 
40 switch ($action) {
41  case "import":
42  if (a_priv('demo')) {
43  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => $GLOBALS['STR_DEMO_RIGHTS_LIMITED']))->fetch();
44  break;
45  }
46  echo $GLOBALS['tplEngine']->createTemplate('admin_import_produits_table.tpl')->fetch();
47  if (isset($_POST['on_update'])) {
48  // Mise à jour de la table de préférence des champs
49  query("UPDATE peel_import_field SET etat='0'");
50  foreach($_POST['on_update'] as $this_id => $this_value) {
51  echo '<input type="hidden" name="on_update[' . $this_id . ']" value="' . String::str_form_value($this_value) . '" />';
52  query("UPDATE peel_import_field
53  SET etat='1'
54  WHERE champs='" . nohtml_real_escape_string($this_value) . "' AND " . get_filter_site_cond('import_field', null, true) . "");
55  if (!affected_rows()) {
56  // Comme etat valait 0 avant, c'est que la ligne n'existait pas, on va donc la créer
57  query("INSERT INTO peel_import_field
58  SET etat='1', champs='" . nohtml_real_escape_string($this_value) . "', site_id='" . nohtml_real_escape_string(get_site_id_sql_set_value($GLOBALS['site_id']))."'");
59  }
60  }
61  }
62  if (!verify_token($_SERVER['PHP_SELF'] . $action)) {
63  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => $GLOBALS['STR_INVALID_TOKEN']))->fetch();
64  } elseif (empty($_POST['type_import'])) {
65  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_TYPE_NOT_CHOSEN']))->fetch();
66  } elseif ($_POST['type_import'] == 'chosen_fields' && empty($_POST['on_update'])) {
67  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_FIELDS_NOT_CHOSEN']))->fetch();
68  } else {
69  $fichier = upload('fichier', false, 'data', $GLOBALS['site_parameters']['image_max_width'], $GLOBALS['site_parameters']['image_max_height']);
70  if (empty($fichier) || !file_exists($GLOBALS['uploaddir'] . '/' . $fichier)) {
71  /* le fichier n'existe pas */
72  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_FILE_NOT_FOUND']))->fetch();
73  } else {
74  $tpl = $GLOBALS['tplEngine']->createTemplate('admin_import_produits_fichier.tpl');
75  $tpl->assign('href', $GLOBALS['repertoire_upload'] . '/' . $fichier);
76  $tpl->assign('name', $fichier);
77  $tpl->assign('STR_FILE', $GLOBALS['STR_FILE']);
78  $tpl->assign('STR_BEFORE_TWO_POINTS', $GLOBALS['STR_BEFORE_TWO_POINTS']);
79  echo $tpl->fetch();
80  if ($_POST['type_import'] == 'chosen_fields') {
81  foreach($_POST['on_update'] as $this_field_name) {
82  // Sélection des colonnes souhaitées par l'utilisateur
83  $selected_product_field_names[] = $this_field_name;
84  }
85  }
86  $fp = String::fopen_utf8($GLOBALS['uploaddir'] . '/' . $fichier, "rb");
87  // Effacement des produits
88  $nbprod = 0;
89  $this_line = String::convert_encoding(fgets($fp, 16777216), GENERAL_ENCODING, $_POST['import_encoding']);
90  if (empty($_POST['columns_separator'])) {
91  // détection automatique
92  if (strpos($this_line, "\t") !== false) {
93  $separator = "\t";
94  } elseif (strpos($this_line, ";") !== false) {
95  $separator = ";";
96  } elseif (strpos($this_line, ",") !== false) {
97  $separator = ",";
98  } else {
99  $separator = "\t";
100  }
101  } elseif ($_POST['columns_separator'] == '\t') {
102  $separator = "\t";
103  } else {
104  $separator = $_POST['columns_separator'];
105  }
106  $field_names = explode($separator, $this_line);
107  $temp_trim_field_names = array();
108 
109  foreach($field_names as $this_key => $this_field_name) {
110  $this_field_name = trim($this_field_name);
111  $field_names[$this_key] = $this_field_name;
112  if ($_POST['type_import'] == 'chosen_fields' && !in_array($this_field_name, $selected_product_field_names)) {
113  // Champ non sélectionné par l'utilisateur pour l'import
114  $columns_skipped[] = $this_field_name;
115  continue;
116  }
117  if (!in_array($this_field_name, $product_field_names) && strpos($this_field_name, "#") === false && strpos($this_field_name, "§") === false) {
118  // Si le champ trouvé dans le fichier n'est pas dans la table produit, et que le nom du produit ne contient pas de séparateur (spécifique aux attributs).
119  if (in_array($this_field_name, $specific_fields_array)) {
120  if(!in_array($this_field_name, array($GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_CATEGORY'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_SIZES'], $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_COLORS'], 'Stock', 'Categorie', 'categorie_id'))) {
121  // Les champs écartés dans la liste ci-dessus ici seront traités dans le script spécifiquement. Les autres champs spécifiques ne sont pas traités
122  $columns_skipped[] = $this_field_name;
123  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => sprintf($GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_COLUMN_NOT_HANDLED'], $this_key, (!empty($this_field_name)?$this_field_name:'[-]'))))->fetch();
124  }
125  } else {
126  // Colonne inconnue
127  $columns_skipped[] = $this_field_name;
128  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => sprintf($GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_COLUMN_NOT_KNOWN'], $this_key, (!empty($this_field_name)?$this_field_name:'[-]'))))->fetch();
129  }
130  continue;
131  }
132  if (in_array($this_field_name, $temp_trim_field_names)) {
133  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => sprintf($GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_INCOHERENT_COLUMNS'], $this_field_name)))->fetch();
134  $skip_import = true;
135  }
136  $temp_trim_field_names[] = $this_field_name;
137  }
138  unset($temp_trim_field_names);
139  $line_number = 0;
140 
141  if (empty($skip_import)) {
142  while (!String::feof($fp)) {
143  unset($product_id);
144  unset($set_sql_fields);
145  unset($field_values);
146  $last_treated_columns = 0;
147  $line_number++;
148  // Si une valeur de cas contient des sauts de ligne, alors on prend quand même la ligne suivante comme si c'était la continuité de cette ligne
149  while (!String::feof($fp) && (empty($field_values) || count($field_values) < count($field_names) - count($columns_skipped))) {
150  // Tant qu'on n'atteint pas fin de fichier
151  $this_line = String::convert_encoding(fgets($fp, 16777216), GENERAL_ENCODING, $_POST['import_encoding']);
152  if (empty($this_line)) {
153  break;
154  }
155  // echo '<hr />Ligne Excel : $this_line';
156  $line_fields = explode($separator, $this_line);
157  foreach($line_fields as $key => $this_field) {
158  // On récupère les valeurs présentes dans la ligne en cours
159  if(isset($field_names[$key + $last_treated_columns])) {
160  if (!isset($field_values[$field_names[$key + $last_treated_columns]])) {
161  $field_values[$field_names[$key + $last_treated_columns]] = trim($this_field);
162  } else {
163  $field_values[$field_names[$key + $last_treated_columns]] .= trim($this_field);
164  }
165  } else {
166  echo $GLOBALS['tplEngine']->createTemplate('global_error.tpl', array('message' => sprintf($GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_ERR_COLUMN_NOT_KNOWN'], $key + $last_treated_columns, $this_field)))->fetch();
167  }
168  }
169  $last_treated_columns += $key;
170  }
171  if (!empty($field_values) && count($field_values) > count($columns_skipped)) {
172  // On a trouvé au moins un champ à importer
173  if (empty($field_values['date_insere'])) {
174  $field_values['date_insere'] = date('Y-m-d H:i:s', time());
175  }
176  if (empty($field_values['date_maj'])) {
177  $field_values['date_maj'] = date('Y-m-d H:i:s', time());
178  }
179  } else {
180  // On n'a trouvé aucun champ sur la ligne en cours, on passe à la ligne suivante
181  continue;
182  }
184  }
185  }
186  fclose($fp);
187  echo $GLOBALS['tplEngine']->createTemplate('global_success.tpl', array('message' => sprintf($GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_MSG_IMPORTATION_OK'], vn($GLOBALS['nbprod_insert']) + vn($GLOBALS['nbprod_update']) + vn($GLOBALS['nbprod_update_null']), vn($GLOBALS['nbprod_update']), vn($GLOBALS['nbprod_update_null']), vn($GLOBALS['nbprod_insert']), vn($GLOBALS['nbprod_categorie_insert']))))->fetch();
188  }
189  }
190  break;
191 
192  default:
193  /* FORMULAIRE DE CHOIX D'IMPORTATION */
194  $tpl = $GLOBALS['tplEngine']->createTemplate('admin_import_produits_form.tpl');
195  $tpl->assign('action', get_current_url(false));
196  $tpl->assign('form_token', get_form_token_input($_SERVER['PHP_SELF'] . 'import'));
197  $tpl_inputs = array();
198  $req = query("SELECT champs, etat, texte_" . $_SESSION['session_langue'] . "
199  FROM peel_import_field
200  WHERE " . get_filter_site_cond('import_field', null, true) . "");
201  while ($result = fetch_assoc($req)) {
202  $fields_explanations_arrays[$result['champs']] = $result;
203  }
204  // Pour afficher categorie_id dans la liste des champs importables
205  $product_field_names[] = 'categorie_id';
206  $product_field_names['Categorie'] = 'Categorie';
207  $product_field_names[$GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_CATEGORY']] = $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_CATEGORY'];
208  $product_field_names[$GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_SIZES']] = $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_SIZES'];
209  $product_field_names[$GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_COLORS']] = $GLOBALS['STR_ADMIN_EXPORT_PRODUCTS_COLORS'];
210  $product_field_names['Stock'] = 'Stock';
211  sort($product_field_names);
212  foreach ($product_field_names as $this_field) {
213  if ($this_field != 'stock') {
214  $tpl_inputs[] = array('field' => $this_field,
215  'issel' => vb($fields_explanations_arrays[$this_field]['etat']) == 1,
216  'explanation' => vb($fields_explanations_arrays[$this_field]['texte']),
217  'is_important' => ($this_field == 'id' || $this_field == 'categorie_id')
218  );
219  }
220  }
221  $tpl->assign('inputs', $tpl_inputs);
222  $tpl->assign('uploaddir', $uploaddir);
223  $tpl->assign('import_encoding', vb($frm['import_encoding']));
224  $tpl->assign('example_href', 'import/exemple_prod.csv');
225  $tpl->assign('STR_BEFORE_TWO_POINTS', $GLOBALS['STR_BEFORE_TWO_POINTS']);
226  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_FORM_TITLE', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_FORM_TITLE']);
227  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_FILE_FORMAT', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_FILE_FORMAT']);
228  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_FILE_FORMAT_EXPLAIN', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_FILE_FORMAT_EXPLAIN']);
229  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_FILE_EXAMPLE', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_FILE_EXAMPLE']);
230  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_IMPORT_MODE', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_IMPORT_MODE']);
231  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_IMPORT_ALL_FIELDS', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_IMPORT_ALL_FIELDS']);
232  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_IMPORT_SELECTED_FIELDS', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_IMPORT_SELECTED_FIELDS']);
233  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_SELECT_FIELDS', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_SELECT_FIELDS']);
234  $tpl->assign('STR_WARNING', $GLOBALS['STR_WARNING']);
235  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_EXPLAIN', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_EXPLAIN']);
236  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_WARNING_ID', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_WARNING_ID']);
237  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_FILE_NAME', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_FILE_NAME']);
238  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_FILE_ENCODING', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_FILE_ENCODING']);
239  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_SEPARATOR', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_SEPARATOR']);
240  $tpl->assign('STR_ADMIN_IMPORT_PRODUCTS_SEPARATOR_EXPLAIN', $GLOBALS['STR_ADMIN_IMPORT_PRODUCTS_SEPARATOR_EXPLAIN']);
241  $tpl->assign('STR_VALIDATE', $GLOBALS['STR_VALIDATE']);
242  echo $tpl->fetch();
243  break;
244 }
245 include($GLOBALS['repertoire_modele'] . "/admin_bas.php");
246 
if(!empty($GLOBALS['site_parameters']['order_specific_field_titles'])) if(check_if_module_active('socolissimo')&&!empty($_REQUEST)&&!empty($_REQUEST['PUDOFOID'])&&!empty($_REQUEST['CEEMAIL'])&&!empty($_REQUEST['SIGNATURE'])&&!empty($_REQUEST['ORDERID'])) elseif(!empty($_POST)) elseif(check_if_module_active('socolissimo')&&!empty($_SESSION['session_commande']['is_socolissimo_order'])) foreach(array('bill'=> 1, 'ship'=> 2) as $address_type=> $session_commande_address_id) $frm['societe1']
$result
static convert_encoding($string, $new_encoding, $original_encoding=null)
Converts the character encoding of string $string to $new_encoding from optionally $original_encoding...
Definition: String.php:375
$GLOBALS['DOC_TITLE']
get_current_url($with_get=true, $get_short_url=false, $take_away_get_args_array=null)
get_current_url()
Definition: fonctions.php:1743
upload($field_name, $rename_file=true, $file_kind=null, $image_max_width=null, $image_max_height=null, $path=null, $new_file_name_without_extension=null, $default_return_value=null)
Fonction d'upload de fichiers.
Definition: fonctions.php:3302
$attributs_array
while($attrib=fetch_assoc($q_nom_attrib)) $columns_skipped
static fopen_utf8($filename, $mode, $force_filename_in_iso_8859=false, $try_filename_in_iso_8859_if_file_not_found=true)
Ouvre un fichier.
Definition: String.php:793
affected_rows($database_object=null)
affected_rows()
Definition: database.php:356
nohtml_real_escape_string($value, $allowed_tags=null)
Protège les données pour insertion dans MySQL ET supprime les tags HTML pour protéger de toute sorte ...
Definition: database.php:400
create_or_update_product($field_values, $columns_skipped=array(), $product_field_names=array(), $specific_fields_array=array(), $admin_mode=false)
Import d'un produit : mise à jour ou création du produit.
Definition: fonctions.php:5445
if(!empty($_GET['id'])) if(isset($_POST['form_name'], $_POST['form_subject'], $_POST['form_text'], $_POST['form_lang'])&&empty($_GET['id'])) if(empty($_GET['id'])) $tpl
static feof($handle)
Tests for end-of-file on a file pointer In contrary of the default feof function, it returns true if ...
Definition: String.php:866
necessite_priv($priv, $demo_allowed=true, $configuration_modification=false)
Cette fonction vérifie si l'utilisateur a les privilèges de $priv.
Definition: fonctions.php:1575
$product_field_names
get_filter_site_cond($table_technical_code, $table_alias=null, $use_strict_rights_if_in_admin=false, $specific_site_id=null, $exclude_public_items=false, $admin_force_multisite_if_allowed=false)
Retourne la condition SQL permettant de filtrer les données pour une table.
Definition: fonctions.php:4643
query($query, $die_if_error=false, $database_object=null, $silent_if_error=false, $security_sql_filter=true)
The query() function is meant to be called anywhere you want to make a query.
Definition: database.php:158
$specific_fields_array
static str_form_value($value, $flags=ENT_COMPAT)
Encode une chaine de caractères pour affichage dans un value="".
Definition: String.php:480
vb(&$var, $default=null)
Variable blanche if $var n'est pas défini, retourne $default, sinon retourne $var.
Definition: format.php:97
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
necessite_identification()
Si l'utilisateur n'est pas connecté à un compte, on affiche une page d'identification et arrête le sc...
Definition: fonctions.php:1596
get_table_field_names($table_name, $link_identifier=null, $silent_if_error=false)
get_table_field_names()
Definition: database.php:495
fetch_assoc($query_result)
fetch_assoc()
Definition: database.php:283
get_form_token_input($name= 'general', $use_existing_token=true, $return_as_input_form=true)
get_form_token_input()
Definition: fonctions.php:94
vn(&$var, $default=0)
Variable nulle if $var n'est pas défini, retourne $default, sinon retourne $var.
Definition: format.php:110
get_site_id_sql_set_value($site_ids)
Retourne la valeur SQL d'un champ INT ou SET suivant que ce soit un entier ou un tableau.
Definition: fonctions.php:4747
a_priv($requested_priv, $demo_allowed=false, $site_configuration_modification=false, $user_id=null)
Renvoie true si l'utilisateur de la session a le privilège $requested_priv ou un droit supérieur Des ...
Definition: user.php:63
$req
Definition: ipn.php:30
verify_token($name= 'general', $delay_in_minutes=60, $check_referer_if_set_by_server=true, $cancel_token=true, $minimum_wait_in_seconds_before_use=0)
Vérification de la validité d'un token Par défaut, un token est valide 1h, et utilisable 1 seule fois...
Definition: fonctions.php:118
$q_nom_attrib
if(defined('IN_PEEL_ADMIN')||IN_INSTALLATION) $_SESSION['session_langue']

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:29:11 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.