<?php
/**
 * sample for metod:
 * GET /product-categories/
 * 
 * @category NOD WebShop API Library
 * @package Samples
 * @copyright Copyright (c) Zitec COM
 * 
 * */

require '../../bootstrap.php';

if(file_exists('../../config.php'))
{
	require_once('../../config.php');
	require_once(DIR_SYSTEM . 'startup.php');
}

//------------------------------------------------ connection with the database -------------------------------------------------
//$conn = mysql_connect('localhost', 'root', '') or die('Connection to MySql failed!');
$conn = @mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('Connection to MySql failed!');
//$db = mysql_select_db('smartpc', $conn) or die('Database not found!');
$db = mysql_select_db(DB_DATABASE, $conn) or die('Database not found!');
//-------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------- define tables --------------------------------------------------------------
$table_setting               			= DB_PREFIX.'setting';
$table_currency               			= DB_PREFIX.'currency';
$table_language              			= DB_PREFIX.'language';
$table_category              			= DB_PREFIX.'category';
$table_category_description  			= DB_PREFIX.'category_description';
$table_product_to_category	 			= DB_PREFIX.'product_to_category';
$table_manufacturer			 			= DB_PREFIX.'manufacturer';
$table_product				 			= DB_PREFIX.'product';
$table_product_description	 			= DB_PREFIX.'product_description';
$table_tax_rate				 			= DB_PREFIX.'tax_rate';
$table_product_image 		 			= DB_PREFIX.'product_image';
$table_product_special		 			= DB_PREFIX.'product_special';
$table_option_value_description			= DB_PREFIX.'option_value_description';
$table_product_option_value				= DB_PREFIX.'product_option_value';

$tabel_product_attribute				= DB_PREFIX.'product_attribute';
$tabel_attribute						= DB_PREFIX.'attribute';
$tabel_attribute_description			= DB_PREFIX.'attribute_description';
$tabel_attribute_group					= DB_PREFIX.'attribute_group';
$tabel_attribute_group_description		= DB_PREFIX.'attribute_group_description';

//-------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------- extract language ID ------------------------------------------------------------
$sql =  "
		SELECT	language_id 
		FROM 	$table_language
		WHERE 	code = (
							SELECT 	value 
							FROM 	$table_setting 
							WHERE 	$table_setting.key='config_language'
							)
		";
$res = mysql_query($sql);
$default_Language_ID = mysql_result($res, 0);
//-------------------------------------------------------------------------------------------------------------------------------

//--------------------------------------------- extract categories from db ------------------------------------------------------
$sql =  "
		SELECT 		$table_category.category_id, $table_category.parent_id, $table_category_description.name
		FROM 		$table_category 
		LEFT JOIN 	$table_category_description ON ($table_category .category_id = $table_category_description.category_id) 
		WHERE 		language_id = $default_Language_ID
		";
$res = mysql_query($sql);

while ($field = mysql_fetch_assoc($res) ){
	$category_Array[$field['category_id']] = $field;
	//print_r($category_Array);echo "<br><br>";
}

//-------------------------------------------------------------------------------------------------------------------------------


$method = new Nod_Api_Methods_ProductCategories(API_USER, API_KEY, RESPONSE_MODE);

$body =  $method->getAll();

echo '<h1>Status code</h1>';
echo $method->getStatusCode();
echo '<h1>Response</h1>';
//echo $method->getBody();
$categorii = json_decode($body,true);
/*$product_categories = $categorii['product_categories'];
foreach ($product_categories as $category) {
	$apicategory_Array[$category['id']] = $category;
	if (is_array($category)) {
		foreach ($category as $category1) {
			if (is_array($category1)){
				foreach($category1 as $category2){
					$apicategory_Array[$category2['id']] = $category2;
					if(is_array($category2)) {
						foreach($category2 as $category3) {
							if (is_array($category3)) {
								foreach($category3 as $category4){
									$apicategory_Array[$category4['id']] = $category4;
								}
							}
						}
					}
				}
			}
		} 
	}
}

foreach ($apicategory_Array as $category_Array) {

	unset ($category_Array['children']);
	$newapicategory_Array[$category_Array['id']] = $category_Array;
}
*/
print_r($categorii);
	
?>


