<?php
class Booking {
	private $aid = '359964';
	private $api_key = '';
	private $hotelids = '';
	private $lang = 'en';
	private $checkin = '';
	private $checkout = '';
	
	private $checkin_monthday = '';
	private $checkin_year_month = '';
	
	private $checkout_monthday = '';
	private $checkout_year_month = '';
	
	private $currency = 'EUR';
	private $currencySymbol = 'EUR';
	
	private $do_availability_check = 1;
	
	private $roomsCount = 0;
	
	private $org_nr_adults = 0;
	private $org_nr_children = 0;
	
	
	private $rooms = array();
	
	
	public function __construct($aff_data, $hotelId = "") {
		if(isset($aff_data['cid']) ) $this->aid = $aff_data['cid'];
		$this->hotelids = $hotelId;
		
		if(isset($aff_data['checkin']) && trim($aff_data['checkin']) != '') {
			$aff_data['checkin'] = $this->date_conv($aff_data['checkin'], 'd/m/Y', 'Y-m-d');
			
			$this->checkin = date("Y-m-d", strtotime($aff_data['checkin']));
			$this->checkin_monthday = date("d", strtotime(trim($aff_data['checkin'])));
			$this->checkin_year_month = date("Y-m", strtotime(trim($aff_data['checkin'])));
		}
		if(isset($aff_data['checkout']) && trim($aff_data['checkout']) != '') {
			
			$aff_data['checkout'] = $this->date_conv($aff_data['checkout'], 'd/m/Y', 'Y-m-d');
			
			$this->checkout = date("Y-m-d", strtotime($aff_data['checkout']));
			$this->checkout_monthday = date("d", strtotime($aff_data['checkout']));
			$this->checkout_year_month = date("Y-m", strtotime(trim($aff_data['checkout'])));
		}
		
		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']);
					$this->org_nr_adults += $rooms_tmp[$i]['adultsCount'];
				}
				else {
					$rooms_tmp[$i]['adultsCount'] = 1;
					$this->org_nr_adults += $rooms_tmp[$i]['adultsCount'];
				}
				
				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']);
							$this->org_nr_children += $rooms_tmp[$i]['childrenCount'];
						}
						else {
							$rooms_tmp[$i]['childrenCount'] = 0;
							unset($rooms_tmp[$i]['children']);
						}
					}
				}
				
			}
			if(count($rooms_tmp) > 0) $rooms = $rooms_tmp;
			
			
			$this->rooms = $rooms;
			
		}
		
	}
	
	public function search_query($aff_data) {
		$url = 'https://www.booking.com/searchresults.html?';
		
		$dest = (isset($aff_data['ck_dest']) && trim($aff_data['ck_dest']) != "") ? trim($aff_data['ck_dest']) : 's|Cyprus';
		
		$params = array(
			'aid'	=> '359964',
			'lang'	=> 'en'
		);
		
		$url .= 'aid=' . $params['aid'];
		$url .= '&amp;' . 'lang=' . $params['lang'];
		
		$dest_ar = explode("|", $dest); 
		if($dest_ar[0] == "r") $params['region'] = $dest_ar[1];
		if($dest_ar[0] == "c") $params['city'] = $dest_ar[1];
		if($dest_ar[0] == "s") $params['ss'] = $dest_ar[1];
		
		if(isset($params['region'])) {
			$url .= '&amp;' . 'region=' . urlencode($params['region']);
		}
		
		if(isset($params['city'])) {
			$url .= '&amp;' . 'city=' . urlencode($params['city']);
		}
		
		if(isset($params['ss'])) {
			$url .= '&amp;' . 'ss=' . urlencode($params['ss']);
			$url .= '&amp;' . 'si=' . urlencode('co');
		}
		
		if(isset($aff_data['checkin']) && trim($aff_data['checkin']) != '') {
			$aff_data['checkin'] = $this->date_conv($aff_data['checkin'], 'd/m/Y', 'Y-m-d');
			
			
			$params['checkin_monthday'] = date("d", strtotime(trim($aff_data['checkin'])));
			$params['checkin_year_month'] = date("Y-m", strtotime(trim($aff_data['checkin'])));
			
			$url .= '&amp;' . 'checkin_monthday=' . urlencode($params['checkin_monthday']);
			$url .= '&amp;' . 'checkin_year_month=' . urlencode($params['checkin_year_month']);
			
			
		}
		if(isset($aff_data['checkout']) && trim($aff_data['checkout']) != '') {
			
			$aff_data['checkout'] = $this->date_conv($aff_data['checkout'], 'd/m/Y', 'Y-m-d');
			
			$params['checkout_monthday'] = date("d", strtotime($aff_data['checkout']));
			$params['checkout_year_month'] = date("Y-m", strtotime(trim($aff_data['checkout'])));
			
			$url .= '&amp;' . 'checkout_monthday=' . urlencode($params['checkout_monthday']);
			$url .= '&amp;' . 'checkout_year_month=' . urlencode($params['checkout_year_month']);
			
		}
		
		if(isset($aff_data['group_adults']) && is_array($aff_data['group_adults'])) {
			foreach ($aff_data['group_adults'] as $i=>$ga) {
				if(intval($ga) > 0) {
					if(!isset($params['group_adults'])) $params['group_adults'] = array();
					$params['group_adults'] [] = $ga;
				}
			}
		}
		
		if(isset($aff_data['group_children']) && is_array($aff_data['group_children'])) {
			foreach ($aff_data['group_children'] as $i=>$ga) {
				if(intval($ga) > 0) {
					if(!isset($params['group_children'])) $params['group_children'] = array();
					$params['group_children'] [] = $ga;
				}
			}
		}
		
		if(isset($aff_data['age']) && is_array($aff_data['age'])) {
			foreach ($aff_data['age'] as $i=>$ga) {
				if(intval($ga) > 0) {
					if(!isset($params['age'])) $params['age'] = array();
					$params['age'] [] = $ga;
				}
			}
		}
		
		/*
		&in_a_group=on
		&org_nr_rooms=1
		&org_nr_adults=2
		&org_nr_children=2
		&group_config=
		*/
		
		if(isset($params['group_adults'])) $params['in_a_group'] = 'on';
		$params['org_nr_rooms'] = intval($aff_data['rooms']);
		$params['org_nr_adults'] = intval($aff_data['adults']);
		if(isset($aff_data['children']) && intval($aff_data['children']) > 0) $params['org_nr_children'] = intval($aff_data['children']);
		
		
		
		$url .= '&amp;' . 'org_nr_rooms=' . urlencode($params['org_nr_rooms']);
		$url .= '&amp;' . 'org_nr_adults=' . urlencode($params['org_nr_adults']);
		
		if(isset($params['org_nr_children'])) {
			$url .= '&amp;' . 'org_nr_children=' . urlencode($params['org_nr_children']);
		}
		
		if(isset($params['in_a_group'])) {
			$url .= '&amp;' . 'in_a_group=' . urlencode($params['in_a_group']);
			$url .= '&amp;' . 'group_config=';
			
			$age_cnt = 0;
			
			for($i=0; $i<$params['org_nr_rooms']; $i++) {
				if(intval($params['group_adults'][$i]) > 0) {
					$url .= '&amp;' . 'group_adults=' . urlencode($params['group_adults'][$i]);
				}
				
				if(isset($params['group_children']) && intval($params['group_children'][$i]) > 0) {
					$url .= '&amp;' . 'group_children=' . urlencode($params['group_children'][$i]);
					for($j=$age_cnt; $j<($age_cnt + $params['group_children'][$i]); $j++) {
						$url .= '&amp;' . 'age=' . urlencode($params['age'][$j]);
						
					}
					$age_cnt += $params['group_children'][$i];
				}
				
				
			}
		}
		
		$url = str_replace('&amp;', '&', $url);
		
		//echo 'URL: ' . $url ;
		//echo '<pre>', var_export($params, true), '</pre>';
		return $url;
	}
	
	
	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://www.booking.com/searchresults.html?aid=' . $this->aid . '&hotelids='.$this->hotelids;
		
		$deep_link .= '&lang=' . $this->lang;
		
		if($this->roomsCount > 0) {
			$deep_link .= '&in_a_group=on&org_nr_rooms=' . $this->roomsCount;
		}
		
		if($this->org_nr_adults > 0) {
			$deep_link .= '&org_nr_adults=' . $this->org_nr_adults;
		}
		
		if($this->org_nr_children > 0) {
			$deep_link .= '&org_nr_children=' . $this->org_nr_children;
		}
		
		if(count($this->rooms) > 0) {
			foreach ($this->rooms as $i=>$room) {
				$tmp = '';
				if(isset($room['adultsCount'])) {
					$tmp .= '&group_adults=' . $room['adultsCount'];
				}
				
				if(isset($room['childrenCount']) && intval($room['childrenCount']) > 0) {
					$tmp .= '&group_children=' . $room['childrenCount'];
					foreach ($room['children'] as $k=>$age) {
						$tmp .= '&age=' . $age;
					}
				}
				
				$deep_link .= $tmp;
			}
		}
		
		
		
		if($this->checkin != "") {
			$deep_link .= '&checkin_monthday=' . rawurlencode($this->checkin_monthday);
			$deep_link .= '&checkin_year_month=' . rawurlencode($this->checkin_year_month);
		}
		
		if($this->checkout != "") {
			$deep_link .= '&checkout_monthday=' . rawurlencode($this->checkout_monthday);
			$deep_link .= '&checkout_year_month=' . rawurlencode($this->checkout_year_month);
		}
		
		$deep_link .= '&dest_type=hotel';
		$deep_link .= '&dest_id=' . $this->hotelids;
		
		//$deep_link .= '&currency=' . $this->currency;
		//$deep_link .= '&do_availability_check=1';
		
		//echo '<br/><a href="' .$deep_link . '" target="_blank">' . $deep_link . '</a>';
		
		return $deep_link;
	}
	
	public function getDeepLinkHotel() {
		$deep_link = 'https://www.booking.com/searchresults.html?aid='.$this->aid.'&hotelids=' . $this->hotelids;
		$deep_link .= '&lang=' . $this->lang;
		$deep_link .= '&dest_type=hotel';
		$deep_link .= '&dest_id=' . $this->hotelids;
		return $deep_link;
	}
	
	public function getDeepLinkHotelDates() {
		$deep_link = 'https://www.booking.com/searchresults.html?aid='.$this->aid.'&hotelids=' . $this->hotelids;
		$deep_link .= '&lang=' . $this->lang;
		
		if($this->checkin != "") {
			$deep_link .= '&checkin_monthday=' . rawurlencode($this->checkin_monthday);
			$deep_link .= '&checkin_year_month=' . rawurlencode($this->checkin_year_month);
		}
		
		if($this->checkout != "") {
			$deep_link .= '&checkout_monthday=' . rawurlencode($this->checkout_monthday);
			$deep_link .= '&checkout_year_month=' . rawurlencode($this->checkout_year_month);
		}
		
		$deep_link .= '&dest_type=hotel';
		$deep_link .= '&dest_id=' . $this->hotelids;
		
		return $deep_link;
	}
	
	public function getDeepLinkHotelDestination($destination) {
		$destination = stripslashes($destination);
		$deep_link = '';
		$region = $destination;
		$d = explode(",", $destination);

		$dest = trim($region);
		if($dest != "") {
			
			$dest = str_replace(array(' ,', ', ', ','), ' ', $dest);
			
			$dest = mysql_real_escape_string($dest);
			
			//$q = "select * from booking_destinations where full_name like '%$dest%'";
			$q = "select deeplink, MATCH(full_name) AGAINST ('$dest') as score from booking_destinations where MATCH(full_name) AGAINST ('$dest') order by score desc limit 0,1";
			//echo $q . '\n';
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) > 0) {
				$row = mysql_fetch_assoc($r);
				
				//print_r($row);
				
				$deep_link = trim($row['deeplink']);
			}
			
		}
		
		if($deep_link == "") {
		
			if(is_array($d) && (count($d) == 2) ) {
				if(trim($d[1]) != "") $region = trim($d[1]);
			}
			
			$region = mysql_escape_string($region);
			
			$q = "select * from booking_regions where region_name like '%$region%'";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) > 0) {
				$row = mysql_fetch_assoc($r);
				
				$deep_link = $row['deeplink'];
			}
		
		}
		
		if($deep_link == "") {
			$deep_link = 'https://www.booking.com/country/cy.html';
		}
		
		
		if($deep_link != "") {
			$deep_link .= '?aid='.$this->aid;
			
			//http://www.booking.com/searchresults.html?src=index&error_url=http%3A%2F%2Fwww.booking.com%2Findex.en-gb.html%3Fsid%3D092d57b118a013b78f382e01be6e0498%3Bdcid%3D1%3B&dcid=1&lang=en-gb&sid=092d57b118a013b78f382e01be6e0498&si=ai%2Cco%2Cci%2Cre%2Cdi&ss=Limassol&checkin_monthday=8&checkin_year_month=2013-4&checkout_monthday=11&checkout_year_month=2013-4&org_nr_rooms=1&org_nr_adults=2&org_nr_children=0&group_adults=2&group_children=0&dest_type=region&dest_id=1969
			
			$deep_link .= '&lang=' . $this->lang;
			
			if($this->checkin != "") {
				$deep_link .= '&checkin_monthday=' . rawurlencode($this->checkin_monthday);
				$deep_link .= '&checkin_year_month=' . rawurlencode($this->checkin_year_month);
			}
			
			if($this->checkout != "") {
				$deep_link .= '&checkout_monthday=' . rawurlencode($this->checkout_monthday);
				$deep_link .= '&checkout_year_month=' . rawurlencode($this->checkout_year_month);
			}
		}
		
		
		return $deep_link;
	}
	
	/*
	http://www.booking.com/searchresults.html?aid=359964&hotelids=10340&lang=en
	&checkin_monthday=10
	&checkin_year_month=2012-06
	&checkout_monthday=12
	&checkout_year_month=2012-06
	&do_availability_check=1
	
	
	http://www.booking.com/searchresults.html?src=searchresults&city=-2738495&order=popularity&ssne=Limassol&ssne_untouched=Limassol&error_url=http%3A%2F%2Fwww.booking.com%2Fsearchresults.en-us.html%3Faid=7344170&label=hotel-241758_T-3_Pool-com_Slice-99999&sid=8ca8cb17beec6b36f3f18688a21779ab&dcid=1&
	checkin_monthday=25&
	checkin_year_month=2013-3
	&checkout_monthday=30
	&checkout_year_month=2013-3
	&city=-2738495
	&class_interval=1
	&csflt=%257B%257D
	&dest_id=-2738495
	&dest_type=city
	&highlighted_hotels=241758
	&inac=0
	&offset=0
	&order=popularity
	&redirected_from_city=0
	&redirected_from_landmark=0
	&review_score_group=empty
	&score_min=0
	&si=ai%252Cco%252Cci%252Cre%252Cdi
	&src=searchresults
	&ss=Limassol
	&ss_all=0
	&ssb=empty
	&ssne=Limassol
	&ssne_untouched=Limassol
	&radius=0&
	&aid=7344170
	&dcid=1
	&label=hotel-241758_T-3_Pool-com_Slice-99999
	&sid=8ca8cb17beec6b36f3f18688a21779ab
	&si=ai%2Cco%2Cci%2Cre%2Cdi
	&ss=Limassol&radius=
	
	&checkin_monthday=25
	&checkin_year_month=2013-3
	&checkout_monthday=30
	&checkout_year_month=2013-3
	&in_a_group=on
	&org_nr_rooms=1
	&org_nr_adults=2
	&org_nr_children=2
	&group_config=
	&group_adults=1
	&group_children=1
	&age=4
	&group_adults=1
	&group_children=2
	&age=0
	&age=7
	&highlighted_hotels=241758
	&dest_type=city
	&dest_id=-2738495
	
	
	&in_a_group=on
	&org_nr_rooms=2
	&org_nr_adults=2
	&org_nr_children=3
	&group_config=
	
	&group_adults=1
	&group_children=1
	&age=4
	
	&group_adults=1
	&group_children=2
	&age=1
	&age=7
	
	
	&highlighted_hotels=241758&dest_type=city&dest_id=-2738495
	
	*/
}
?>