<?php

	require_once WPSG_PATH_MOD.'mod_statistics/wpsg_statisticabstract.php';
	
	/**
	 * Darstellung der Kundenkarte
	 * @author daniel
	 */
	class wpsg_customerMap extends wpsg_statisticabstract
	{
		
		public function getAll($point = 0, $params = array())
		{
			
			$this->shop->view['wpsg_mod_statistics']['marker'] = array();
			
			$arCustomer = $this->db->fetchAssoc("SELECT * FROM `".WPSG_TBL_KU."` ");
			
			foreach ($arCustomer as $c)
			{
				
				if (!wpsg_isSizedString($c['wpsg_mod_statistics_long']) && !wpsg_isSizedString($c['wpsg_mod_statistics_lat']))
				{
					 
					$arLongLat = $this->geocode($c['plz'].' '.$c['ort'].','.$c['strasse']);

					if ($arLongLat !== null)
					{
							
						$this->db->UpdateQuery(WPSG_TBL_KU, array(
							'wpsg_mod_statistics_long' => wpsg_q($arLongLat['long']),
							'wpsg_mod_statistics_lat' => wpsg_q($arLongLat['lat'])
						), "`id` = '".wpsg_q($c['id'])."'");
					
						$this->shop->view['wpsg_mod_statistics']['marker'][] = array(
								'long' => $arLongLat['long'],
								'lat' => $arLongLat['lat'],
								'customer' => $c
						);
							
					}
					else
					{
						
						// Adresse konnte nicht dekodiert werden
						$this->db->UpdateQuery(WPSG_TBL_KU, array(
							'wpsg_mod_statistics_long' => '-1',
							'wpsg_mod_statistics_lat' => '-1'
						), "`id` = '".wpsg_q($c['id'])."'");
						
					}
					
				}				
				else if (wpsg_isSizedString($c['wpsg_mod_statistics_long']) && wpsg_isSizedString($c['wpsg_mod_statistics_lat']) && $c['wpsg_mod_statistics_long'] != '-1' && $c['wpsg_mod_statistics_lat'] != '-1')
				{
					
					$this->shop->view['wpsg_mod_statistics']['marker'][] = array(
						'long' => $c['wpsg_mod_statistics_long'],
						'lat' => $c['wpsg_mod_statistics_lat'],
						'customer' => $c
					);
					
				}
				
			} 
			
		} // public function getAll($point = 0, $params = array())
		
		/**
		 * Ermittelt aus einer Adresse die Längne und Breitengrade und gibt diese zurück
		 */
		function geocode($string)
		{
		
			$string = str_replace (" ", "+", urlencode($string));
			$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";
		
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $details_url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$response = json_decode(curl_exec($ch), true);
		
			if ($response['status'] != 'OK') 
			{
				
				return null;
				
			}
		  
			$long = $response['results'][0]['geometry']['location']['lat'];
			$lat = $response['results'][0]['geometry']['location']['lng'];
		
			$array = array(
				'lat' => $lat,
				'long' => $long
			);
		
			return $array;
		
		} // function geocode($string)
		
	} // class wpsg_customerMap extends wpsg_statisticabstract

?>