<?php

	/**
	 * Ermöglicht den Bestellablauf auf einer Seite
	 * @author daniel
	 */
	class wpsg_mod_onepagecheckout extends wpsg_mod_basic
	{
		
		var $lizenz = 1;
		var $id = 1700; 
		var $hilfeURL = 'http://wpshopgermany.maennchen1.de/?p=3419'; 
		var $version = "9.9.9";
		var $free = true;
		
		/*
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('EinSeitenCheckout', 'wpsg');
			$this->group = __('Bestellung', 'wpsg');
			$this->desc = __('Ermöglicht den Bestellablauf auf einer Seite', 'wpsg');
									
		} // public function __construct()
		
		public function settings_edit()
		{
			 
			global $wpdb;
			
			$pages = get_pages();
			
			$arPages = array(
				'-1' => __('Neu anlegen und zuordnen', 'wpsg')
			);
			
			foreach ($pages as $k => $v)
			{
				$arPages[$v->ID] = $v->post_title.' (ID:'.$v->ID.')';
			}
			
			$this->shop->view['arPages'] = $arPages;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_onepagecheckout/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->createPage(__('Kasse', 'wpsg'), 'wpsg_mod_onepagecheckout_page', $_REQUEST['wpsg_mod_onepagecheckout_page']);
						
		} // public function settings_save()
			 
		public function content_filter(&$content)
		{
				
			if (get_the_id() <= 0) return;
				
			if (get_the_id() == $this->shop->get_option('wpsg_mod_onepagecheckout_page'))
			{
				
				$content = $this->onepage();
				
				return -2;
				
			}
			
		} // public function content_filter(&$content)
		
		public function template_redirect() 
		{
			
			if (get_the_id() <= 0) return;
				
			if (get_the_id() == $this->shop->get_option('wpsg_mod_onepagecheckout_page') && isset($_REQUEST['wpsg_checkout']))
			{
				
				// Warenkorb wurde abgeschickt
				$this->shop->basket->initFromSession();
				
				// Überhaupt Produkte drin?
				$arProductIDs = $this->shop->basket->getProductIDs();					
				if (!wpsg_isSizedArray($arProductIDs)) { $this->shop->redirect($this->shop->getUrl(wpsg_ShopController::URL_BASKET)); }
				 
				// Modulprüfungen
				$bError = true; $this->shop->callMods('checkBasket', array(&$bError));				
				if ($bError === false) { $this->shop->redirect($this->shop->getUrl(wpsg_ShopController::URL_BASKET)); }
				
			}
			
		}
		
		/* Modulfunktionen */ 
		
		public function onepage()
		{

			$this->shop->basket->initFromSession();
								
			$this->shop->view['pflicht'] = $this->shop->loadPflichtFeldDaten();			
			$this->shop->view['laender'] = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_LAND."` ORDER BY `name` ASC");			
			$this->shop->view['basket'] = $this->shop->basket->toArray();			
			if (wpsg_isSizedArray($_SESSION['wpsg']['errorFields'])) $this->shop->view['error'] = $_SESSION['wpsg']['errorFields'];
									
			$this->shop->checkShippingAvailable();
			$this->shop->checkPaymentAvailable();
			
			$this->shop->view['arShipping'] = $this->shop->arShipping;
			$this->shop->view['arPayment'] = $this->shop->arPayment;
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_onepagecheckout/onepage.phtml', false);
			
		} // public function onepage()
		
	} // class wpsg_mod_onepagecheckout extends wpsg_mod_basic

?>