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