Add event to Google/Outlook.com/Office365 Calendar

Tips submitted by PHPMaker users
Post Reply
philmills
User
Posts: 557

Add event to Google/Outlook.com/Office365 Calendar

Post by philmills »

Here's how to add events to Google Calendar directly from your phpmaker site.
Make sure you already have a DATETIME field named "Appointment", and you have a text field named "EventName"
My code assumes an appointment duration of 1hr, but you can edit that.
in case you have a DATETIME field for both start and end of the event, you can use that too

  1. To the table (mine is called Appointments) containing tha DATETIME field, add a custom field named "AddToCalendar"
  2. Custom field expression should be: cast('' as SIGNED) - this is effectively a dummy expression as we'll be using Row_Rendered to add the actual code.
  3. Go to function Row_Rendered for your table, and insert this code:
    $eventName=$this->EventName->CurrentValue; //customise this to suit the field name in your table
    
    $studentid = ExecuteScalar("SELECT fk_StudentID FROM Students_Further_Study WHERE Students_Further_Study.id='".$this->fk_StudyID->CurrentValue."'");
    $studentname = ExecuteScalar("SELECT V_Fullname FROM Students_Directory WHERE Students_Directory.id='".$studentid."'");
    
    $caltype="1"; // 1 = Google, 2 = Outlook.com, 3 = Office365
    
    	switch($caltype){
    		case "1"; //Google
    		$calprefix = "https://www.google.com/calendar/render?action=TEMPLATE&dates=";
    		$calapptstart = $this->Appointment->CurrentValue;
    		$calapptend = strtotime($calapptstart) + 60*60;
    		$calstartend = date('Ymd\THis', strtotime($calapptstart))."/".date('Ymd\THis', $calapptend);
    		$studentname1 = preg_replace('/\s+/', '+', $studentname);
    		$calsuffix = "&text=Appointment+".$studentname1;
    		break;
    		case "2"; //Outlook
    		$calprefix = "https://outlook.live.com/calendar/0/deeplink/compose?enddt=";
    		$calapptstart = $this->Appointment->CurrentValue;
    		$calapptend = strtotime($calapptstart) + 60*60;
    		$calstartend = date('Y-m-d\TH:i', $calapptend)."&path=/calendar/action/compose&rru=addevent&startdt=".date('Y-m-d\TH:i', strtotime($calapptstart));
    		$studentname1 = preg_replace('/\s+/', '%20', $studentname);
    		$calsuffix = "&subject=Appointment%3A%20".$studentname1;
    		break;
    		case "3"; //Office365
    		$calprefix = "https://outlook.office.com/calendar/0/deeplink/compose?enddt=";
    		$calapptstart = $this->Appointment->CurrentValue;
    		$calapptend = strtotime($calapptstart) + 60*60;
    		$calstartend = date('Y-m-d\TH:i', $calapptend)."&path=/calendar/action/compose&rru=addevent&startdt=".date('Y-m-d\TH:i', strtotime($calapptstart));
    		$studentname1 = preg_replace('/\s+/', '%20', $studentname);
    		$calsuffix = "&subject=Appointment%3A%20".$studentname1;
    		break;
    	}
    $AddtoCalLink = $calprefix.$calstartend.$calsuffix;
    
    //display the calendar link and icon
    	$this->AddToCalendar->ViewValue = "<span class='fas fa-calendar-plus'></span> <a href='".$AddtoCalLink."' target='_blank'>Add to calendar</a>";
    	$this->AddToCalendar->TooltipValue = "Add to my Google Calendar";

Post Reply