3 * Data class for event RSVPs
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
35 * Data class for event RSVPs
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41 * @link http://status.net/
43 * @see Managed_DataObject
45 class RSVP extends Managed_DataObject
47 const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
48 const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
49 const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
51 public $__table = 'rsvp'; // table name
52 public $id; // varchar(36) UUID
53 public $uri; // varchar(255)
54 public $profile_id; // int
55 public $event_id; // varchar(36) UUID
56 public $response; // tinyint
57 public $created; // datetime
60 * Add the compound profile_id/event_id index to our cache keys
61 * since the DB_DataObject stuff doesn't understand compound keys
62 * except for the primary.
66 function _allCacheKeys() {
67 $keys = parent::_allCacheKeys();
68 $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id,
69 'event_id' => $this->event_id));
74 * The One True Thingy that must be defined and declared.
76 public static function schemaDef()
79 'description' => 'Plan to attend event',
81 'id' => array('type' => 'char',
84 'description' => 'UUID'),
85 'uri' => array('type' => 'varchar',
88 'profile_id' => array('type' => 'int'),
89 'event_id' => array('type' => 'char',
92 'description' => 'UUID'),
93 'response' => array('type' => 'char',
95 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
96 'created' => array('type' => 'datetime',
99 'primary key' => array('id'),
100 'unique keys' => array(
101 'rsvp_uri_key' => array('uri'),
102 'rsvp_profile_event_key' => array('profile_id', 'event_id'),
104 'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')),
105 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
106 'indexes' => array('rsvp_created_idx' => array('created')),
110 function saveNew($profile, $event, $verb, $options=array())
112 if (array_key_exists('uri', $options)) {
113 $other = RSVP::getKV('uri', $options['uri']);
114 if (!empty($other)) {
115 // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
116 throw new ClientException(_m('RSVP already exists.'));
120 $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
121 'event_id' => $event->id));
123 if (!empty($other)) {
124 // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
125 throw new ClientException(_m('RSVP already exists.'));
130 $rsvp->id = UUID::gen();
131 $rsvp->profile_id = $profile->id;
132 $rsvp->event_id = $event->id;
133 $rsvp->response = self::codeFor($verb);
135 if (array_key_exists('created', $options)) {
136 $rsvp->created = $options['created'];
138 $rsvp->created = common_sql_now();
141 if (array_key_exists('uri', $options)) {
142 $rsvp->uri = $options['uri'];
144 $rsvp->uri = common_local_url('showrsvp',
145 array('id' => $rsvp->id));
150 self::blow('rsvp:for-event:%s', $event->id);
152 // XXX: come up with something sexier
154 $content = $rsvp->asString();
156 $rendered = $rsvp->asHTML();
158 $options = array_merge(array('object_type' => $verb),
161 if (!array_key_exists('uri', $options)) {
162 $options['uri'] = $rsvp->uri;
165 $eventNotice = $event->getNotice();
167 if (!empty($eventNotice)) {
168 $options['reply_to'] = $eventNotice->id;
171 $saved = Notice::saveNew($profile->id,
173 array_key_exists('source', $options) ?
174 $options['source'] : 'web',
180 function codeFor($verb)
193 // TRANS: Exception thrown when requesting an undefined verb for RSVP.
194 throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
198 static function verbFor($code)
202 return RSVP::POSITIVE;
205 return RSVP::NEGATIVE;
208 return RSVP::POSSIBLE;
211 // TRANS: Exception thrown when requesting an undefined code for RSVP.
212 throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
218 $notice = Notice::getKV('uri', $this->uri);
219 if (empty($notice)) {
220 // TRANS: Server exception thrown when requesting a non-exsting notice for an RSVP ("please respond").
221 // TRANS: %s is the RSVP with the missing notice.
222 throw new ServerException(sprintf(_m('RSVP %s does not correspond to a notice in the database.'),$this->id));
227 static function fromNotice(Notice $notice)
230 $rsvp->uri = $notice->uri;
231 if (!$rsvp->find(true)) {
232 throw new NoResultException($rsvp);
237 static function forEvent(Happening $event)
239 $keypart = sprintf('rsvp:for-event:%s', $event->id);
241 $idstr = self::cacheGet($keypart);
243 if ($idstr !== false) {
244 $ids = explode(',', $idstr);
251 $rsvp->selectAdd('id');
253 $rsvp->event_id = $event->id;
256 while ($rsvp->fetch()) {
260 self::cacheSet($keypart, implode(',', $ids));
263 $rsvps = array(RSVP::POSITIVE => array(),
264 RSVP::NEGATIVE => array(),
265 RSVP::POSSIBLE => array());
267 foreach ($ids as $id) {
268 $rsvp = RSVP::getKV('id', $id);
270 $verb = self::verbFor($rsvp->response);
271 $rsvps[$verb][] = $rsvp;
278 function getProfile()
280 $profile = Profile::getKV('id', $this->profile_id);
281 if (empty($profile)) {
282 // TRANS: Exception thrown when requesting a non-existing profile.
283 // TRANS: %s is the ID of the non-existing profile.
284 throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
291 $event = Happening::getKV('id', $this->event_id);
293 // TRANS: Exception thrown when requesting a non-existing event.
294 // TRANS: %s is the ID of the non-existing event.
295 throw new Exception(sprintf(_m('No event with ID %s.'),$this->event_id));
302 $event = Happening::getKV('id', $this->event_id);
304 return self::toHTML($this->getProfile(),
311 $event = Happening::getKV('id', $this->event_id);
313 return self::toString($this->getProfile(),
318 static function toHTML($profile, $event, $response)
324 // TRANS: HTML version of an RSVP ("please respond") status for a user.
325 // TRANS: %1$s is a profile URL, %2$s a profile name,
326 // TRANS: %3$s is an event URL, %4$s an event title.
327 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
330 // TRANS: HTML version of an RSVP ("please respond") status for a user.
331 // TRANS: %1$s is a profile URL, %2$s a profile name,
332 // TRANS: %3$s is an event URL, %4$s an event title.
333 $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>");
336 // TRANS: HTML version of an RSVP ("please respond") status for a user.
337 // TRANS: %1$s is a profile URL, %2$s a profile name,
338 // TRANS: %3$s is an event URL, %4$s an event title.
339 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
342 // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
343 // TRANS: %s is the non-existing response code.
344 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
349 // TRANS: Used as event title when not event title is available.
350 // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
351 $eventTitle = _m('an unknown event');
353 $notice = $event->getNotice();
354 $eventUrl = $notice->getUrl();
355 $eventTitle = $event->title;
359 htmlspecialchars($profile->profileurl),
360 htmlspecialchars($profile->getBestName()),
361 htmlspecialchars($eventUrl),
362 htmlspecialchars($eventTitle));
365 static function toString($profile, $event, $response)
371 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
372 // TRANS: %1$s is a profile name, %2$s is an event title.
373 $fmt = _m('%1$s is attending %2$s.');
376 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
377 // TRANS: %1$s is a profile name, %2$s is an event title.
378 $fmt = _m('%1$s is not attending %2$s.');
381 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
382 // TRANS: %1$s is a profile name, %2$s is an event title.
383 $fmt = _m('%1$s might attend %2$s.');
386 // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
387 // TRANS: %s is the non-existing response code.
388 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
393 // TRANS: Used as event title when not event title is available.
394 // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
395 $eventTitle = _m('an unknown event');
397 $notice = $event->getNotice();
398 $eventTitle = $event->title;
402 $profile->getBestName(),
406 function delete($useWhere=false)
408 self::blow('rsvp:for-event:%s', $this->id);
409 return parent::delete($useWhere);