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
"; } 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
"; } 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
"; 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
"; } else { echo "Brak pliku: $src\n
"; } $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
"; echo ""; } else { echo "Eksport zakonczony.\n
"; } ?>