- Implemented database connection using Medoo for MySQL. - Fetched active manufacturers from the database. - Retrieved XML data from a specified URL and saved it locally. - Created a new XML structure for responsible producers and integrated it into the existing XML feed. - Added responsible manufacturer information to each product offer in the XML. - Handled file operations including deletion of old files and renaming of the final output. - Included error handling for various stages of the process to ensure robustness.
145 lines
5.3 KiB
PHP
145 lines
5.3 KiB
PHP
<?php
|
|
include '../_plugins/medoo.php';
|
|
|
|
// Połączenie z bazą danych
|
|
$mdb = new medoo([
|
|
'database_type' => 'mysql',
|
|
'database_name' => 'admin_lulandia',
|
|
'server' => 'dedyk8.cyber-folks.pl',
|
|
'username' => 'admin_lulandia',
|
|
'password' => '6_atdQ-N#xlN-t#0',
|
|
'charset' => 'utf8'
|
|
]);
|
|
|
|
// Pobranie aktywnych producentów
|
|
$manufacturers = $mdb->select('materac_x13gpsr_responsible_manufacturer', '*', ['active' => 1]);
|
|
|
|
// Pobranie pliku XML
|
|
$url = "http://lulandia.pl/modules/pricewars2/service.php?id_xml=7";
|
|
$localFile = "ceneo_lulandia_feed_temp.xml";
|
|
unlink( $localFile ); // Usunięcie starego pliku, jeśli istnieje
|
|
unlink( "ceneo_lulandia_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.");
|
|
}
|
|
|
|
// 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
|
|
$responsibleProducers = new SimpleXMLElement('<responsibleProducers/>');
|
|
|
|
foreach ( $manufacturers as $manufacturer )
|
|
{
|
|
$producer = $responsibleProducers -> addChild( 'p' );
|
|
$producer -> addAttribute( 'id', $manufacturer['id_x13gpsr_responsible_manufacturer'] );
|
|
|
|
$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'] ?? '' ) );
|
|
|
|
$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) {
|
|
die("Nie udało się wczytać istniejącego pliku XML.");
|
|
}
|
|
|
|
// Dodanie sekcji <responsibleProducers> zaraz po otwarciu <offers>
|
|
$domExisting = dom_import_simplexml($existingXml)->ownerDocument;
|
|
$domResponsibleProducers = dom_import_simplexml($responsibleProducers)->ownerDocument->documentElement;
|
|
|
|
// Znalezienie elementu <offers>
|
|
$offersElement = $domExisting->getElementsByTagName('offers')->item(0);
|
|
if ($offersElement === null) {
|
|
die("Nie znaleziono elementu <offers> w pliku XML.");
|
|
}
|
|
|
|
// Wstawienie <responsibleProducers> zaraz po otwarciu <offers>
|
|
$offersElement->insertBefore(
|
|
$domExisting->importNode($domResponsibleProducers, true),
|
|
$offersElement->firstChild
|
|
);
|
|
|
|
// 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 <o>
|
|
foreach ($existingXml->xpath('//o') as $offer) {
|
|
$productId = (string) $offer['id'];
|
|
|
|
// Pobranie id_x13gpsr_responsible_manufacturer z tabeli materac_x13gpsr_responsible_manufacturer_product
|
|
$responsibleManufacturerId = $mdb->get(
|
|
'materac_x13gpsr_responsible_manufacturer_product',
|
|
'id_x13gpsr_responsible_manufacturer',
|
|
['id_product' => $productId]
|
|
);
|
|
|
|
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',
|
|
['id_brand' => $brandId]
|
|
);
|
|
}
|
|
}
|
|
|
|
// Dodanie elementu <a> z nazwą producenta odpowiedzialnego
|
|
if ($responsibleManufacturerId) {
|
|
$manufacturerName = $mdb->get(
|
|
'materac_x13gpsr_responsible_manufacturer',
|
|
'id_x13gpsr_responsible_manufacturer',
|
|
['id_x13gpsr_responsible_manufacturer' => $responsibleManufacturerId]
|
|
);
|
|
|
|
$responsibleElement = $offer->addChild('a', "<![CDATA[$manufacturerName]]>");
|
|
$responsibleElement->addAttribute('name', 'Producent odpowiedzialny');
|
|
}
|
|
}
|
|
|
|
// Zapisanie zmodyfikowanego pliku XML
|
|
if ($existingXml->asXML($localFile) === false) {
|
|
die("Nie udało się zapisać zmodyfikowanego pliku XML.");
|
|
}
|
|
|
|
echo "Plik XML został pomyślnie zaktualizowany.";
|
|
|
|
// remove _temp from filename
|
|
$finalFile = str_replace('_temp', '', $localFile);
|
|
if (rename($localFile, $finalFile)) {
|
|
echo "Plik XML został pomyślnie przeniesiony do docelowej lokalizacji.";
|
|
} else {
|
|
echo "Nie udało się przenieść pliku XML do docelowej lokalizacji.";
|
|
}
|
|
|
|
echo "<br>Plik XML został pomyślnie zaktualizowany. URL: " . $finalFile;
|
|
?>
|