<?
namespace admin\factory;

class Baselinker {

  // zapis wiązania produktów
  static public function bundling_products_save( $values ) {
    global $mdb;

    foreach ( $values as $key => $val ) {
      $key = explode( '_', $key );
      $mdb -> update( 'pp_shop_products', [ 'baselinker_product_id' => $val ], [ 'id' => $key[1] ] );
    }
  }

  // pobranie produktów z Baselinkera
  static public function products_list() {
    global $settings;

    $methodParams = '{
      "storage_id": "bl_1"
    }';

    $apiParams = [
      "token" => $settings['baselinker_api'],
      "method" => "getProductsList",
      "parameters" => $methodParams
    ];

    $curl = curl_init( "https://api.baselinker.com/connector.php" );
    curl_setopt( $curl, CURLOPT_POST, 1 );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, http_build_query( $apiParams ) );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    $response = json_decode( curl_exec( $curl ), true );

    if ( $response['status'] == 'SUCCESS' )
      return $response['products'];
  }
}