<?php
require_once '../_plugins/medoo.php';
require_once '../settings.php';

$mdb = new medoo( [
  'database_type' => 'mysql',
  'database_name' => $settings['db']['database'],
  'server' => $settings['db']['host'] ,
  'username' => $settings['db']['user'],
  'password' => $settings['db']['password'],
  'charset' => 'utf8'
] );

$url = 'https://api.ekomi.de/v3/getProductfeedback?auth=154480|1f82bae5daf2ee2275540cef9&version=cust-1.0.0&type=csv&charset=utf-8&caching=none&fields=date,order_id,product_id,rating,feedback,firstname,lastname';

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);

$lines = explode("\n", $response);
foreach ( $lines as $line )
{
  $line_data = str_getcsv($line, ',');
  if ( is_array( $line_data ) )
  {
    if ( $mdb -> count( 'pakanaturypl', [ 'order_id' => $line_data[1] ] ) )
    {
      $mdb -> update( 'pakanaturypl', [
        'name' => $line_data[5] ? $line_data[5] : null,
        'surname' => $line_data[6] ? $line_data[6] : null
      ], [ 'order_id' => $line_data[1] ] );
      echo "Updated: {$line_data[1]}<br>";
    }
  }
}