2017 © Pedro Peláez
 

library free-busy-cal

Produce a free/busy calendar of availability from iCal files or CalDAV server calendars.

image

mporcheron/free-busy-cal

Produce a free/busy calendar of availability from iCal files or CalDAV server calendars.

  • Monday, June 26, 2017
  • by mporcheron
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Free/Busy Calendar - https://www.porcheron.uk/fbc

Usage

An example usage can be found in example.php. This file connects to a CalDAV server, extracts two weeks of dates (excluding weekends) and generates an HTML table. A sample walkthough of the code is below:, (*1)

If you are loading an iCal file, use the Calendar class:, (*2)

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

Alternatively:, (*3)

  • 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)

Howevever, if your calendar is retrieved from a CalDAV server and not an ICS file, use the CalDAVCalendar class:, (*4)

$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:, (*5)

$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:, (*6)

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

Only generate a calendar between 9am (inclusive) and 5pm (exclusive):, (*7)

$fbc->setTimeRange(9, 17);

Fetch the calendars and process them:, (*8)

$fbc->fetchAndParse();

Print out the calendar as a table, default date and time formats, the labels Free and Busy for slots, and show times as ranges (i.e. start – end):, (*9)

$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:, (*10)

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

Testing

This has been tested with the ICS file from Office 365 and iCloud CalDAV., (*11)

Questions/Issues?

Please submit a GitHub issue. Documentation auto-generated from the code can be found on GitHub pages or the docs directory., (*12)

The Versions

26/06 2017

dev-master

9999999-dev

Produce a free/busy calendar of availability from iCal files or CalDAV server calendars.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Porcheron

24/06 2017

v0.4.1

0.4.1.0

Produce a free/busy calendar of availability from iCal files or CalDAV server calendars.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Porcheron

23/06 2017

v0.4

0.4.0.0

Calculate availability from an iCal server

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Porcheron

13/06 2016

v0.3.1

0.3.1.0

Calculate availability from an iCal server

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Porcheron

10/06 2016

v0.3

0.3.0.0

Calculate availability from an iCal server

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Porcheron

28/03 2016

v0.2

0.2.0.0

Calculate availability from an iCal server

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Porcheron

12/02 2015

v0.1

0.1.0.0

  Sources   Download

The Requires