<?php

	/**
	 * Mit diesem Modul ist es möglich zu den Produkten verschiedene Attribute im Backend zu definieren
	 * Diese können dann im Produkttemplate angezeigt werden
	 */
	class wpsg_mod_produktattribute extends wpsg_mod_basic
	{
	
		var $id = 85;
		var $lizenz = 1;
		var $inline = true;
		var $hilfeURL = 'http://wpshopgermany.de/?p=335';
	
		/**
		 * Costructor
		 */
		public function __construct()
		{
				
			parent::__construct();
				
			$this->name = __('Produktattribute', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Erlaubt es Produktattribute zu erstellen, die dann in der Produktverwaltung ausgefüllt und im Frontend angezeigt werden.', 'wpsg');
	
		} // public function __construct()
	
		public function install()
		{
	
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
	
			/**
			 * Tabelle für die Produktattribute
			 */
			$sql = "CREATE TABLE ".WPSG_TBL_AT." (
			   		id mediumint(9) NOT NULL AUTO_INCREMENT,
			   		name varchar(100) NOT NULL,
			   		typ varchar(100) NOT NULL, 
			   		auswahl varchar(1000) NOT NULL,  	
			   		autoshow int(1) NOT NULL,	
			   		PRIMARY KEY  (id)
			   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			 
			dbDelta($sql);
 
			/**
			 * Tabelle für die Werte der Produktattribute
			 */
			$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS_AT." (
			   		p_id mediumint(9) NOT NULL,
			   		a_id mediumint(9) NOT NULL,
			   		value TEXT NOT NULL,
			   		KEY p_id (p_id),
			   		KEY a_id (a_id)
			   	) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	
			dbDelta($sql);
				
		} // public function install()
	
		public function be_ajax()
		{
				
			if ($_REQUEST['do'] == 'add')
			{
	
				$new_name = __('Anklicken um Bezeichnung zu ändern ...', 'wpsg');
	
				$at_id = $this->db->ImportQuery(WPSG_TBL_AT, array(
					'name' => wpsg_q($new_name),
					'typ' => 0
				));
	
				$this->shop->addTranslationString('wpsg_mod_produktattribute_'.$at_id, $new_name);
	
				$this->pa_listAction(); die();
	
			}
			else if ($_REQUEST['do'] == 'genPACode')
			{
	
				$this->shop->view['id'] = $_REQUEST['pa_id'];
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/genPACode.phtml');
				die();
	
			}
			else if ($_REQUEST['do'] == 'inlinedit')
			{
	
				if ($_REQUEST['field'] == 'name')
				{
	
					$this->db->UpdateQuery(WPSG_TBL_AT, array(
						'name' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['pa_id'])."'");
						
					$this->shop->addTranslationString('wpsg_mod_produktattribute_'.$_REQUEST['pa_id'], $_REQUEST['value']);
						
					die(stripslashes($_REQUEST['value']));
						
				}
				else if ($_REQUEST['field'] == 'show')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_AT, array(
						'autoshow' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['pa_id'])."'");					
					
					die("1");
					
				}
				else if ($_REQUEST['field'] == 'typ')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_AT, array(
						'typ' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['pa_id'])."'");
					
					die("1");
					
				}
				else if ($_REQUEST['field'] == 'delete')
				{
					
					$this->db->Query("DELETE FROM `".WPSG_TBL_AT."` WHERE `id` = '".wpsg_q($_REQUEST['pa_id'])."'");
					
					die($this->pa_listAction());
					
				}
				else if ($_REQUEST['field'] == 'auswahl')
				{
						
					$this->db->UpdateQuery(WPSG_TBL_AT, array(
						'auswahl' => wpsg_q($_REQUEST['value'])
					), "`id` = '".wpsg_q($_REQUEST['pa_id'])."'");
						
					$this->shop->addTranslationString('wpsg_mod_produktattribute_'.$_REQUEST['pa_id'], $_REQUEST['value']);
						
					die(stripslashes($_REQUEST['value']));
						
				}
	
			}
	
		} // public function be_ajax()
	
		public function settings_edit()
		{
				
			$this->shop->mod = &$this;
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/settings_edit.phtml');
				
		} // public function settings_edit()
	
		public function settings_save()
		{
				 
				
		} // public function settings_save()
	 
		public function produkt_save(&$produkt_id)
		{
			 
			foreach ((array)$_REQUEST as $pa_id => $pa_value)
			{

				// Musste ich so abändern, da der RTE keine namen Felder vom Typ pa[name] erlaubt.
				if (preg_match('/^pa_/', $pa_id))
				{
					
					$pa_id = substr($pa_id, 3);
										
					$nExists = $this->db->fetchOne("
						SELECT COUNT(*) FROM `".WPSG_TBL_PRODUCTS_AT."` WHERE `a_id` = '".wpsg_q($pa_id)."' AND `p_id` = '".wpsg_q($produkt_id)."' 
					");
		
					$pa = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_AT."` WHERE `id` = '".wpsg_q($pa_id)."'");
					
					if ($pa['typ'] == 1 && $this->shop->get_option('wpsg_options_nl2br') == '1') $pa_value = nl2br($pa_value);
					
					if ($nExists > 0)
					{
							
						$this->db->UpdateQuery(WPSG_TBL_PRODUCTS_AT, array(
							'value' => wpsg_q($pa_value)
						), "`a_id` = '".wpsg_q($pa_id)."' AND `p_id` = '".wpsg_q($produkt_id)."' ");
							
					}
					else
					{
							
						$this->db->ImportQuery(WPSG_TBL_PRODUCTS_AT, array(
							'value' => wpsg_q($pa_value),
							'a_id' => wpsg_q($pa_id),
							'p_id' => wpsg_q($produkt_id)
						));
							
					}
	
				}
					
			}
	
		} // public function produkt_save(&$produkt_id)
	
		public function produkt_save_translation(&$produkt_id, &$trans_id)
		{
			
			$this->produkt_save($trans_id);
			
		} // public function produkt_save_translation(&$produkt_id, &$trans_id)
		
		public function produkt_createTranslation(&$produkt_id, &$trans_id)
		{
			
			$arAttributeValues = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_PRODUCTS_AT."` WHERE `p_id` = '".wpsg_q($produkt_id)."'");
			
			foreach ($arAttributeValues as $a)
			{
				
				$a['p_id'] = $trans_id;
				
				$this->db->ImportQuery(WPSG_TBL_PRODUCTS_AT, $a);
				
			}
			
		} // public function produkt_createTranslation(&$produkt_id, &$trans_id)
		
		public function produkt_copy(&$produkt_id, &$copy_id) { 
			
			// Wie bei der erstellung einer neuen Übersetzung ...
			$this->produkt_createTranslation($produkt_id, $copy_id);
			
		} // public function produkt_copy(&$produkt_id, &$copy_id)
		
		public function produkt_edit_content(&$produkt_data)
		{

			// Nur für angelegte Produkte
			if ($produkt_data['id'] <= 0) return false;
			 			
			$this->shop->view['data'] = $produkt_data;
			$this->shop->view['data']['pa'] = $this->db->fetchAssoc("
				SELECT
					A.`id`, A.`name`, A.`typ`, PA.`value`, A.`auswahl`
				FROM
					`".WPSG_TBL_AT."` AS A
						LEFT JOIN `".WPSG_TBL_PRODUCTS_AT."` AS PA ON (PA.`a_id` = A.`id` AND PA.`p_id` = '".wpsg_q($produkt_data['id'])."')				  			
			"); 
				
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/produkt_edit_content.phtml');
				
		} // public function produkt_edit_content(&$produkt_data)
	
		public function product_bottom(&$produkt_id, $template_index) 
		{ 
			
			$p_id = $produkt_id;
			
			if ($this->shop->isOtherLang())
			{
					
				$produkt_trans_id = $this->db->fetchOne("SELECT `id` FROM `".WPSG_TBL_PRODUCTS."` WHERE `lang_parent` = '".wpsg_q($p_id)."' AND `lang_locale` = '".wpsg_q($this->shop->getCurrentLanguage())."'");
					
				if (wpsg_isSizedInt($produkt_trans_id)) $p_id = $produkt_trans_id;
 			
			}

			$this->shop->view['wpsg_mod_produktattribute']['data'] = $this->db->fetchAssoc("
				SELECT
					PAT.`value`,
					AT.`typ`,
					AT.`autoshow`,
					AT.`name`
				FROM
					`".WPSG_TBL_PRODUCTS_AT."` AS PAT 
						LEFT JOIN  `".WPSG_TBL_AT."` AS AT ON (PAT.`a_id` = AT.`id`)
				WHERE 
					PAT.`p_id` = '".wpsg_q($p_id)."'
			");
			 
			if (wpsg_isSizedArray($this->shop->view['wpsg_mod_produktattribute']['data']))
			{
			
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/produkt_bottom.phtml');
				
			}
			
		} // public function product_bottom(&$produkt_id, $template_index)
				
		public function basket_row(&$p, $i)
		{
			
			$this->shop->view['wpsg_mod_produktattribute']['data'] = $this->db->fetchAssocField("
				SELECT	
					AT.`id` 
				FROM 
					`".WPSG_TBL_AT."` AS AT 
				WHERE
					AT.`autoshow` = '1'
			");
			
			$this->shop->view['wpsg_mod_produktattribute']['id'] = $this->shop->getProduktID($p['id']);			
			$this->shop->view['i'] = $i;
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/basket_row.phtml');
			
		} // public function basket_row(&$p, $i)
		
		public function overview_row(&$p, $i)
		{
				
			$this->shop->view['wpsg_mod_produktattribute']['data'] = $this->db->fetchAssocField("
				SELECT
					AT.`id`
				FROM
					`".WPSG_TBL_AT."` AS AT
				WHERE
					AT.`autoshow` = '1'
			");
				
			$this->shop->view['wpsg_mod_produktattribute']['id'] = $this->shop->getProduktID($p['id']);				
			$this->shop->view['i'] = $i;
				
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/overview_row.phtml');
				
		} // public function overview_row(&$p, $i)
		
		public function mail_row($i, $p)
		{
		
			$this->shop->view['wpsg_mod_produktattribute']['data'] = $this->db->fetchAssocField("
				SELECT
					AT.`id`
				FROM
					`".WPSG_TBL_AT."` AS AT
				WHERE
					AT.`autoshow` = '1'
			");
		
			$this->shop->view['wpsg_mod_produktattribute']['id'] = $this->shop->getProduktID($p['id']);		
			$this->shop->view['i'] = $i;
		
			if ($this->shop->htmlMail === true)
			{
					
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/mail_row_html.phtml');
					
			}
			else
			{
					
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/mail_row.phtml');
					
			} 
		
		} // public function mail_row($i, $p)
		
		public function replaceUniversalPlatzhalter(&$arReplace, $order_id = false, $kunden_id = false, $rechnung_id = false, $product_id = false, $product_index = false) 
		{ 
		
			if ($product_id !== false && $product_id > 0)
			{
			
				$arAttribute = $this->db->fetchAssocField("SELECT `id` FROM `".WPSG_TBL_AT."`");
			
				foreach ((array)$arAttribute as $pa_id)
				{
					
					$arReplace['/%at_'.$pa_id.'%/'] = $this->getAttributeValue($product_id, $pa_id);
					
				}
				
			}
						
		} // public function replaceUniversalPlatzhalter(&$arReplace, $order_id = false, $kunden_id = false, $rechnung_id = false, $product_id = false, $product_index = false)
				
		public function notifyURL(&$url, &$produkt_key, &$menge, &$order_id, &$typ, &$arSend) 
		{
			
			$product_id = $this->shop->getProduktID($produkt_key);
			
			if ($product_id !== false && $product_id > 0)
			{
				
				$arAttribute = $this->db->fetchAssocField("SELECT `id` FROM `".WPSG_TBL_AT."`");
			
				foreach ((array)$arAttribute as $pa_id)
				{
					
					$arSend['at_'.$pa_id] = $this->getAttributeValue($product_id, $pa_id);
					
				}
				
			}
			
		} // public function notifyURL(&$url, &$produkt_key, &$menge, &$order_id, &$typ, &$arSend)
		
		/**
		 * Gibt den Namen eines Attributs zurück
		 */
		public function getAttributeLabel($pa_id)
		{
	 
			$name = $this->db->fetchOne("SELECT `name` FROM `".WPSG_TBL_AT."` WHERE `id` = '".wpsg_q($pa_id)."'");
				
			return __($name, 'wpsg');
				
		} // public function getAttributeLabel($pa_id)
	
		/**
		 * Gibt den Wert für ein Attribut zurück
		 */
		public function getAttributeValue($p_id, $pa_id)
		{
 		
			if ($this->shop->isOtherLang())
			{
			
				$produkt_trans_id = $this->db->fetchOne("SELECT `id` FROM `".WPSG_TBL_PRODUCTS."` WHERE `lang_parent` = '".wpsg_q($p_id)."' AND `lang_locale` = '".wpsg_q($this->shop->getCurrentLanguage())."'");
					
				if (wpsg_isSizedInt($produkt_trans_id)) $p_id = $produkt_trans_id;
				
			}
 
			$pa = $this->db->fetchRow("
				SELECT 
					PA.`value`, A.`typ`
				FROM 
					`".WPSG_TBL_PRODUCTS_AT."` AS PA 
						LEFT JOIN `".WPSG_TBL_AT."` AS A ON (A.`id` = PA.`a_id`)
				WHERE 
					PA.`a_id` = '".wpsg_q($pa_id)."' AND 
					PA.`p_id` = '".wpsg_q($p_id)."'
			");
			 
			if ($pa['typ'] == '1') /* RTE Feld */
			{
	
				// Filter auf Feld anwenden (RTE)
				// Den wpsgContentFilter deaktivieren um rekursion zu vermeiden
				remove_filter('the_content', array($GLOBALS['wpsg_sc'], 'content_filter'));
				$value = apply_filters('the_content', $pa['value']);
				add_filter('the_content', array($GLOBALS['wpsg_sc'], 'content_filter'));
	
			}
			else if ($pa['typ'] == '3')
			{
	
				// Checkbox
				if ($pa['value'] == '1') $value = __('Ja', 'wpsg');
				else $value = __('Nein', 'wpsg');
	
			}
			else
			{
	
				// Auswahl oder normales Textfeld
				$value = $pa['value'];
			}
				
			return $value;
				
		} // public function getAttributeValue($p_id, $pa_id)
	
		/**
		 * Zeichnet die Liste der Produktattribute für das Backend
		 */
		public function pa_listAction()
		{
	
			$this->shop->view['data'] = $this->db->fetchAssoc("
					SELECT * FROM `".WPSG_TBL_AT."` ORDER BY `id` ASC
				");
				
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_produktattribute/list.phtml');
				
		} // private function pa_listAction()
	
	} // class wpsg_mod_produktattribute extends wpsg_mod_basic

?>