142 lines
5.1 KiB
PHP
142 lines
5.1 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.");
|
|
}
|
|
|
|
if (file_put_contents($localFile, $xmlContent) === false) {
|
|
die("Nie udało się zapisać pliku XML.");
|
|
}
|
|
|
|
// Tworzenie fragmentu responsibleProducers jako SimpleXMLElement
|
|
$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 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.");
|
|
}
|
|
|
|
// Import responsibleProducers do DOM
|
|
$importedProducers = $dom->importNode(dom_import_simplexml($responsibleProducers), true);
|
|
|
|
// Wstawienie <responsibleProducers> jako pierwszego dziecka <offers>
|
|
$offersList = $dom->getElementsByTagName('offers');
|
|
if ($offersList->length === 0) {
|
|
die("Nie znaleziono elementu <offers>.");
|
|
}
|
|
$offersElement = $offersList->item(0);
|
|
$offersElement->insertBefore($importedProducers, $offersElement->firstChild);
|
|
|
|
// Iteracja po <o>
|
|
$offers = $dom->getElementsByTagName('o');
|
|
foreach ($offers as $offer) {
|
|
$productId = $offer->getAttribute('id');
|
|
|
|
// Szukanie id producenta odpowiedzialnego
|
|
$responsibleManufacturerId = $mdb->get(
|
|
'materac_x13gpsr_responsible_manufacturer_product',
|
|
'id_x13gpsr_responsible_manufacturer',
|
|
['id_product' => $productId]
|
|
);
|
|
|
|
if (!$responsibleManufacturerId) {
|
|
$brandId = $mdb->get('materac_product', 'id_manufacturer', ['id_product' => $productId]);
|
|
if ($brandId) {
|
|
$responsibleManufacturerId = $mdb->get(
|
|
'materac_x13gpsr_responsible_manufacturer_brand',
|
|
'id_x13gpsr_responsible_manufacturer',
|
|
['id_brand' => $brandId]
|
|
);
|
|
}
|
|
}
|
|
|
|
if ($responsibleManufacturerId) {
|
|
$manufacturerName = $mdb->get(
|
|
'materac_x13gpsr_responsible_manufacturer',
|
|
'id_x13gpsr_responsible_manufacturer',
|
|
['id_x13gpsr_responsible_manufacturer' => $responsibleManufacturerId]
|
|
);
|
|
|
|
if ($manufacturerName) {
|
|
$a = $dom->createElement('a');
|
|
$a->setAttribute('name', 'Producent odpowiedzialny');
|
|
$cdata = $dom->createCDATASection($manufacturerName);
|
|
$a->appendChild($cdata);
|
|
|
|
$attrs = $offer->getElementsByTagName('attrs');
|
|
if ($attrs->length > 0) {
|
|
$attrsElement = $attrs->item(0);
|
|
} else {
|
|
$attrsElement = $dom->createElement('attrs');
|
|
$offer->appendChild($attrsElement);
|
|
}
|
|
$attrsElement->appendChild($a);
|
|
}
|
|
}
|
|
|
|
// 🔹 DODANE: Zmiana avail na 14 dla nazw zawierających "Janpol Fini"
|
|
$nameNodes = $offer->getElementsByTagName('name');
|
|
if ($nameNodes->length > 0) {
|
|
$nameText = $nameNodes->item(0)->textContent;
|
|
if (stripos($nameText, 'Janpol Fini') !== false or stripos( $nameText, 'Fini Janpol') !== false) {
|
|
$offer->setAttribute('avail', '14');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Zapisanie zmodyfikowanego pliku XML
|
|
if (!$dom->save($localFile)) {
|
|
die("Nie udało się zapisać zmodyfikowanego pliku XML.");
|
|
}
|
|
|
|
// Zmiana nazwy pliku
|
|
$finalFile = str_replace('_temp', '', $localFile);
|
|
if (rename($localFile, $finalFile)) {
|
|
echo "Plik XML został pomyślnie zaktualizowany i przeniesiony do: <br>$finalFile";
|
|
} else {
|
|
echo "Nie udało się przenieść pliku XML do docelowej lokalizacji.";
|
|
}
|
|
?>
|