<?php
class ControllerToolWebservice extends Controller
{

	var $_generaldata = array();
	protected $_name = 'webservice';

	public function index()
	{
/*
		$params = array(
			'user' => 'piyer',
			'password' => 'sonya2014',
			'limit' => '2',
			'page' => '1',
			'category_id' => 0,
			'brand_id' => '',
		);

		$url = HTTPS_SERVER . 'index.php?route=tool/webservice/getproducts';
		$curl = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_RETURNTRANSFER => 1,
			CURLOPT_URL => $url,
			CURLOPT_USERAGENT => 'Codular Sample cURL Request',
			CURLOPT_POST => 1,
			CURLOPT_POSTFIELDS => $params
		));

		$resp = curl_exec($curl);
		curl_close($curl);

		var_dump($resp); die();

        */
        
        /*
        $dom = new DOMDocument( '1.0', 'iso-8859-1' );
			
				$list_of_teams = $dom->createElement( 'items' );
				$dom->appendChild( $list_of_teams );
				foreach ($results as $result) {
					$dom->createElement( 'item' );
					$team = $result[0];
					$list_of_teams->appendChild( $team );
					foreach ($result as $k => $v) {
						$v = [0];
						$k = [1];
						$team->appendChild( $dom->createElement( $k ) );
						$name = '';
						$name->appendChild( $dom->createTextNode( $v ) );
					}
				}
				
				$xml_result = $dom->saveXML();
				$team_xml = new DOMDocument();
				$team_xml->loadXML( utf8_encode( $xml_result ) );
				echo header( 'Content-type: text/xml' );
                echo $team_xml->saveXML();
		 */

	}

	public function getproducts()
	{
		$json = array();
		if ($this->request->post) {
			if (!$this->serviceKontrol($this->request->post)) {
				$json['products'] = $this->printGetProducts($this->request->post);
			} else {
				$json['error'] = $this->serviceKontrol($this->request->post);
			}
		} else {
			$json['error'] = '101';
		}


		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	private function serviceKontrol($post)
	{
		$error = '';

		if (!isset($post['user']) and !$post['user']) {
			$error = '98';
		}

		if (!isset($post['password']) and !$post['password']) {
			$error = '99';
		}

		if ($post['user'] != 'piyer' and $post['password'] != 'sonya2014') {
			$error = '100';
		}


		return $error;
	}

	private function printGetProducts($data)
	{
		if ($this->request->server['HTTPS']) {
			$config_url = $this->config->get('config_ssl') . 'image/';
		} else {
			$config_url = $this->config->get('config_url') . 'image/';
		}

		if(isset($data['start']) AND $data['start']){
			$page = $data['start'] ;
		}else{
			$page = 1;
		}

		if(isset($data['limit']) AND $data['limit']){
			$limit = $limit['limit'] ;
		}else{
			$limit = 100;
		}

		if($limit > 1000){
			$limit = 500;
		}

		$page = 1 ;
		$limit = 100;
		$this->load->model('catalog/product');
		$this->load->model('tool/webservice');
		$product_filter = array(
			'filter_category_id' => '',
			'brand' => '',
			'start' => ($page - 1) * $limit,
			'limit' => $limit,
		);
		$products = $this->model_catalog_product->getProducts($product_filter);
		$product_data = array();
		foreach ($products as $key => $product) {
			
			$price = $product['price'];
			$special = $product['special'] ? $product['special'] : null;

			if($product['tax_class_id'] == 1){
				$tax = '18';
			}else if($product['tax_class_id'] == 2){
				$tax = '8';
			}else{
				$tax = null;
			}

			if($product['currency_id'] == 1){
				$currency = 'TRY';
			}else if($product['currency_id'] == 2){
				$currency = 'USD';
			}else if($product['currency_id'] == 3){
				$currency = 'EUR';
			}else{
				$currency = null;
			}

			$images = $this->model_catalog_product->getProductImages($product['product_id']);
			$product_images = array();
			if($product['image']){
				$product_images[] = $config_url . $product['image'];
			}
			foreach ($images as $key => $image) {
				$product_images[] = $config_url . $image['image'];
			}

			$product_description_data = $this->model_tool_webservice->getProductDescriptions($product['product_id']);
			$product_attribute_data = $this->model_tool_webservice->getProductAttributes($product['product_id']);
			$product_option_data = $this->model_tool_webservice->getProductOptions($product['product_id']);
			//$categories = $this->model_tool_webservice->getProductCategories($product['product_id']);

			$product_data[] = array(
				'prodct_id' => $product['product_id'],
				'model' => $product['model'],
				'price' => $price,
				'special' => $special,
				'quantity' => $product['quantity'],
				'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']),
				'tax' => $tax,
				'currency' => $currency,
				'brand' => $product['manufacturer'],
				'images' => $product_images,
				'description' => $product_description_data,
				'attribute' => $product_attribute_data,
				'option' => $product_option_data,
				//'categories' => $categories,
				
			);
		}
		return $product_data;
	}

	public function errordescriptions()
	{
		// 101 Post değeri bulunamadı
		// 100 Şifre veya Kullanıcı adı yanlış
		// 99 Şifre alanı gönderilmedi
		// 98 Kullanıcı adı gönderilmedi
	}

}

?>
