mporcheron/free-busy-cal

此包已被废弃,不再维护。未建议替代包。

从iCal文件或CalDAV服务器日历生成可用性日程表。

v0.4.1 2017-06-24 15:27 UTC

This package is auto-updated.

Last update: 2021-08-14 09:42:34 UTC


README

使用

示例用法可以在example.php中找到。此文件连接到CalDAV服务器,提取两周的日期(不包括周末)并生成HTML表格。以下是代码的示例说明

如果您正在加载iCal文件,请使用Calendar

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

或者

  • 如果文件通过网络访问,请使用setUrl(url)函数而不是setFile(file)
  • 如果您有日历源在字符串中,请使用函数setiCal(source)

然而,如果您的日历是从CalDAV服务器而不是ICS文件中检索的,请使用CalDAVCalendar

$iCloud = (new MPorcheron\FreeBusyCal\CalDAVCalendar())
    ->setUsername('my.apple.id@me.com')
    ->setPassword('application-specific-password')
    ->setPrincipalUrl('https://caldav.icloud.com/123456789876543/principal/');

创建Generator对象并添加一个或多个日历

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

设置要提取的日期范围,例如,从本周一开始,持续14天(即两周),但排除周末

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

仅生成上午9点(含)至下午5点(不含)之间的日程表

$fbc->setTimeRange(9, 17);

获取日历并处理它们

$fbc->fetchAndParse();

以表格形式打印日程表,默认日期和时间格式,以及FreeBusy标签,显示时间范围为(即开始 – 结束)

$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;
});

或者测试特定的时间/日期(例如,2016年5月4日下午5点)是否可用

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

测试

此功能已在Office 365和iCloud CalDAV的ICS文件上进行了测试。

问题/疑问?

请提交GitHub问题。代码自动生成的文档可以在GitHub pagesdocs目录中找到。