PageRenderTime 144ms CodeModel.GetById 101ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/lib/Sabre/CalDAV/Property/Invite.php

https://github.com/KOLANICH/SabreDAV
PHP | 228 lines | 115 code | 43 blank | 70 comment | 15 complexity | 85c833fe9489d83bc55bf7ad387c778a MD5 | raw file
  1<?php
  2
  3namespace Sabre\CalDAV\Property;
  4
  5use Sabre\CalDAV\SharingPlugin as SharingPlugin;
  6use Sabre\DAV;
  7use Sabre\CalDAV;
  8
  9/**
 10 * Invite property
 11 *
 12 * This property encodes the 'invite' property, as defined by
 13 * the 'caldav-sharing-02' spec, in the http://calendarserver.org/ns/
 14 * namespace.
 15 *
 16 * @see https://trac.calendarserver.org/browser/CalendarServer/trunk/doc/Extensions/caldav-sharing-02.txt
 17 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
 18 * @author Evert Pot (http://evertpot.com/)
 19 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
 20 */
 21class Invite extends DAV\Property {
 22
 23    /**
 24     * The list of users a calendar has been shared to.
 25     *
 26     * @var array
 27     */
 28    protected $users;
 29
 30    /**
 31     * The organizer contains information about the person who shared the
 32     * object.
 33     *
 34     * @var array
 35     */
 36    protected $organizer;
 37
 38    /**
 39     * Creates the property.
 40     *
 41     * Users is an array. Each element of the array has the following
 42     * properties:
 43     *
 44     *   * href - Often a mailto: address
 45     *   * commonName - Optional, for example a first and lastname for a user.
 46     *   * status - One of the SharingPlugin::STATUS_* constants.
 47     *   * readOnly - true or false
 48     *   * summary - Optional, description of the share
 49     *
 50     * The organizer key is optional to specify. It's only useful when a
 51     * 'sharee' requests the sharing information.
 52     *
 53     * The organizer may have the following properties:
 54     *   * href - Often a mailto: address.
 55     *   * commonName - Optional human-readable name.
 56     *   * firstName - Optional first name.
 57     *   * lastName - Optional last name.
 58     *
 59     * If you wonder why these two structures are so different, I guess a
 60     * valid answer is that the current spec is still a draft.
 61     *
 62     * @param array $users
 63     */
 64    public function __construct(array $users, array $organizer = null) {
 65
 66        $this->users = $users;
 67        $this->organizer = $organizer;
 68
 69    }
 70
 71    /**
 72     * Returns the list of users, as it was passed to the constructor.
 73     *
 74     * @return array
 75     */
 76    public function getValue() {
 77
 78        return $this->users;
 79
 80    }
 81
 82    /**
 83     * Serializes the property in a DOMDocument
 84     *
 85     * @param DAV\Server $server
 86     * @param \DOMElement $node
 87     * @return void
 88     */
 89    public function serialize(DAV\Server $server,\DOMElement $node) {
 90
 91       $doc = $node->ownerDocument;
 92
 93       if (!is_null($this->organizer)) {
 94
 95           $xorganizer = $doc->createElement('cs:organizer');
 96
 97           $href = $doc->createElement('d:href');
 98           $href->appendChild($doc->createTextNode($this->organizer['href']));
 99           $xorganizer->appendChild($href);
100
101           if (isset($this->organizer['commonName']) && $this->organizer['commonName']) {
102               $commonName = $doc->createElement('cs:common-name');
103               $commonName->appendChild($doc->createTextNode($this->organizer['commonName']));
104               $xorganizer->appendChild($commonName);
105           }
106           if (isset($this->organizer['firstName']) && $this->organizer['firstName']) {
107               $firstName = $doc->createElement('cs:first-name');
108               $firstName->appendChild($doc->createTextNode($this->organizer['firstName']));
109               $xorganizer->appendChild($firstName);
110           }
111           if (isset($this->organizer['lastName']) && $this->organizer['lastName']) {
112               $lastName = $doc->createElement('cs:last-name');
113               $lastName->appendChild($doc->createTextNode($this->organizer['lastName']));
114               $xorganizer->appendChild($lastName);
115           }
116
117           $node->appendChild($xorganizer);
118
119
120       }
121
122       foreach($this->users as $user) {
123
124           $xuser = $doc->createElement('cs:user');
125
126           $href = $doc->createElement('d:href');
127           $href->appendChild($doc->createTextNode($user['href']));
128           $xuser->appendChild($href);
129
130           if (isset($user['commonName']) && $user['commonName']) {
131               $commonName = $doc->createElement('cs:common-name');
132               $commonName->appendChild($doc->createTextNode($user['commonName']));
133               $xuser->appendChild($commonName);
134           }
135
136           switch($user['status']) {
137
138               case SharingPlugin::STATUS_ACCEPTED :
139                   $status = $doc->createElement('cs:invite-accepted');
140                   $xuser->appendChild($status);
141                   break;
142               case SharingPlugin::STATUS_DECLINED :
143                   $status = $doc->createElement('cs:invite-declined');
144                   $xuser->appendChild($status);
145                   break;
146               case SharingPlugin::STATUS_NORESPONSE :
147                   $status = $doc->createElement('cs:invite-noresponse');
148                   $xuser->appendChild($status);
149                   break;
150               case SharingPlugin::STATUS_INVALID :
151                   $status = $doc->createElement('cs:invite-invalid');
152                   $xuser->appendChild($status);
153                   break;
154
155           }
156
157           $xaccess = $doc->createElement('cs:access');
158
159           if ($user['readOnly']) {
160                $xaccess->appendChild(
161                    $doc->createElement('cs:read')
162                );
163           } else {
164                $xaccess->appendChild(
165                    $doc->createElement('cs:read-write')
166                );
167           }
168           $xuser->appendChild($xaccess);
169
170           if (isset($user['summary']) && $user['summary']) {
171               $summary = $doc->createElement('cs:summary');
172               $summary->appendChild($doc->createTextNode($user['summary']));
173               $xuser->appendChild($summary);
174           }
175
176           $node->appendChild($xuser);
177
178       }
179
180
181    }
182
183    /**
184     * Unserializes the property.
185     *
186     * This static method should return a an instance of this object.
187     *
188     * @param \DOMElement $prop
189     * @param array $propertyMap
190     * @return DAV\IProperty
191     */
192    static function unserialize(\DOMElement $prop, array $propertyMap) {
193
194        $xpath = new \DOMXPath($prop->ownerDocument);
195        $xpath->registerNamespace('cs', CalDAV\Plugin::NS_CALENDARSERVER);
196        $xpath->registerNamespace('d',  'urn:DAV');
197
198        $users = array();
199
200        foreach($xpath->query('cs:user', $prop) as $user) {
201
202            $status = null;
203            if ($xpath->evaluate('boolean(cs:invite-accepted)', $user)) {
204                $status = SharingPlugin::STATUS_ACCEPTED;
205            } elseif ($xpath->evaluate('boolean(cs:invite-declined)', $user)) {
206                $status = SharingPlugin::STATUS_DECLINED;
207            } elseif ($xpath->evaluate('boolean(cs:invite-noresponse)', $user)) {
208                $status = SharingPlugin::STATUS_NORESPONSE;
209            } elseif ($xpath->evaluate('boolean(cs:invite-invalid)', $user)) {
210                $status = SharingPlugin::STATUS_INVALID;
211            } else {
212                throw new DAV\Exception('Every cs:user property must have one of cs:invite-accepted, cs:invite-declined, cs:invite-noresponse or cs:invite-invalid');
213            }
214            $users[] = array(
215                'href' => $xpath->evaluate('string(d:href)', $user),
216                'commonName' => $xpath->evaluate('string(cs:common-name)', $user),
217                'readOnly' => $xpath->evaluate('boolean(cs:access/cs:read)', $user),
218                'summary' => $xpath->evaluate('string(cs:summary)', $user),
219                'status' => $status,
220            );
221
222        }
223
224        return new self($users);
225
226    }
227
228}