<?php

	/**
	 * Modul um Anfragen an Produkt zu stellen
	 * @author Daschmi (daschmi@daschmi.de)
	 */
	class wpsg_mod_request extends wpsg_mod_basic
	{
		
		var $lizenz = 1;
		var $id = 3150;
		var $hilfeURL = 'http://wpshopgermany.maennchen1.de/?p=1281055';
		var $version = "9.9.9";
		var $free = false;
		
		const TYP_NO = 0;
		const TYP_YES = 1;
		const TYP_YESWITHBASKET = 2;
		
		const ACTION_STAYONPAGE = 0;
		const ACTION_GOTOREQUESTPAGE = 1;
		const ACTION_SHOWREQUESTPAGELB = 2;
		
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('AnfrageProdukte', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Ermöglicht es für bestimmte Produkte Anfragen an den Shopbetreiber zu stellen.', 'wpsg');
			
		} // public function __construct()
		
		public function install()
		{
			
			$this->shop->checkDefault('wpsg_mod_request_afterinsert', wpsg_mod_request::ACTION_GOTOREQUESTPAGE);
			
			$this->shop->checkDefault('wpsg_mod_request_requestpageimage', '1');
			$this->shop->checkDefault('wpsg_mod_request_customerdata', '1');
			
		} // public function install()
		
		public function settings_edit()
		{

			$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['pages'] = $arPages;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
									
			$this->shop->createPage(__('Anfrage', 'wpsg'), 'wpsg_mod_request_page', $_REQUEST['wpsg_mod_request_page']);
			$this->shop->createPage(__('Vielen Dank für Ihre Anfrage', 'wpsg'), 'wpsg_mod_request_redirect', $_REQUEST['wpsg_mod_request_redirect']);
			
			$this->shop->update_option('wpsg_mod_request_afterinsert', $_REQUEST['wpsg_mod_request_afterinsert']);
			$this->shop->update_option('wpsg_mod_request_customerdata', $_REQUEST['wpsg_mod_request_customerdata']);
			
			if ($this->shop->hasMod('wpsg_mod_ordervars')) $this->shop->update_option('wpsg_mod_request_ordervars', $_REQUEST['wpsg_mod_request_ordervars']);
			if ($this->shop->hasMod('wpsg_mod_productvars')) $this->shop->update_option('wpsg_mod_request_productvars', $_REQUEST['wpsg_mod_request_productvars']);
			
		} // public function settings_save()
		
		public function template_redirect() 
		{ 
			
			if (isset($_REQUEST['wpsg']['submit_request']) && wpsg_isSizedInt($_REQUEST['wpsg']['menge']))
			{
 
				// Variantenmodulschlüssel generieren
				$this->shop->callMod('wpsg_mod_varianten', 'basket_preInsert');
				
				$this->addProductToRequestlist($_REQUEST['wpsg']['produkt_id'], $_REQUEST['wpsg']['menge']);
				
				if ($this->shop->get_option('wpsg_mod_request_afterinsert') == wpsg_mod_request::ACTION_GOTOREQUESTPAGE)
				{
					
					$this->shop->redirect($this->getRequestPageUrl());
					
				}
				else if ($this->shop->get_option('wpsg_mod_request_afterinsert') == wpsg_mod_request::ACTION_SHOWREQUESTPAGELB)
				{
					
					die('<div class="requestlist_ajax_wrap">'.$this->getRequestListHTML().'</div>');
					
				}
				else 
				{
					
					$this->shop->redirect($_REQUEST['myReferer']);
					
				}
				
			}
			else if (isset($_REQUEST['submit_requestpage_refresh']))
			{
				
				$this->refreshRequestList();
				
				$this->shop->addFrontendMessage(__('Anfrageliste wurde aktualisiert.', 'wpsg'));
				
				$this->shop->redirect($this->getRequestPageUrl());
				
			}
			else if (isset($_REQUEST['submit_requestpage']))
			{
				
				// Anfrageformular wurde abgeschickt
				$this->refreshRequestList();
				
				$bOK = $this->checkRequestList();
				
				if ($bOK === true)
				{
					
					$this->doRequest();
					
					if ($this->shop->get_option('wpsg_mod_request_page') === $this->shop->get_option('wpsg_mod_request_redirect')) $this->shop->addFrontendMessage(__('Vielen Dank für Ihre Anfrage.', 'wpsg'));
						
					if (wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_redirect')))
					{
						
						$this->shop->redirect(get_permalink($this->shop->get_option('wpsg_mod_request_redirect')));
						
					}
					
				}
				
				$this->shop->redirect($this->getRequestPageUrl());
				
			}
			else if (wpsg_isSizedString($_REQUEST['wpsg_mod'], 'wpsg_mod_request'))
			{
				
				if ($_REQUEST['wpsg_action'] == 'removeProduct')
				{
					
					$bOK = $this->removeProductFromList($_REQUEST['product_index']);
					
					if ($bOK === true) $this->shop->addFrontendMessage(__('Produkt erfolgreich aus Anfrageliste entfernt.', 'wpsg'));
					else $this->shop->addFrontendError(__('Produkt konnte nicht aus Anfrageliste entfernt werden.', 'wpsg'));
					
					$this->shop->redirect($this->getRequestPageUrl());
					
				}
				
			}
			
		} // public function template_redirect()
		
		public function content_filter(&$content)
		{
			
			$page_id = get_the_ID();
			
			if (wpsg_isSizedInt($page_id) && $page_id == $this->shop->get_option('wpsg_mod_request_page'))
			{
				 
				$content = $this->getRequestListHTML();
				
				return -2;
				
			}
			
		} // public function content_filter(&$content)
		
		public function produkt_edit_allgemein(&$produkt_data) 
		{
		 
			// In der Übersetzung nicht anzeigen oder beim ersten anlegen
			if (wpsg_isSizedString($_REQUEST['wpsg_lang']) || !wpsg_isSizedInt($_REQUEST['edit_id'])) return;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/produkt_edit_allgemein.phtml');
				
		} // public function produkt_edit_allgemein(&$produkt_data)
						
		public function produkt_save(&$product_id)
		{
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			$oProduct->setMeta('wpsg_mod_request_set', $_REQUEST['wpsg_mod_request_set']);
			
		} // public function produkt_save(&$produkt_id)
		
		public function product_bottom(&$product_id, $template_index)
		{
				 
			if (!$this->isRequestProduct($product_id)) return;
			
			$this->shop->view['wpsg_mod_request']['template_index'] = $template_index;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/product_bottom.phtml');
				
		} // public function product_bottom(&$produkt_id, $template_index)
		
		public function admin_presentation()
		{
		
			if ($this->shop->hasMod('wpsg_mod_produktbilder'))
			{
			
				echo wpsg_drawForm_Checkbox('wpsg_mod_request_requestpageimage', __('Produktbild auf Anfrageseite anzeigen', 'wpsg'), $this->shop->get_option('wpsg_mod_request_requestpageimage'));
				
			}
			 	
		} // public function admin_presentation()
		
		public function admin_presentation_submit()
		{
			
			if ($this->shop->hasMod('wpsg_mod_produktbilder'))
			{
				
				$this->shop->update_option('wpsg_mod_request_requestpageimage', $_REQUEST['wpsg_mod_request_requestpageimage']);

			}
				
		} // public function admin_presentation_submit()
		
		public function admin_emailconf()
		{
			
			echo wpsg_drawEMailConfig(
				'requestmailadmin',
				__('Mail an Shopbetreiber bei neuer Anfrage', 'wpsg'),
				__('E-Mail die bei einer neuen Anfrage an den Shopbetreiber gesendet wird.', 'wpsg'),
				true);
			
			echo wpsg_drawEMailConfig(
				'requestmailcustomer',
				__('PDFdownload Cron E-Mail', 'wpsg'),
				__('E-Mail die bei einer neuen Anfrage an den Kunden gesendet wird. (Bestätigung der Anfrage).', 'wpsg'));
		  
		} // public function admin_emailconf()
		
		public function admin_emailconf_save()
		{
		
			wpsg_saveEMailConfig("requestmailadmin");
			wpsg_saveEMailConfig("requestmailcustomer");
			 
		} // public function admin_emailconf_save()
		
		/* Modulfunktionen */
		
		public function getRequestListHTML()
		{
			
			$this->shop->view['requestlist'] = $this->getRequestlistData();
			$this->shop->view['pflicht'] = $this->shop->loadPflichtFeldDaten();
			$this->shop->view['countries'] = wpsg_country::find();
			
			if (sizeof($this->shop->view['requestlist']['tax']) > 1 || $this->shop->get_option('wpsg_showMwstAlways') == '1') $this->shop->view['addColspan'] = 1;
			else $this->shop->view['addColspan'] = 0;
			
			if ($this->shop->hasMod('wpsg_mod_ordervars') && wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_ordervars'))) $this->shop->view['orderVars'] = $this->shop->callMod('wpsg_mod_ordervars', 'getOrderVars');
			
			$content = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/requestpage.phtml', false);
			
			return $content;
			
		} // public function getRequestListHTML()
		
		/**
		 * Gibt die URL der Anfrageseite zurück
		 */
		public function getRequestPageUrl()
		{
			
			return get_permalink($this->shop->get_option('wpsg_mod_request_page'));
			
		} // public function getRequestPageUrl()
				
		/**
		 * Gibt die URL zum entfernen eines Produktes aus der Anfrageliste zurück
		 * @param Integer $product_index
		 */
		public function getRemoveLinkURL($product_index)
		{
			
			$remove_url = $this->getRequestPageUrl();
			
			if (strpos($remove_url, '?') === false) $remove_url .= '?wpsg_mod=wpsg_mod_request&wpsg_action=removeProduct&product_index='.$product_index;
			else $remove_url .= '&wpsg_mod=wpsg_mod_request&wpsg_action=removeProduct&product_index='.$product_index;
			
			return $remove_url;
			
		} // public function getRemoveLinkURL($product_index)
		
		/**
		 * Entfernt ein Produkt anhand des Produktindexes aus der Anfrageliste
		 * @param Integer $product_index
		 */
		public function removeProductFromList($product_index)
		{
			
			if (wpsg_isSizedArray($_SESSION['wpsg']['wpsg_mod_request']['products']) && array_key_exists($product_index, $_SESSION['wpsg']['wpsg_mod_request']['products']))
			{
				
				unset($_SESSION['wpsg']['wpsg_mod_request']['products'][$product_index]);
				
				return true;
				
			}
			else
			{
				
				return false;
				
			}
			
		} // public function removeProductFromList($product_index)
		
		/**
		 * Gibt die Produkt der Anfrageliste als Array zurück
		 */
		public function getRequestlistData()
		{
			
			$arReturn = array(
				'products' => array(),
				'tax' => array(),
				'customer' => array(),
				'sum' => array(
					'price' => 0,
					'price_netto' => 0,
					'price_brutto' => 0,
					'weight' => 0							
				),
				'product_count' => 0,
				'ov' => array()
			);
						
			foreach ((array)$_SESSION['wpsg']['wpsg_mod_request']['products'] as $ses_key => $ses_product)
			{
			 
				$arProductReturn = $ses_product;
				$arProductReturn['product_data'] = $this->shop->loadProduktArray($ses_product['product_id'], array('id' => $ses_product['product_key']));
				$arProductReturn['product_index'] = $ses_key;
								
				// Die Preise hole ich aus den Produktdaten
				$arProductReturn['price_netto'] = $arProductReturn['product_data']['preis_netto'];
				$arProductReturn['price_brutto'] = $arProductReturn['product_data']['preis_brutto'];
				
				// Angezeigter Preis
				if ($this->shop->getFrontendTaxView() === WPSG_NETTO) $arProductReturn['price'] = $arProductReturn['price_netto'];
				else $arProductReturn['price'] = $arProductReturn['price_brutto'];
								
				wpsg_addSet($arReturn['sum']['price_netto'], $arProductReturn['price_netto'] * $arProductReturn['amount']);
				wpsg_addSet($arReturn['sum']['price_brutto'], $arProductReturn['price_brutto'] * $arProductReturn['amount']);
				wpsg_addSet($arReturn['sum']['price'], $arProductReturn['price'] * $arProductReturn['amount']);
				
				// MwSt Sätze
				$tax_country = $this->shop->getDefaultCountry();
				$tax_key = $arProductReturn['product_data']['mwst_key'];
				$tax_country_key = $tax_key.'_'.$tax_country->id;			
				
				if (array_key_exists($tax_country_key, $arReturn['tax']))
				{
					
					$arReturn['tax'][$tax_country_key]['tax_base'] += $arProductReturn['price_brutto'];
					$arReturn['tax'][$tax_country_key]['tax_base'] += ($arProductReturn['price_brutto'] - $arProductReturn['price_netto']) * $arProductReturn['amount'];
					
				}
				else 
				{

					$name = wpsg_ff($tax_country->getTax($tax_key), '%');
					if ($tax_country->id != $this->shop->getDefaultCountry(true)) $name .= ' / '.$tax_country->name;
					
					$arReturn['tax'][$tax_country_key] = array(
						'name' => $name,
						'tax_key' => $tax_key,
						'tax_country' => $tax_country->id,
						'tax_value' => $tax_country->getTax($tax_key),
						'tax_base' => $arProductReturn['price_brutto'] * $arProductReturn['amount'],
						'tax_calc_value' => ($arProductReturn['price_brutto'] - $arProductReturn['price_netto']) * $arProductReturn['amount']
					);
															
				}
				
				// Eventuelle Zusatzzeilen zählen für Ausgabe rowspan	
				$arProductReturn['addRowspan'] = 0;				
				if ($this->shop->hasMod('wpsg_mod_varianten') && $this->shop->callMod('wpsg_mod_varianten', 'isVariantsProductKey', array($ses_product['product_key'])))
				{
					
					$arProductReturn['variantenInfo'] = $this->shop->callMod('wpsg_mod_varianten', 'getVariantenInfoArray', array($ses_product['product_key']));
					
					$arProductReturn['addRowspan'] ++;
									
				}
				
				if ($this->shop->hasMod('wpsg_mod_weight') && wpsg_isSizedInt($this->shop->get_option('wpsg_mod_weight_showRequestPageProduct')))
				{
					
					$arReturn['sum']['weight'] += $arProductReturn['product_data']['weight'] * $arProductReturn['amount'];
					
					$arProductReturn['addRowspan'] ++;
					
				}
				
				if ($this->shop->hasMod('wpsg_mod_productvars') && wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_productvars')))
				{
					
					$arProductReturn['product_data']['pv'] = $this->shop->callMod('wpsg_mod_productvars', 'getAllProductVars', array($arProductReturn['product_id']));
					
					$arProductReturn['addRowspan'] += sizeof($arProductReturn['product_data']['pv']);
					
				}
				
				
				$arReturn['products'][] = $arProductReturn;
				$arReturn['product_count'] += 1;
				
			}
			
			// Kundendaten
			if (wpsg_isSizedArray($_SESSION['wpsg']['wpsg_mod_request']['customer']))
			{
				
				$arReturn['customer'] = $_SESSION['wpsg']['wpsg_mod_request']['customer'];
				
			}
			
			// Bestelldaten
			if ($this->shop->hasMod('wpsg_mod_ordervars') && wpsg_isSizedArray($_SESSION['wpsg']['wpsg_mod_request']['ov']))
			{
				
				$arReturn['ov'] = $_SESSION['wpsg']['wpsg_mod_request']['ov'];
				
			}
						
			return $arReturn;
			
		} // public function getRequestlistData()
		
		/**
		 * Überprüft die Anfrageliste
		 */
		public function checkRequestList()
		{
			
			// Daten der Anfrageliste
			$requestlist = $this->getRequestlistData();
			
			// Pflichfelder
			$required_fields = $this->shop->loadPflichtFeldDaten();
			
			// OK Ja/Nein
			$bOK = true;
			
			// Sind Produkte drin?
			if (!wpsg_isSizedInt($requestlist['product_count']))
			{
				
				$this->shop->addFrontendError(__('Keine Produkte in der Anfrageliste!', 'wpsg'));
				$bOK = false;
				
			}
			
			// E-Mail Validierung korrekt
			if ($required_fields['emailconfirm'] == '1' && $requestlist['customer']['email'] != $requestlist['customer']['email2'])
			{
				
				$this->shop->addFrontendError(__('Bitte überprüfen Sie die Eingaben in der E-Mail Bestätigung!', 'wpsg'));
				$bOK = false;
				
			}
			
			foreach ($requestlist['customer'] as $k => $v)
			{
				
				if (in_array($k, array("firma", "vname", "name", "fax", "tel", "strasse", "plz", "ort", "ustidnr")))				
				{
					
					if (array_key_exists($k, $required_fields) && $required_fields[$k] != 1 && !wpsg_isSizedString($v))
					{
						
						$bOK = false;
						
						switch ($k)
						{
							
							case 'firma'; $this->shop->addFrontendError(__('Bitte im Feld "Firma" eine Angabe machen!'. 'wpsg')); break;
							case 'vname': $this->shop->addFrontendError(__('Bitte im Feld "Vorname" eine Angabe machen!', 'wpsg')); break;
							case 'name': $this->shop->addFrontendError(__('Bitte im Feld "Name" eine Angabe machen!', 'wpsg')); break; 
							case 'fax': $this->shop->addFrontendError(__('Bitte die Eingaben im Feld "Fax" überprüfen!', 'wpsg')); break;
							case 'tel': $this->shop->addFrontendError(__('Bitte die Eingabe im Feld "Telefonnummer" überprüfen!', 'wpsg')); break;
							case 'strasse': $this->shop->addFrontendError(__('Bitte die Eingaben im Feld "Straße" überprüfen!', 'wpsg')); break;
							case 'plz': $this->shop->addFrontendError(__('Bitte die Eingaben im Feld "PLZ" überprüfen!', 'wpsg')); break;
							case 'ort': $this->shop->addFrontendError(__('Bitte die Eingaben im Feld "Ort" überprüfen!', 'wpsg')); break;
							case 'ustidnr': $this->shop->addFrontendError(__('Bitte die Eingaben im Feld "UStIdNr." prüfen!', 'wpsg')); break;
							
						}
						
					}
					
				}
				//else if (array_key_exists($k, $required_fields) && $required_fields[$k] != 1 && $k === "geb" && !wspg_isValidGeb($v))
				if (
						(wspg_isValidGeb($v) && wpsg_isSizedString($v)) ||
						!array_key_exists($k, $required_fields)
				)
				{
					
					$bOK = false; $this->shop->addFrontendError(__('Bitte die Eingabe im Feld "Geburtsdatum" überprüfen!', 'wpsg'));
					
				}
				else if (array_key_exists($k, $required_fields) && $required_fields[$k] != 1 && $k === "email" && !wpsg_isValidEMail($v))
				{
					
					$bOK = false; $this->shop->addFrontendError(__('Bitte die Eingabe der E-Mail Adresse überprüfen!', 'wspg'));
					
				}
				else if (array_key_exists($k, $required_fields) && $required_fields[$k] != 1 && $k === "land" && !wpsg_isSizedInt($v))
				{
					
					$bOK = false; $this->shop->addFrontendError(__('Bitte ein Land auswählen!', 'wpsg'));
					
				}
				else if (array_key_exists($k, $required_fields) && $required_fields[$k] != 1 && $k === "title" && $v === "-1")
				{
					
					$bOK = false; $this->shop->addFrontendError('Bitte im Feld "Anrede" eine Angabe machen!', 'wpsg');
					
				}
				else if ($k === "custom")
				{
					
					if (wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_customerdata')))
					{
					
						foreach ($v as $custom_key => $custom)
						{
							
							if ($required_fields['custom'][$custom_key]['show'] <= 0)
							{
								
								// Benutzerdefiniertes Feld ist ein Pflichtfeld
								if ($required_fields['custom'][$custom_key]['typ'] == 0 && !wpsg_isSizedString($custom))
								{
									
									// Textfeld ist leer
									$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte machen Sie in Feld "#1#" eine Angabe!', 'wpsg'), __($required_fields['custom'][$custom_key]['name'], 'wpsg')));
																			
								}
								else if ($required_fields['custom'][$custom_key]['typ'] == 1)
								{
									
									// Select
									$possible_select = explode('|', $required_fields['custom'][$custom_key]['auswahl']);
									
									if (!in_array($custom, $possible_select)) 
									{
										
										$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte eine Auswahl im Feld "#1#" treffen!', 'wpsg'), __($required_fields['custom'][$custom_key]['name'], 'wpsg')));
										
									}
									
								}
								else if ($required_fields['custom'][$custom_key]['typ'] == 2 && !wpsg_isSizedInt($custom))
								{
									
									// Checkbox nicht aktiv
									$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte "#1#" akzeptieren!', 'wpsg'), __($required_fields['custom'][$custom_key]['name'], 'wpsg')));
									
								}
								
							}
							
						}
						
					}
					
				}
					
			}
			
			// Bestellvariablen
			if ($this->shop->hasMod('wpsg_mod_ordervars') && wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_ordervars')))
			{
				
				$ov_db = $this->shop->callMod('wpsg_mod_ordervars', 'getOrderVars');
				
				foreach ((array)$requestlist['ov'] as $ov_id => $ov_value)
				{
					
					if (wpsg_isSizedInt($ov_db[$ov_id]['pflicht']))
					{
						
						if ($ov_db[$ov_id]['typ'] === '1') // Auswahl
						{
							
							// Select
							$possible_select = explode('|', $ov_db[$ov_id]['auswahl']);
							
							if (!in_array($ov_value, $possible_select))
							{
									
								$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte eine Auswahl im Feld "#1#" treffen!', 'wpsg'), $ov_db[$ov_id]['name']));
									
							}
							
						}
						else if ($ov_db[$ov_id]['typ'] === '2' && !wpsg_isSizedString($ov_value)) // Textfeld
						{
							
							$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte machen Sie in Feld "#1#" eine Angabe!', 'wpsg'), $ov_db[$ov_id]['name']));
							
						}
						else if ($ov_db[$ov_id]['typ'] === '3' && !wpsg_isSizedInt($ov_value)) // Checkbox
						{
							
							$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte "#1#" akzeptieren!', 'wpsg'), $ov_db[$ov_id]['name']));
							
						}
						
					}
					
				}
				
			}
			
			// Produktvariablen
			if ($this->shop->hasMod('wpsg_mod_productvars') && wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_productvars')))
			{
				
				foreach ($requestlist['products'] as $request_product)
				{
					
					$pv_active = $this->shop->callMod('wpsg_mod_productvars', 'getAllProductVars', array($request_product['product_id']));
					
					foreach ($pv_active as $pv)
					{
						
						if ($pv['typ'] === '1') 
						{
							
							// Auswahl
							$possible_select = explode('|', $pv['auswahl']);
							
							if (!in_array(wpsg_getStr($request_product['pv'][$pv['id']]), $possible_select))
							{
								
								$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte eine Auswahl im Feld "#1#" in Produkt "#2#" treffen!', 'wpsg'), $pv['name'], $request_product['product_data']['name']));
								
							}
							
						}
						else if ($pv['typ'] === '2' && !wpsg_isSizedString($request_product['pv'][$pv['id']]))
						{
							
							// Kein Text
							$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte machen Sie eine Angabe im Feld "#1#" in Produkt "#2#"!', 'wpsg'), $pv['name'], $request_product['product_data']['name']));
							
						}
						else if ($pv['typ'] === '3' && !wpsg_isSizedInt($request_product['pv'][$pv['id']]))
						{
							
							// Checkbox nicht aktiviert
							$bOK = false; $this->shop->addFrontendError(wpsg_translate(__('Bitte akzeptieren Sie "#1#" in Produkt "#2#"!', 'wpsg'), $pv['name'], $request_product['product_data']['name']));
							
						}
						
					}
					
				}
				 
			}
			
			return $bOK;
			 			
		} // public function checkRequestList()
		
		/**
		 * Aktualisiert die Anfrageliste mit den Daten aus dem Request
		 */
		public function refreshRequestList()
		{
		 
			// Keine Tags und damit keine XSS Atacken
			$_REQUEST['wpsg']['request'] = wpsg_xss($_REQUEST['wpsg']['request']);
			
			// Anzahl an Produkten
			foreach ((array)$_REQUEST['wpsg']['amount'] as $ses_index => $amount)
			{
				
				if (array_key_exists($ses_index, $_SESSION['wpsg']['wpsg_mod_request']['products']))
				{
				
					$product_id = $_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_index]['product_id'];
					$oProduct = wpsg_product::getInstance($product_id);
					
					if (wpsg_isSizedInt($amount))
					{
					
						// 0 = Nur einmal mit beliebiger Menge
						// 1 = Mehrfach mit beliebiger Menge
						// 2 = Mehrfach mit Menge1
						// 4 = Nur einmal mit Menge 1 
						
						if (in_array($oProduct->basket_multiple, array(0, 1)))
						{
							
							$_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_index]['amount'] = $amount;
							
						}
						else if ($oProduct->basket_multiple == 4)
						{
							
							$_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_index]['amount'] = 1;
							
						}
						else if ($oProduct->basket_multiple == 2 && $amount > 1)
						{
							
							for ($i = 1; $i < $amount; $i ++)
							{
								
								$this->addProductToRequestlist($_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_index]['product_key'], 1);
								
							}
							
						}
						
					}
					else 
					{
						
						unset($_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_index]);
						
					}

				}
				
			}
			
			// Kundendaten
			if (wpsg_isSizedArray($_REQUEST['wpsg']['request']['customer'])) 
			{
				 
				$_SESSION['wpsg']['wpsg_mod_request']['customer'] = $_REQUEST['wpsg']['request']['customer'];
				
			}
			
			// Bestellvariablen
			if ($this->shop->hasMod('wpsg_mod_ordervars'))
			{
				
				$_SESSION['wpsg']['wpsg_mod_request']['ov'] = $_REQUEST['wpsg']['request']['ov'];
				
			}
			
			// Produktvariablen
			if ($this->shop->hasMod('wpsg_mod_productvars'))
			{
				
				foreach ((array)$_REQUEST['wpsg']['request']['pv'] as $ses_index => $pv)
				{
					
					if (array_key_exists($ses_index, $_SESSION['wpsg']['wpsg_mod_request']['products']))
					{
												
						$_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_index]['pv'] = $pv;
						
					}
					
				}
				
			}
						
		} // public function refreshRequestList()
		
		/**
		 * Fügt ein Produkt in die Session für die Produktliste ein
		 * @param String $product_key Der Key des Produkts
		 * @param Integer $amount Menge
		 */
		public function addProductToRequestlist($product_key, $amount)
		{
			
			$product_id = $this->shop->getProduktId($product_key);
			$oProduct = wpsg_product::getInstance($product_id);
			 
			if (!$this->isRequestProduct($product_id)) return;
			 				
			// In Session eintragen
			if (!wpsg_isSizedArray($_SESSION['wpsg']['wpsg_mod_request']['products'])) $_SESSION['wpsg']['wpsg_mod_request']['products'] = array();
			
			$bDrin = false;
			foreach ($_SESSION['wpsg']['wpsg_mod_request']['products'] as $ses_key => $ses_product)
			{
												
				if ($ses_product['product_key'] == $product_key && $product_id == $ses_product['product_id'] && !wpsg_isSizedInt($oProduct->basket_multiple))
				{
			
					$_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_key]['amount'] += $amount;
					$_SESSION['wpsg']['wpsg_mod_request']['products'][$ses_key]['referer'] = $_REQUEST['myReferer'];
					return;
						
				}
					
			}
			
			if ($bDrin === false)
			{
					
				$_SESSION['wpsg']['wpsg_mod_request']['products'][] = array(
					'product_id' => $product_id,
					'product_key' => $product_key,					
					'referer' => $_REQUEST['myReferer'],
					'amount' => $amount	
				);
									
			}
			
		} // public function addProductToRequestlist($product_key, $amount)
		
		/**
		 * Gibt true oder false zurück, jenachdem ob das Produkt auf die Merkliste gesetzt werden kann
		 * @param int $product_id
		 */
		public function isRequestProduct($product_id)
		{
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			$wpsg_mod_request_set = $oProduct->getMeta('wpsg_mod_request_set');
			
			if (is_numeric($wpsg_mod_request_set) && in_array($wpsg_mod_request_set, array(wpsg_mod_request::TYP_YES, wpsg_mod_request::TYP_YESWITHBASKET))) return true;
			else return false;
			
		} // public function isRequestProduct($product_id)
		
		/**
		 * Gibt true oder false zurück ob das Produkt käuflich erworben werden kann.
		 * Für die Anzeige des Buttons "In den Warenkorb" im Produkttemplate
		 * @param int $product_id
		 */
		public function productForSale($product_key)
		{
			
			$product_id = $this->shop->getProduktID($product_key);
			
			$oProduct = wpsg_product::getInstance($product_id);
			
			$wpsg_mod_request_set = $oProduct->getMeta('wpsg_mod_request_set');
			
			if (!is_numeric($wpsg_mod_request_set) || in_array($wpsg_mod_request_set, array(wpsg_mod_request::TYP_NO, wpsg_mod_request::TYP_YESWITHBASKET))) return true;
			else return -2;
			
		} // public function productForSale($product_id)
		
		/**
		 * Führt die Anfrage aus und sendet die Mails an den Shop Betreiber und Kunden
		 */
		public function doRequest()
		{
			
			$this->shop->view['pflicht'] = $this->shop->loadPflichtFeldDaten();
			$this->shop->view['requestlist'] = $this->getRequestlistData();
			if ($this->shop->hasMod('wpsg_mod_ordervars') && wpsg_isSizedInt($this->shop->get_option('wpsg_mod_request_ordervars'))) $this->shop->view['orderVars'] = $this->shop->callMod('wpsg_mod_ordervars', 'getOrderVars');
			
			if (sizeof($this->shop->view['requestlist']['tax']) > 1 || $this->shop->get_option('wpsg_showMwstAlways') == '1') $this->shop->view['addColspan'] = 1;
			else $this->shop->view['addColspan'] = 0;
			
			// Adminmail
			$mail_text = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/adminmail.phtml', false);
			
			if ($this->shop->get_option('wpsg_htmlmail') === '1') $mail_html = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/adminmail_html.phtml', false);
			else $mail_html = false;
			
			$empfaenger = $this->shop->get_option('wpsg_adminmail_empfaenger');
			if (wpsg_isSizedString($this->shop->get_option('wpsg_requestmailadmin_empfaenger'))) $empfaenger = $this->shop->get_option('wpsg_requestmailadmin_empfaenger');
			
			// Senden
			$this->shop->sendMail($mail_text, $empfaenger, 'requestmailadmin', array(), false, false, $mail_html);
			
			// Kundenmail
			$mail_text = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/customermail.phtml', false);
			
			if ($this->shop->get_option('wpsg_htmlmail') === '1') $mail_html = $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_request/customermail_html.phtml', false);
			else $mail_html = false;
			
			$empfaenger = $this->shop->view['requestlist']['customer']['email'];
			
			// Senden
			$this->shop->sendMail($mail_text, $empfaenger, 'requestmailcustomer', array(), false, false, $mail_html);
			 
			// Produkte aus der Liste löschen
			$_SESSION['wpsg']['wpsg_mod_request']['products'] = array();
			
		} // public function doRequest()
		
	} // class wpsg_mod_request extends wpsg_mod_basic

?>