alpha_full/admin/publier/scriptsImports/import_csv.php
2026-04-06 22:58:51 +02:00

349 lines
14 KiB
PHP
Executable File

<?php
$nomFicAppelant = basename(__FILE__);
// Fonction pour valider les données d'entrée
function validateProductData($data) {
$errors = [];
if (empty($data['refPrd'])) {
$errors[] = "Référence produit manquante. ";
}
// $data['prixHt'] = ( str_replace(',', '.', $data['prixHt']));
if (empty($data['prixHt']) || !preg_match('/^\d+(\.\d{1,4})?$/', $data['prixHt'])) {
$errors[] = "Le prix HT est invalide. ";
}
// Ajouter plus de validations selon les besoins
return $errors;
}
// Fonction pour charger les produits dans la base de données
function loadProductsToDb($data, $idPdo) {
foreach ($data as $product) {
$errors = validateProductData($product);
if (count($errors) > 0) {
foreach ($errors as $error) {
echo "<p style='color:red;'>Erreur: $error</p>";
}
return false; // Arrêter si une erreur est trouvée
}
// Attribuer un ID unique
$stmt = $idPdo->query("SELECT MAX(idPrd) FROM produits");
$maxId = $stmt->fetchColumn();
$newId = $maxId + 1;
// Insertion dans la table produits
$stmt = $idPdo->prepare("INSERT INTO produits ( idPrd, refPrd, idFam, idCat, lienImagePt, lienImageGd, promo, gondole, new, afficheGond, prixHt, prixTtc, tva, idEtat, delais, uniteDelais, poids, unitePoids, annee, stock, idPaysPrd, idMailPrd, couleur1, couleur2, couleur3, idLargeur, uniteLarg, idHauteur, uniteHaut, idLongueur, uniteLong, idDates, idType1, idType2, idType3, idType4, idType5, idType6, idType7, idType8, idType9, idType10, idType11, idType12, idType13, idType14, idType15, idType16, idType17, idType18, idType19, idType20, idType21, idType22, idType23,lienImgGauche, lienImgDroite, lienImgDessus, lienImgDessous, lienVideo, lienNotice, statusPrd )
VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
$stmt->execute([
$newId,
$product['refPrd'],
$product['idFam'],
$product['idCat'],
$product['lienImagePt'],
$product['lienImageGd'],
$product['promo'],
$product['gondole'],
$product['new'],
$product['afficheGond'],
$product['prixHt'],
$product['prixTtc'],
$product['tva'],
$product['idEtat'],
$product['delais'],
$product['uniteDelais'],
$product['poids'],
$product['unitePoids'],
$product['annee'],
$product['stock'],
$product['idPaysPrd'],
$product['idMailPrd'],
$product['couleur1'],
$product['couleur2'],
$product['couleur3'],
$product['idLargeur'],
$product['uniteLarg'],
$product['idHauteur'],
$product['uniteHaut'],
$product['idLongueur'],
$product['uniteLong'],
$product['idDates'],
$product['idType1'],
$product['idType2'],
$product['idType3'],
$product['idType4'],
$product['idType5'],
$product['idType6'],
$product['idType7'],
$product['idType8'],
$product['idType9'],
$product['idType10'],
$product['idType11'],
$product['idType12'],
$product['idType13'],
$product['idType14'],
$product['idType15'],
$product['idType16'],
$product['idType17'],
$product['idType18'],
$product['idType19'],
$product['idType20'],
$product['idType21'],
$product['idType22'],
$product['idType23'],
$product['lienImgGauche'],
$product['lienImgDroite'],
$product['lienImgDessus'],
$product['lienImgDessous'],
$product['lienVideo'],
$product['lienNotice'],
$product['statusPrd']
]);
}
return true;
}
// Fonction pour valider les données d'entrée libelle
function validateLibProductData($data) {
$errors = [];
if (empty($data['nomPrd'])) {
$errors[] = "Nom produit produit manquante. ";
}
if (empty($data['descCourt'])) {
$errors[] = "Description produit produit manquante. ";
}
// Ajouter plus de validations selon les besoins
return $errors;
}
// Fonction pour charger les libelles produits dans la base de données
function loadLibProductsToDb($data, $idPdo) {
foreach ($data as $libelleProducts) {
$errors = validateLibProductData($libelleProducts);
if (count($errors) > 0) {
foreach ($errors as $error) {
echo "<p style='color:red;'>Erreur: $error</p>";
}
return false; // Arrêter si une erreur est trouvée
}
// Attribuer un ID unique
$stmt = $idPdo->query("SELECT MAX(idPrd) FROM produits");
$maxId = $stmt->fetchColumn();
$newId = $maxId + 1;
// Insertion dans la table produitslibelle
$stmtLibelle = $idPdo->prepare("INSERT INTO produitslibelle (numPrd, idPrd, idLg, nomPrd, descCourt, descLong) VALUES (?, ?, ?, ?, ?, ?)");
// Exemple avec 'FRA'
$stmtLibelle->execute([0, $newId, 'FRA', $libelleProducts['nomPrd'], $libelleProducts['descCourt'], $libelleProducts['descLong'] ]);
// Exemple avec 'DEU'
$libelleProducts['descCourt']= gTranslate($libelleProducts['descCourt'], 'de');
$stmtLibelle->execute([0, $newId, 'DEU', $libelleProducts['nomPrd'], $libelleProducts['descCourt'], $libelleProducts['descLong'] ]);
// Exemple avec 'ENG'
$libelleProducts['descCourt']= gTranslate($libelleProducts['descCourt'], 'en');
$stmtLibelle->execute([0, $newId, 'ENG', $libelleProducts['nomPrd'], $libelleProducts['descCourt'], $libelleProducts['descLong'] ]);
// Exemple avec 'ESP'
$libelleProducts['descCourt']= gTranslate($libelleProducts['descCourt'], 'es');
$stmtLibelle->execute([0, $newId, 'ESP', $libelleProducts['nomPrd'], $libelleProducts['descCourt'], $libelleProducts['descLong'] ]);
// Exemple avec 'ITA'
$libelleProducts['descCourt']= gTranslate($libelleProducts['descCourt'], 'it');
$stmtLibelle->execute([0, $newId, 'ITA', $libelleProducts['nomPrd'], $libelleProducts['descCourt'], $libelleProducts['descLong'] ]);
}
return true;
}
// Traitement du formulaire pour le fichier
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file']['tmp_name'])) {
$fileType = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ($fileType === 'csv') {
$file = fopen($_FILES['file']['tmp_name'], 'r');
// Initialisation des tableaux avant la boucle
$products = [];
$libelleProducts = []; // Initialisation de la variable $libelleProducts
while (($line = fgetcsv($file)) !== false) {
$products[] = [
'refPrd' => $line[0],
'idFam' => 1,
'idCat' => 1,
'lienImagePt' => './Catalogue/_images_produits/prdDefaut.png',
'lienImageGd' => './Catalogue/_images_produits/grandes/prdDefaut.png',
'promo' => 0.00,
'gondole' => 0,
'new' => 0,
'afficheGond' => 0,
'prixHt' => $line[1],
'prixTtc' => 0.0000,
'tva' => 0,
'idEtat' => 1,
'delais' => 1,
'uniteDelais' => 0,
'poids' => 0.00,
'unitePoids' => 0,
'annee' => '-',
'stock' => 1,
'idPaysPrd' => 0,
'idMailPrd' => '-',
'couleur1' => 0,
'couleur2' => 0,
'couleur3' => 0,
'idLargeur' => 0.00,
'uniteLarg' => 1,
'idHauteur' => 0.00,
'uniteHaut' => 1,
'idLongueur' => 0.00,
'uniteLong' => 1,
'idDates' => 0,
'idType1' => 0,
'idType2' => 0,
'idType3' => 0,
'idType4' => 0,
'idType5' => 0,
'idType6' => 0,
'idType7' => 0,
'idType8' => 0,
'idType9' => 0,
'idType10' => 0,
'idType11' => 0,
'idType12' => 0,
'idType13' => 0,
'idType14' => 0,
'idType15' => 0,
'idType16' => 0,
'idType17' => 0,
'idType18' => 0,
'idType19' => 0,
'idType20' => 0,
'idType21' => 0,
'idType22' => 0,
'idType23' => 0,
'lienImgGauche' => '-',
'lienImgDroite' => '-',
'lienImgDessus' => '-',
'lienImgDessous' => '-',
'lienVideo' => '-',
'lienNotice' => '-',
'statusPrd' => 1
];
$libelleProducts[] = [
'numPrd' => 0,
'idPrd' => 0,
'nomPrd' => $line[2],
'descCourt' => $line[3],
'descLong' => '-'
];
}
fclose($file);
$result = loadProductsToDb($products, $idPdo);
if ($result) {
echo "<p style='color:green;'>Produits chargés avec succès.</p>";
}
$result = loadLibProductsToDb($libelleProducts, $idPdo);
if ($result) {
echo "<p style='color:green;'>Libellés produits chargés avec succès.</p>";
}
}
}
// Traitement du formulaire pour le fichier d'images
if (isset($_FILES['imageZip'])) {
$zip = new ZipArchive;
if ($zip->open($_FILES['imageZip']['tmp_name']) === TRUE) {
$extractPath = './Catalogue/_images_produits/';
$zip->extractTo($extractPath);
$zip->close();
echo "<p style='color:green;'>Images extraites avec succès.</p>";
} else {
echo "<p style='color:red;'>Erreur lors de l'extraction du fichier zip.</p>";
}
}
?>
<style>
.container {
width: 80%;
margin: 30px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
}
form {
margin-top: 20px;
}
input[type="file"], button {
padding: 10px;
margin: 10px;
}
.help-icon {
color: #007BFF;
cursor: pointer;
}
.help-content {
display: none;
background-color: #f9f9f9;
padding: 20px;
border: 1px solid #ddd;
margin-top: 10px;
}
.help-content ul {
padding-left: 20px;
}
.help-content li {
margin-bottom: 10px;
}
</style>
<div class="container">
<h1>Chargement d'articles</h1>
<!-- Formulaire pour charger le fichier -->
<form method="POST" enctype="multipart/form-data">
<label for="file">Choisir un fichier CSV:</label>
<input type="file" name="file" id="file" accept=".csv">
<button class="boutonsWebmaster" type="submit"><i class="fa-solid fa-upload"></i>Charger les articles</button>
</form>
<!-- Formulaire pour charger le fichier zip d'images -->
<br><form method="POST" enctype="multipart/form-data">
<label for="imageZip">Choisir un fichier ZIP d'images:</label>
<input type="file" name="imageZip" id="imageZip" accept=".zip">
<button class="boutonsWebmaster" type="submit"><i class="fa-solid fa-upload"></i>Charger les Images</button>
</form>
<!-- Icône d'aide -->
<p class="help-icon" onclick="toggleHelp()">Aide</p>
<!-- Contenu de l'aide modale -->
<div class="help-content" id="helpContent">
<h3>Format attendu du fichier CSV :</h3>
<ul>
<li><strong>Colonne 1 :</strong> Référence du produit (obligatoire)</li>
<li><strong>Colonne 2 :</strong> Prix HT (obligatoire, numérique)</li>
<li><strong>Colonne 3 :</strong> Nom du produit (obligatoire)</li>
<li><strong>Colonne 4 :</strong> Description courte (obligatoire)</li>
</ul>
<p><strong>Exemple de fichier CSV :</strong></p>
<pre>
P001,19.99,Produit 1,Description produit 1;
P002,29.99,Produit 2,Description produit 2;
</pre>
</div>
</div>
<script>
// Fonction pour afficher/masquer l'aide
function toggleHelp() {
var helpContent = document.getElementById('helpContent');
helpContent.style.display = (helpContent.style.display === 'none' || helpContent.style.display === '') ? 'block' : 'none';
}
</script>