From e0f9588e7d0019a26fc32576854b04e2d0ed0fe4 Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Mon, 26 May 2025 10:44:30 +0200 Subject: [PATCH] Refactor XML feed handling for ceneo_drmaterac and ceneo_lulandia to improve structure and maintainability --- drmaterac.pl/ceneo_drmaterac_feed.php | 11 ++- drmaterac.pl/ceneo_lulandia_feed.php | 109 +++++++++++--------------- 2 files changed, 56 insertions(+), 64 deletions(-) diff --git a/drmaterac.pl/ceneo_drmaterac_feed.php b/drmaterac.pl/ceneo_drmaterac_feed.php index 87f2937..9190dec 100644 --- a/drmaterac.pl/ceneo_drmaterac_feed.php +++ b/drmaterac.pl/ceneo_drmaterac_feed.php @@ -20,6 +20,7 @@ $localFile = "ceneo_drmaterac_feed_temp.xml"; unlink( $localFile ); // Usunięcie starego pliku, jeśli istnieje unlink( "ceneo_drmaterac_feed.xml" ); // Usunięcie starego pliku, jeśli istnieje + $xmlContent = file_get_contents($url); if ($xmlContent === false) { die("Nie udało się pobrać pliku XML."); @@ -104,7 +105,15 @@ foreach ($offers as $offer) { $a->setAttribute('name', 'Producent odpowiedzialny'); $cdata = $dom->createCDATASection($manufacturerName); $a->appendChild($cdata); - $offer->appendChild($a); + + $attrs = $offer->getElementsByTagName('attrs'); + if ($attrs->length > 0) { + $attrsElement = $attrs->item(0); + } else { + $attrsElement = $dom->createElement('attrs'); + $offer->appendChild($attrsElement); + } + $attrsElement->appendChild($a); } } } diff --git a/drmaterac.pl/ceneo_lulandia_feed.php b/drmaterac.pl/ceneo_lulandia_feed.php index 1b1403b..253dff4 100644 --- a/drmaterac.pl/ceneo_lulandia_feed.php +++ b/drmaterac.pl/ceneo_lulandia_feed.php @@ -25,74 +25,56 @@ if ($xmlContent === false) { die("Nie udało się pobrać pliku XML."); } -// Zapisanie zawartości do lokalnego pliku if (file_put_contents($localFile, $xmlContent) === false) { die("Nie udało się zapisać pliku XML."); } -// Tworzenie nowego fragmentu XML dla producentów +// Tworzenie fragmentu responsibleProducers jako SimpleXMLElement $responsibleProducers = new SimpleXMLElement(''); -foreach ( $manufacturers as $manufacturer ) -{ - $producer = $responsibleProducers -> addChild( 'p' ); - $producer -> addAttribute( 'id', $manufacturer['id_x13gpsr_responsible_manufacturer'] ); +foreach ($manufacturers as $manufacturer) { + $producer = $responsibleProducers->addChild('p'); + $producer->addAttribute('id', $manufacturer['id_x13gpsr_responsible_manufacturer']); + $producer->addChild('name', htmlspecialchars($manufacturer['name'])); - $producer -> addChild( 'name', htmlspecialchars( $manufacturer['name'] ) ); + $address = $producer->addChild('address'); + $country_iso_code = $mdb->get('materac_country', 'iso_code', ['id_country' => $manufacturer['id_country']]); + $address->addChild('countryCode', htmlspecialchars($country_iso_code ?? '')); + $address->addChild('street', htmlspecialchars($manufacturer['address'] ?? '')); + $address->addChild('postalCode', htmlspecialchars($manufacturer['postcode'] ?? '')); + $address->addChild('city', htmlspecialchars($manufacturer['city'] ?? '')); - $address = $producer -> addChild( 'address' ); - - $country_iso_code = $mdb -> get( 'materac_country', 'iso_code', [ 'id_country' => $manufacturer['id_country'] ] ); - $address -> addChild( 'countryCode', htmlspecialchars( $country_iso_code ?? '' ) ); - - $address -> addChild( 'street', htmlspecialchars( $manufacturer['address'] ?? '' ) ); - $address -> addChild( 'postalCode', htmlspecialchars( $manufacturer['postcode'] ?? '' ) ); - $address -> addChild( 'city', htmlspecialchars( $manufacturer['city'] ?? '' ) ); - - $contact = $producer -> addChild( 'contact' ); - $contact->addChild('email', htmlspecialchars($manufacturer['email'] ?? '')); - $contact->addChild('phoneNumber', htmlspecialchars($manufacturer['phone'] ?? '')); + $contact = $producer->addChild('contact'); + $contact->addChild('email', htmlspecialchars($manufacturer['email'] ?? '')); + $contact->addChild('phoneNumber', htmlspecialchars($manufacturer['phone'] ?? '')); } -// Wczytanie istniejącego pliku XML -$existingXml = simplexml_load_file($localFile); -if ($existingXml === false) { +// Wczytanie istniejącego pliku XML jako DOMDocument +$dom = new DOMDocument(); +$dom->preserveWhiteSpace = false; +$dom->formatOutput = true; + +if (!$dom->load($localFile)) { die("Nie udało się wczytać istniejącego pliku XML."); } -// Dodanie sekcji zaraz po otwarciu -$domExisting = dom_import_simplexml($existingXml)->ownerDocument; -$domResponsibleProducers = dom_import_simplexml($responsibleProducers)->ownerDocument->documentElement; +// Import responsibleProducers do DOM +$importedProducers = $dom->importNode(dom_import_simplexml($responsibleProducers), true); -// Znalezienie elementu -$offersElement = $domExisting->getElementsByTagName('offers')->item(0); -if ($offersElement === null) { - die("Nie znaleziono elementu w pliku XML."); +// Wstawienie jako pierwszego dziecka +$offersList = $dom->getElementsByTagName('offers'); +if ($offersList->length === 0) { + die("Nie znaleziono elementu ."); } +$offersElement = $offersList->item(0); +$offersElement->insertBefore($importedProducers, $offersElement->firstChild); -// Wstawienie zaraz po otwarciu -$offersElement->insertBefore( - $domExisting->importNode($domResponsibleProducers, true), - $offersElement->firstChild -); +// Iteracja po +$offers = $dom->getElementsByTagName('o'); +foreach ($offers as $offer) { + $productId = $offer->getAttribute('id'); -// Zapisanie zmodyfikowanego pliku XML -if ($domExisting->save($localFile) === false) { - die("Nie udało się zapisać zmodyfikowanego pliku XML."); -} - -// Pobranie pliku XML -$localFile = "ceneo_lulandia_feed_temp.xml"; -$existingXml = simplexml_load_file($localFile); -if ($existingXml === false) { - die("Nie udało się wczytać istniejącego pliku XML."); -} - -// Iteracja po elementach -foreach ($existingXml->xpath('//o') as $offer) { - $productId = (string) $offer['id']; - - // Pobranie id_x13gpsr_responsible_manufacturer z tabeli materac_x13gpsr_responsible_manufacturer_product + // Szukanie id producenta odpowiedzialnego $responsibleManufacturerId = $mdb->get( 'materac_x13gpsr_responsible_manufacturer_product', 'id_x13gpsr_responsible_manufacturer', @@ -100,11 +82,8 @@ foreach ($existingXml->xpath('//o') as $offer) { ); if (!$responsibleManufacturerId) { - // Jeśli nie znaleziono, pobierz id_manufacturer z tabeli materac_product $brandId = $mdb->get('materac_product', 'id_manufacturer', ['id_product' => $productId]); - if ($brandId) { - // Szukaj w tabeli materac_x13gpsr_responsible_manufacturer_brand $responsibleManufacturerId = $mdb->get( 'materac_x13gpsr_responsible_manufacturer_brand', 'id_x13gpsr_responsible_manufacturer', @@ -125,25 +104,29 @@ foreach ($existingXml->xpath('//o') as $offer) { $a->setAttribute('name', 'Producent odpowiedzialny'); $cdata = $dom->createCDATASection($manufacturerName); $a->appendChild($cdata); - $offer->appendChild($a); + + $attrs = $offer->getElementsByTagName('attrs'); + if ($attrs->length > 0) { + $attrsElement = $attrs->item(0); + } else { + $attrsElement = $dom->createElement('attrs'); + $offer->appendChild($attrsElement); + } + $attrsElement->appendChild($a); } } } // Zapisanie zmodyfikowanego pliku XML -if ($existingXml->asXML($localFile) === false) { +if (!$dom->save($localFile)) { die("Nie udało się zapisać zmodyfikowanego pliku XML."); } -echo "Plik XML został pomyślnie zaktualizowany."; - -// remove _temp from filename +// Zmiana nazwy pliku $finalFile = str_replace('_temp', '', $localFile); if (rename($localFile, $finalFile)) { - echo "Plik XML został pomyślnie przeniesiony do docelowej lokalizacji."; + echo "Plik XML został pomyślnie zaktualizowany i przeniesiony do:
$finalFile"; } else { echo "Nie udało się przenieść pliku XML do docelowej lokalizacji."; } - -echo "
Plik XML został pomyślnie zaktualizowany. URL: " . $finalFile; -?> \ No newline at end of file +?>