<?php

	/**
	 * Erlaubt die Definition und Anzeige von Lieferzeiten 
	 * @author Daschmi 
	 */
	class wpsg_mod_deliverytime extends wpsg_mod_basic
	{
		
		var $lizenz = 1;
		var $id = 1610;
		var $hilfeURL = 'http://wpshopgermany.de/?p=3968';
		
		const MODE_DAYS = 1;
		const MODE_SELECT = 2;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
				
			parent::__construct();
				
			$this->name = __('Lieferzeit', 'wpsg');
			$this->group = __('Produkte', 'wpsg');
			$this->desc = __('Erlaubt die Definition und Anzeige von Lieferzeiten.', 'wpsg');
		
		} // public function __construct()
		
		public function install()
		{
			
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
			
			/**
			 * Produkt Tabelle erweitern
			 */ 
			$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS." (
		   		wpsg_mod_deliverytime_deliverytime VARCHAR(255) NOT NULL
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   	   	 
   			dbDelta($sql); 			
			
			// Vorgaben
			$this->shop->checkDefault('wpsg_mod_deliverytime_mode', self::MODE_DAYS);
			$this->shop->checkDefault('wpsg_mod_deliverytime_mode_days_default', '7');
			$this->shop->checkDefault('wpsg_mod_deliverytime_mode_select_values', __('3 Tage, 1 Woche, 3 Wochen', 'wpsg'), false, true);
			$this->shop->checkDefault('wpsg_mod_deliverytime_mode_select_default', __('1 Woche', 'wpsg'), false, true);
			$this->shop->checkDefault('wpsg_mod_deliverytime_show_product', '1');
			$this->shop->checkDefault('wpsg_mod_deliverytime_show_basket', '1');
			$this->shop->checkDefault('wpsg_mod_deliverytime_show_overview', '1');
			$this->shop->checkDefault('wpsg_mod_deliverytime_show_mail', '1');
			
		} // public function install()
		
		public function settings_edit()
		{
			
			$this->shop->view['wpsg_mod_deliverytime']['arSelection'] = $this->getPossibleSelection();
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_deliverytime/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_deliverytime_mode', $_REQUEST['wpsg_mod_deliverytime_mode']);
			$this->shop->update_option('wpsg_mod_deliverytime_mode_select_values', $_REQUEST['wpsg_mod_deliverytime_mode_select_values']);
			$this->shop->update_option('wpsg_mod_deliverytime_mode_select_default', $_REQUEST['wpsg_mod_deliverytime_mode_select_default']);
			$this->shop->update_option('wpsg_mod_deliverytime_mode_days_default', $_REQUEST['wpsg_mod_deliverytime_mode_days_default']);
			
			$this->shop->update_option('wpsg_mod_deliverytime_show_product', $_REQUEST['wpsg_mod_deliverytime_show_product']);
			$this->shop->update_option('wpsg_mod_deliverytime_show_basket', $_REQUEST['wpsg_mod_deliverytime_show_basket']);
			$this->shop->update_option('wpsg_mod_deliverytime_show_overview', $_REQUEST['wpsg_mod_deliverytime_show_overview']);
			$this->shop->update_option('wpsg_mod_deliverytime_show_mail', $_REQUEST['wpsg_mod_deliverytime_show_mail']);
			
			$this->shop->addTranslationString('wpsg_mod_deliverytime_mode_select_values', $_REQUEST['wpsg_mod_deliverytime_mode_select_values']);
			$this->shop->addTranslationString('wpsg_mod_deliverytime_mode_select_default', $_REQUEST['wpsg_mod_deliverytime_mode_select_default']);
			
		} // public function settings_save()
		
		public function produkt_edit_allgemein(&$produkt_data) 
		{ 
						
			if ($this->shop->get_option('wpsg_mod_deliverytime_mode') == self::MODE_SELECT)
			{
				
				$this->shop->view['wpsg_mod_deliverytime']['arSelection'] = $this->getPossibleSelection();
				
			}
			
			$this->shop->view['wpsg_mod_deliverytime']['deliverytime'] = $produkt_data['wpsg_mod_deliverytime_deliverytime'];
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_deliverytime/produkt_edit_allgemein.phtml');
			
		} // public function produkt_edit_allgemein(&$produkt_data)
		
		public function produkt_save_before(&$produkt_data)
		{
				
			$produkt_data['wpsg_mod_deliverytime_deliverytime'] = wpsg_q($_REQUEST['wpsg_mod_deliverytime']['deliverytime']);
				
		} // public function produkt_save_before(&$produkt_data)
		
		public function mail_row($index, $produkt) 
		{ 
			
			if ($this->shop->get_option('wpsg_mod_deliverytime_show_mail') != '1') return;
			
			$this->shop->view['wpsg_mod_deliverytime']['deliverytime'] = $this->getProductDeliveryTime($this->shop->getProduktID($produkt['id']));
			
			if ($this->shop->htmlMail === true)
			{
					
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_deliverytime/mail_row_html.phtml');
					
			}
			else
			{
			
				$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_deliverytime/mail_row.phtml');
					
			}
			
		} // public function mail_row($index, $produkt)
		
		public function basket_row(&$p, $i) 
		{ 
			
			if ($this->shop->get_option('wpsg_mod_deliverytime_show_basket') != '1') return;
			
			$this->shop->view['wpsg_mod_deliverytime']['deliverytime'] = $this->getProductDeliveryTime($this->shop->getProduktID($p['id']));
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_deliverytime/basket_row.phtml');
			
		} // public function basket_row(&$p, $i)
		
		public function overview_row(&$p, $i) 
		{ 

			if ($this->shop->get_option('wpsg_mod_deliverytime_show_overview') != '1') return;
			
			$this->shop->view['wpsg_mod_deliverytime']['deliverytime'] = $this->getProductDeliveryTime($this->shop->getProduktID($p['id']));
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_deliverytime/overview_row.phtml');
			
		} // public function overview_row(&$p, $i)
		
		public function wpsg_mod_export_loadFields(&$arFields) 
		{ 
			
			$arFields[20]['fields']['product_deliverytime'] = __('Lieferzeit', 'wpsg');
			
		} // public function wpsg_mod_export_loadFields(&$arFields)
		
		public function wpsg_mod_export_getValue(&$return, $field_value, $o_id, $p_id, $productkey, $product_index, $profil_separator) 
		{ 
			
			if ($field_value == 'product_deliverytime')
			{
				
				$return = $this->getProductDeliveryTime($p_id);
				
			}
			
		} // public function wpsg_mod_export_getValue(&$return, $field_value, $o_id, $p_id, $productkey, $product_index, $profil_separator)
		
		/* Modul Functions */
		
		/**
		 * Gibt die Lieferzeit eines Produktes anhand seiner ID zurück
		 * @param unknown $product_id
		 */
		public function getProductDeliveryTime($product_id)
		{
			
			$product_data = $this->shop->cache->loadProduct($product_id);
			
			if ($this->shop->get_option('wpsg_mod_deliverytime_mode') == self::MODE_DAYS)
			{
			
				if (!wpsg_isSizedString($product_data['wpsg_mod_deliverytime_deliverytime']))
				{
				
					return $this->shop->get_option('wpsg_mod_deliverytime_mode_days_default').__(' Tage', 'wpsg');	
				
				}
				else
				{
					
					return $product_data['wpsg_mod_deliverytime_deliverytime'].__(' Tage', 'wpsg');
					
				}
				
			}
			else if ($this->shop->get_option('wpsg_mod_deliverytime_mode') == self::MODE_SELECT)
			{
			
				$arPossibleDeliveryTimes = $this->getPossibleSelection();
				
				if (!in_array($product_data['wpsg_mod_deliverytime_deliverytime'], $arPossibleDeliveryTimes))
				{
					
					return __($this->shop->get_option('wpsg_mod_deliverytime_mode_select_default'), 'wpsg');
										
				}
				else
				{
					
					return __($product_data['wpsg_mod_deliverytime_deliverytime'], 'wpsg');
					
				}
				
			}
			
		} // public function getProductDeliveryTime($product_id)
		
		/**
		 * Gibt die möglichen Lieferzeiten als Array zurück, ist nur Sinnvoll wenn 
		 * MODE_SELECT aktiv ist.
		 */
		public function getPossibleSelection()
		{
			
			return wpsg_trim(explode(',', $this->shop->get_option('wpsg_mod_deliverytime_mode_select_values')));
			
		} // public function getPossibleSelection()
						
	} // class wpsg_mod_deliverytime extends wpsg_mod_basic

?>