128 lines
4.1 KiB
PHP
128 lines
4.1 KiB
PHP
<?php
|
|
// Konfiguracja bazy
|
|
$host = "localhost";
|
|
$db = "interblue_sklep";
|
|
$user = "interblue_sklep";
|
|
$pass = "2212+#++@pSVSb4";
|
|
$prefix = "ps_"; // zmien jesli masz inny prefix
|
|
|
|
// Katalog docelowy
|
|
$exportDir = __DIR__ . "/export_images";
|
|
$exportDirWithEan = $exportDir . "/with_ean";
|
|
$exportDirWithoutEan = $exportDir . "/without_ean";
|
|
|
|
if (!is_dir($exportDirWithEan)) {
|
|
mkdir($exportDirWithEan, 0777, true);
|
|
}
|
|
|
|
if (!is_dir($exportDirWithoutEan)) {
|
|
mkdir($exportDirWithoutEan, 0777, true);
|
|
}
|
|
|
|
// Tryb testowy - podaj EAN jednego produktu, np. "5901234567890"
|
|
// Jesli pusty, poleci dla wszystkich produktow
|
|
$testEan = "";
|
|
$batchSize = 100; // liczba produktow na jedno wykonanie
|
|
$refreshDelaySeconds = 1; // opoznienie odswiezania miedzy paczkami
|
|
|
|
// Polaczenie z DB
|
|
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Pobierz produkty
|
|
if ($testEan !== "") {
|
|
$sql = "SELECT p.id_product, p.ean13
|
|
FROM {$prefix}product p
|
|
WHERE p.ean13 = :ean
|
|
ORDER BY p.id_product ASC";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute(['ean' => $testEan]);
|
|
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
$hasMore = false;
|
|
$nextLastId = null;
|
|
echo "Tryb testowy - eksportuje tylko produkt o EAN: {$testEan}\n<br>";
|
|
} else {
|
|
$lastId = isset($_GET['last_id']) ? (int)$_GET['last_id'] : 0;
|
|
if ($lastId < 0) {
|
|
$lastId = 0;
|
|
}
|
|
|
|
$sql = "SELECT p.id_product, p.ean13
|
|
FROM {$prefix}product p
|
|
WHERE p.id_product > :last_id
|
|
ORDER BY p.id_product ASC
|
|
LIMIT :batch_size";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->bindValue(':last_id', $lastId, PDO::PARAM_INT);
|
|
$stmt->bindValue(':batch_size', $batchSize, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$nextLastId = $lastId;
|
|
if (!empty($products)) {
|
|
$lastProduct = end($products);
|
|
$nextLastId = (int)$lastProduct['id_product'];
|
|
}
|
|
|
|
$sqlHasMore = "SELECT 1
|
|
FROM {$prefix}product p
|
|
WHERE p.id_product > :next_last_id
|
|
LIMIT 1";
|
|
$stmtHasMore = $pdo->prepare($sqlHasMore);
|
|
$stmtHasMore->execute(['next_last_id' => $nextLastId]);
|
|
$hasMore = (bool)$stmtHasMore->fetchColumn();
|
|
|
|
echo "Eksport paczki produktow (start po ID: {$lastId}, rozmiar: {$batchSize})\n<br>";
|
|
}
|
|
|
|
foreach ($products as $prod) {
|
|
$ean = trim((string)$prod['ean13']);
|
|
$id_product = (int)$prod['id_product'];
|
|
$hasEan = $ean !== '';
|
|
$targetDir = $hasEan ? $exportDirWithEan : $exportDirWithoutEan;
|
|
$baseName = $hasEan ? $ean : "ID{$id_product}";
|
|
|
|
// Pobierz zdjecia produktu
|
|
$sqlImg = "SELECT id_image
|
|
FROM {$prefix}image
|
|
WHERE id_product = :id_product
|
|
ORDER BY position ASC";
|
|
$stmt = $pdo->prepare($sqlImg);
|
|
$stmt->execute(['id_product' => $id_product]);
|
|
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (!$images) {
|
|
$label = $hasEan ? "EAN: {$ean}" : "ID: {$id_product}";
|
|
echo "Brak zdjec dla {$label}\n<br>";
|
|
continue;
|
|
}
|
|
|
|
$i = 1;
|
|
foreach ($images as $img) {
|
|
$id_image = (int)$img['id_image'];
|
|
|
|
// Sciezka do pliku (PrestaShop - struktura katalogow np. 123 -> /1/2/3/123.jpg)
|
|
$path = implode('/', str_split((string)$id_image)) . "/" . $id_image . ".jpg";
|
|
$src = __DIR__ . "/img/p/" . $path;
|
|
|
|
if (file_exists($src)) {
|
|
$dest = $targetDir . "/" . $baseName . "_" . $i . ".jpg";
|
|
copy($src, $dest);
|
|
echo "Zapisano: $dest\n<br>";
|
|
} else {
|
|
echo "Brak pliku: $src\n<br>";
|
|
}
|
|
$i++;
|
|
}
|
|
}
|
|
|
|
if ($testEan === "" && $hasMore) {
|
|
$self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8');
|
|
$nextUrl = $self . '?last_id=' . $nextLastId;
|
|
echo "Paczka zakonczona. Nastepna paczka startuje za {$refreshDelaySeconds}s... (last_id={$nextLastId})\n<br>";
|
|
echo "<meta http-equiv=\"refresh\" content=\"{$refreshDelaySeconds};url={$nextUrl}\">";
|
|
} else {
|
|
echo "Eksport zakonczony.\n<br>";
|
|
}
|
|
?>
|