<?php
/**
 * @created 2007-08-08
 * @file export.php
 * @author dswistowski <dswistowski@firma.gery.pl>
 * 
 * @brief Plik generujący xml eksportujący dane do serwisu adcentral.pl
 */
 chdir('..');
 
 require('includes/application_top.php');
 $f = file_get_contents('http://www.adcentral.pl/confirm.php');
 if(!array_key_exists('code', $_REQUEST) || md5($_REQUEST['code'].'gyuw89etr7837rasfstr2')!=$f)
 	die();
 
 //Tak zaczynają się wszystkie strony oscommerce
 $navigation->remove_current_page();
 $connection = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die('Couldn\'t make connection.');
 $db = mysql_select_db(DB_DATABASE, $connection) or die(mysql_error());
 
 if ($_GET['language'] == '') {
    $lang_query = tep_db_query('select languages_id, code from ' . TABLE_LANGUAGES . ' where directory = \'' . $language . '\'');
 } else {
    $cur_language = tep_db_output($_GET['language']);
    $lang_query = tep_db_query('select languages_id, code from ' . TABLE_LANGUAGES . ' where code = \'' . $cur_language . '\'');
 }

 if (tep_db_num_rows($lang_query)) {
	  $lang_a = tep_db_fetch_array($lang_query);
      $lang_code = $lang_a['code'];
	  $lang_id = $lang_a['languages_id'];
}
 //lista produktów do eksportu
 $sql = "SELECT group_id as dzial_id, 
 				products_name as naglowek,
        		products_description as opis,       		        		        		        		        
        		products_price as cena,
        		'o' as forma_sprzedazy,
				's' as rodzaj,        		
        		products_date_added as data_dodania,
        		IFNULL(products_date_available, products_date_added + interval 1 year) as data_waznosci, 
        		products_quantity as ileSztuk,
        		products_description as szczegoly,
        		products_image as zdjecia
        
        FROM products p
        LEFT JOIN `".TABLE_PRODUCTS_DESCRIPTION."` pd
               ON pd.products_id = p.products_id
              AND pd.language_id = '$lang_id'      
        LEFT JOIN `".TABLE_PRODUCTS_TO_CATEGORIES."` p2c
               ON p2c.products_id=p.products_id
        JOIN `".TABLE_ADCENTRAL_CATEGORIES."` tac
        	   ON p2c.categories_id = tac.category_id	 		
        WHERE 
              p.products_status=1  
        GROUP BY p.products_id
        ORDER BY p.products_id DESC
         ";
  $sql_result = mysql_query($sql,$connection) or die("Couldn\'t execute query:<br />$sql <br/>".mysql_error());
  
  //zamiana na xml
  $doc = new DOMDocument('1.0');
  $doc->formatOutput = true;
  $root = $doc->createElement('ogloszenia');
  $doc->appendChild($root);
  while ($row = mysql_fetch_assoc($sql_result))
  { 
  	$ogloszenie = $doc->createElement('ogloszenie');
  	if($row['zdjecia'])
  	{
  		$zdjecie = HTTP_SERVER.'/'.DIR_WS_IMAGES.$row['zdjecia'];
  		unset($row['zdjecia']);
  		$zdjecia_node = $doc->createElement('zdjecia');
  		$ogloszenie->appendChild($zdjecia_node);
  		$item = $doc->createElement('item');
  		$item->appendChild($doc->createTextNode($zdjecie));
  		$zdjecia_node->appendChild($item);
  	}
  	foreach($row as $node_name=>$node_value)
  	{
  		$tmp_node = $doc->createElement($node_name);
  		$tmp_node->appendChild($doc->createTextNode($node_value));
  		$ogloszenie->appendChild($tmp_node); 
  	};
  	
  	$root->appendChild($ogloszenie);
  }
  //wyplucie wyniku
  
  echo $doc->saveXML();
?>

