ver. 0.318: shopPRO export produktów + nowe API endpoints

- NEW: IntegrationsRepository::shopproExportProduct() — eksport produktu do
  zdalnej instancji shopPRO (pola główne, tłumaczenia, custom fields, zdjęcia)
- NEW: sendImageToShopproApi() — wysyłka zdjęć przez API shopPRO (base64 POST)
- REFACTOR: shopproImportProduct() — wydzielono shopproDb() i
  missingShopproSetting(); dodano security_information, producer_id,
  custom fields, alt zdjęcia
- NEW: AttributeRepository::ensureAttributeForApi() i
  ensureAttributeValueForApi() — idempotent find-or-create dla słowników
- NEW: API POST dictionaries/ensure_attribute — utwórz lub znajdź atrybut
- NEW: API POST dictionaries/ensure_attribute_value — utwórz lub znajdź wartość
- NEW: API POST products/upload_image — przyjmuje base64, zapisuje plik i DB
- NEW: IntegrationsController::shoppro_product_export() — akcja admina
- NEW: przycisk "Eksportuj do shopPRO" w liście produktów
- NEW: pole API key w ustawieniach integracji shopPRO

Tests: 765 tests, 2153 assertions — all green

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 11:43:17 +01:00
parent 9a351c16ee
commit 702e3a94be
16 changed files with 656 additions and 14 deletions

View File

