1<?php
2
3namespace Sabre\CalDAV\Xml\Property;
4
5use Sabre\CalDAV\Plugin;
6use Sabre\DAV;
7use Sabre\DAV\Xml\Element\Sharee;
8use Sabre\Xml\Writer;
9use Sabre\Xml\XmlSerializable;
10
11/**
12 * Invite property
13 *
14 * This property encodes the 'invite' property, as defined by
15 * the 'caldav-sharing-02' spec, in the http://calendarserver.org/ns/
16 * namespace.
17 *
18 * @see https://trac.calendarserver.org/browser/CalendarServer/trunk/doc/Extensions/caldav-sharing-02.txt
19 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
20 * @author Evert Pot (http://evertpot.com/)
21 * @license http://sabre.io/license/ Modified BSD License
22 */
23class Invite implements XmlSerializable {
24
25    /**
26     * The list of users a calendar has been shared to.
27     *
28     * @var Sharee[]
29     */
30    protected $sharees;
31
32    /**
33     * Creates the property.
34     *
35     * @param Sharee[] $sharees
36     */
37    function __construct(array $sharees) {
38
39        $this->sharees = $sharees;
40
41    }
42
43    /**
44     * Returns the list of users, as it was passed to the constructor.
45     *
46     * @return array
47     */
48    function getValue() {
49
50        return $this->sharees;
51
52    }
53
54    /**
55     * The xmlSerialize method is called during xml writing.
56     *
57     * Use the $writer argument to write its own xml serialization.
58     *
59     * An important note: do _not_ create a parent element. Any element
60     * implementing XmlSerializable should only ever write what's considered
61     * its 'inner xml'.
62     *
63     * The parent of the current element is responsible for writing a
64     * containing element.
65     *
66     * This allows serializers to be re-used for different element names.
67     *
68     * If you are opening new elements, you must also close them again.
69     *
70     * @param Writer $writer
71     * @return void
72     */
73    function xmlSerialize(Writer $writer) {
74
75        $cs = '{' . Plugin::NS_CALENDARSERVER . '}';
76
77        foreach ($this->sharees as $sharee) {
78
79            if ($sharee->access === DAV\Sharing\Plugin::ACCESS_SHAREDOWNER) {
80                $writer->startElement($cs . 'organizer');
81            } else {
82                $writer->startElement($cs . 'user');
83
84                switch ($sharee->inviteStatus) {
85                    case DAV\Sharing\Plugin::INVITE_ACCEPTED :
86                        $writer->writeElement($cs . 'invite-accepted');
87                        break;
88                    case DAV\Sharing\Plugin::INVITE_DECLINED :
89                        $writer->writeElement($cs . 'invite-declined');
90                        break;
91                    case DAV\Sharing\Plugin::INVITE_NORESPONSE :
92                        $writer->writeElement($cs . 'invite-noresponse');
93                        break;
94                    case DAV\Sharing\Plugin::INVITE_INVALID :
95                        $writer->writeElement($cs . 'invite-invalid');
96                        break;
97                }
98
99                $writer->startElement($cs . 'access');
100                switch ($sharee->access) {
101                    case DAV\Sharing\Plugin::ACCESS_READWRITE :
102                        $writer->writeElement($cs . 'read-write');
103                        break;
104                    case DAV\Sharing\Plugin::ACCESS_READ :
105                        $writer->writeElement($cs . 'read');
106                        break;
107
108                }
109                $writer->endElement(); // access
110
111            }
112
113            $href = new DAV\Xml\Property\Href($sharee->href);
114            $href->xmlSerialize($writer);
115
116            if (isset($sharee->properties['{DAV:}displayname'])) {
117                $writer->writeElement($cs . 'common-name', $sharee->properties['{DAV:}displayname']);
118            }
119            if ($sharee->comment) {
120                $writer->writeElement($cs . 'summary', $sharee->comment);
121            }
122            $writer->endElement(); // organizer or user
123
124        }
125
126    }
127
128}
129