<?php
/**
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John T. Daly <jd@ta-edv.de>
* 
* Easyway Shop is a web e-commerce system
*/

include_once './core/itemhelper.class.php';
include_once './core/item.class.php';

class admin_item_actions {
	
	private $base_object;
	private $layout_object;
	private $db;
	
	function __construct($base_object, $layout_object) {
		$this->layout_object = $layout_object;
		$this->base_object = $base_object;
		$this->db = $base_object->db;
	}
		
	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} elseif (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}
		
		if ($action == "get_variant_price") {
			$this->get_variant_price();
		} elseif ($action == "ping") {
			$this->ping();
		} elseif ($action == "get_google_categories") {
			$this->get_google_categories($_POST['parent_id']);
		} elseif ($action == "update_image") {
			$this->update_image();
		} elseif ($action == "update_accessory_ordering") {
			$this->update_accessory_ordering();
		}
	}
	
	protected function get_attribute($name) {
		if (isset($_POST[$name])) {
			return $_POST[$name];
		} elseif (isset($_GET[$name])) {
			return $_GET[$name];
		}
		
		return false;
	}
	
	protected function update_accessory_ordering() {
		$id = $this->get_attribute('item_id');
		$accessories = $this->get_attribute('accessories');
		
		if ($id && $accessories) {
			$position = $accessories['position'];
			$item_id = $accessories['item_id'];
			$len = count($position);
			
			//$this->db->autocommit(false);
			
			for ($i = 0; $i < $len; $i++) {
				$sql = 'UPDATE
							item_item_assign
						SET
							position=' . $this->db->real_escape_string($position[$i]) . '
						WHERE
							item_parent_id=' . $this->db->real_escape_string($id) . '
						AND item_child_id=' . $this->db->real_escape_string($item_id[$i]);
				
				echo $sql;
				echo "<br />";
				//$this->db->query($sql);
			}
			exit();
			//$result = $this->db->commit();
			//$this->db->autocommit(true);
		}
		
		echo "ok no accessories or no itemmnd?";
		
		exit();
	}
	
	private function update_image() {
		if (isset($_POST['image_id'])) {
			$image_id = $_POST['image_id'];
		} else {
			$image_id = false;
		}
		
		if (isset($_POST['key'])) {
			$key = $_POST['key'];
		} else {
			$key = false;
		}
		
		if (isset($_POST['value'])) {
			$value = $_POST['value'];
		} else {
			$value = false;
		}
		
		if ($image_id && $key && $value) {
			if ($key == 'variant') {
				$value = explode('_', $value);
				
				$sql = "UPDATE item_files
						SET attribut1='" . $this->db->real_escape_string($value[0]) . "'
						, attribut2='" . $this->db->real_escape_string($value[1]) . "'
						WHERE id=" . $this->db->real_escape_string($image_id);
			} else {
				$sql = 'UPDATE item_files
						SET ' . $this->db->real_escape_string($key) . "='" . $this->db->real_escape_string($value) . "'
						WHERE id=" . $this->db->real_escape_string($image_id);
			}
			
			if ($this->db->query($sql)) {
				$return_data = array('status' => 'success');
			} else {
				$return_data = array('status' => 'error');
			}
		} else {
			$return_data = array('status' => 'error');
		}
		
		echo json_encode($return_data);
		exit();
	}

	private function get_variant_price() {
		if (isset($_GET['id'])) {
			$item_id = $_GET['id'];
		} else {
			$item_id = false;
		}
		
		if (isset($_GET['variant1'])) {
			$variant1 = $_GET['variant1'];
		} else {
			$variant1 = false;
		}
		
		if (isset($_GET['variant2'])) {
			$variant2 = $_GET['variant2'];
		} else {
			$variant2 = false;
		}
		
		if (isset($_GET['group'])) {
			$group_id = $_GET['group'];
		} else {
			$group_id = false;
		}
		
		if ($item_id && $variant1 && $variant2 && $group_id) {
			$taxrate = ItemHelper::get_item_taxrate($item_id);
			if (!$taxrate) {
				// TODO: is there a default taxrate somewhere?
				$taxrate = 19;
			}
			$result = ItemHelper::get_item_variant_prices($item_id, $group_id, $taxrate);

			if ($result) {
				if ($variant2 == 'null') {
					$variant2 = '';
				}
				
				$return_data = $result[$variant1][$variant2];
				$return = array('status' => 'success', 'data' => $return_data);
			} else {
				$return = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'missing argument');
		}
		
		echo json_encode($return);
		exit();
	}
	
	private function ping() {
		$data = $_GET;
		
		unset($data['admin_modul']);
		unset($data['action']);
		
		foreach ($data as $key => $value) {
			echo $key . ' : ' . $value . '<br />';
		}
		
		exit();
	}
	
	
	private function get_google_categories($parent_id) {
		$item_object = new Item($this->base_object);
		$google_item_categories = $item_object->get_all_google_item_categories($parent_id);
		
		echo json_encode($google_item_categories);
		
		exit();
	}
}