<?php
class Expedia {
	private $cid = '';
	private $api_key = '';
	private $hotelId = '';
	private $lang = 'en';
	private $checkin = '';
	private $checkout = '';
	private $currency = 'EUR';
	private $currencySymbol = 'EUR';
	private $roomsCount = 0;
	private $rooms = array();
	
	public function __construct($aff_data, $hotelId = "") {
		$this->cid = $aff_data['cid'];
		$this->hotelId = $hotelId;
		
		if(isset($aff_data['checkin']) && trim($aff_data['checkin']) != '') {
			//$this->checkin = date("m/d/Y", strtotime(trim($aff_data['checkin'])));
			$this->checkin = $this->date_conv(trim($aff_data['checkin']), 'd/m/Y', 'm/d/Y');
		}
		if(isset($aff_data['checkout']) && trim($aff_data['checkout']) != '') {
			//$this->checkout = date("m/d/Y", strtotime(trim($aff_data['checkout'])));
			$this->checkout = $this->date_conv(trim($aff_data['checkout']), 'd/m/Y', 'm/d/Y');
		}
		
		if(isset($aff_data['roomsCount']) && intval($aff_data['roomsCount']) >=1) $this->roomsCount = intval($aff_data['roomsCount']);
		
		if($this->roomsCount > 0) {
			$rooms = array();
			$rooms_tmp = array();
			for ($i=0; $i<$this->roomsCount; $i++) {
				
				$rooms_tmp[$i] = array();
				if(isset($aff_data['rooms'][$i]['adultsCount']) && intval($aff_data['rooms'][$i]['adultsCount']) > 0) {
					$rooms_tmp[$i]['adultsCount'] = intval($aff_data['rooms'][$i]['adultsCount']);
				}
				else {
					$rooms_tmp[$i]['adultsCount'] = 1;
				}
				
				if(isset($aff_data['rooms'][$i]['childrenCount'])) {
					$rooms_tmp[$i]['childrenCount'] = intval($aff_data['rooms'][$i]['childrenCount']);
				}
				
				if(isset($rooms_tmp[$i]['childrenCount']) && intval($rooms_tmp[$i]['childrenCount']) > 0) {
					if(isset($aff_data['rooms'][$i]['children']) && is_array($aff_data['rooms'][$i]['children'])) {
						
						$rooms_tmp[$i]['children'] = array();
						
						foreach ($aff_data['rooms'][$i]['children'] as $k=>$age) {
							if(intval($age) >= 0) $rooms_tmp[$i]['children'][] = intval($age);
						}
						
						if(count($rooms_tmp[$i]['children']) > 0) $rooms_tmp[$i]['childrenCount'] = count($rooms_tmp[$i]['children']);
						else {
							$rooms_tmp[$i]['childrenCount'] = 0;
							unset($rooms_tmp[$i]['children']);
						}
					}
				}
				
			}
			if(count($rooms_tmp) > 0) $rooms = $rooms_tmp;
			
			
			$this->rooms = $rooms;
			
		}
	}
	
	public function date_conv($date, $from_format, $to_format='Y-m-d') {
		$con_date = DateTime::createFromFormat($from_format, $date);
		
		return $con_date->format($to_format);
	}
	
	public function getDeepLinkHotelBook() {
		$deep_link = 'https://expedia-affiliate.cyprus-hotel.com/templates/' . $this->cid . '/hotels/' . $this->hotelId . '/rooms?';
		
		$deep_link .= 'lang=' . $this->lang;
		
		if($this->roomsCount > 0) {
			$deep_link .= '&roomsCount=' . $this->roomsCount;
		}
		
		if(count($this->rooms) > 0) {
			foreach ($this->rooms as $i=>$room) {
				$tmp = '';
				if(isset($room['adultsCount'])) {
					$tmp .= '&rooms[' . $i . '].adultsCount=' . $room['adultsCount'];
				}
				
				if(isset($room['childrenCount']) && intval($room['childrenCount']) > 0) {
					$tmp .= '&rooms[' . $i . '].childrenCount=' . $room['childrenCount'];
					foreach ($room['children'] as $k=>$age) {
						$tmp .= '&rooms[' . $i . '].children[' . $k . '].age=' . $age;
					}
				}
				
				$deep_link .= $tmp;
			}
		}
		
		
		
		if($this->checkin != "") {
			$deep_link .= '&checkin=' . rawurlencode($this->checkin);
		}
		
		if($this->checkout != "") {
			$deep_link .= '&checkout=' . rawurlencode($this->checkout);
		}
		
		$deep_link .= '&currency=' . $this->currency;
		
		//echo '<br/><br/><a href="' .$deep_link . '" target="_blank">' . $deep_link . '</a>';
		
		return $deep_link;
	}
	
