Horde_ActiveSync_Driver_Base::buildFbString PHP Method

buildFbString() public static method

The values are as follows: 0 - Free 1 - Tentative 2 - Busy 3 - OOF 4 - No data available. Though currently we only provide a Free/Busy/Unknown differentiation.
Since: 2.4.0
public static buildFbString ( stdClass $fb, Horde_Date $start, Horde_Date $end ) : string
$fb stdClass The fb information. An object containing: - s: The start of the period covered. - e: The end of the period covered. - b: An array of busy periods.
$start Horde_Date The start of the period requested by the client.
$end Horde_Date The end of the period requested by the client.
return string The EAS freebusy string.
    public static function buildFbString($fb, Horde_Date $start, Horde_Date $end)
    {
        if (empty($fb)) {
            return false;
        }
        // Calculate total time span.
        $end_ts = $end->timestamp();
        $start_ts = $start->timestamp();
        $sec = $end_ts - $start_ts;
        $fb_start = new Horde_Date($fb->s);
        $fb_end = new Horde_Date($fb->e);
        // Number of 30 minute periods.
        $period_cnt = ceil($sec / 1800);
        // Requested range is completely out of the available range.
        if ($start_ts >= $fb_end->timestamp() || $end_ts < $fb_start->timestamp()) {
            return str_repeat('4', $period_cnt);
        }
        // We already know we don't have any busy periods.
        if (empty($fb->b) && $fb_end->timestamp() <= $end_ts) {
            return str_repeat('0', $period_cnt);
        }
        $eas_fb = '';
        // Move $start to the start of the available data.
        while ($start_ts < $fb_start->timestamp() && $start_ts <= $end_ts) {
            $eas_fb .= '4';
            $start_ts += 1800;
            // 30 minutes
        }
        // The rest is assumed free up to $fb->e
        while ($start_ts <= $fb_end->timestamp() && $start_ts <= $end_ts) {
            $eas_fb .= '0';
            $start_ts += 1800;
        }
        // The remainder is also unavailable
        while ($start_ts <= $end_ts) {
            $eas_fb .= '4';
            $start_ts += 1800;
        }
        // Now put in the busy blocks.
        while (list($b_start, $b_end) = each($fb->b)) {
            $offset = $b_start - $start->timestamp();
            $duration = ceil(($b_end - $b_start) / 1800);
            if ($offset > 0) {
                $eas_fb = substr_replace($eas_fb, str_repeat('2', $duration), floor($offset / 1800), $duration);
            }
        }
        return $eas_fb;
    }