<?php

	/**
	 * Controller für die Produktverwaltung
	 */
	class wpsg_ProduktController extends wpsg_SystemController
	{
		
		/**
		 * Übernimmt die Verteilung der Anfragen
		 */
		public function dispatch()
		{
			
			parent::dispatch();
			
			if (wpsg_isSizedString($_REQUEST['action'], 'add'))
			{
				$this->addAction();	
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'edit'))
			{
				$this->editAction();
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'export'))
			{
				$this->exportAction();				
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'import'))
			{
				$this->importAction();
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'copy'))
			{
				$this->copyAction();
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'del'))
			{
				$this->delAction();
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'save'))
			{
				$this->saveAction();
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'select'))
			{
				$this->selectAction();
			}
			else if (wpsg_isSizedString($_REQUEST['action'], 'ajax'))
			{
				$this->ajaxAction();
			}
			else
			{
				$this->indexAction();
			}
			
		} // public function dispatch()		
		
		/**
		 * Nimmt Ajax Anfragen innerhalb der Produktverwaltung entgege
		 */
		public function ajaxAction()
		{
			
			if (isset($_REQUEST['mod']))
			{
				
				$this->shop->callMod($_REQUEST['mod'], 'produkt_ajax');
				
			}
			
		} // public function ajaxAction()
		
		/**
		 * Stellt die Übersicht der Produkte im Backend dar
		 */
		public function indexAction()
		{
			
			$search = array();
			$strQueryWHERE = "";
			
			if (isset($_REQUEST['s']) && $_REQUEST['s'] != '')
			{
				
				$strQueryWHERE_OR = "";
				
				if ($this->shop->hasMod('wpsg_mod_varianten')) $strQueryWHERE_OR .= " OR `mod_varianten` LIKE '%".wpsg_q($_REQUEST['s'])."%' ";					
				
				$strQueryWHERE .= " 
					AND (
						P.`name` LIKE '%".wpsg_q($_REQUEST['s'])."%' OR
						P.`anr` LIKE '%".wpsg_q($_REQUEST['s'])."%'
						".$strQueryWHERE_OR."
					)
				";

				$search['s'] = $_REQUEST['s'];
				
			}
			
			if (isset($_REQUEST['search']['pgruppe']) && $_REQUEST['search']['pgruppe'] > 0)
			{
				
				$strQueryWHERE .= " AND P.`pgruppe` = '".wpsg_q($_REQUEST['search']['pgruppe'])."' ";
				$search['pgruppe'] = $_REQUEST['search']['pgruppe'];
				
			}
			
			$strQueryORDER = '';
			
			if (!isset($_REQUEST['search']['order']))
			{
				
				$search['order'] = 'name';
								
			}
			else
			{

				$search['order'] = $_REQUEST['search']['order'];
				
			}
			
			switch ($search['order'])
			{
				
				case 'mwst': $strQueryORDER = "P.`mwst_key`"; break;		
				case 'anr': $strQueryORDER = "P.`anr`"; break;
				case 'stock': $strQueryORDER = "P.`stock`"; break;
				case 'pgruppe': $strQueryORDER = "`pgruppe_name`"; break;	
				case 'price':

					if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO) $strQueryORDER = "P.`preis_netto`"; 
					else $strQueryORDER = "P.`preis`";

					break;
					
				case 'id': $strQueryORDER = "P.`id`"; break;
					
				default: $strQueryORDER = "P.`name`"; break;  
				
			}
			
			if (!isset($_REQUEST['search']['ascdesc']))
			{
				
				$search['ascdesc'] = 'asc';
				
			}
			else
			{
				
				$search['ascdesc'] = $_REQUEST['search']['ascdesc'];
				
			}
			
			$this->shop->view['search'] = $search;
						
			$GLOBALS['wpsg_produkt_index_query_where'] = $strQueryWHERE;
			
			$nPerPage = $this->get_option('wpsg_produkte_perpage');
			if ($nPerPage <= 0) $nPerPage = 10;
			
			if (isset($_REQUEST['seite'])) $seite = $_REQUEST['seite']; else $seite = 1;
									
			$this->shop->view['count'] = $this->db->fetchOne("
				SELECT
					COUNT(*)
				FROM	
					`".WPSG_TBL_PRODUCTS."` AS P
				WHERE
					`deleted` != '1' AND 
					`lang_parent` = 0  
					".$strQueryWHERE."
			");
			
			$this->shop->view['seite'] = $seite;
			$this->shop->view['pages'] = ceil($this->shop->view['count'] / $nPerPage); 			
			if ($this->shop->view['seite'] <= 0 || $this->shop->view['seite'] > $this->shop->view['pages']) $this->shop->view['seite'] = 1;
			
			$strQuerySELECT = "";
			
			if ($this->shop->hasMod('wpsg_mod_productgroups'))
			{
				
				$strQuerySELECT .= ", (SELECT PG.`name` FROM `".WPSG_TBL_PRODUCTS_GROUP."` AS PG WHERE PG.`id` = P.`pgruppe`) AS `pgruppe_name` ";
				
			}
			
			$this->shop->view['data'] = $this->db->fetchAssoc("
				SELECT
					P.*
					".$strQuerySELECT." 				
				FROM	
					`".WPSG_TBL_PRODUCTS."` AS P
				WHERE
					`deleted` != '1' AND
					`lang_parent` = 0 
					".$strQueryWHERE."
				ORDER BY
					".wpsg_q($strQueryORDER)." ".wpsg_q($search['ascdesc'])."
				LIMIT
					".(($this->shop->view['seite'] - 1) * $nPerPage).", ".$nPerPage."
			");
			
			$default_country = $this->shop->getDefaultCountry();
			
			foreach ($this->shop->view['data'] as &$d)
			{
								
				$d['mwst_value'] = $default_country->getTax($d['mwst_key']); 
				
			}
			
			$this->shop->render(WPSG_PATH_VIEW.'/produkt/index.phtml');
			
		} // public function indexAction()
		
		/**
		 * Wird beim exportieren der Produkte aufgerufen
		 */
		public function exportAction()
		{
			
			$arData = $this->db->fetchAssoc("SELECT * FROM `".wpsg_q(WPSG_TBL_PRODUCTS)."` WHERE `deleted` != '1'");
			
			if (!wpsg_isSizedArray($arData)) { $this->addBackendError(__('Keine Daten zum Exportieren vorhanden.', 'wpsg')); $this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&amp;action=index'); return; }
			
			foreach ($arData as $k => $v)
			{
				
				// Produktattribute laden
				if ($this->shop->hasMod('wpsg_mod_produktattribute'))
				{
										
					$att = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_AT."`");
					
					foreach ($att as $a)
					{
						
						$arData[$k]["ATT_".$a['name']] = $this->db->fetchOne("SELECT `value` FROM `".WPSG_TBL_PRODUCTS_AT."` WHERE `p_id` = '".wpsg_q($v['id'])."' AND `a_id` = '".wpsg_q($a['id'])."' ");
						
					}
						
				}
				
			}
			
			$mb = new wpsg_mod_basic();
			$path = $mb->getTmpFilePath();
			
			$fp = fopen($path.'/wpsg_productexport.csv', 'w');
			fputcsv($fp, array_keys($arData[0]));;
			foreach ($arData as $e)
			{
				
				// Zeilenumbrüche entfernen
				if (get_option('wpsg_impexp_clearlinebreak') === '1')
				{
					foreach ($e as $k => $v) { $e[$k] = preg_replace('/\r|\n/', '', $v); }										
				}
				
				fputcsv($fp, $e, ',', '"');
				
			}
			fclose($fp);
			
			header('Content-type: application/download');
			header('Content-Disposition:inline; filename="wpsg_productexport.csv"');
			header('Expires: 0');
			header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
			header('Pragma:public');
				 
			readfile($path.'/wpsg_productexport.csv');
			die(); 
			
		} // public function exportAction()
		
		/**
		 * Wird beim importieren der Produkte aufgerufen. Zeichnet das Upload Formular und führt auch den Import durch 
		 */
		public function importAction()
		{
			 
			if (isset($_REQUEST['wpsg_import']) && file_exists($_FILES['wpsg_importfile']['tmp_name']))
			{
				
				// Import starten 
 
				$nImported = 0;
				
				$keys = array();
				$handle = fopen($_FILES['wpsg_importfile']['tmp_name'], "r");  
				
				$i = 0;
				while (($row = fgetcsv ($handle, 0, ",")) !== FALSE ) 
				{
					
					if ($i > 0)
					{
						
						$data = array();
						foreach ($keys as $k => $k_name)
						{							
							$data[$k_name] = wpsg_q($row[$k]);
						}
						
						if ($data['id'] <= 0) unset($data['id']);
					  
						// Alte Sachen lösche ich vor dem Import mit der Übergebenen ID !
						
						// Produkt löschen
						$this->db->Query("DELETE FROM `".WPSG_TBL_PRODUCTS."` WHERE `id` = '".wpsg_q($data['id'])."'");
						
						// Attributwerte löschen
						if ($this->shop->hasMod('wpsg_mod_produktattribute'))
						{
							$this->db->Query("DELETE FROM `".WPSG_TBL_PRODUCTS_AT."` WHERE `p_id` = '".wpsg_q($data['id'])."'");
						}
						
						$nImported ++;
						
						unset($data['v_id']);
						unset($data['mwst_value']);
						unset($data['ptemplate']);

						$data_import = $data;
						
						foreach ($data_import as $k => $v)
						{ 
							if (preg_match("/^ATT_(.*)/", $k)) { unset($data_import[$k]); }
						}
					    
						$pNeu_id = $this->db->ImportQuery(WPSG_TBL_PRODUCTS, $data_import, true);
						 
						// Attribute speichern
						foreach ($data as $k => $v)
						{
							
							if (preg_match("/^ATT_(.*)/", $k))
							{
								
								// Attribute ID auslesen
								$att_id = $this->db->fetchOne("SELECT `id` FROM `".WPSG_TBL_AT."` WHERE `name` = '".wpsg_q(preg_replace("/^ATT_/", "", $k))."' ");
								
								if ($att_id > 0)
								{
								
									// Attribut speichern
									$this->db->ImportQuery(WPSG_TBL_PRODUCTS_AT, array(
										"p_id" => wpsg_q($pNeu_id),
										"a_id" => wpsg_q($att_id),
										"value" => wpsg_q($v)
									));
								
								}
									
							}
							
						}
						
					}
					else
					{

						// Schlüssel erzeugen
						$keys = $row;
					
					}
					
					$i ++;
					
			    }
			    
			    fclose($handle);
				
			    $this->shop->addBackendMessage(wpsg_translate(__('#1# Produkte wurden importiert.', 'wpsg'), $nImported));			    
			    die($this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&amp;action=index'));
			    			    
			}
			else if (isset($_REQUEST['wpsg_import']))
			{

				$this->shop->addBackendError(__('Keine Datei zum Import angegeben.', 'wpsg'));
				$this->shop->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&amp;action=import');
				
			}
			
			$this->shop->render(WPSG_PATH_VIEW.'/produkt/import.phtml');
			
		} // public function importAction()
		
		/**
		 * Wird beim bearbeiten aufgerufen
		 */
		public function editAction()
		{
			
			// Verfügbare Produkttemplates
			$this->shop->view['templates'] = $this->shop->loadProduktTemplates();
						
			if (isset($_REQUEST['wpsg_lang']))
			{
				
				$product_translated_id = $this->db->fetchRow("
					SELECT
						P.`id`
					FROM
						`".WPSG_TBL_PRODUCTS."` AS P 
					WHERE
						P.`lang_parent` = '".wpsg_q($_REQUEST['edit_id'])."' AND
						P.`lang_locale` = '".wpsg_q($_REQUEST['wpsg_lang'])."'
				");
				
				if ($product_translated_id <= 0)
				{
					
					$arLang = $this->shop->getStoreLanguages();
										
					// Übersetzung anlegen
					$product_data_original = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_PRODUCTS."` WHERE `id` = '".wpsg_q($_REQUEST['edit_id'])."'");

					unset($product_data_original['id']);
					$product_data_original['lang_parent'] = wpsg_q($_REQUEST['edit_id']);
					$product_data_original['lang_locale'] = wpsg_q($_REQUEST['wpsg_lang']);
					$product_data_original['name'] .= ' ['.$arLang[$_REQUEST['wpsg_lang']]['name'].']';
					$product_data_original['beschreibung'] .= ' ['.$arLang[$_REQUEST['wpsg_lang']]['name'].']';
					if (trim($product_data_original['detailname']) != '') $product_data_original['detailname'] .= ' ['.$arLang[$_REQUEST['wpsg_lang']]['name'].']';					
					
					$product_translated_id = $this->db->ImportQuery(WPSG_TBL_PRODUCTS, $product_data_original);
										
					$this->shop->callMods('produkt_createTranslation', array(&$_REQUEST['edit_id'], &$product_translated_id));
					
				}
				
				$this->shop->view['data'] = $this->db->fetchRow("
					SELECT
						P.*
					FROM
						`".WPSG_TBL_PRODUCTS."` AS P 
					WHERE
						P.`lang_parent` = '".wpsg_q($_REQUEST['edit_id'])."' AND
						P.`lang_locale` = '".wpsg_q($_REQUEST['wpsg_lang'])."'
				"); 
								
			}
			else
			{ 
				
				$this->shop->view['data'] = $this->db->fetchRow("
					SELECT
						P.*
					FROM
						`".WPSG_TBL_PRODUCTS."` AS P 
					WHERE
						P.`id` = '".wpsg_q($_REQUEST['edit_id'])."'
				");
				
				$default_country = $this->shop->getDefaultCountry();
				$this->shop->view['data']['mwst_value'] = $default_country->getTax($this->shop->view['data']['mwst_key']);
				
				if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO) $this->shop->view['data']['tax_sum_value'] = wpsg_calculateSteuer($this->shop->view['data']['preis'], WPSG_NETTO, $this->shop->view['data']['mwst_value']); 
				else $this->shop->view['data']['tax_sum_value'] = wpsg_calculateSteuer($this->shop->view['data']['preis'], WPSG_BRUTTO, $this->shop->view['data']['mwst_value']);
				
			}
			
			// Produktobject			 
			$this->shop->view['oProduct'] = wpsg_product::getInstance($_REQUEST['edit_id']);
			 	
			// Erlaubte Zahlungsarten
			$this->shop->view['allowedPayment'] = wpsg_trim((array)explode(',', $this->shop->view['data']['allowedpayments']));
			
			// Erlaubte Versandarten
			$this->shop->view['allowedShipping'] = wpsg_trim((array)explode(',', $this->shop->view['data']['allowedshipping']));
			 
			$this->shop->callMods('produkt_edit', array(&$this->shop->view['data']));
		 
			// Steuersätze des Standardlandes ermitteln
			$this->shop->view['arTaxGroup'] = array(
				'a' => 'A', 
				'b' => 'B', 
				'c' => 'C', 
				'd' => 'D'
			);
			
			if (wpsg_isSizedInt($this->shop->get_option('wpsg_defaultland')))
			{
			
				$default_country = wpsg_country::getInstance($this->shop->get_option('wpsg_defaultland'));
				
				foreach ($this->shop->view['arTaxGroup'] as &$tax_group)
				{
					
					$tax_value = $default_country->getTax(strtolower($tax_group));
					
					if (!is_null($tax_value)) $tax_group .= ' ('.wpsg_ff($tax_value, '%').' / '.$default_country->kuerzel.')';
					
				}

			}
			
			
			$this->shop->render(WPSG_PATH_VIEW.'/produkt/addedit.phtml');
			
		} // public function editAction()
		
		/**
		 * Wird beim kopieren eines Produkts aufgerufen
		 */
		public function copyAction()
		{
			
			$produkt_db = $this->shop->cache->loadProduct($_REQUEST['edit_id']);
			
			// Neues Produkt anlegen
			unset($produkt_db['id']);
			
			$produkt_db['cdate'] = 'NOW()';			
			$produkt_db['name'] = '['.__('KOPIE', 'wpsg').'] '.$produkt_db['name'];
			
			$produkt_db = wpsg_q($produkt_db);
			
			$new_id = $this->db->ImportQuery(WPSG_TBL_PRODUCTS, $produkt_db);
			
			// Übersetzungen kopieren wenn vorhanden
			$produkt_translations = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_PRODUCTS."` WHERE `lang_parent` = '".wpsg_q($_REQUEST['edit_id'])."'");
			
			foreach ((array)$produkt_translations as $p)
			{
				
				unset($p['id']);
				$p['lang_parent'] = $new_id;
				
				$p = wpsg_q($p);
				
				$this->db->ImportQuery(WPSG_TBL_PRODUCTS, $p);
				
			}
			
			$this->shop->callMods('produkt_copy', array(&$_REQUEST['edit_id'], &$new_id));
			
			$this->addBackendMessage(__('Produkt wurde erfolgreich kopiert.', 'wpsg'));
			
			$this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&action=index');
			
		} // public function copyAction()
		
		/**
		 * Speichert ein Produkt
		 */
		public function saveAction()
		{

			if ($this->shop->get_option('wpsg_options_nl2br') == '1')
			{
				
				$_REQUEST['beschreibung'] = nl2br($_REQUEST['beschreibung']);

			}
			
			if (isset($_REQUEST['wpsg_lang']))
			{
				
				// Übersetzung speichern
				
				$trans_id = $this->db->fetchOne("SELECT `id` FROM `".WPSG_TBL_PRODUCTS."` WHERE `lang_parent` = '".wpsg_q($_REQUEST['edit_id'])."' AND `lang_locale` = '".wpsg_q($_REQUEST['wpsg_lang'])."'");
				
				if ($trans_id <= 0) die(__('ERROR: Übersetzung existiert noch nicht, das dürfte nicht passieren!', 'wpsg'));
				
				$data = array(
					'name' => wpsg_q($_REQUEST['name']),
					'detailname' => wpsg_q($_REQUEST['detailname']),
					'beschreibung' => wpsg_q($_REQUEST['beschreibung'])					
				);
				
				$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, $data, "`id` = '".wpsg_q($trans_id)."'");

				$this->addBackendMessage(__('Übersetzung erfolgreich gespeichert', 'wpsg'));
				
				$this->shop->callMods('produkt_save_translation', array(&$_REQUEST['edit_id'], &$trans_id));
				
				if (isset($_REQUEST['submit_index'])) $this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&action=index');
				else $this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&action=edit&edit_id='.$_REQUEST['edit_id'].'&wpsg_lang='.$_REQUEST['wpsg_lang']);
				
			}
			else
			{
			
				// Reguläres Produkt speichern
								
				$data = array(
					'name' => wpsg_q(wpsg_getStr($_REQUEST['name'])),
					'detailname' => wpsg_q(wpsg_getStr($_REQUEST['detailname'])),
					'beschreibung' => wpsg_q(wpsg_getStr($_REQUEST['beschreibung'])),
					'anr' => wpsg_q(wpsg_getStr($_REQUEST['anr'])), 
					'mwst_key' => wpsg_q(wpsg_getStr($_REQUEST['mwst_key'])),		
					'ptemplate_file' => wpsg_q(wpsg_getStr($_REQUEST['ptemplate_file'])),
					'posturl' => wpsg_q(wpsg_getStr($_REQUEST['posturl'])),
					'posturl_verkauf' => wpsg_q(wpsg_getStr($_REQUEST['posturl_verkauf'])),
					'posturl_bezahlung' => wpsg_q(wpsg_getStr($_REQUEST['posturl_bezahlung'])),
					'partikel' => wpsg_q(wpsg_getStr($_REQUEST['partikel'])),
					'euleistungsortregel' => wpsg_q(wpsg_getStr($_REQUEST['euleistungsortregel'])),
					'basket_multiple' => wpsg_q(wpsg_getStr($_REQUEST['basket_multiple']))
				);

				// Erlaubte Zahlungsarten speichern
				$data['allowedpayments'] = '';
				if ($_REQUEST['wpsg_paymentmethods_select'] == '1')
				{
				
					$arAllowedPayments = wpsg_trim($_REQUEST['wpsg_paymentmethods'], '0');
					if (wpsg_isSizedArray($arAllowedPayments)) $data['allowedpayments'] = implode(',', $arAllowedPayments);
					else $data['allowedpayments'] = '';
					
				}
				
				// Erlaubte Versandarten speichern
				$data['allowedshipping'] = '';
				if ($_REQUEST['wpsg_shippingmethods_select'] == '1')
				{
					
					$arAllowedShipping = wpsg_trim($_REQUEST['wpsg_shippingmethods'], '0');
					if (wpsg_isSizedArray($arAllowedShipping)) $data['allowedshipping'] = implode(',', $arAllowedShipping);
					else $data['allowedshipping'] = '';
					
				}
				 
				$data['preis'] = wpsg_tf($_REQUEST['preis']);
				 			
				$this->shop->callMods('produkt_save_before', array(&$data));

				if ($_REQUEST['edit_id'] > 0)
				{
					
					$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, $data, "`id` = '".wpsg_q($_REQUEST['edit_id'])."'");
					$this->addBackendMessage(__('Produkt erfolgreich gespeichert.', 'wpsg'));
					
				}
				else
				{
	
					$data['cdate'] = 'NOW()';
					$_REQUEST['edit_id'] = $this->db->ImportQuery(WPSG_TBL_PRODUCTS, $data);
					$this->addBackendMessage(__('Produkt erfolgreich angelegt.', 'wpsg'));
					
				}

				// Artikelnummer = id, wenn nicht gesetzt
				if (trim($data['anr']) == '')
				{
					
					$data = array('anr' => wpsg_q($_REQUEST['edit_id']));
					
					$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, $data, "`id` = '".wpsg_q($_REQUEST['edit_id'])."'");

					// Produkt Object Cache löschen
					$this->shop->cache->clearProductCache($_REQUEST['edit_id']);
					
				}
				
				$this->shop->callMods('produkt_save', array(&$_REQUEST['edit_id']));
						
				if (isset($_REQUEST['submit_index'])) $this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&action=index');
				else $this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&action=edit&edit_id='.$_REQUEST['edit_id']);
				
			}
			
			die();
			
		} // public function saveAction()
		
		/**
		 * Wird beim erstellen eines neuen Produktes aufgerufen
		 */
		public function addAction()
		{
			
			// Verfügbare Produkttemplates
			$this->shop->view['templates'] = $this->shop->loadProduktTemplates();
			 
			$this->shop->view['data'] = array();
			$this->shop->callMods('produkt_add', array(&$this->shop->view['data']));
			
			$this->shop->render(WPSG_PATH_VIEW.'/produkt/addedit.phtml');
			
		} // public function addAction()
		
		/**
		 * Wird beim löschen eines Produktes aufgerufen
		 */
		public function delAction()
		{
			
			$this->db->UpdateQuery(WPSG_TBL_PRODUCTS, array(
				'deleted' => '1'
			), "`id` = '".wpsg_q($_REQUEST['edit_id'])."'");		
			
			$this->shop->callMods('produkt_del', array($_REQUEST['edit_id']));
			
			// Übersetzung löschen
			$this->db->UpdateQuerY(WPSG_TBL_PRODUCTS, array(
				'deleted' => '1'
			), "`lang_parent` = '".wpsg_q($_REQUEST['edit_id'])."'");
			
			$this->addBackendMessage(__('Produkt erfolgreich gelöscht.', 'wpsg'));
			
			$this->redirect(WPSG_URL_WP.'wp-admin/admin.php?page=wpsg-Produkt&action=index');
			
		} // public function delAction()
		 		
		/**
		 * Dialog für die Produktauswahl
		 */
		public function selectAction()
		{
			
			if ($_REQUEST['wpsg_mode'] == 'filterDialog')
			{
				
				die($this->shop->render(WPSG_PATH_VIEW.'/produkt/select_filter.phtml'));
				
			}
			else if ($_REQUEST['wpsg_mode'] == 'filter')
			{
 
				$strQueryWHERE = '';
				 
				if (wpsg_isSizedString($_REQUEST['filter_name'])) $strQueryWHERE .= " AND P.`name` LIKE '%".wpsg_q($_REQUEST['filter_name'])."%' ";
				
				$arProductIDs = $this->db->fetchAssocField("
					SELECT
				 		P.`id`
					FROM
				 		`".WPSG_TBL_PRODUCTS."` AS P
				 	WHERE
				 		P.`deleted` = '0' AND
				 		P.`lang_parent` = '0'
						".$strQueryWHERE."
				 ");
				
				$this->shop->view['arProducts'] = array();
				
				// Aufwerten
				foreach ($arProductIDs as $p_id)
				{
					
					$product_data = $this->shop->loadProduktArray($p_id);
					$bVariante = false;					
					
					if ($this->shop->hasMod('wpsg_mod_varianten'))
					{
				
						$arVarianten = $this->shop->callMod('wpsg_mod_varianten', 'loadVarianten', array($p_id));
				
						if (wpsg_isSizedArray($arVarianten))
						{
					
							$bVariante = true;									
					
						}
				
					}
					
					if (!$bVariante) $this->shop->view['arProducts'][] = $product_data;
					
				} 
				
				die($this->shop->render(WPSG_PATH_VIEW.'/produkt/select_filter_productlist.phtml'));
				
			}
			else if ($_REQUEST['wpsg_mode'] == 'wpsg_mod_relatedproducts')
			{ 
				
				// Bei der Auswahl für die Ähnlichen Produkte sollte das aktuelle Produkt und bereits relevante nicht zur auswahl stehen
				$this->shop->view['arProdukte'] = $this->db->fetchAssocField("
					SELECT
						P.`id`,
						P.`name`
					FROM
						`".WPSG_TBL_PRODUCTS."` AS P
							LEFT JOIN `".WPSG_TBL_PRODUCTS_REL."` AS RP ON (RP.`rel_id` = P.`id`) 
					WHERE
						P.`deleted` = '0' AND
						P.`lang_parent` = '0' AND
						P.`id` != '".wpsg_q($_REQUEST['edit_id'])."'
				", "id", "name"); 
				
			}
			else
			{

				$this->shop->view['arProdukte'] = $this->db->fetchAssocField("
					SELECT
						`id`, `name`
					FROM
						`".WPSG_TBL_PRODUCTS."`
					WHERE
						`deleted` = '0' AND
						`lang_parent` = '0'
				", "id", "name");
				
				if ($this->shop->hasMod('wpsg_mod_productgroups'))
				{
				
					$this->shop->view['arProduktgroups'] = $this->db->fetchAssocField("
						SELECT
							PG.*
						FROM
							`".WPSG_TBL_PRODUCTS_GROUP."` AS PG
						WHERE
							PG.`deleted` != '1'					
					", "id", "name");
					
					$this->shop->view['arProductgroupsOrder'] = array(
						'id' => __('Interne ID', 'wpsg'),
						'name' => __('Produktname', 'wpsg'),
						'anr' => __('Artikelnummer', 'wpsg'),
						'preis' => __('Preis', 'wpsg')
					);
					
					$this->shop->view['arProductgroupsDirection'] = array(
						'asc' => __('Aufsteigend', 'wpsg'),
						'desc' => __('Absteigend', 'wpsg')
					);
					
				}
					
			}
			
			$this->shop->view['arTemplates'] = $this->shop->loadProduktTemplates();
			array_unshift($this->shop->view['arTemplates'], __('Aus Produkt', 'wpsg'));			
						
			die($this->shop->render(WPSG_PATH_VIEW.'/produkt/select.phtml'));
			
		} // public function selectAction()
		
	} // class wpsg_ProduktController extends wpsg_SystemController

?>