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 * Get an instance by key
62 * @param string $k Key to use to lookup (usually 'id' for this class)
63 * @param mixed $v Value to lookup
65 * @return RSVP object found, or null for no hits
67 function staticGet($k, $v=null)
69 return Memcached_DataObject::staticGet('RSVP', $k, $v);
73 * Get an instance by compound key
75 * @param array $kv array of key-value mappings
77 * @return Bookmark object found, or null for no hits
82 return Memcached_DataObject::pkeyGet('RSVP', $kv);
86 * Add the compound profile_id/event_id index to our cache keys
87 * since the DB_DataObject stuff doesn't understand compound keys
88 * except for the primary.
92 function _allCacheKeys() {
93 $keys = parent::_allCacheKeys();
94 $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id,
95 'event_id' => $this->event_id));
100 * The One True Thingy that must be defined and declared.
102 public static function schemaDef()
105 'description' => 'Plan to attend event',
107 'id' => array('type' => 'char',
110 'description' => 'UUID'),
111 'uri' => array('type' => 'varchar',
114 'profile_id' => array('type' => 'int'),
115 'event_id' => array('type' => 'char',
118 'description' => 'UUID'),
119 'response' => array('type' => 'char',
121 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
122 'created' => array('type' => 'datetime',
125 'primary key' => array('id'),
126 'unique keys' => array(
127 'rsvp_uri_key' => array('uri'),
128 'rsvp_profile_event_key' => array('profile_id', 'event_id'),
130 'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')),
131 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
132 'indexes' => array('rsvp_created_idx' => array('created')),
136 function saveNew($profile, $event, $verb, $options=array())
138 if (array_key_exists('uri', $options)) {
139 $other = RSVP::staticGet('uri', $options['uri']);
140 if (!empty($other)) {
141 // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
142 throw new ClientException(_m('RSVP already exists.'));
146 $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
147 'event_id' => $event->id));
149 if (!empty($other)) {
150 // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
151 throw new ClientException(_m('RSVP already exists.'));
156 $rsvp->id = UUID::gen();
157 $rsvp->profile_id = $profile->id;
158 $rsvp->event_id = $event->id;
159 $rsvp->response = self::codeFor($verb);
161 if (array_key_exists('created', $options)) {
162 $rsvp->created = $options['created'];
164 $rsvp->created = common_sql_now();
167 if (array_key_exists('uri', $options)) {
168 $rsvp->uri = $options['uri'];
170 $rsvp->uri = common_local_url('showrsvp',
171 array('id' => $rsvp->id));
176 self::blow('rsvp:for-event:%s', $event->id);
178 // XXX: come up with something sexier
180 $content = $rsvp->asString();
182 $rendered = $rsvp->asHTML();
184 $options = array_merge(array('object_type' => $verb),
187 if (!array_key_exists('uri', $options)) {
188 $options['uri'] = $rsvp->uri;
191 $eventNotice = $event->getNotice();
193 if (!empty($eventNotice)) {
194 $options['reply_to'] = $eventNotice->id;
197 $saved = Notice::saveNew($profile->id,
199 array_key_exists('source', $options) ?
200 $options['source'] : 'web',
206 function codeFor($verb)
219 // TRANS: Exception thrown when requesting an undefined verb for RSVP.
220 throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
224 static function verbFor($code)
228 return RSVP::POSITIVE;
231 return RSVP::NEGATIVE;
234 return RSVP::POSSIBLE;
237 // TRANS: Exception thrown when requesting an undefined code for RSVP.
238 throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
244 $notice = Notice::staticGet('uri', $this->uri);
245 if (empty($notice)) {
246 // TRANS: Server exception thrown when requesting a non-exsting notice for an RSVP ("please respond").
247 // TRANS: %s is the RSVP with the missing notice.
248 throw new ServerException(sprintf(_m('RSVP %s does not correspond to a notice in the database.'),$this->id));
253 static function fromNotice($notice)
255 return RSVP::staticGet('uri', $notice->uri);
258 static function forEvent($event)
260 $keypart = sprintf('rsvp:for-event:%s', $event->id);
262 $idstr = self::cacheGet($keypart);
264 if ($idstr !== false) {
265 $ids = explode(',', $idstr);
272 $rsvp->selectAdd('id');
274 $rsvp->event_id = $event->id;
277 while ($rsvp->fetch()) {
281 self::cacheSet($keypart, implode(',', $ids));
284 $rsvps = array(RSVP::POSITIVE => array(),
285 RSVP::NEGATIVE => array(),
286 RSVP::POSSIBLE => array());
288 foreach ($ids as $id) {
289 $rsvp = RSVP::staticGet('id', $id);
291 $verb = self::verbFor($rsvp->response);
292 $rsvps[$verb][] = $rsvp;
299 function getProfile()
301 $profile = Profile::staticGet('id', $this->profile_id);
302 if (empty($profile)) {
303 // TRANS: Exception thrown when requesting a non-existing profile.
304 // TRANS: %s is the ID of the non-existing profile.
305 throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
312 $event = Happening::staticGet('id', $this->event_id);
314 // TRANS: Exception thrown when requesting a non-existing event.
315 // TRANS: %s is the ID of the non-existing event.
316 throw new Exception(sprintf(_m('No event with ID %s.'),$this->event_id));
323 $event = Happening::staticGet('id', $this->event_id);
325 return self::toHTML($this->getProfile(),
332 $event = Happening::staticGet('id', $this->event_id);
334 return self::toString($this->getProfile(),
339 static function toHTML($profile, $event, $response)
345 // TRANS: HTML version of an RSVP ("please respond") status for a user.
346 // TRANS: %1$s is a profile URL, %2$s a profile name,
347 // TRANS: %3$s is an event URL, %4$s an event title.
348 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
351 // TRANS: HTML version of an RSVP ("please respond") status for a user.
352 // TRANS: %1$s is a profile URL, %2$s a profile name,
353 // TRANS: %3$s is an event URL, %4$s an event title.
354 $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>");
357 // TRANS: HTML version of an RSVP ("please respond") status for a user.
358 // TRANS: %1$s is a profile URL, %2$s a profile name,
359 // TRANS: %3$s is an event URL, %4$s an event title.
360 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
363 // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
364 // TRANS: %s is the non-existing response code.
365 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
371 // TRANS: Used as event title when not event title is available.
372 // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
373 $eventTitle = _m('an unknown event');
375 $notice = $event->getNotice();
376 $eventUrl = $notice->bestUrl();
377 $eventTitle = $event->title;
381 htmlspecialchars($profile->profileurl),
382 htmlspecialchars($profile->getBestName()),
383 htmlspecialchars($eventUrl),
384 htmlspecialchars($eventTitle));
387 static function toString($profile, $event, $response)
393 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
394 // TRANS: %1$s is a profile name, %2$s is an event title.
395 $fmt = _m('%1$s is attending %2$s.');
398 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
399 // TRANS: %1$s is a profile name, %2$s is an event title.
400 $fmt = _m('%1$s is not attending %2$s.');
403 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
404 // TRANS: %1$s is a profile name, %2$s is an event title.
405 $fmt = _m('%1$s might attend %2$s.');
408 // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
409 // TRANS: %s is the non-existing response code.
410 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
415 // TRANS: Used as event title when not event title is available.
416 // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
417 $eventTitle = _m('an unknown event');
419 $notice = $event->getNotice();
420 $eventTitle = $event->title;
424 $profile->getBestName(),
430 self::blow('rsvp:for-event:%s', $event->id);