
<?php
 
 //http://psoug.org/snippet/Send-iCal-Email-Meeting-Requests-using-PHP_934.htm

	header( 'content-type: text/html; charset=utf-8' );
 
function sendIcalEmail($prenom,$email,$date_du_jour , $heure_debut , $heure_fin, $nb_crenaux, $dtstart, $dtend , $formule )
{
 
	date_default_timezone_set('Europe/Paris');
 
	$from_name = "Robot Massage - no reply";
	$from_address = "robmyname@mydomain.com";
	$subject = "Réservation massage"; 
	$location = "Salle de repos";
 
	//Create Mime Boundry
	$mime_boundary = "----Réservation----".MD5(time());
 
	//Create Email Headers
	$headers = "From: ".$from_name." <".$from_address.">\n";
	$headers .= "Reply-To: ".$from_name." <".$from_address.">\n";
 
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
	$headers .= "Content-class: urn:content-classes:calendarmessage\n";
 
	//Create Email Body (HTML)
	$message .= "--$mime_boundary\n";
	$message .= "Content-Type: text/html; charset=UTF-8\n";
	$message .= "Content-Transfer-Encoding: 8bit\n\n";
 
	$message .= "<html>\n";
	$message .= "<body>\n";
	$message .= '<p>'.$prenom.',</p>';
    $message .= "<p> Tu viens de réserver un ou plusieurs créneaux pour la date suivante: </p>";
    $message .= "<p> Le $date_du_jour de $heure_debut à $heure_fin soit $nb_crenaux créneaux. </p>";
	$message .= '<p>'."Tu peux annuler un rendez-vous en utilisant l'interface Web.".'</p>';
    $message .= '<p>'."A bientôt :)".'</p>';
	  
	$message .= "</body>\n";
	$message .= "</html>\n";
	$message .= "--$mime_boundary\n";
	
	
	$vcal = "BEGIN:VCALENDAR\r\n";
	$vcal .= "VERSION:2.0\r\n";
	$vcal .= "PRODID:-//nonstatics.com//OrgCalendarWebTool//EN\r\n";
	$vcal .= "METHOD:REQUEST\r\n";
	
	$vcal .= "BEGIN:VTIMEZONE\r\n";
	$vcal .= "TZID:Central Europe Standard Time\r\n";
	$vcal .= "BEGIN:STANDARD\r\n";
	$vcal .= "DTSTART:16011028T030000\r\n";
	$vcal .= "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n";
	$vcal .= "TZOFFSETFROM:+0200\r\n";
	$vcal .= "TZOFFSETTO:+0100\r\n";
	$vcal .= "END:STANDARD\r\n";
	$vcal .= "BEGIN:DAYLIGHT\r\n";
	$vcal .= "DTSTART:16010325T020000\r\n";
	$vcal .= "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3\r\n";
	$vcal .= "TZOFFSETFROM:+0100\r\n";
	$vcal .= "TZOFFSETTO:+0200\r\n";
	$vcal .= "END:DAYLIGHT\r\n";
	$vcal .= "END:VTIMEZONE\r\n";
	
	$vcal .= "BEGIN:VEVENT\r\n";
	$vcal .= "ORGANIZER;CN=\" ".$from_name . "\":mailto:" .$email ."\r\n";
	
	$vcal .= "UID:".date('Ymd').'T'.date('His')."-".rand()."-nonstatics.com\r\n";
	$vcal .= "DTSTAMP:".date('Ymd').'T'.date('His')."\r\n";
	$vcal .= "DTSTART:$dtstart\r\n";
	$vcal .= "DTEND:$dtend\r\n"; 
	$vcal .= "LOCATION:$location\r\n";
	$vcal .= "SUMMARY:$summary\r\n";
	
	$vcal .= "DESCRIPTION: Tu viens de réserver un ou plusieurs créneaux pour la date suivante: $str_date \r\n";
	
	// https://www.luc-sanchez.fr/2014/11/generer-un-evenement-ical-dans-outlook-en-php/#.Wd_XNVu0NaQ
	
	$vcal .= "BEGIN:VALARM\r\n";
	//$vcal .= "TRIGGER:-PT15M\r\n";  // 3H avant PT3H
	$vcal .= "TRIGGER:-PT180M\r\n";  // updated
	
	$vcal .= "ACTION:DISPLAY\r\n";
	$vcal .= "DESCRIPTION:Reminder\r\n";
	$vcal .= "END:VALARM\r\n";
	$vcal .= "END:VEVENT\r\n";
	$vcal .= "END:VCALENDAR\r\n";

	//Create ICAL Content (Google rfc 2445 for details and examples of usage) 
/*	
	$ical =    'BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
ORGANIZER:MAILTO:'.$from_address.'
DTSTART:'.$dtstart.'
DTEND:'.$dtend.'
LOCATION:'.$meeting_location.'
TRANSP:OPAQUE
SEQUENCE:0
UID:'.$cal_uid.'
DTSTAMP:'.$todaystamp.'
DESCRIPTION:'.$meeting_description.'
SUMMARY:'.$subject.'
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR';   
 */
 
	$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
	$message .= "Content-Transfer-Encoding: 8bit\n\n";
	$message .= $vcal;            
	
	$mail_sent = @mail( $email, $subject, $message, $headers );
 
	if($mail_sent)  
	{
		return true;
	} 
	else
	{
		return false;
	}   
 
}
 
 