@@ -668,15 +668,12 @@ class IntegrationsRepository
public function shopproImportProduct( int $productId ): array
{
$settings = $this->getSettings( 'shoppro' );
$missingSetting = $this->missingShopproSetting( $settings, [ 'domain', 'db_name', 'db_host', 'db_user' ] );
if ( $missingSetting !== null ) {
return [ 'success' => false, 'message' => 'Brakuje konfiguracji shopPRO: ' . $missingSetting . '.' ];
}
$mdb2 = new \medoo( [
'database_type' => 'mysql',
'database_name' => $settings['db_name'],
'server' => $settings['db_host'],
'username' => $settings['db_user'],
'password' => $settings['db_password'],
'charset' => 'utf8'
] );
$mdb2 = $this->shopproDb( $settings );
$product = $mdb2->get( 'pp_shop_products', '*', [ 'id' => $productId ] );
if ( !$product )
@@ -700,6 +697,7 @@ class IntegrationsRepository
'additional_message_text' => $product['additional_message_text'],
'additional_message_required'=> $product['additional_message_required'],
'weight' => $product['weight'],
'producer_id' => $product['producer_id'] ?? null,
] );
$newProductId = $this->db->id();
@@ -729,6 +727,20 @@ class IntegrationsRepository
'warehouse_message_nonzero'=> $lang['warehouse_message_nonzero'],
'canonical' => $lang['canonical'],
'xml_name' => $lang['xml_name'],
'security_information' => $lang['security_information'] ?? null,
] );
}
}
// Import custom fields
$customFields = $mdb2->select( 'pp_shop_products_custom_fields', '*', [ 'id_product' => $productId ] );
if ( is_array( $customFields ) ) {
foreach ( $customFields as $field ) {
$this->db->insert( 'pp_shop_products_custom_fields', [
'id_product' => $newProductId,
'name' => (string)($field['name'] ?? ''),
'type' => (string)($field['type'] ?? 'text'),
'is_required' => !empty( $field['is_required'] ) ? 1 : 0,
] );
}
}
@@ -759,6 +771,7 @@ class IntegrationsRepository
$this->db->insert( 'pp_shop_products_images', [
'product_id' => $newProductId,
'src' => '/upload/product_images/product_' . $newProductId . '/' . $imageName,
'alt' => $image['alt'] ?? '',
'o' => $image['o'],
] );
}
@@ -766,4 +779,215 @@ class IntegrationsRepository
return [ 'success' => true, 'message' => 'Produkt został zaimportowany.' ];
}
// ── ShopPRO export ──────────────────────────────────────────
public function shopproExportProduct( int $productId ): array
{
$settings = $this->getSettings( 'shoppro' );
$missingSetting = $this->missingShopproSetting( $settings, [ 'db_name', 'db_host', 'db_user' ] );
if ( $missingSetting !== null ) {
return [ 'success' => false, 'message' => 'Brakuje konfiguracji shopPRO: ' . $missingSetting . '.' ];
}
$product = $this->db->get( 'pp_shop_products', '*', [ 'id' => $productId ] );
if ( !$product ) {
return [ 'success' => false, 'message' => 'Nie znaleziono produktu do eksportu.' ];
}
$mdb2 = $this->shopproDb( $settings );
$mdb2->insert( 'pp_shop_products', [
'price_netto' => $product['price_netto'] ?? null,
'price_brutto' => $product['price_brutto'] ?? null,
'vat' => $product['vat'] ?? null,
'stock_0_buy' => $product['stock_0_buy'] ?? 0,
'quantity' => $product['quantity'] ?? 0,
'wp' => $product['wp'] ?? null,
'sku' => $product['sku'] ?? '',
'ean' => $product['ean'] ?? '',
'custom_label_0' => $product['custom_label_0'] ?? null,
'custom_label_1' => $product['custom_label_1'] ?? null,
'custom_label_2' => $product['custom_label_2'] ?? null,
'custom_label_3' => $product['custom_label_3'] ?? null,
'custom_label_4' => $product['custom_label_4'] ?? null,
'additional_message' => $product['additional_message'] ?? 0,
'additional_message_text' => $product['additional_message_text'] ?? null,
'additional_message_required'=> $product['additional_message_required'] ?? 0,
'weight' => $product['weight'] ?? null,
'producer_id' => $product['producer_id'] ?? null,
] );
$newProductId = (int) $mdb2->id();
if ( $newProductId <= 0 ) {
return [ 'success' => false, 'message' => 'Podczas eksportowania produktu wystąpił błąd.' ];
}
$languages = $this->db->select( 'pp_shop_products_langs', '*', [ 'product_id' => $productId ] );
if ( is_array( $languages ) ) {
foreach ( $languages as $lang ) {
$mdb2->insert( 'pp_shop_products_langs', [
'product_id' => $newProductId,
'lang_id' => $lang['lang_id'] ?? '',
'name' => $lang['name'] ?? '',
'short_description' => $lang['short_description'] ?? null,
'description' => $lang['description'] ?? null,
'tab_name_1' => $lang['tab_name_1'] ?? null,
'tab_description_1' => $lang['tab_description_1'] ?? null,
'tab_name_2' => $lang['tab_name_2'] ?? null,
'tab_description_2' => $lang['tab_description_2'] ?? null,
'meta_title' => $lang['meta_title'] ?? null,
'meta_description' => $lang['meta_description'] ?? null,
'meta_keywords' => $lang['meta_keywords'] ?? null,
'seo_link' => $lang['seo_link'] ?? null,
'copy_from' => $lang['copy_from'] ?? null,
'warehouse_message_zero' => $lang['warehouse_message_zero'] ?? null,
'warehouse_message_nonzero'=> $lang['warehouse_message_nonzero'] ?? null,
'canonical' => $lang['canonical'] ?? null,
'xml_name' => $lang['xml_name'] ?? null,
'security_information' => $lang['security_information'] ?? null,
] );
}
}
$customFields = $this->db->select( 'pp_shop_products_custom_fields', '*', [ 'id_product' => $productId ] );
if ( is_array( $customFields ) ) {
foreach ( $customFields as $field ) {
$mdb2->insert( 'pp_shop_products_custom_fields', [
'id_product' => $newProductId,
'name' => (string)($field['name'] ?? ''),
'type' => (string)($field['type'] ?? 'text'),
'is_required' => !empty( $field['is_required'] ) ? 1 : 0,
] );
}
}
$images = $this->db->select( 'pp_shop_products_images', '*', [ 'product_id' => $productId ] );
if ( is_array( $images ) && count( $images ) > 0 ) {
$missingImageApiSetting = $this->missingShopproSetting( $settings, [ 'domain', 'api_key' ] );
if ( $missingImageApiSetting !== null ) {
return [ 'success' => false, 'message' => 'Brakuje konfiguracji shopPRO dla wysylki zdjec: ' . $missingImageApiSetting . '.' ];
}
}
if ( is_array( $images ) ) {
foreach ( $images as $image ) {
$remoteImageSrc = $this->sendImageToShopproApi(
(string)($image['src'] ?? ''),
(int)$newProductId,
(string)($settings['domain'] ?? ''),
(string)($settings['api_key'] ?? ''),
(string)($image['alt'] ?? ''),
(int)($image['o'] ?? 0)
);
if ( $remoteImageSrc === '' ) {
return [ 'success' => false, 'message' => 'Nie udalo sie wyslac zdjec produktu przez API shopPRO.' ];
}
}
}
return [
'success' => true,
'message' => 'Produkt został wyeksportowany (ID: ' . $newProductId . ').',
];
}
private function missingShopproSetting( array $settings, array $requiredKeys ): ?string
{
foreach ( $requiredKeys as $requiredKey ) {
if ( trim( (string)($settings[$requiredKey] ?? '') ) === '' ) {
return $requiredKey;
}
}
return null;
}
private function shopproDb( array $settings ): \medoo
{
return new \medoo( [
'database_type' => 'mysql',
'database_name' => $settings['db_name'],
'server' => $settings['db_host'],
'username' => $settings['db_user'],
'password' => $settings['db_password'] ?? '',
'charset' => 'utf8'
] );
}
private function sendImageToShopproApi(
string $src,
int $remoteProductId,
string $remoteDomain,
string $apiKey,
string $alt,
int $position
): string
{
$src = trim( $src );
if ( $src === '' ) {
return '';
}
$localSourcePath = '..' . $src;
if ( !is_file( $localSourcePath ) ) {
return '';
}
$content = @file_get_contents( $localSourcePath );
if ( $content === false ) {
return '';
}
$remoteDomain = trim( $remoteDomain );
if ( $remoteDomain === '' ) {
return '';
}
if ( strpos( $remoteDomain, 'http://' ) !== 0 && strpos( $remoteDomain, 'https://' ) !== 0 ) {
$remoteDomain = 'https://' . $remoteDomain;
}
$remoteDomain = rtrim( $remoteDomain, '/' );
$url = $remoteDomain . '/api.php?endpoint=products&action=upload_image';
$payload = [
'id' => $remoteProductId,
'file_name' => basename( $src ),
'content_base64' => base64_encode( $content ),
'alt' => $alt,
'o' => $position,
];
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $payload, JSON_UNESCAPED_UNICODE ) );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json',
'X-Api-Key: ' . $apiKey,
] );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
$response = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
curl_close( $ch );
return '';
}
$httpCode = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
if ( $httpCode >= 400 || $response === false ) {
return '';
}
$responseData = json_decode( (string) $response, true );
if ( !is_array( $responseData ) || ( $responseData['status'] ?? '' ) !== 'ok' ) {
return '';
}
return (string)($responseData['data']['src'] ?? '');
}
}