<?php

	/**
	 * wpShopGermany Modul zur Paketverfolung
	 * @author Daschmi (23.10.2014)
	 */
	class wpsg_mod_packagetracking extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 3130;
		var $hilfeURL = 'http://wpshopgermany.maennchen1.de/?p=661503';
		var $inline = true;
		var $version = "9.9.9";
		
		public function __construct()
		{
		
			parent::__construct();
		
			$this->name = __('Paketverfolgung', 'wpsg');
			$this->group = __('Versand', 'wpsg');
			$this->desc = __('Ermöglicht Verwaltung und zusendung von Tracking Codes zur Paketverfolgung.', 'wpsg');
		
			if (!defined('WPSG_TBL_TRACKINGPROVIDER')) define('WPSG_TBL_TRACKINGPROVIDER', $this->shop->prefix.'wpsg_trackingprovider');
			
		} // public function __construct()
		
		public function install()
		{
		
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
		
			/* Tabelle der Tracking Anbieter */ 
			$sql = "CREATE TABLE ".WPSG_TBL_TRACKINGPROVIDER." (
				id mediumint(9) NOT NULL AUTO_INCREMENT,				
				name VARCHAR(255) NOT NULL,
				url VARCHAR(500) NOT NULL,			
				deleted INT(11) NOT NULL,	
			  	PRIMARY KEY  (id)			  	
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			
			dbDelta($sql);					
			
			/* Tracking Code in der Bestellung */
			$sql = "CREATE TABLE ".WPSG_TBL_ORDER." (
		   		wpsg_mod_packagetracking_code VARCHAR(255) NOT NULL,
				wpsg_mod_packagetracking_provider INT(11) NOT NULL,
		   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		
			dbDelta($sql);
		
			/* Spalte für die Voreinstellung bei der Versandart */ 
			$sql = "CREATE TABLE ".WPSG_TBL_VA." (
				wpsg_mod_packagetracking_provider INT(11) NOT NULL
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
				
			dbDelta($sql);
			
			$nExists = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_TRACKINGPROVIDER."`");
			
			if ($nExists <= 0)
			{
				
				/* Voreinstellungen */
				$arPreset = array(
					array(
						'name' => 'DHL',
						'url' => 'http://nolp.dhl.de/nextt-online-public/track.do?idc=%order_trackingid%&lang=de'
					),
					array(
						'name' => 'DHL-Intraship',
						'url' => 'http://nolp.dhl.de/nextt-online-public/set_identcodes.do?lang=de&idc=%order_trackingid%'
					),
					array(
						'name' => 'Hermes - Profipaketservice',
						'url' => 'https://www.myhermes.de/wps/portal/paket/SISYR?auftragsNummer=%order_trackingid%'
					),
					array(
						'name' => 'Hermes - Privatpaketservice',
						'url' => 'http://privatpaketservice.hlg.de/wps/portal/SENDUNGSSTATUS?auftragsNummerN=%order_trackingid%'
					),
					array(
						'name' => 'DPD',
						'url' => 'http://www.dpd.de/cgi-bin/delistrack?typ=1&lang=de&pknr=%order_trackingid%'
					),
					array(
						'name' => 'DPD (Extranet)',
						'url' => 'http://extranet.dpd.de/cgi-bin/delistrack?typ=1&lang=de&pknr=%order_trackingid%'
					),
					array(
						'name' => 'DeutschePost Einschreiben',
						'url' => 'http://nolp.dhl.de/nextt-online-public/set_identcodes.do?lang=de&idc=%order_trackingid%'
					),
					array(
						'name' => 'GLS',
						'url' => 'http://www.gls-germany.com/de/online/paketstatus.php3?datatype=paketnr&filter=all&paketliste[0]=%order_trackingid%&search_x=1'
					),
					array(
						'name' => 'GLS Europe',
						'url' => 'http://www.gls-group.eu/276-I-PORTAL-WEB/content/GLS/DE03/EN/5004.htm?txtRefNo=%order_trackingid%&txtAction=71000'
					),
					array(
						'name' => 'UPS',
						'url' => 'http://wwwapps.ups.com/ietracking/tracking.cgi?tracknum=%order_trackingid%&IATA=de&Lang=ger'
					)
				);
				
				foreach ($arPreset as $p)
				{
				
					$nID = $this->db->ImportQuery(WPSG_TBL_TRACKINGPROVIDER, array(
						'name' => $p['name'],
						'url' => $p['url']
					));
				
					$this->shop->addTranslationString('wpsg_mod_packagetracking_'.$nID.'_name', $p['name']);
					$this->shop->addTranslationString('wpsg_mod_packagetracking_'.$nID.'_url', $p['url']);

				}
				
			}
			
		} // public function install()
		
		public function settings_edit()
		{
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_packagetracking/settings_edit.phtml');
			
		} // public function settings_edit()
				
		public function be_ajax()
		{
				
			if ($_REQUEST['do'] == 'add')
			{
				
				$dummy_name = __('Klicken Sie hier, um den Namen zu ändern.', 'wpsg');
				$dummy_url = __('http://tracking/%order_trackingid%', 'wpsg');
				
				$tp_id = $this->db->ImportQuery(WPSG_TBL_TRACKINGPROVIDER, array(
					'name' => $dummy_name,
					'url' => $dummy_url						
				));
				
				$this->shop->addTranslationString('wpsg_mod_packagetracking_'.$tp_id.'_name', $dummy_name);
				$this->shop->addTranslationString('wpsg_mod_packagetracking_'.$tp_id.'_url', $dummy_url);
				
				die ($this->renderTrackingList());
				
			}
			else if ($_REQUEST['do'] == 'delete')
			{
				
				if (!wpsg_isSizedInt($_REQUEST['trackingprovider_id'])) $this->shop->throwErrorCode('3130_3');
				
				$tp_id = $this->db->UpdateQuery(WPSG_TBL_TRACKINGPROVIDER, array(
					'deleted' => '1'						
				), " `id` = '".wpsg_q($_REQUEST['trackingprovider_id'])."' ");

				die($this->renderTrackingList());
				
			}
			else if ($_REQUEST['do'] == 'inlinedit')
			{
				
				if (!wpsg_isSizedInt($_REQUEST['trackingprovider_id'])) $this->shop->throwErrorCode('3130_2');
				
				if ($_REQUEST['field'] == 'name')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_TRACKINGPROVIDER, array(
						'name' => wpsg_q($_REQUEST['value'])							
					), " `id` = '".wpsg_q($_REQUEST['trackingprovider_id'])."' ");
					
					$this->shop->addTranslationString('wpsg_mod_packagetracking_'.$_REQUEST['trackingprovider_id'].'_name', $_REQUEST['value']);
					
					die($_REQUEST['value']);
					
				}
				else if ($_REQUEST['field'] == 'url')
				{
					
					$this->db->UpdateQuery(WPSG_TBL_TRACKINGPROVIDER, array(
						'url' => wpsg_q(html_entity_decode($_REQUEST['value']))
					), " `id` = '".wpsg_q($_REQUEST['trackingprovider_id'])."' ");
						
					$this->shop->addTranslationString('wpsg_mod_packagetracking_'.$_REQUEST['trackingprovider_id'].'_url', $_REQUEST['value']);
					
					die($_REQUEST['value']);
					
				}
				
				$this->shop->throwErrorCode('3130_1');
				
			}
			else if ($_REQUEST['do'] == 'setDefaultProvider')
			{
				
				$this->db->UpdateQuery(WPSG_TBL_VA, array(
					'wpsg_mod_packagetracking_provider' => wpsg_q($_REQUEST['trackingprovider_id'])
				), " `id` = '".wpsg_q($_REQUEST['va_id'])."' ");
				
				die("1");
				
			}
			
		} // public function be_ajax()
		
		public function wpsg_mod_versandarten_listrow(&$va) 
		{

			$this->shop->view['wpsg_mod_packagetracking']['va'] = $va;
			$this->shop->view['wpsg_mod_packagetracking']['provider'] = $this->db->fetchAssoc("
				SELECT
					*
				FROM
					`".WPSG_TBL_TRACKINGPROVIDER."`
				WHERE
					`deleted` != '1'
				ORDER BY
					`id` ASC	
			");
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_packagetracking/wpsg_mod_versandarten_listrow.phtml');
			
		} // public function wpsg_mod_versandarten_listrow(&$va)
		
		public function order_view_switchStatus(&$order_id)  
		{
			
			$oOrder = wpsg_order::getInstance($order_id);
			
			/* Voreinstellung */
			$this->shop->view['wpsg_mod_packagetracking']['preset'] = 0;
			
			if (wpsg_isSizedInt($oOrder->wpsg_mod_packagetracking_provider)) 
			{
				
				$this->shop->view['wpsg_mod_packagetracking']['preset'] = $oOrder->wpsg_mod_packagetracking_provider;
				
			}
			else if (preg_match('/6_\d+/', $oOrder->type_shipping))
			{
					
				$va_id = substr($oOrder->type_shipping, 2);
				
				$va_preset = $this->db->fetchOne("SELECT `wpsg_mod_packagetracking_provider` FROM `".WPSG_TBL_VA."` WHERE `id` = '".wpsg_q($va_id)."' ");

				if (wpsg_isSizedInt($va_preset)) $this->shop->view['wpsg_mod_packagetracking']['preset'] = $va_preset;
					
			}			
			
			$this->shop->view['wpsg_mod_packagetracking']['oOrder'] = $oOrder;
			$this->shop->view['wpsg_mod_packagetracking']['provider'] = $this->db->fetchAssoc("
				SELECT
					*
				FROM
					`".WPSG_TBL_TRACKINGPROVIDER."`
				WHERE
					`deleted` != '1' OR `id` = '".wpsg_q($this->shop->view['wpsg_mod_packagetracking']['preset'])."'
				ORDER BY
					`id` ASC
			");
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_packagetracking/order_view_switchStatus.phtml');
			
		} // public function order_view_switchStatus(&$db_order)
		
		public function setOrderStatus($order_id, $status_id, $inform) 
		{ 
			
			if (isset($_REQUEST['wpsg_mod_packagetracking_provider']))
			{
				
				$this->db->UpdateQuery(WPSG_TBL_ORDER, array(
					'wpsg_mod_packagetracking_code' => wpsg_q($_REQUEST['wpsg_mod_packagetracking_code']),
					'wpsg_mod_packagetracking_provider' => wpsg_q($_REQUEST['wpsg_mod_packagetracking_provider'])
				), " `id` = '".wpsg_q($order_id)."' ");
												
			}
			
		} // public function setOrderStatus($order_id, $status_id, $inform)
		
		public function mail_status(&$order, &$customer) 
		{ 
			 
			if ($_REQUEST['status'] != 250 || !wpsg_isSizedInt($order['wpsg_mod_packagetracking_provider']) || !wpsg_isSizedString($order['wpsg_mod_packagetracking_code'])) return;
			
			$this->shop->view['wpsg_mod_packagetracking']['trackingURL'] = $this->getTrackingURL($order['id']);
			
			if ($this->shop->htmlMail === true)
			{

				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_packagetracking/mail_status_html.phtml');
				
			}
			else
			{
			
				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_packagetracking/mail_status.phtml');
				
			}
			 
		} // public function mail_status(&$order, &$customer, &$status_old, &$status_neu)
		
		/* Modulfunktionen */
		
		public function getTrackingURL(&$order_id)
		{

			$oOrder = wpsg_order::getInstance($order_id, true);
			
			if (!wpsg_isSizedString($oOrder->wpsg_mod_packagetracking_code) || !wpsg_isSizedInt($oOrder->wpsg_mod_packagetracking_provider)) return '';

			$url = $this->db->fetchOne("SELECT `url` FROM `".WPSG_TBL_TRACKINGPROVIDER."` WHERE `id` = '".wpsg_q($oOrder->wpsg_mod_packagetracking_provider)."' ");

			if (!wpsg_isSizedString($url)) return '';
			
			$url = $this->shop->replaceUniversalPlatzhalter($url, $order_id, false, false, false, array(
				'/%order_trackingid%/i' => $oOrder->wpsg_mod_packagetracking_code
			));
			
			return $url;
			
		} // public function getTrackingURL(&$order_id)
		
		public function renderTrackingList()
		{
			
			$this->shop->view['wpsg_mod_packagetracking']['provider'] = $this->db->fetchAssoc("
				SELECT
					*
				FROM
					`".WPSG_TBL_TRACKINGPROVIDER."`
				WHERE
					`deleted` != '1'
				ORDER BY
					`id` ASC
			");
			
			return $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_packagetracking/settings_edit_list.phtml');
			
		} // public function renderTrackingList()
		
	} // class wpsg_mod_packagetracking extends wpsg_mod_basic

?>