aboutsummaryrefslogtreecommitdiffstats
path: root/libcalendaring/lib/libcalendaring_recurrence.php
blob: bbc4976ce2585f574e42ffc555f6d6509b4ac562 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

/**
 * Recurrence computation class for shared use
 *
 * Uitility class to compute reccurrence dates from the given rules
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2012-2014, 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 libcalendaring_recurrence
{
    protected $lib;
    protected $start;
    protected $next;
    protected $engine;
    protected $recurrence;
    protected $dateonly = false;
    protected $hour = 0;

    /**
     * Default constructor
     *
     * @param object calendar The calendar plugin instance
     */
    function __construct($lib)
    {
      // use Horde classes to compute recurring instances
      // TODO: replace with something that has less than 6'000 lines of code
      require_once(__DIR__ . '/Horde_Date_Recurrence.php');

      $this->lib = $lib;
    }

    /**
     * Initialize recurrence engine
     *
     * @param array  The recurrence properties
     * @param object DateTime The recurrence start date
     */
    public function init($recurrence, $start = null)
    {
        $this->recurrence = $recurrence;

        $this->engine = new Horde_Date_Recurrence($start);
        $this->engine->fromRRule20(libcalendaring::to_rrule($recurrence));

        $this->set_start($start);

        if (is_array($recurrence['EXDATE'])) {
            foreach ($recurrence['EXDATE'] as $exdate) {
                if (is_a($exdate, 'DateTime')) {
                    $this->engine->addException($exdate->format('Y'), $exdate->format('n'), $exdate->format('j'));
                }
            }
        }
        if (is_array($recurrence['RDATE'])) {
            foreach ($recurrence['RDATE'] as $rdate) {
                if (is_a($rdate, 'DateTime')) {
                    $this->engine->addRDate($rdate->format('Y'), $rdate->format('n'), $rdate->format('j'));
                }
            }
        }
    }

    /**
     * Setter for (new) recurrence start date
     *
     * @param object DateTime The recurrence start date
     */
    public function set_start($start)
    {
        $this->start = $start;
        $this->dateonly = $start->_dateonly;
        $this->next = new Horde_Date($start, $this->lib->timezone->getName());
        $this->hour = $this->next->hour;
        $this->engine->setRecurStart($this->next);
    }

    /**
     * Get date/time of the next occurence of this event
     *
     * @return mixed DateTime object or False if recurrence ended
     */
    public function next()
    {
        $time = false;
        $after = clone $this->next;
        $after->mday = $after->mday + 1;
        if ($this->next && ($next = $this->engine->nextActiveRecurrence($after))) {
            // avoid endless loops if recurrence computation fails
            if (!$next->after($this->next)) {
                return false;
            }
            // fix time for all-day events
            if ($this->dateonly) {
                $next->hour = $this->hour;
                $next->min = 0;
            }

            $time = $next->toDateTime();
            $this->next = $next;
        }

        return $time;
    }

    /**
     * Get the end date of the occurence of this recurrence cycle
     *
     * @return DateTime|bool End datetime of the last occurence or False if recurrence exceeds limit
     */
    public function end()
    {
        // recurrence end date is given
        if ($this->recurrence['UNTIL'] instanceof DateTime) {
            return $this->recurrence['UNTIL'];
        }

        // take the last RDATE entry if set
        if (is_array($this->recurrence['RDATE']) && !empty($this->recurrence['RDATE'])) {
            $last = end($this->recurrence['RDATE']);
            if ($last instanceof DateTime) {
              return $last;
            }
        }

        // run through all items till we reach the end
        if ($this->recurrence['COUNT']) {
            $last = $this->start;
            $this->next = new Horde_Date($this->start, $this->lib->timezone->getName());
            while (($next = $this->next()) && $c < 1000) {
                $last = $next;
                $c++;
            }
        }

        return $last;
    }

}

© 2014-2024 Faster IT GmbH | imprint | privacy policy