if (typeof ga == \'function\'){
ga(\'send\', \'pageview\', \''.$pagename.'\')
} ';
}
return $output;
}
function abc_booking_validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
function abc_booking_formatDate($string) {
$dateformat = getAbcSetting("dateformat");
$old_date_timestamp = strtotime($string);
$new_date = date_i18n($dateformat, $old_date_timestamp);
return $new_date;
}
function abc_booking_formatDateToDB($string) {
$dateformat = getAbcSetting("dateformat");
$old_date_timestamp = strtotime($string);
$new_date = date('Y-m-d', $old_date_timestamp);
return $new_date;
}
function abc_booking_dateDiffInDays($timestamp1, $timestamp2){
$seconds1 = date_i18n("U", $timestamp1);
$seconds2 = date_i18n("U", $timestamp2);
$diffSeconds = $timestamp1 - $timestamp2;
$diffSeconds = $diffSeconds/86400;
$days = (int)$diffSeconds;
return $days;
}
function abc_booking_dateFormatToJS($string) {
$dateformat = 'mm/dd/yy';
if($string == "Y-m-d") {
$dateformat = 'yy-mm-dd';
} elseif($string == "d.m.Y") {
$dateformat = 'dd.mm.yy';
} elseif($string == "d/m/Y") {
$dateformat = 'dd/mm/yy';
} elseif($string == "m/d/Y") {
$dateformat = 'mm/dd/yy';
}
return $dateformat;
}
function getAbcSetting($settingName) {
$output = '';
if($settingName == 'firstdayofweek'){
$output = get_option('start_of_week');
} else {
$output = get_option('abc_'.$settingName);
}
return $output;
}
function abc_booking_getTotalPrice($calendarId, $startDate, $numberOfDays) {
global $wpdb;
$calendarId = intval($calendarId);
$totalSum = 0;
$normFromValue = abc_booking_formatDateToDB($startDate);
$er = $wpdb->get_row('SELECT pricePreset FROM '.$wpdb->prefix.'advanced_booking_calendar_calendars WHERE id = '.$calendarId, ARRAY_A);
$pricePreset = $er["pricePreset"];
$query = 'SELECT * FROM `wp_advanced_booking_calendar_seasons_assignment` a
INNER JOIN `wp_advanced_booking_calendar_seasons` s
ON a.season_id = s.id
WHERE a.calendar_id = '.$calendarId.'
AND a.end >= \''.$normFromValue.'\'
ORDER BY s.lastminute DESC, a.start ASC';
$er = $wpdb->get_results($query, ARRAY_A);
$days = array();
$dayCount = 0;
if($wpdb->num_rows > 0){
foreach($er as $row) {
$time = strtotime($normFromValue);
for( $i = 0; $i < $numberOfDays; $i++) {
if(strtotime($row["start"]) <= $time && strtotime($row["end"]) >= $time && !isset($days[date_i18n("Y-m-d", $time)]) ) {
$totalSum += $row["price"];
$days[date_i18n("Y-m-d", $time)] = true;
$dayCount++;
}
$time += 86400;
}
}
if($dayCount < $numberOfDays){
$totalSum += ($numberOfDays - $dayCount)*$pricePreset;
}
}else{
for( $i = 0; $i < $numberOfDays; $i++) {
$totalSum += $pricePreset;
}
}
return $totalSum;
}
function abc_booking_getBookingVars(){
$bookingVars = array('abc_calendar_name', 'abc_total_price', 'abc_checkin_date', 'abc_checkout_date', 'abc_person_count', 'abc_first_name', 'abc_last_name',
'abc_email', 'abc_phone', 'abc_address', 'abc_zip', 'abc_city', 'abc_country', 'abc_message');
return $bookingVars;
}
function abc_booking_setContentTypeHTML($content_type){
return 'text/html';
}
// Returns a snapshot of the availability table for a booking. Used by sendAbcAdminMail()
function abc_booking_getAvailabilityOverview($bookingData){
global $wpdb;
$dateformat = getAbcSetting("dateformat");
$startDate = strtotime('-4 days', strtotime($bookingData["start"])); // +/- 4 days to show what is going on around the current booking
$endDate = strtotime('+4 days', strtotime($bookingData["end"]));
$numberOfDays = abc_booking_dateDiffInDays($endDate, $startDate);
if($numberOfDays > 20){
$endDate = strtotime('+24 days', $startDate);
}
$output = '
| | ';
for($tempDate = $startDate; $tempDate <= $endDate; $tempDate = strtotime('+1 day', $tempDate)){
$output .= ''.date_i18n('D', $tempDate).'
'.date_i18n('j', $tempDate).'
'.date_i18n('M', $tempDate).' | ';
}
$output .= '
';
$bookings = array();
$bookingQuery = $wpdb->get_results('SELECT * FROM '.$wpdb->prefix.'advanced_booking_calendar_bookings WHERE end >= "'.date_i18n("Y-m-d", $startDate).'" AND start <= "'.date_i18n("Y-m-d", $endDate).'" AND state = "confirmed" ORDER BY start', ARRAY_A);
foreach($bookingQuery as $bookingRow){
$bookings[$bookingRow["room_id"]][] = $bookingRow; // Getting all confirmed bookings for the current timeframe
}
$bookingData["room_id"] = getAbcRoomId($bookingData["calendar_id"], $bookingData["start"], $bookingData["end"]); // Adding current booking to the already confirmed bookings
$bookings[$bookingData["room_id"]][] = $bookingData;
$er = $wpdb->get_results('SELECT * FROM '.$wpdb->prefix.'advanced_booking_calendar_rooms WHERE calendar_id='.$bookingData["calendar_id"].' ORDER BY name', ARRAY_A);
foreach($er as $rooms) {
$output .= '| '.$rooms["name"].' | ';
$roomRowDate = $startDate;
for($i = 0; $i < ($numberOfDays*2); $i++){
$colSpan = 1;
if (isset($bookings[$rooms["id"]])){ // Checking for bookings for the current room
$success = false;
for($j = 0; $j < count($bookings[$rooms["id"]]); $j++){
// Checking if a booking started before startDate
if($bookings[$rooms["id"]][$j]["start"] < date_i18n("Y-m-d", $startDate) && $i==0){
$tempEndDate = strtotime($bookings[$rooms["id"]][$j]["end"]);
$dayOffset = 0;
if ($tempEndDate > $endDate){
$tempEndDate = $endDate;
$dayOffset = 1;
$success = true;
}
$dateDiff = abc_booking_dateDiffInDays($tempEndDate, $startDate);
$colSpan = ($dateDiff+1)*2;
$text = $bookings[$rooms["id"]][$j]["last_name"].',
'.sprintf( _n('%d person', '%d persons', $bookings[$rooms["id"]][$j]["persons"], 'advanced-booking-calendar'), $bookings[$rooms["id"]][$j]["persons"] ).',
'.date_i18n($dateformat, strtotime($bookings[$rooms["id"]][$j]["start"])).'
- '.date_i18n($dateformat, strtotime($bookings[$rooms["id"]][$j]["end"]));
if(mb_strlen($text, "utf-8") > $colSpan*1.5){
$text = mb_substr($text, 0, $colSpan*1.5, "utf-8").'...';
}
$output .= ''.$text.' | ';
$i += ((abc_booking_dateDiffInDays($tempEndDate, $startDate))*2)+1+$dayOffset;
$roomRowDate = strtotime('+'.$dateDiff.' day', $roomRowDate);
}elseif($bookings[$rooms["id"]][$j]["start"] == date_i18n("Y-m-d", $roomRowDate) && $i%2==1) {
$tempEndDate = strtotime($bookings[$rooms["id"]][$j]["end"]);
$dayOffset = 0;
$cssClass = '';
if ($tempEndDate > $endDate){
$tempEndDate = $endDate;
$dayOffset = 1;
$success = true;
$cssClass .= ' abcAvailabilityTableEnding';
}
$dateDiff = abs(abc_booking_dateDiffInDays(strtotime($bookings[$rooms["id"]][$j]["start"]), $tempEndDate));
$colSpan = ($dateDiff*2)+$dayOffset;
$text = $bookings[$rooms["id"]][$j]["last_name"].',
'.sprintf( _n('%d person', '%d persons', $bookings[$rooms["id"]][$j]["persons"], 'advanced-booking-calendar'), $bookings[$rooms["id"]][$j]["persons"] ).',
'.date_i18n($dateformat, strtotime($bookings[$rooms["id"]][$j]["start"])).'
- '.date_i18n($dateformat, strtotime($bookings[$rooms["id"]][$j]["end"]));
if(mb_strlen($text, "utf-8") > $colSpan*1.5){
$text = mb_substr($text, 0, $colSpan*1.5, "utf-8").'...';
}
$output .= ''.$text.' | ';
$i += ($dateDiff*2)+$dayOffset;
$roomRowDate = strtotime('+'.$dateDiff.' day', $roomRowDate);
}
}
if(!$success){
$output .= ' | ';
}
} else{
$output .= ' | ';
}
if($i%2==1 || $i == 1){
$roomRowDate = strtotime('+1 day', $roomRowDate);
}
}
$output .= '
';
}
$output .= '
';
return $output;
}
function sendAbcGuestMail($bookingData){
global $wpdb;
$row = $wpdb->get_row('SELECT name FROM '.$wpdb->prefix.'advanced_booking_calendar_calendars WHERE id = '.intval($bookingData["calendar_id"]), ARRAY_A);
$placeholder = array();
$placeholder["abc_calendar_name"] = $row["name"];
$totalPrice = '';
if(!isset($bookingData["price"])){
$numberOfDays = floor((strtotime($bookingData["end"]) - strtotime($bookingData["start"]))/(60*60*24));
$totalPrice = abc_booking_getTotalPrice($bookingData["calendar_id"], $bookingData["start"], $numberOfDays);
} else {
$totalPrice = $bookingData["price"];
}
$totalPrice .= ' '.getAbcSetting('currency');
$placeholder["abc_total_price"] = $totalPrice;
$placeholder["abc_checkin_date"] = abc_booking_formatDate($bookingData["start"]);
$placeholder["abc_checkout_date"] = abc_booking_formatDate($bookingData["end"]);
$placeholder["abc_person_count"] = $bookingData["persons"];
$placeholder["abc_first_name"] = $bookingData["first_name"];
$placeholder["abc_last_name"] = $bookingData["last_name"];
$placeholder["abc_email"] = $bookingData["email"];
$placeholder["abc_phone"]= $bookingData["phone"];
$placeholder["abc_address"] = $bookingData["address"];
$placeholder["abc_zip"] = $bookingData["zip"];
$placeholder["abc_city"] = $bookingData["city"];
$placeholder["abc_country"] = $bookingData["country"];
$placeholder["abc_message"] = $bookingData["message"];
$adminEmail = getAbcSetting('email');
$subject = '';
$text = '';
switch ($bookingData["state"]) {
case 'open':
$subject = get_option('abc_subject_unconfirmed');
$text = get_option('abc_text_unconfirmed');
break;
case 'confirmed':
$subject = get_option('abc_subject_confirmed');
$text = get_option('abc_text_confirmed');
break;
case 'canceled':
$subject = get_option('abc_subject_canceled');
$text = get_option('abc_text_canceled');
break;
case 'rejected':
$subject = get_option('abc_subject_rejected');
$text = get_option('abc_text_rejected');
break;
}
$bookingVars = abc_booking_getBookingVars();
foreach($bookingVars as $var){
$subject = str_replace('['.$var.']', $placeholder[$var], $subject);
$text = str_replace('['.$var.']', $placeholder[$var], $text);
}
$headers = 'From: '.get_option('blogname').' <'.$adminEmail.'>'."\r\n";
wp_mail($placeholder["abc_email"], $subject, $text, $headers); // Sending email to customer
}
function sendAbcAdminMail($bookingData){
global $wpdb;
$requestQuery = "SELECT name FROM ".$wpdb->prefix."advanced_booking_calendar_calendars WHERE id = ".$bookingData["calendar_id"];
$er = $wpdb->get_row($requestQuery);
$calendarName = $er->name;
$numberOfDays = floor((strtotime($bookingData["end"]) - strtotime($bookingData["start"]))/(60*60*24));
$totalPrice = abc_booking_getTotalPrice($bookingData["calendar_id"], $bookingData["start"], $numberOfDays).' '.getAbcSetting('currency');
$adminEmail = getAbcSetting('email');
$headers = 'From: '.get_option('blogname').' <'.$adminEmail.'>'."\r\n";
$subject = __('Booking Request', 'advanced-booking-calendar').' '.get_option('blogname');
$adminBody = '
'.__('New Booking Request', 'advanced-booking-calendar').'
'.date_i18n(getAbcSetting('dateformat'), strtotime($bookingData["start"])).' - '.date_i18n(getAbcSetting('dateformat'), strtotime($bookingData["end"])).'
Name: '.$bookingData["first_name"].' '.$bookingData["last_name"].'
From: '.date_i18n(getAbcSetting('dateformat'), strtotime($bookingData["start"])).'
To: '.date_i18n(getAbcSetting('dateformat'), strtotime($bookingData["end"])).'
Persons: '.$bookingData["persons"].'
Room Type: '.$calendarName.'
Email: '.$bookingData["email"].'
Phone: '.$bookingData["phone"].'
Address: '.$bookingData["address"].'
ZIP Code: '.$bookingData["zip"].'
City: '.$bookingData["city"].'
Country: '.$bookingData["country"].'
Total price: '.$totalPrice.'
Message: '.$bookingData["message"].'
|
'.abc_booking_getAvailabilityOverview($bookingData).'
|
|
|
|
|
|
|
|
';
add_filter('wp_mail_content_type', 'abc_booking_setContentTypeHTML'); // Activating HTML
wp_mail($adminEmail, $subject, $adminBody, $headers); // Sending email
remove_filter('wp_mail_content_type', 'abc_booking_setContentTypeHTML'); // Deactivating HTML
}
function setAbcBooking($bookingData){ // Inserts booking in DB, returns booking ID
global $wpdb;
$row = $wpdb->get_row('SELECT name FROM '.$wpdb->prefix.'advanced_booking_calendar_calendars WHERE id = '.sanitize_text_field($bookingData["calendar_id"]), ARRAY_A);
$calendarName = $row["name"];
$normFromValue = abc_booking_formatDateToDB($bookingData["start"]);
$normToValue = abc_booking_formatDateToDB($bookingData["end"]);
$roomId = getAbcRoomId(sanitize_text_field($bookingData["calendar_id"]), $normFromValue, $normToValue);
if($roomId < 1){
die('No room available. Booking canceled.');
}
$numberOfDays = floor((strtotime($bookingData["end"]) - strtotime($bookingData["start"]))/(60*60*24));
$totalPrice = abc_booking_getTotalPrice($bookingData["calendar_id"], $bookingData["start"], $numberOfDays);
$wpdb->insert( $wpdb->prefix.'advanced_booking_calendar_bookings', array(
'start' => $normFromValue,
'end' => $normToValue,
'calendar_id' => sanitize_text_field($bookingData["calendar_id"]),
'persons' => sanitize_text_field($bookingData["persons"]),
'first_name' => sanitize_text_field($bookingData["first_name"]),
'last_name' => sanitize_text_field($bookingData["last_name"]),
'email' => sanitize_text_field($bookingData["email"]),
'phone' => sanitize_text_field($bookingData["phone"]),
'address' => sanitize_text_field($bookingData["address"]),
'zip' => sanitize_text_field($bookingData["zip"]),
'city' =>sanitize_text_field( $bookingData["city"]),
'country' => sanitize_text_field($bookingData["country"]),
'message' => sanitize_text_field($bookingData["message"]),
'price' => $totalPrice,
'state' => sanitize_text_field($bookingData["state"]),
'room_id' => $roomId
));
return $wpdb->insert_id;
}
function getAbcRoomId($calId, $abcFromValue, $abcToValue) { // Returns id > 0, if rooms is available for a timeperiod
global $wpdb;
$roomId = 0;
$normFromValue = abc_booking_formatDateToDB($abcFromValue);
$normToValue = abc_booking_formatDateToDB($abcToValue);
// Getting lowest id for an available room
$query = 'SELECT r.id FROM '.$wpdb->prefix.'advanced_booking_calendar_rooms r
LEFT OUTER JOIN (
SELECT DISTINCT room_id FROM '.$wpdb->prefix.'advanced_booking_calendar_bookings
WHERE calendar_id = '.$calId.'
AND state = \'confirmed\'
AND (
(start <= \''.$normFromValue.'\' AND end >=\''.$normToValue.'\')
OR (start >= \''.$normFromValue.'\' AND end <= \''.$normToValue.'\')
OR (start >= \''.$normFromValue.'\' AND start < \''.$normToValue.'\')
OR (start <= \''.$normFromValue.'\' AND end >= \''.$normToValue.'\')
OR (end <= \''.$normFromValue.'\' AND end >= \''.$normToValue.'\')
OR (end > \''.$normFromValue.'\' AND end <= \''.$normToValue.'\')
)
) b
ON r.id = b.room_id
WHERE r.calendar_id = '.$calId.'
AND b.room_id IS NULL
ORDER BY r.id';
$er = $wpdb->get_row($query, ARRAY_A);
if(isset($er["id"])){
$roomId = $er["id"];
} // Else $roomId = 0 ==> Overlap or no availability
return $roomId;
}
function getAbcAvailability($calId, $abcFromValue, $abcToValue) {// Checks if a calendar is availability for a timeframe. Returns true if succesful
$success = false;
if(getAbcRoomId($calId, $abcFromValue, $abcToValue) > 0){
$success = true;
}
return $success;
}
// Subscribing and unsubscribing to newsletter. Just the email is transmitted, nothing else.
function subscribeAbcNewsletter($email, $unsubscribe = 0){
$url = 'https://booking-calendar-plugin.com/mc/mailchimp.php';
$data = array('mail' => urlencode($email), 'unsubscribe' => urlencode($unsubscribe));
$data_string = '';
foreach($data as $key=>$value) {
$data_string .= $key.'='.$value.'&';
}
rtrim($data_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
curl_close($ch);
}
function getAbcColor($colorInt){
$colorCode = '';
if(is_int($colorInt)){
$colorNumber = $colorInt%12;
switch($colorNumber){
case 0:
$colorCode = '#a6c6ce';
break;
case 1:
$colorCode = '#ff6d2a';
break;
case 2:
$colorCode = '#00c2e6';
break;
case 3:
$colorCode = '#f9cb01';
break;
case 4:
$colorCode = '#217084';
break;
case 5:
$colorCode = '#ff9969';
break;
case 6:
$colorCode = '#4cd4ed';
break;
case 7:
$colorCode = '#fbda4d';
break;
case 8:
$colorCode = '#639ba9';
break;
case 9:
$colorCode = '#ffc5aa';
break;
case 10:
$colorCode = '#99e7f5';
break;
case 11:
$colorCode = '#fdea99';
break;
}
}
return $colorCode;
}
?>