\MPorcheron\FreeBusyCalGenerator

FreeBusyCal generator - this is the main class used to generate a free busy calendar.

If you are loading an iCal file, use the Calendar class:

 $cal = (new MPorcheron\FreeBusyCal\Calendar())->setFile('calendar.ics');

Alternatively:

  • if the file is accessed over the internet, use the `setUrl(url)` function instead of `setFile(file)`.
  • if you have the calendar source inside a string, use the function `setiCal(source)`
  • If your calendar is retrieved from a CalDAV server, use the `CalDAVCalendar` class:
 $iCloud = (new MPorcheron\FreeBusyCal\CalDAVCalendar())
     ->setUsername('my.apple.id@me.com')
     ->setPassword('application-specific-password')
     ->setPrincipalUrl('https://caldav.icloud.com/123456789876543/principal/');

Create the Generator object and add one or more calendars:

$fbc = new \MPorcheron\FreeBusyCal\Generator($cal, $iCloud);

Set the date range to extract, e.g. start from this Monday, and run for 14 days (i.e. two weeks), but exclude weekends:

 $fbc->setDateRange(new \DateTime('Monday this week'), 14, false);

Only generate a calendar between 9am (inclusive) and 5pm (exclusive), and show a slot every 30 minutes:

 $fbc->setTimeRange(9, 17, 30);

Fetch the calendars and process them:

 $fbc->fetchAndParse();

Print out the calendar table, with the class cal, default date and time formats, the labels Free and Busy for slots, and show times as ranges (i.e. start – end) as opposed to just start time:

$contents = $fbc->generate(function (Fbc\FreeBusyCalendar &$cal) {
    $output = '<table class="cal">';

    // Output table headers with days
    $output .= '<tr><th></th>';
    $days = [ 'S', 'M', 'T', 'W', 'T', 'F', 'S' ];
    foreach ($cal->getCalendarDates(Fbc\FreeBusyCalendar::DATE_FORMAT) as $label => &$dt) {
        $output .= '<th class="day">'. $days[$dt->format('N')] .'</th>';
    }
    $output .= '</tr>';

    // Output table headers with dates
    $output .= '<tr><th></th>';
    foreach ($cal->getCalendarDates(Fbc\FreeBusyCalendar::DATE_FORMAT) as $label => &$dt) {
        $output .= '<th class="date">'. $label .'</th>';
    }
    $output .= '</tr>';

    // Iterate through each time and $output .= the availability
    $times = $cal->getCalendarTimes(Fbc\FreeBusyCalendar::TIME_FORMAT);
    foreach ($times as $hour => $temp) {
        foreach ($temp as $minute => $labels) {
            $output .= '<tr><td class="time">'. $labels[0];
            if ($showRange) {
                $output .= '&nbsp;&ndash;&nbsp;' . $labels[1];
            }
            $output .= '</td>';

            foreach ($cal->getCalendarDates(Fbc\FreeBusyCalendar::DATE_FORMAT) as $dt) {
                if ($cal->isFree($dt->format('Y-m-d'), $hour, $minute)) {
                    $output .= '<td class="avail free">Free</td>';
                } else {
                    $output .= '<td class="avail busy">Busy</td>';
                }
            }
        }
        $output .= '</td>';
    }
    $output .= '</table>';

    return $output;
});

Alternatively test if a specific time/date (i.e. 5pm on 4th May 2016) is available:

 $cal = $fbc->getFreeBusyCalendar();
 $free = $cal->isFree('2016-05-04', 17, 0);

Summary

Methods
Properties
Constants
__construct()
getConfig()
addCalendar()
setDateRange()
setTimeRange()
fetchAndParse()
generate()
getFreeBusyCalendar()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$calendars
$config
$freeBusyCalendar
N/A

Properties

$calendars

$calendars : array<mixed,\MPorcheron\FreeBusyCal\Calendar>

Type

array<mixed,\MPorcheron\FreeBusyCal\Calendar> — Array of calendars to scrape for data.

$config

$config : array<mixed,mixed>

Type

array<mixed,mixed> — Configuration data.

Methods

__construct()

__construct(\MPorcheron\FreeBusyCal\Calendar  $cal) 

Construct the controller and populate it with the configuration values. Constructing this class sets the time limit for script execution to indefinite and the default timezone because of a PHP oddity.

Output buffering is started also.

Parameters

\MPorcheron\FreeBusyCal\Calendar $cal

(One or more) calendars to extract data from.

getConfig()

getConfig() : array<mixed,mixed>

Get the congiruation data.

Returns

array<mixed,mixed> —

Configuration data.

addCalendar()

addCalendar(\MPorcheron\FreeBusyCal\Calendar  $cal) 

Add a calendar to extract data from.

Parameters

\MPorcheron\FreeBusyCal\Calendar $cal

Calendar to also extact data from.

setDateRange()

setDateRange(\DateTime  $startDate, integer  $numDays = 7, boolean  $includeWeekends = false) : \MPorcheron\FreeBusyCal\Calendar

Set the date range to generate data for.

Parameters

\DateTime $startDate

When to start the calendar from.

integer $numDays

Number of days from the start date to generate calendar for.

boolean $includeWeekends

Set to false to ignore weekends. Note weekennds still count in the number of days (i.e. 7 days, and $includeWeekends starting on a Monday, will show Mon - Fri.)

Returns

\MPorcheron\FreeBusyCal\Calendar

$this.

setTimeRange()

setTimeRange(integer  $startHour, integer  $endHour, integer  $interval = 60) : \MPorcheron\FreeBusyCal\Generator

Set the time range to generate data for.

Parameters

integer $startHour

First hour of the day to start output at (midnight = 0). Minimum is 1, maximum is 22, default is 9.

integer $endHour

Last hour of the day print output for (midnight = 0). Minimum is $startHour, maximum is 23, default is 17.

integer $interval

How many mniutes to break each slot in the calendar up by (60 = segment by hour). You should make this number one of the following to fit evenly into the hour: 1,2,3,4,5,6,10,12,15,20,30,60. Minumum is 1, maximum is 60, default is 60.

Returns

\MPorcheron\FreeBusyCal\Generator

$this.

fetchAndParse()

fetchAndParse(boolean  $refetch = false) : \MPorcheron\FreeBusyCal\Generator

Fetch and parse the data needed to generate the availability calendar.

Parameters

boolean $refetch

Refetch iCal data if it has already been fetched once.

Returns

\MPorcheron\FreeBusyCal\Generator

$this.

generate()

generate(\MPorcheron\FreeBusyCal\function  $func) : \MPorcheron\FreeBusyCal\Generator

Generates the calendar and returs output.

Parameters

\MPorcheron\FreeBusyCal\function $func

Function that takes a single paramter of a \MPorcheron\FreeBusyCal\FreeBusyCalendar` and returns out the output as a string.

Throws

\BadFunctionCallException

If the calendar hasn't been fetched yet.

Returns

\MPorcheron\FreeBusyCal\Generator

Output from the print function.

getFreeBusyCalendar()

getFreeBusyCalendar() : \MPorcheron\FreeBusyCal\FreeBusyCalendar

Retrieve the calendar of availability.

Throws

\BadFunctionCallException

If the calendar hasn't been fetched yet.

Returns

\MPorcheron\FreeBusyCal\FreeBusyCalendar

Calendar availability.