[insert_php]
session_start();
if(!isset($_SESSION[‘access_token’])) {
header(‘Location: http://professionalmaids.co.uk/google-login.php’);
exit();
}
if(isset($_GET[‘client_id’])) {
$time = ” @”;
if($_GET[‘flexibility’] != ‘None’) {
$time .= $_GET[‘flexibility’];
} else {
$time .= $_GET[‘preferredtime’];
}
$client_id = $_GET[‘client_id’];
$summary = $_GET[‘title’] . ” ” . $_GET[‘fullname’] . “(” . $_GET[‘totalhours’] . ” Hours)” . $time;
$address = $_GET[‘address’] . “\n\r” . $_GET[‘postcode’];
$description = ($_GET[‘keys’] == true ? “We Have Keys \n\r” : “”);
$description .= ($_GET[‘alarm’] == true ? “The House is Alarmed \n\r” : “”);
$description .= ($_GET[‘invoice’] == false ? “Client Pays Cash: £”.$_GET[‘totalpervisit’].” \n\r” : “”);
$description .= (isset($_GET[‘mobile’]) ? “Contact Number: “.$_GET[‘mobile’].” \n\r” : “”);
$description .= “\n\r”;
$description .= ($_GET[‘laundry’] == true ? “We do the laundry \n\r” : “”);
$description .= ($_GET[‘ironing’] == true ? “We do ironing \n\r” : “”);
$description .= ($_GET[‘dishes’] == true ? “We wash dishes \n\r” : “”);
$description .= ($_GET[‘rubbish’] == true ? “We remove the rubbish \n\r” : “”);
$description .= “\n\r”;
$description .= $_GET[‘instructions’].” \n\r”;
$description .= “\n\r”;
$description .= “Cliend Id: “. $client_id;
$frequency = strtoupper($_GET[‘frequency’]);
if ($frequency == “FORTNIGHTLY”) {
$interval = 2;
$frequency = “WEEKLY”;
} else {
$interval = 0 + $_GET[‘interval’];
}
$byday = “”;
if ($_GET[‘monday’] == “Mondays”) {
$byday = ‘MO,’;
}
if ($_GET[‘tuesday’] == “Tuesdays”) {
$byday .= “TU,”;
}
if ($_GET[‘wednesday’] == “Wednesdays”) {
$byday .= “WE,”;
}
if ($_GET[‘thursday’] == “Thursdays”) {
$byday .= “TH,”;
}
if ($_GET[‘friday’] == “Fridays”) {
$byday .= “FR,”;
}
if ($_GET[‘saturday’] == “Saturdays”) {
$byday .= “SA,”;
}
$byday = rtrim($byday,’,’);
$stdate = DateTime::createFromFormat(‘d/m/Y’, $_GET[‘startdate’]);
$stdateYmd = $stdate->format(‘Y-m-d’);
$startdatetime = $stdateYmd . “T” . $_GET[‘preferredtime’] . “:00”;
$enddatetime = date(“Y-m-d\TH:i”, strtotime(‘+1 hour’,strtotime($startdatetime))) . “:00”;
$timezone = “Europe/London”;
// Create event on primary calendar
$event_id = CreateCalendarEvent(‘6v5v8sg4ga5sdhock3ecvdo7sg@group.calendar.google.com’, $summary, $address, $description, $startdatetime, $enddatetime, $timezone, $frequency, $interval, $byday, $_SESSION[‘access_token’]);
try {
echo json_encode([ ‘event_id’ => $event_id ]);
} finally {
header(‘Location: http://professionalmaids.co.uk/cp/visits’);
exit();
}
}
function CreateCalendarEvent($calendar_id, $summary, $address, $description, $startdatetime, $enddatetime, $timezone, $frequency, $interval, $byday, $access_token) {
$url_events = ‘https://www.googleapis.com/calendar/v3/calendars/’ . $calendar_id . ‘/events’;
$curlPost = array();
$curlPost[‘summary’] = $summary;
$curlPost[‘description’] = $description;
$curlPost[‘location’] = $address;
$curlPost[‘start’] = array(‘dateTime’ => $startdatetime, ‘timeZone’ => $timezone);
$curlPost[‘end’] = array(‘dateTime’ => $enddatetime, ‘timeZone’ => $timezone);
if($frequency != “ONE-OFF”) {
$curlPost[‘recurrence’] = array(‘RRULE:FREQ=’.strtoupper($frequency).’;INTERVAL=’.$interval.’;BYDAY=’.strtoupper($byday));
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_events);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Authorization: Bearer ‘. $access_token, ‘Content-Type: application/json’));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception(‘Error : Failed to create event’);
return;
}
[/insert_php]