createConnection("sf/partner.wsdl.xml"); $mylogin = $mySforceConnection->login($login, $password); $query = "SELECT DurationInMinutes,Subject, ActivityDateTime, ActivityDate,IsAllDayEvent FROM Event WHERE ActivityDate > ".date('Y-m-d',strtotime('- 24 hours')); $queryResult = $mySforceConnection->query($query); $records = $queryResult->records; return $records; } /* * We generate a regular ical calendar */ function generateCalendar($records) { $ics = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN\n"; foreach ($records as $record) { $sObject = new SObject($record); $eventStartTs = strtotime($sObject->fields->ActivityDateTime); $length = $sObject->fields->DurationInMinutes; $eventEndTs = $eventStartTs + $length*60; if ($sObject->fields->IsAllDayEvent == "true"){ $ics .= icAllDayEventLine(strtotime($sObject->fields->ActivityDate),$sObject->fields->Subject); }else{ $ics .= icEventLine($eventStartTs,$eventEndTs); } } $ics .= "END:VCALENDAR\n"; return $ics; } /* * We generate a free busy calendar */ function generateFreeBusy($records) { $ics = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN\n"; $ics .= "BEGIN:VFREEBUSY\n"; $ics .= "ORGANIZER:MAILTO:\n"; $isc .= "DTSTART:".icFormatDateTime(mktime())."\n"; $ics .= "DTEND:".icFormatDateTime(strtotime("+6 month",mktime()))."\n"; foreach ($records as $record) { $sObject = new SObject($record); $eventStartTs = strtotime($sObject->fields->ActivityDateTime); $length = $sObject->fields->DurationInMinutes; $eventEndTs = $eventStartTs + $length*60; $ics .= icFreeBusyLine($eventStartTs,$eventEndTs); } $ics .= "URL:".$PHP_SELF."\n"; $ics .= "END:VFREEBUSY\n"; $ics .= "END:VCALENDAR\n"; return $ics; } /* * Format a timestamp into a ical datetime format */ function icFormatDateTime($timestamp) { $year = gmdate('Y',$timestamp); $month = gmdate('m',$timestamp); $day = gmdate('d',$timestamp); $hour = gmdate('H',$timestamp); $minute = gmdate('i',$timestamp); return $year.$month.$day."T".$hour.$minute."00Z"; } /* * Format a timestamp into a ical date format */ function icFormatDate($timestamp) { $year = gmdate('Y',$timestamp); $month = gmdate('m',$timestamp); $day = gmdate('d',$timestamp); return $year.$month.$day; } /* * Generate a event line for a regular calendar generation. */ function icEventLine($startDateTs,$endDateTs) { return "BEGIN:VEVENT\nDTSTART:".icFormatDateTime($startDateTs)."\nDTEND:".icFormatDateTime($endDateTs)."\nSUMMARY:Busy\nEND:VEVENT\n"; } function icAllDayEventLine($startDateTs,$subject='Busy') { // We want to display the real title of the event if the word trip is in it. if (preg_match('/trip/i',$subject) == 0){ $subject = 'Busy'; } return "BEGIN:VEVENT\nDTSTART:".icFormatDate($startDateTs)."\nDTEND:".icFormatDate(strtotime('+ 24 hours', $startDateTs))."\nSUMMARY:".$subject."\nEND:VEVENT\n". "BEGIN:VEVENT\nDTSTART:".icFormatDateTime(strtotime(gmdate('Y-m-d 11:00:00',$startDateTs)))."\nDTEND:".icFormatDateTime(strtotime(gmdate('Y-m-d 21:00:00',$startDateTs)))."\nSUMMARY:".$subject."\nEND:VEVENT\n"; } /* * Generate a line for a free/busy calendar type. */ function icFreeBusyLine($startDate,$endDate) { return 'FREEBUSY:'.icFormatDate($startDate).'/'.icFormatDate($endDate)."\n"; } ?>