<?php
        require_once("../../config/connect.php");
    
    $sqlabp = "SELECT DISTINCT bon_id,woo_order_id from abgeschlossener_bon;";
	
	$Q1abp = mysql_query($sqlabp);
	if (!$Q1abp) echo "Fehler " . mysql_error();
	else {
		while ($abp = mysql_fetch_assoc($Q1abp)) {

			$bon_id_shopify = $abp['woo_order_id'];
			
			$note = SHOPIFY_FILIALE . "." . FILIALSYSTEM_KASSA_NAME . "." . $abp['bon_id'];
			
			$data['order']['id'] = $bon_id_shopify;
			$data['order']['note'] = $note;
			
			$items_string = json_encode($data);
			echo "<pre>";
			print_r($data);
			echo "</pre>";
			$data_retval = put_table_api_shopify_url('orders/'.$bon_id_shopify.'.json',$items_string);
			
			echo "<pre>";
			print_r($data_retval);
			echo "</pre>";
		}
	}

        

function put_table_api_shopify_url($resource, $items_string = null)
{
	//$token ='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';
	//$url =  'https://markta-demo.myshopify.com/admin/api/2023-04/';
	$token = FILIALSYSTEM_TOKEN;
	//$url =  'https://markta-demo.myshopify.com/admin/api/2023-04/';
	$url = FILIALSYSTEM_URL_ZENTRALSYSTEM;


	$ch = curl_init();


	$url .= $resource;

	echo $url;
	
	curl_setopt($ch, CURLOPT_URL, $url);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	
	if (!is_null($items_string))
		curl_setopt($ch, CURLOPT_POSTFIELDS, $items_string);

	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		"Content-Type: application/json",
		"X-Shopify-Access-Token: $token"
		
	));

	$response = curl_exec($ch);
	////log_to_file($response);
	if ($response === false) {
		//log_to_file('Curl-Fehler: ' . curl_error($ch) . '<br>');
		echo 'Curl-Fehler: ' . curl_error($ch) . '<br>';
		return (false);
	}
	curl_close($ch);

	//var_dump($response);
	$resArr = json_decode($response, true);
	return ($resArr);
}

function get_table_api_shopify_url($strurl,$object_shopify)
	{
		
			

		$token = FILIALSYSTEM_TOKEN;
		$url = FILIALSYSTEM_URL_ZENTRALSYSTEM;
		$nextPage = NULL;
		$url .= $strurl;
		$curl = curl_init();
echo "<br>".$url."<br>";
		// set result limit and Basic auth

		curl_setopt_array(
		$curl,
		array(
			CURLOPT_URL => $url."?limit=250",
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => "",
			CURLOPT_CUSTOMREQUEST => "GET",
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0,
			CURLOPT_HTTPHEADER => array(
					"Content-Type: application/json",
					"X-Shopify-Access-Token: $token"
				)
		)
		);

		// call back function to parse Headers and get Next Page Link
		curl_setopt(
		$curl,
		CURLOPT_HEADERFUNCTION,
		function($curl, $header) use (&$nextPage) {
			$len = strlen($header);
			$header = explode(':', $header, 2);

			if (count($header) < 2) // ignore invalid headers
			return $len;

			if (trim($header[0]) === "Link" && strpos($header[1], 'next') !== false) {
			$links = explode(',', $header[1], 2);

			$link = count($links) === 2 ? $links[1] : $links[0];
			if (preg_match('/<(.*?)>/', $link, $match) === 1) $nextPage = $match[1];
			}

			return $len;
		}
		);

		// First request

		$response = curl_exec($curl);
		//$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
				
		//$header = substr($response_shopify, 0, $header_size);
		//$response = substr($response_shopify, $header_size);

		if (curl_errno($curl)) {
		$error_msg = curl_error($curl);
		print_r($error_msg);
		}
		$parsedResponse =json_decode($response, true);

		$result = null;
		
		//var_dump($parsedResponse);
		$result = get_object_shopify($parsedResponse, $object_shopify);
		

		// generate new requests till next page is available

		while ($nextPage !== NULL) {
		curl_setopt($curl, CURLOPT_URL, $nextPage);

		$result = get_object_shopify($parsedResponse, $object_shopify);

		//$parsedResponse->$object_shopify = [];
		$nextPage = NULL;

		$response = curl_exec($curl);
		$parsedResponse = json_decode($response, true);

		if (curl_errno($curl)) {
			$error_msg = curl_error($curl);
		} else {

			$result = get_object_shopify($parsedResponse, $object_shopify);

			//$result = array_merge($result, $parsedResponse->$object_shopify);
			sleep(2);
		}
};

curl_close($curl);
return  $result;
}

function get_object_shopify($parsedResponse,$object_shopify)
{
	switch ($object_shopify) {
		case "products":
			$result = $parsedResponse['products'];
			break;
		case "collects":
			$result = $parsedResponse['collects'];
			break;
		case "collection":
			$result = $parsedResponse['collection'];
			break;
		case "variants":
			$result = $parsedResponse['variants'];
			break;
		case "metafields":
			$result = $parsedResponse['metafields'];
			break;
		case "order":
			$result = $parsedResponse['order'];
			break;
		case "fulfillment_orders":
			$result = $parsedResponse['fulfillment_orders'];
			break;
		case "fulfillment":
			$result = $parsedResponse['fulfillment'];
			break;
			
			
		default:
			$result =  $parsedResponse;
		
	}

	return $result;
}

function post_table_api_shopify_url($resource, $items_string = null)
{
	//$token ='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';
	//$url =  'https://markta-demo.myshopify.com/admin/api/2023-04/';
	$token = FILIALSYSTEM_TOKEN;
	//$url =  'https://markta-demo.myshopify.com/admin/api/2023-04/';
	$url = FILIALSYSTEM_URL_ZENTRALSYSTEM;


	$ch = curl_init();


	$url .= $resource;

	echo $url;
	
	curl_setopt($ch, CURLOPT_URL, $url);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_POST, TRUE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

	if (!is_null($items_string))
		curl_setopt($ch, CURLOPT_POSTFIELDS, $items_string);

	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		"Content-Type: application/json",
		"X-Shopify-Access-Token: $token"
	));

	$response = curl_exec($ch);
	////log_to_file($response);
	if ($response === false) {
		//log_to_file('Curl-Fehler: ' . curl_error($ch) . '<br>');
		echo 'Curl-Fehler: ' . curl_error($ch) . '<br>';
		return (false);
	}
	curl_close($ch);

	//var_dump($response);
	$resArr = json_decode($response, true);
	return ($resArr);
}
?>