<?php 
class ControllerModuleGiftTeaser extends Controller  {

	public function index() { 
		$this->load->model('module/giftTeaser');
		$setting = $this->model_module_giftTeaser->getSetting('giftTeaser', $this->config->get('store_id'));
		if($setting['giftTeaser']['Enabled'] && $setting['giftTeaser']['Enabled'] == 'yes' ) {
			$this->load->model('tool/image');
			$this->document->addStyle('catalog/view/theme/'.$this->config->get('config_template').'/stylesheet/giftTeaser.css');
			$this->data['cart_total'] = $this->cart->getTotal();	
			$this->data['data'] = $setting['giftTeaser'];
			$gifts = $this->model_module_giftTeaser->getCurrentGifts();	
			foreach ($gifts as $key => $gift) {
				$gifts[$key]['description'] = $this->buildDescription($gift);	
				$gifts[$key]['image'] = $this->model_tool_image->resize($gift['image'], $setting['giftTeaser']['giftImageWidth'], $setting['giftTeaser']['giftImageHeight']);
			}
			$this->data['gifts']= $gifts;
			$this->template = $this->config->get('config_template') . '/template/module/giftTeaser/giftTeaser.tpl';
			$this->response->setOutput($this->render());
		}
	}
	
	public function refresh_gifts() {
		$this->cart->restartGifts();
	}

	private function buildDescription($gift = array()) {
		$this->load->language('module/giftTeaser');
		$properties = unserialize($gift['condition_properties']);
		$descriptions =unserialize(base64_decode($gift['description']));
		$description = html_entity_decode($descriptions['desc_' . $this->config->get('config_language')]);
		return $description;
	}
	
	public function emptyExcludedGifts(){
		$this->load->model('module/giftTeaser');	
		unset($this->session->data['excludedGifts']);
		$this->model_module_giftTeaser->updateGiftsInCart();			
	}
	
	private function getCatalogURL() {
        if (isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) {
            $storeURL = HTTP_SERVER;
        } else {
            $storeURL = HTTPS_SERVER;
        } 
        return $storeURL;
    }
}
?>