	public function getDeepLinkHotel() {
		$deep_link = 'https://expedia-affiliate.cyprus-hotel.com/templates/' . $this->cid . '/hotels/list?hotelId=' . $this->hotelId;
		$deep_link .= '&lang=' . $this->lang;
		$deep_link .= '&currency=' . $this->currency;
		//echo '<br/><br/><a href="' .$deep_link . '" target="_blank">' . $deep_link . '</a>';
		return $deep_link;
	}
	
	
	public function getDeepLinkHotelDates() {
		$deep_link = 'https://expedia-affiliate.cyprus-hotel.com/templates/' . $this->cid . '/hotels/' . $this->hotelId . '/rooms?';
		
		$deep_link .= 'lang=' . $this->lang;
		
		$deep_link .= '&roomsCount=1';
		$deep_link .= '&rooms[0].adultsCount=2';
		
		if($this->checkin != "") {
			$deep_link .= '&checkin=' . rawurlencode($this->checkin);
		}
		
		if($this->checkout != "") {
			$deep_link .= '&checkout=' . rawurlencode($this->checkout);
		}
		
		$deep_link .= '&currency=' . $this->currency;
		
		//echo '<br/><br/><a href="' .$deep_link . '" target="_blank">' . $deep_link . '</a>';
		
		return $deep_link;
	}
	
	public function getDeepLinkHotelDestination($destination) {
		//$deep_link = 'http://expedia-affiliate.cyprus-hotel.com/templates/' . $this->cid . '/hotels/' . $this->hotelId . '/list?requestVersion=V3';
		//$deep_link .= '&destination=' . urlencode($destination);
		$destinationId = '';
		$destination = stripslashes($destination);
		$deep_link = '';
		$region = $destination;
		$d = explode(",", $destination);
		
		$dest = trim($region);
		if($dest != "") {
			
			$dest = str_replace(array(' ,', ', ', ','), ' ', $dest);
			
			//$q = "select * from booking_destinations where full_name like '%$dest%'";
			$q = "select DestinationID, MATCH(Destination) AGAINST ('$dest') as score from expedia_destinations where MATCH(Destination) AGAINST ('$dest') order by score desc limit 0,1";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) > 0) {
				$row = mysql_fetch_assoc($r);
				$destinationId = trim($row['DestinationID']);
			}
		}
		
		$deep_link = 'https://expedia-affiliate.cyprus-hotel.com/templates/' . $this->cid . '/hotels/' . $this->hotelId . '/list';
		
		//&datelessValidating=false
		//&pagename=ToSearchResults&linkId=Home:Hot:SrcWiz:Search
		$deep_link .= '?lang=' . $this->lang;
		
		if($destinationId != "") {
			$deep_link .= '&targetId=AREA-' . $destinationId . urlencode('|') . 'cities';
		}
		
		
		if($this->roomsCount > 0) {
			$deep_link .= '&roomsCount=' . $this->roomsCount;
		}
		
		if(count($this->rooms) > 0) {
			foreach ($this->rooms as $i=>$room) {
				$tmp = '';
				if(isset($room['adultsCount'])) {
					$tmp .= '&rooms[' . $i . '].adultsCount=' . $room['adultsCount'];
				}
				
				if(isset($room['childrenCount']) && intval($room['childrenCount']) > 0) {
					$tmp .= '&rooms[' . $i . '].childrenCount=' . $room['childrenCount'];
					foreach ($room['children'] as $k=>$age) {
						$tmp .= '&rooms[' . $i . '].children[' . $k . '].age=' . $age;
					}
				}
				
				$deep_link .= $tmp;
			}
		}
		
		if($this->checkin != "" && $this->checkout != "") {
			$deep_link .= '&checkin=' . rawurlencode($this->checkin);
			$deep_link .= '&checkout=' . rawurlencode($this->checkout);
			$deep_link .= 'datelessValidating=false';
		}
		else {
			$deep_link .= 'datelessValidating=true';
		}
		
		
		$deep_link .= '&currency=' . $this->currency;
		
		//echo '<br/><br/><a href="' .$deep_link . '" target="_blank">' . $deep_link . '</a>';
		
		return $deep_link;
	}
	
	//http://expedia-affiliate.cyprus-hotel.com/templates/421017/hotels/223446/rooms?roomsCount=1&rooms[0].adultsCount=2&rooms[0].childrenCount=2&rooms[0].children[0].age=4&rooms[0].children[1].age=7&checkin=3%2F25%2F13&checkout=3%2F28%2F13&currency=EUR&currencySymbol=%E2%82%AC&lang=en
}
?>

