summaryrefslogtreecommitdiff
path: root/plugins/libkolab/lib/kolab_date_recurrence.php
blob: b2511f2b1c258de36f68cfa6134fa90180be0448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

/**
 * Recurrence computation class for xcal-based Kolab format objects
 *
 * Utility class to compute instances of recurring events.
 * It requires the libcalendaring PHP module to be installed and loaded.
 *
 * @version @package_version@
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2012, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
class kolab_date_recurrence
{
    private /* EventCal */ $engine;
    private /* kolab_format_xcal */ $object;
    private /* DateTime */ $start;
    private /* DateTime */ $next;
    private /* cDateTime */ $cnext;
    private /* DateInterval */ $duration;

    /**
     * Default constructor
     *
     * @param array The Kolab object to operate on
     */
    function __construct($object)
    {
        $data = $object->to_array();

        $this->object = $object;
        $this->engine = $object->to_libcal();
        $this->start = $this->next = $data['start'];
        $this->cnext = kolab_format::get_datetime($this->next);

        if (is_object($data['start']) && is_object($data['end']))
            $this->duration = $data['start']->diff($data['end']);
        else
            $this->duration = new DateInterval('PT' . ($data['end'] - $data['start']) . 'S');
    }

    /**
     * Get date/time of the next occurence of this event
     *
     * @param boolean Return a Unix timestamp instead of a DateTime object
     * @return mixed  DateTime object/unix timestamp or False if recurrence ended
     */
    public function next_start($timestamp = false)
    {
        $time = false;

        if ($this->engine && $this->next) {
            if (($cnext = new cDateTime($this->engine->getNextOccurence($this->cnext))) && $cnext->isValid()) {
                $next = kolab_format::php_datetime($cnext);
                $time = $timestamp ? $next->format('U') : $next;
                $this->cnext = $cnext;
                $this->next = $next;
            }
        }

        return $time;
    }

    /**
     * Get the next recurring instance of this event
     *
     * @return mixed Array with event properties or False if recurrence ended
     */
    public function next_instance()
    {
        if ($next_start = $this->next_start()) {
            $next_end = clone $next_start;
            $next_end->add($this->duration);

            $next = $this->object->to_array();
            $next['start'] = $next_start;
            $next['end'] = $next_end;

            $recurrence_id_format = $next['allday'] ? 'Ymd' : 'Ymd\THis';
            $next['recurrence_date'] = clone $next_start;
            $next['_instance'] = $next_start->format($recurrence_id_format);

            unset($next['_formatobj']);

            return $next;
        }

        return false;
    }

    /**
     * Get the end date of the occurence of this recurrence cycle
     *
     * @return DateTime|bool End datetime of the last event or False if recurrence exceeds limit
     */
    public function end()
    {
        $event = $this->object->to_array();

        // recurrence end date is given
        if ($event['recurrence']['UNTIL'] instanceof DateTime) {
            return $event['recurrence']['UNTIL'];
        }

        // let libkolab do the work
        if ($this->engine && ($cend = $this->engine->getLastOccurrence()) && ($end_dt = kolab_format::php_datetime(new cDateTime($cend)))) {
            return $end_dt;
        }

        // determine a reasonable end date if none given
        if (!$event['recurrence']['COUNT'] && $event['end'] instanceof DateTime) {
          switch ($event['recurrence']['FREQ']) {
            case 'YEARLY':  $intvl = 'P100Y'; break;
            case 'MONTHLY': $intvl = 'P20Y';  break;
            default:        $intvl = 'P10Y';  break;
          }

          $end_dt = clone $event['end'];
          $end_dt->add(new DateInterval($intvl));
          return $end_dt;
        }

        return false;
    }
}