//////////////////////////////////////////////////////////////////////
function create_time($heure)
{
		if(strlen($heure) == 5)
		{  	$pieces = explode("H",$heure);
			$heure_debut = $pieces[0].":".$pieces[1].":00";
		}
	
		if(strlen($heure) == 3)
		{	$heure_debut = substr($heure,0,-1).":00:00";
		}
		
		return($heure_debut);
}
//////////////////////////////////////////////////////////////////////
function create_stop_time($heure, $formule)
{

		if($formule == "courte")
		{	switch ($heure)
				{
					case	"12:20:00":
							$heure_fin = "12:40:00";
							break;
					case	"12:40:00":
							$heure_fin = "13:00:00";
							break;
					case 	"13:00:00":
							$heure_fin = "13:20:00";
							break;
					case 	"13:20:00":
							$heure_fin = "13:40:00";
							break;	
					case	"13:40:00":
							$heure_fin = "14:00:00";
							break;
					case	"14:00:00":
							$heure_fin = "14:20:00";
							break;
					case 	"14:20:00":
							$heure_fin = "14:40:00";
							break;
					case 	"14:40:00":
							$heure_fin = "15:00:00";
							break;		
				
				} 
		}
		
		if($formule == "longue")
		{	switch ($heure)
				{
					case	"12:00:00":
							$heure_fin = "12:30:00";
							break;
					case	"12:30:00":
							$heure_fin = "13:00:00";
							break;
					case 	"13:00:00":
							$heure_fin = "13:30:00";
							break;
					case 	"13:30:00":
							$heure_fin = "14:00:00";
							break;	
					case	"14:00:00":
							$heure_fin = "14:30:00";
							break;
					case	"14:30:00":
							$heure_fin = "15:00:00";
							break;
					case 	"15:00:00":
							$heure_fin = "15:30:00";
							break;
					case 	"15:30:00":
							$heure_fin = "16:00:00";
							break;		
				
				} 
		}	
			
			
			return($heure_fin);
}
//////////////////////////////////////////////////////////////////////

function outlook($prenom, $email , $date, $str, $formule)
{
		// remplacer la virgule par un espace
		$heure = str_replace(',', ' ', $str);
 
		$nb_crenaux = mb_substr_count($heure, "H");
		if(mb_substr_count($heure, "H") >= 2)
		{ 
			// plusieurs creneau
			$pieces = explode(" ",$heure);
			$heure_debut = create_time($pieces[0]);
			$val = $pieces[count($pieces)-1];
			$heure_fin = create_time($val);
			$heure_fin = create_stop_time($heure_fin, $formule);
		}
	
		else
		{ 	
			// 	un seul creneau
			$heure_debut = "";
			$heure_fin = "";
			$heure_debut = create_time($heure);
			$heure_fin = create_stop_time($heure_debut, $formule);
		}	
 
		$pieces = explode("-",$date);
		//echo "<pre>";
		//print_r($pieces);
		$date_du_jour = $pieces[0]." ".$pieces[1]." ".$pieces[2];
		
		$jour = $pieces[0];
		$mois = $pieces[1];
		$annee = $pieces[2];
		
		$pieces = explode(":",$heure_debut);
		$heure1 = $pieces[0];
		$minute1 = $pieces[1];
		$seconde1 = $pieces[2];
		
		$pieces = explode(":",$heure_fin);
		$heure2 = $pieces[0];
		$minute2 = $pieces[1];
		$seconde2 = $pieces[2];
		
		//int mktime ( int hour, int minute, int second, int month, int day, int year )
		$tsStart = mktime($heure1, $minute1, $seconde1, $mois, $jour, $annee);
		$dtstart = date('Ymd',$tsStart).'T'.date('His',$tsStart);
		//echo $dtstart;
	
		$tsEnd = mktime($heure2, $minute2, $seconde2, $mois, $jour, $annee);
		$dtend = date('Ymd',$tsEnd).'T'.date('His',$tsEnd);
		//echo $dtend;
	
		//14H20 *** 31-03-2016 *** 03/31/2016 14:20:00 *** 03/31/2016 14:40:00 *** 14:20:00 *** 14:40:00 
		
		$result = sendIcalEmail($prenom , $email , $date_du_jour , $heure_debut , $heure_fin, $nb_crenaux, $dtstart, $dtend, $formule  );
 
		/* if($result)
		{
			echo "Email sent successfully.";
		} 
		else 
		{
			echo "A problem occurred sending email";
		} */
			
}

//////////////////////////////////////////////////////////////////////

	//outlook("Patrice","patrice.delpy@onsemi.com" , "14-01-2016", "13H20");

	//echo "done";
	
?>

