RSVP.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * Data class for event RSVPs
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2011, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Data class for event RSVPs
  34. *
  35. * @category Event
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  39. * @link http://status.net/
  40. *
  41. * @see Managed_DataObject
  42. */
  43. class RSVP extends Managed_DataObject
  44. {
  45. const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
  46. const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
  47. const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
  48. public $__table = 'rsvp'; // table name
  49. public $id; // varchar(36) UUID
  50. public $uri; // varchar(255)
  51. public $profile_id; // int
  52. public $event_id; // varchar(36) UUID
  53. public $response; // tinyint
  54. public $created; // datetime
  55. /**
  56. * Add the compound profile_id/event_id index to our cache keys
  57. * since the DB_DataObject stuff doesn't understand compound keys
  58. * except for the primary.
  59. *
  60. * @return array
  61. */
  62. function _allCacheKeys() {
  63. $keys = parent::_allCacheKeys();
  64. $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id,
  65. 'event_id' => $this->event_id));
  66. return $keys;
  67. }
  68. /**
  69. * The One True Thingy that must be defined and declared.
  70. */
  71. public static function schemaDef()
  72. {
  73. return array(
  74. 'description' => 'Plan to attend event',
  75. 'fields' => array(
  76. 'id' => array('type' => 'char',
  77. 'length' => 36,
  78. 'not null' => true,
  79. 'description' => 'UUID'),
  80. 'uri' => array('type' => 'varchar',
  81. 'length' => 255,
  82. 'not null' => true),
  83. 'profile_id' => array('type' => 'int'),
  84. 'event_id' => array('type' => 'char',
  85. 'length' => 36,
  86. 'not null' => true,
  87. 'description' => 'UUID'),
  88. 'response' => array('type' => 'char',
  89. 'length' => '1',
  90. 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
  91. 'created' => array('type' => 'datetime',
  92. 'not null' => true),
  93. ),
  94. 'primary key' => array('id'),
  95. 'unique keys' => array(
  96. 'rsvp_uri_key' => array('uri'),
  97. 'rsvp_profile_event_key' => array('profile_id', 'event_id'),
  98. ),
  99. 'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')),
  100. 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
  101. 'indexes' => array('rsvp_created_idx' => array('created')),
  102. );
  103. }
  104. function saveNew($profile, $event, $verb, $options=array())
  105. {
  106. if (array_key_exists('uri', $options)) {
  107. $other = RSVP::getKV('uri', $options['uri']);
  108. if (!empty($other)) {
  109. // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
  110. throw new ClientException(_m('RSVP already exists.'));
  111. }
  112. }
  113. $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
  114. 'event_id' => $event->id));
  115. if (!empty($other)) {
  116. // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
  117. throw new ClientException(_m('RSVP already exists.'));
  118. }
  119. $rsvp = new RSVP();
  120. $rsvp->id = UUID::gen();
  121. $rsvp->profile_id = $profile->id;
  122. $rsvp->event_id = $event->id;
  123. $rsvp->response = self::codeFor($verb);
  124. if (array_key_exists('created', $options)) {
  125. $rsvp->created = $options['created'];
  126. } else {
  127. $rsvp->created = common_sql_now();
  128. }
  129. if (array_key_exists('uri', $options)) {
  130. $rsvp->uri = $options['uri'];
  131. } else {
  132. $rsvp->uri = common_local_url('showrsvp',
  133. array('id' => $rsvp->id));
  134. }
  135. $rsvp->insert();
  136. self::blow('rsvp:for-event:%s', $event->id);
  137. // XXX: come up with something sexier
  138. $content = $rsvp->asString();
  139. $rendered = $rsvp->asHTML();
  140. $options = array_merge(array('object_type' => $verb),
  141. $options);
  142. if (!array_key_exists('uri', $options)) {
  143. $options['uri'] = $rsvp->uri;
  144. }
  145. $eventNotice = $event->getNotice();
  146. if (!empty($eventNotice)) {
  147. $options['reply_to'] = $eventNotice->id;
  148. }
  149. $saved = Notice::saveNew($profile->id,
  150. $content,
  151. array_key_exists('source', $options) ?
  152. $options['source'] : 'web',
  153. $options);
  154. return $saved;
  155. }
  156. function codeFor($verb)
  157. {
  158. switch ($verb) {
  159. case RSVP::POSITIVE:
  160. return 'Y';
  161. break;
  162. case RSVP::NEGATIVE:
  163. return 'N';
  164. break;
  165. case RSVP::POSSIBLE:
  166. return '?';
  167. break;
  168. default:
  169. // TRANS: Exception thrown when requesting an undefined verb for RSVP.
  170. throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
  171. }
  172. }
  173. static function verbFor($code)
  174. {
  175. switch ($code) {
  176. case 'Y':
  177. return RSVP::POSITIVE;
  178. break;
  179. case 'N':
  180. return RSVP::NEGATIVE;
  181. break;
  182. case '?':
  183. return RSVP::POSSIBLE;
  184. break;
  185. default:
  186. // TRANS: Exception thrown when requesting an undefined code for RSVP.
  187. throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
  188. }
  189. }
  190. function getNotice()
  191. {
  192. $notice = Notice::getKV('uri', $this->uri);
  193. if (empty($notice)) {
  194. // TRANS: Server exception thrown when requesting a non-exsting notice for an RSVP ("please respond").
  195. // TRANS: %s is the RSVP with the missing notice.
  196. throw new ServerException(sprintf(_m('RSVP %s does not correspond to a notice in the database.'),$this->id));
  197. }
  198. return $notice;
  199. }
  200. static function fromNotice(Notice $notice)
  201. {
  202. $rsvp = new RSVP();
  203. $rsvp->uri = $notice->uri;
  204. if (!$rsvp->find(true)) {
  205. throw new NoResultException($rsvp);
  206. }
  207. return $rsvp;
  208. }
  209. static function forEvent(Happening $event)
  210. {
  211. $keypart = sprintf('rsvp:for-event:%s', $event->id);
  212. $idstr = self::cacheGet($keypart);
  213. if ($idstr !== false) {
  214. $ids = explode(',', $idstr);
  215. } else {
  216. $ids = array();
  217. $rsvp = new RSVP();
  218. $rsvp->selectAdd();
  219. $rsvp->selectAdd('id');
  220. $rsvp->event_id = $event->id;
  221. if ($rsvp->find()) {
  222. while ($rsvp->fetch()) {
  223. $ids[] = $rsvp->id;
  224. }
  225. }
  226. self::cacheSet($keypart, implode(',', $ids));
  227. }
  228. $rsvps = array(RSVP::POSITIVE => array(),
  229. RSVP::NEGATIVE => array(),
  230. RSVP::POSSIBLE => array());
  231. foreach ($ids as $id) {
  232. $rsvp = RSVP::getKV('id', $id);
  233. if (!empty($rsvp)) {
  234. $verb = self::verbFor($rsvp->response);
  235. $rsvps[$verb][] = $rsvp;
  236. }
  237. }
  238. return $rsvps;
  239. }
  240. function getProfile()
  241. {
  242. $profile = Profile::getKV('id', $this->profile_id);
  243. if (empty($profile)) {
  244. // TRANS: Exception thrown when requesting a non-existing profile.
  245. // TRANS: %s is the ID of the non-existing profile.
  246. throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
  247. }
  248. return $profile;
  249. }
  250. function getEvent()
  251. {
  252. $event = Happening::getKV('id', $this->event_id);
  253. if (empty($event)) {
  254. // TRANS: Exception thrown when requesting a non-existing event.
  255. // TRANS: %s is the ID of the non-existing event.
  256. throw new Exception(sprintf(_m('No event with ID %s.'),$this->event_id));
  257. }
  258. return $event;
  259. }
  260. function asHTML()
  261. {
  262. $event = Happening::getKV('id', $this->event_id);
  263. return self::toHTML($this->getProfile(),
  264. $event,
  265. $this->response);
  266. }
  267. function asString()
  268. {
  269. $event = Happening::getKV('id', $this->event_id);
  270. return self::toString($this->getProfile(),
  271. $event,
  272. $this->response);
  273. }
  274. static function toHTML($profile, $event, $response)
  275. {
  276. $fmt = null;
  277. switch ($response) {
  278. case 'Y':
  279. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  280. // TRANS: %1$s is a profile URL, %2$s a profile name,
  281. // TRANS: %3$s is an event URL, %4$s an event title.
  282. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
  283. break;
  284. case 'N':
  285. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  286. // TRANS: %1$s is a profile URL, %2$s a profile name,
  287. // TRANS: %3$s is an event URL, %4$s an event title.
  288. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is not attending <a href='%3\$s'>%4\$s</a>.</span>");
  289. break;
  290. case '?':
  291. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  292. // TRANS: %1$s is a profile URL, %2$s a profile name,
  293. // TRANS: %3$s is an event URL, %4$s an event title.
  294. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
  295. break;
  296. default:
  297. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  298. // TRANS: %s is the non-existing response code.
  299. throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
  300. }
  301. if (empty($event)) {
  302. $eventUrl = '#';
  303. // TRANS: Used as event title when not event title is available.
  304. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  305. $eventTitle = _m('an unknown event');
  306. } else {
  307. $notice = $event->getNotice();
  308. $eventUrl = $notice->getUrl();
  309. $eventTitle = $event->title;
  310. }
  311. return sprintf($fmt,
  312. htmlspecialchars($profile->profileurl),
  313. htmlspecialchars($profile->getBestName()),
  314. htmlspecialchars($eventUrl),
  315. htmlspecialchars($eventTitle));
  316. }
  317. static function toString($profile, $event, $response)
  318. {
  319. $fmt = null;
  320. switch ($response) {
  321. case 'Y':
  322. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  323. // TRANS: %1$s is a profile name, %2$s is an event title.
  324. $fmt = _m('%1$s is attending %2$s.');
  325. break;
  326. case 'N':
  327. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  328. // TRANS: %1$s is a profile name, %2$s is an event title.
  329. $fmt = _m('%1$s is not attending %2$s.');
  330. break;
  331. case '?':
  332. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  333. // TRANS: %1$s is a profile name, %2$s is an event title.
  334. $fmt = _m('%1$s might attend %2$s.');
  335. break;
  336. default:
  337. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  338. // TRANS: %s is the non-existing response code.
  339. throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
  340. break;
  341. }
  342. if (empty($event)) {
  343. // TRANS: Used as event title when not event title is available.
  344. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  345. $eventTitle = _m('an unknown event');
  346. } else {
  347. $notice = $event->getNotice();
  348. $eventTitle = $event->title;
  349. }
  350. return sprintf($fmt,
  351. $profile->getBestName(),
  352. $eventTitle);
  353. }
  354. function delete($useWhere=false)
  355. {
  356. self::blow('rsvp:for-event:%s', $this->id);
  357. return parent::delete($useWhere);
  358. }
  359. }