<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
require_once '../_plugins/phpmailer/class.phpmailer.php';
require_once '../_plugins/phpmailer/class.smtp.php';
require_once '../_plugins/class.simplexml.php';
require_once '../settings.php';

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://abexil.pl/data/export/feed10010_117b39e9e5dafc04ae9d8cf4.xml" );
curl_setopt( $ch, CURLOPT_FAILONERROR, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
$xml = curl_exec( $ch );
curl_close( $ch );
$xml = simplexml_load_string( $xml, null , LIBXML_NOCDATA ) or die("Error: Cannot create object");

$json = json_encode( $xml );
$array = json_decode( $json, true );

$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', 'abexil' );
$xml -> addChild( 'updated', date( 'Y-m-d' ) );

foreach ( $array['products']['product'] as $product )
{
  foreach ( $product['sizes'] as $size )
  {
    $productXml = $xml -> addChild( 'entry' );
    $productXml -> addChild( 'g:id', $product['@attributes']['id'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:gtin', $size['@attributes']['code_producer'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:title', $product['description']['name'][0] . ' (' . $size['@attributes']['name'] . ')', 'http://base.google.com/ns/1.0' );

    $stock = false;
    foreach ( $size['stock'] as $stock )
      if ( $stock['@attributes']['stock_quantity'] > 0 )
        $stock = true;

    $productXml -> addChild( 'g:availability', $stock ? 'in_stock' : 'out_of_stock', 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:brand', $product['producer']['@attributes']['name'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:link', $product['card']['@attributes']['url'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:image_link', $product['images']['large']['image']['@attributes']['url'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:description', $product['description']['short_desc'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:product_type', $product['category_idosell']['@attributes']['path'], 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:condition', 'new', 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:price', $product['srp']['@attributes']['gross'] . ' PLN', 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:sale_price', $product['price']['@attributes']['gross'] . ' PLN', 'http://base.google.com/ns/1.0' );
    $productXml -> addChild( 'g:google_product_category', $product['category_idosell']['@attributes']['id'], 'http://base.google.com/ns/1.0' );
  }
}

$xml -> asXML( "abexil.xml");
$mail = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = $settings['email_host'];
$mail->Port = $settings['email_port'];
$mail->Username = $settings['email_login'];
$mail->Password = $settings['email_password'];
$mail->CharSet = "UTF-8";
$mail->SMTPOptions = array(
  'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
  )
);

$mail -> AddReplyTo( 'biuro@project-pro.pl' );
$mail -> SetFrom( 'www@cdn.projectpro.pl', 'www@cdn.projectpro.pl' );
$mail -> AddAddress( 'biuro@project-pro.pl', '') ;
$mail -> Subject = 'Abexil.pl - uruchomiono cron - ' . date(' Y-m-d H:i:s' );
$mail -> Body = '<p>Abexil.pl - uruchomiono cron - ' . date(' Y-m-d H:i:s' ) . '</p>';
$mail->IsHTML(true);
$mail->Send();
echo 'Abexil.pl - uruchomiono cron - ' . date(' Y-m-d H:i:s' );
?>
