<?php

	/**
	 * Mit diesem Modul können die Produkte im Frontend per Ajax gefiltert werden
	 * @author daniel
	 */
	class wpsg_mod_productfilter extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 1850;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
				
			parent::__construct();
				
			$this->name = __('Frontend Produktfilter', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Ermöglicht die Filter der Produkte im Frontend nach Produktattributen, Varianten und Preisen.', 'wpsg');
		
		} // public function __construct()
		
		public function settings_edit()
		{
			
			$arSet = $this->shop->get_option('wpsg_mod_productfilter_show');

			// Produktattribute?
			if ($this->shop->hasMod('wpsg_mod_produktattribute'))
			{
				
				$this->shop->view['wpsg_mod_productfilter']['arPA'] = $this->db->fetchAssoc("
					SELECT * FROM `".WPSG_TBL_AT."` AS A 
				");
				
				// Gesetzt?
				if (wpsg_isSizedArray($arSet['pa']))
				{
					
					foreach ($this->shop->view['wpsg_mod_productfilter']['arPA'] as $k => $pa)
					{
						
						if (in_array($pa['id'], $arSet['pa']))
						{
						
							$this->shop->view['wpsg_mod_productfilter']['arPA'][$k]['set'] = '1';
							
						}
						
					}
					
				}
								
			}

			// Varianten
			if ($this->shop->hasMod('wpsg_mod_varianten'))
			{
				
				$arVariantenProducts = $this->db->fetchAssocField("
					SELECT P.`id` FROM `".WPSG_TBL_PRODUCTS."` AS P WHERE LENGTH(P.`mod_varianten`) > 0  
				", "id");
				
				if (wpsg_isSizedArray($arVariantenProducts))
				{
								
					$this->shop->view['wpsg_mod_productfilter']['arVarianten'] = array();
					
					foreach ($arVariantenProducts as $product_id)
					{
						
						$arVariInfo = $this->shop->callMod('wpsg_mod_varianten', 'loadVarianten', array($product_id));
						
						if (wpsg_isSizedArray($arVariInfo))
						{
							
							foreach ($arVariInfo as $var_id => $var)
							{
								
								if ($var['aktiv'] == '1')
								{
								
									if (!array_key_exists($var['name'], $this->shop->view['wpsg_mod_productfilter']['arVarianten']))
									{
										
										$this->shop->view['wpsg_mod_productfilter']['arVarianten'][$var['name']] = in_array($var['name'], $arSet['varianten']);
										
									}
									
								}
								
							}
							
						}
						
					}
					
				}
				
			}
			
			// Seiten
			$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['wpsg_mod_productfilter']['pages'] = $arPages;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_productfilter/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$arSave = array();
			
			// Produktattribute 
			if ($this->shop->hasMod('wpsg_mod_produktattribute'))
			{
			
				$arSave['pa'] = array();
				if (wpsg_isSizedArray($_REQUEST['wpsg_mod_productfilter']['pa']))
				{

					foreach ($_REQUEST['wpsg_mod_productfilter']['pa'] as $k => $v)
					{
						
						if ($v > 0) $arSave['pa'][] = $v;
						
					}
					
				}
				
			}
			
			// Varianten
			if ($this->shop->hasMod('wpsg_mod_varianten'))
			{
				
				$arSave['varianten'] = array();
				$arSave['variationenCache'] = array();
				
				if (wpsg_isSizedArray($_REQUEST['wpsg_mod_productfilter']['varianten']))
				{
					
					foreach ($_REQUEST['wpsg_mod_productfilter']['varianten'] as $k => $v)
					{
						
						if ($v != '0') 
						{
														
							$arSave['varianten'][] = $v;
							
							$arVariantenProducts = $this->db->fetchAssocField("
								SELECT P.`id` FROM `".WPSG_TBL_PRODUCTS."` AS P WHERE LENGTH(P.`mod_varianten`) > 0  
							", "id");
							
							foreach ($arVariantenProducts as $product_id)
							{
							
								$this->addVariationen($product_id, $v, $arSave['variationenCache']);
								
							}
							
						}
						
					} 
					
				}
				
			} 
			
			// Seite für Übersicht anlegen 
			$this->shop->createPage(__('Produkte', 'wpsg'), 'wpsg_mod_productfilter_page', $_REQUEST['wpsg_mod_productfilter_page']);
			 
			$this->shop->update_option('wpsg_mod_productfilter_show', $arSave);
			$this->shop->update_option('wpsg_mod_productfilter_pricefilter', $_REQUEST['wpsg_mod_productfilter_pricefilter']);
						
		} // public function settings_save()

		public function load() 
		{ 

			require_once(dirname(__FILE__).'/mod_productfilter/wpsg_mod_productfilter_widget.class.php');
			add_action('widgets_init', create_function('', 'return register_widget("wpsg_mod_productfilter_widget");'));
			
		} // public function load()
		
		/** Modulfunktionen */
		
		/**
		 * Lädt die Variationen der Variante für die spätere Anzeige im Filter
		 * @param $product_id
		 * @param $var_name
		 * @param $variationenCache
		 */
		function addVariationen($product_id, $var_name, &$variationenCache)
		{
			
			$arVariInfo = $this->shop->callMod('wpsg_mod_varianten', 'loadVarianten', array($product_id));
			
			foreach ($arVariInfo as $var_id => $var)
			{
				
				if ($var['aktiv'] == '1' && $var['name'] == $var_name)
				{
					
					if (!array_key_exists($var_name, $variationenCache))
					{
						
						$variationenCache[$var_name] = array(
							'typ' => $var['typ'],
							'vari' => array()
						);	
						
					}
					
					foreach ((array)$var['vari'] as $vari_id => $vari)
					{
						
						if (!in_array($vari['name'], $variationenCache[$var_name]['vari']))
						{
						
							if ($vari['aktiv'] == '1') $variationenCache[$var_name]['vari'][] = $vari['name'];

						}
						
					}
					
				}
				
			}
			
		} // function addVariationen($product_id, $var_name, &$variationenCache)
		
	} // class wpsg_mod_productfilter extends wpsg_mod_basic

?>