feat: Add custom_label_4 handling in API and update documentation for new endpoints

This commit is contained in:
2026-03-11 00:11:24 +01:00
parent ab1f682806
commit cad3a20bb5
4 changed files with 138 additions and 19 deletions

59
api.php
View File

@@ -58,7 +58,7 @@ function api_validate_api_key( $mdb )
function api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id )
{
return $mdb -> query(
'SELECT p.id, p.name, p.title, p.google_product_category
'SELECT p.id, p.name, p.title, p.google_product_category, p.custom_label_4
FROM products p
JOIN clients cl ON p.client_id = cl.id
WHERE p.offer_id = :offer_id
@@ -203,26 +203,67 @@ if ( \S::get( 'action' ) == 'product_custom_label_4_set' )
{
api_validate_api_key( $mdb );
$offer_id = trim( \S::get( 'offer_id' ) );
$client_id_param = trim( \S::get( 'client_id' ) );
$custom_label_4 = trim( \S::get( 'custom_label_4' ) );
$offer_id = trim( (string) \S::get( 'offer_id' ) );
$client_id_param = (int) \S::get( 'client_id' );
$custom_label_4 = trim( (string) ( \S::get( 'custom_label_4' ) ?? \S::get( 'value' ) ?? '' ) );
if ( !$offer_id || !$client_id_param )
if ( $offer_id === '' || $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, (int) $client_id_param );
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id_param );
if ( !$product )
{
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
\factory\Products::set_product_data( $product['id'], 'custom_label_4', $custom_label_4 );
\factory\Products::add_product_comment( $product['id'], 'Zmiana etykiety 4 na: ' . $custom_label_4 . ' (API)' );
$update_result = \factory\Products::set_product_data( (int) $product['id'], 'custom_label_4', $custom_label_4 );
api_json_response( [ 'result' => 'ok' ] );
if ( !$update_result )
{
api_json_response( [ 'result' => 'error', 'message' => 'Failed to update custom_label_4' ], 500 );
}
\factory\Products::add_product_comment( (int) $product['id'], 'Zmiana etykiety 4 na: ' . ( $custom_label_4 !== '' ? $custom_label_4 : '(puste)' ) . ' (API)' );
api_json_response( [
'result' => 'ok',
'product_id' => (int) $product['id'],
'offer_id' => $offer_id,
'client_id' => $client_id_param,
'custom_label_4' => $custom_label_4
] );
}
// Odczyt custom_label_4 dla produktu przez API
if ( \S::get( 'action' ) == 'product_custom_label_4_get' )
{
api_validate_api_key( $mdb );
$offer_id = trim( (string) \S::get( 'offer_id' ) );
$client_id_param = (int) \S::get( 'client_id' );
if ( $offer_id === '' || $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id_param );
if ( !$product )
{
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
api_json_response( [
'result' => 'ok',
'product_id' => (int) $product['id'],
'offer_id' => $offer_id,
'client_id' => $client_id_param,
'custom_label_4' => trim( (string) ( $product['custom_label_4'] ?? '' ) )
] );
}
// Zmiana tytulu produktu przez API