<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED);
include '../_plugins/medoo.php';

class SimpleXMLElementExtended extends SimpleXMLElement
{
  private function addCDataToNode(SimpleXMLElement $node, $value = '')
  {
    if ($domElement = dom_import_simplexml($node))
    {
      $domOwner = $domElement->ownerDocument;
      $domElement->appendChild($domOwner->createCDATASection("{$value}"));
    }
  }

  public function addChildWithCData($name = '', $value = '')
  {
    $newChild = parent::addChild($name);
    if ($value) $this->addCDataToNode($newChild, "{$value}");
    return $newChild;
  }

  public function addCData($value = '')
  {
    $this->addCDataToNode($this, "{$value}");
  }
}

$mdb = new medoo([
  'database_type' => 'mysql',
  'database_name' => 'host117523_temp',
  'server' => 'localhost',
  'username' => 'host117523_temp',
  'password' => 'XAGwZ3XRa9fgtM63um7v',
  'charset' => 'utf8'
]);

$xml_file =  'dekodum-sk.xml';

// Jeśli plik XML już istnieje, wczytujemy go, inaczej tworzymy nowy
if ( file_exists( $xml_file ) )
{
  $xml_content = file_get_contents( $xml_file );
  $xml = new SimpleXMLElementExtended( $xml_content );
}
else
{
  $xml = new SimpleXMLElementExtended( '<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"/>' );
  $xml -> addChild( 'title', 'Dekodum' );
  $xml -> addChild( 'updated', date( 'Y-m-d' ) );
}

if ( $product = $mdb -> get( 'dekodum_xml', '*', [ 'AND' => [ 'updated_sk' => 1, 'xml' => 0 ] ] ) )
{
  $productXml = $xml->addChild('item');
  $productXml -> addChild( 'g:id', $product['product_id'], 'http://base.google.com/ns/1.0' );
  $productXml -> addChild( 'title', $product['title'] );
  $productXml -> addChild( 'link', $product['link'] );
  $productXml -> addChild( 'g:image_link', $product['image_link'], 'http://base.google.com/ns/1.0' );
  $productXml -> addChild( 'g:mpn', $product['mpn'], 'http://base.google.com/ns/1.0' );
  $productXml -> addChild( 'g:brand', $product['brand'], 'http://base.google.com/ns/1.0' );
  $productXml -> addChild( 'g:google_product_category', $product['google_product_category'], 'http://base.google.com/ns/1.0' );
  $productXml -> addChild( 'g:item_group_id', $product['item_group_id'], 'http://base.google.com/ns/1.0' );

  $mdb -> update( 'dekodum_xml', [ 'xml' => 1 ], [ 'product_id' => $product['product_id'] ] );

  if ( $xml -> asXML( $xml_file ) )
  {
    echo '<p>Wygenerowałem xml: https://cdn.projectpro.pl/dekodum.cz/' . $xml_file . '</p>';
  }

  if ( $mdb -> count( 'dekodum_xml', [ 'AND' => [ 'updated_sk' => 1, 'xml' => 0 ] ] ) )
  {
    echo '<p>Pozostało produktów do wygenerowania: ' . $mdb -> count( 'dekodum_xml', [ 'AND' => [ 'updated_sk' => 1, 'xml' => 0 ] ] ) . '</p>';
    // reload page after .1 second
    echo '<script>setTimeout(function(){location.reload();},100);</script>';
  }
  else
  {
    echo '<p>Wygenerowałem wszystkie produkty</p>';
  }
}
else {
  echo '<p>Brak produktów do wygenerowania</p>';
}