~ubuntu-branches/ubuntu/vivid/php-horde-icalendar/vivid-proposed

« back to all changes in this revision

Viewing changes to Horde_Icalendar-2.0.7/lib/Horde/Icalendar/Vevent.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-11 12:33:46 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130811123346-jvp1p9qn0h6bv61d
Tags: 2.0.7-1
New upstream version 2.0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Class representing vEvents.
 
4
 *
 
5
 * Copyright 2003-2013 Horde LLC (http://www.horde.org/)
 
6
 *
 
7
 * See the enclosed file COPYING for license information (LGPL). If you
 
8
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 
9
 *
 
10
 * @author   Mike Cochrane <mike@graftonhall.co.nz>
 
11
 * @category Horde
 
12
 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 
13
 * @package  Icalendar
 
14
 */
 
15
class Horde_Icalendar_Vevent extends Horde_Icalendar
 
16
{
 
17
    /**
 
18
     * The component type of this class.
 
19
     *
 
20
     * @var string
 
21
     */
 
22
    public $type = 'vEvent';
 
23
 
 
24
    /**
 
25
     * TODO
 
26
     *
 
27
     * @return TODO
 
28
     */
 
29
    public function exportvCalendar()
 
30
    {
 
31
        // Default values.
 
32
        $requiredAttributes = array(
 
33
            'DTSTAMP' => time(),
 
34
            'UID' => strval(new Horde_Support_Uuid())
 
35
        );
 
36
 
 
37
        $method = null;
 
38
        if (!empty($this->_container)) {
 
39
            try {
 
40
                $method = $this->_container->getAttribute('METHOD');
 
41
            } catch (Horde_Icalendar_Exception $e) {
 
42
            }
 
43
        }
 
44
 
 
45
        switch ($method) {
 
46
        case 'PUBLISH':
 
47
            $requiredAttributes['DTSTART'] = time();
 
48
            $requiredAttributes['SUMMARY'] = '';
 
49
            break;
 
50
 
 
51
        case 'REQUEST':
 
52
            $requiredAttributes['ATTENDEE'] = '';
 
53
            $requiredAttributes['DTSTART'] = time();
 
54
            $requiredAttributes['SUMMARY'] = '';
 
55
            break;
 
56
 
 
57
        case 'REPLY':
 
58
            $requiredAttributes['ATTENDEE'] = '';
 
59
            break;
 
60
 
 
61
        case 'ADD':
 
62
            $requiredAttributes['DTSTART'] = time();
 
63
            $requiredAttributes['SEQUENCE'] = 1;
 
64
            $requiredAttributes['SUMMARY'] = '';
 
65
            break;
 
66
 
 
67
        case 'CANCEL':
 
68
            $requiredAttributes['ATTENDEE'] = '';
 
69
            $requiredAttributes['SEQUENCE'] = 1;
 
70
            break;
 
71
 
 
72
        case 'REFRESH':
 
73
            $requiredAttributes['ATTENDEE'] = '';
 
74
            break;
 
75
 
 
76
        default:
 
77
            $requiredAttributes['DTSTART'] = time();
 
78
            break;
 
79
        }
 
80
 
 
81
        foreach ($requiredAttributes as $name => $default_value) {
 
82
            try {
 
83
                $this->getAttribute($name);
 
84
            } catch (Horde_Icalendar_Exception $e) {
 
85
                $this->setAttribute($name, $default_value);
 
86
            }
 
87
        }
 
88
 
 
89
        return $this->_exportvData('VEVENT');
 
90
    }
 
91
 
 
92
    /**
 
93
     * Update the status of an attendee of an event.
 
94
     *
 
95
     * @param $email    The email address of the attendee.
 
96
     * @param $status   The participant status to set.
 
97
     * @param $fullname The full name of the participant to set.
 
98
     */
 
99
    public function updateAttendee($email, $status, $fullname = '')
 
100
    {
 
101
        foreach ($this->_attributes as $key => $attribute) {
 
102
            if ($attribute['name'] == 'ATTENDEE' &&
 
103
                $attribute['value'] == 'mailto:' . $email) {
 
104
                $this->_attributes[$key]['params']['PARTSTAT'] = $status;
 
105
                if (!empty($fullname)) {
 
106
                    $this->_attributes[$key]['params']['CN'] = $fullname;
 
107
                }
 
108
                unset($this->_attributes[$key]['params']['RSVP']);
 
109
                return;
 
110
            }
 
111
        }
 
112
        $params = array('PARTSTAT' => $status);
 
113
        if (!empty($fullname)) {
 
114
            $params['CN'] = $fullname;
 
115
        }
 
116
        $this->setAttribute('ATTENDEE', 'mailto:' . $email, $params);
 
117
    }
 
118
 
 
119
    /**
 
120
     * Return the organizer display name or email.
 
121
     *
 
122
     * @return string  The organizer name to display for this event.
 
123
     */
 
124
    public function organizerName()
 
125
    {
 
126
        try {
 
127
            $organizer = $this->getAttribute('ORGANIZER', true);
 
128
        } catch (Horde_Icalendar_Exception $e) {
 
129
            return Horde_Icalendar_Translation::t("An unknown person");
 
130
        }
 
131
 
 
132
        if (isset($organizer[0]['CN'])) {
 
133
            return $organizer[0]['CN'];
 
134
        }
 
135
 
 
136
        $organizer = parse_url($this->getAttribute('ORGANIZER'));
 
137
 
 
138
        return $organizer['path'];
 
139
    }
 
140
 
 
141
    /**
 
142
     * Update this event with details from another event.
 
143
     *
 
144
     * @param Horde_Icalendar_Vevent $vevent  The vEvent with latest details.
 
145
     */
 
146
    public function updateFromvEvent($vevent)
 
147
    {
 
148
        $newAttributes = $vevent->getAllAttributes();
 
149
        foreach ($newAttributes as $newAttribute) {
 
150
            try {
 
151
                $this->getAttribute($newAttribute['name']);
 
152
            } catch (Horde_Icalendar_Exception $e) {
 
153
                // Already exists so just add it.
 
154
                $this->setAttribute($newAttribute['name'],
 
155
                                    $newAttribute['value'],
 
156
                                    $newAttribute['params']);
 
157
                continue;
 
158
            }
 
159
 
 
160
            // Already exists so locate and modify.
 
161
            $found = false;
 
162
 
 
163
            // Try matching the attribte name and value incase
 
164
            // only the params changed (eg attendee updating
 
165
            // status).
 
166
            foreach ($this->_attributes as $id => $attr) {
 
167
                if ($attr['name'] == $newAttribute['name'] &&
 
168
                    $attr['value'] == $newAttribute['value']) {
 
169
                    // merge the params
 
170
                    foreach ($newAttribute['params'] as $param_id => $param_name) {
 
171
                        $this->_attributes[$id]['params'][$param_id] = $param_name;
 
172
                    }
 
173
                    $found = true;
 
174
                    break;
 
175
                }
 
176
            }
 
177
            if (!$found) {
 
178
                // Else match the first attribute with the same
 
179
                // name (eg changing start time).
 
180
                foreach ($this->_attributes as $id => $attr) {
 
181
                    if ($attr['name'] == $newAttribute['name']) {
 
182
                        $this->_attributes[$id]['value'] = $newAttribute['value'];
 
183
                        // Merge the params.
 
184
                        foreach ($newAttribute['params'] as $param_id => $param_name) {
 
185
                            $this->_attributes[$id]['params'][$param_id] = $param_name;
 
186
                        }
 
187
                        break;
 
188
                    }
 
189
                }
 
190
            }
 
191
        }
 
192
    }
 
193
 
 
194
    /**
 
195
     * Update just the attendess of event with details from another
 
196
     * event.
 
197
     *
 
198
     * @param Horde_Icalendar_Vevent $vevent  The vEvent with latest details
 
199
     */
 
200
    public function updateAttendeesFromvEvent($vevent)
 
201
    {
 
202
        $newAttributes = $vevent->getAllAttributes();
 
203
        foreach ($newAttributes as $newAttribute) {
 
204
            if ($newAttribute['name'] != 'ATTENDEE') {
 
205
                continue;
 
206
            }
 
207
 
 
208
            try {
 
209
                $this->getAttribute($newAttribute['name']);
 
210
            } catch (Horde_Icalendar_Exception $e) {
 
211
                // Already exists so just add it.
 
212
                $this->setAttribute($newAttribute['name'],
 
213
                                    $newAttribute['value'],
 
214
                                    $newAttribute['params']);
 
215
                continue;
 
216
            }
 
217
 
 
218
            // Already exists so locate and modify.
 
219
            $found = false;
 
220
            // Try matching the attribte name and value incase
 
221
            // only the params changed (eg attendee updating
 
222
            // status).
 
223
            foreach ($this->_attributes as $id => $attr) {
 
224
                if ($attr['name'] == $newAttribute['name'] &&
 
225
                    $attr['value'] == $newAttribute['value']) {
 
226
                    // Merge the params.
 
227
                    foreach ($newAttribute['params'] as $param_id => $param_name) {
 
228
                        $this->_attributes[$id]['params'][$param_id] = $param_name;
 
229
                    }
 
230
                    $found = true;
 
231
                    break;
 
232
                }
 
233
            }
 
234
 
 
235
            if (!$found) {
 
236
                // Else match the first attribute with the same
 
237
                // name (eg changing start time).
 
238
                foreach ($this->_attributes as $id => $attr) {
 
239
                    if ($attr['name'] == $newAttribute['name']) {
 
240
                        $this->_attributes[$id]['value'] = $newAttribute['value'];
 
241
                        // Merge the params.
 
242
                        foreach ($newAttribute['params'] as $param_id => $param_name) {
 
243
                            $this->_attributes[$id]['params'][$param_id] = $param_name;
 
244
                        }
 
245
                        break;
 
246
                    }
 
247
                }
 
248
            }
 
249
        }
 
250
    }
 
251
 
 
252
}