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
46 class RSVP extends Managed_DataObject
48 const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
49 const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
50 const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
52 public $__table = 'rsvp'; // table name
53 public $id; // varchar(36) UUID
54 public $uri; // varchar(255)
55 public $profile_id; // int
56 public $event_id; // varchar(36) UUID
57 public $response; // tinyint
58 public $created; // datetime
61 * Get an instance by key
63 * @param string $k Key to use to lookup (usually 'id' for this class)
64 * @param mixed $v Value to lookup
66 * @return RSVP object found, or null for no hits
69 function staticGet($k, $v=null)
71 return Memcached_DataObject::staticGet('RSVP', $k, $v);
75 * Get an instance by compound key
77 * @param array $kv array of key-value mappings
79 * @return Bookmark object found, or null for no hits
85 return Memcached_DataObject::pkeyGet('RSVP', $kv);
89 * Add the compound profile_id/event_id index to our cache keys
90 * since the DB_DataObject stuff doesn't understand compound keys
91 * except for the primary.
95 function _allCacheKeys() {
96 $keys = parent::_allCacheKeys();
97 $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id,
98 'event_id' => $this->event_id));
103 * The One True Thingy that must be defined and declared.
105 public static function schemaDef()
108 'description' => 'Plan to attend event',
110 'id' => array('type' => 'char',
113 'description' => 'UUID'),
114 'uri' => array('type' => 'varchar',
117 'profile_id' => array('type' => 'int'),
118 'event_id' => array('type' => 'char',
121 'description' => 'UUID'),
122 'response' => array('type' => 'char',
124 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
125 'created' => array('type' => 'datetime',
128 'primary key' => array('id'),
129 'unique keys' => array(
130 'rsvp_uri_key' => array('uri'),
131 'rsvp_profile_event_key' => array('profile_id', 'event_id'),
133 'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')),
134 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
135 'indexes' => array('rsvp_created_idx' => array('created')),
139 function saveNew($profile, $event, $verb, $options=array())
141 if (array_key_exists('uri', $options)) {
142 $other = RSVP::staticGet('uri', $options['uri']);
143 if (!empty($other)) {
144 throw new ClientException(_m('RSVP already exists.'));
148 $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
149 'event_id' => $event->id));
151 if (!empty($other)) {
152 throw new ClientException(_m('RSVP already exists.'));
157 $rsvp->id = UUID::gen();
158 $rsvp->profile_id = $profile->id;
159 $rsvp->event_id = $event->id;
160 $rsvp->response = self::codeFor($verb);
162 if (array_key_exists('created', $options)) {
163 $rsvp->created = $options['created'];
165 $rsvp->created = common_sql_now();
168 if (array_key_exists('uri', $options)) {
169 $rsvp->uri = $options['uri'];
171 $rsvp->uri = common_local_url('showrsvp',
172 array('id' => $rsvp->id));
177 self::blow('rsvp:for-event:%s', $event->id);
179 // XXX: come up with something sexier
181 $content = $rsvp->asString();
183 $rendered = $rsvp->asHTML();
185 $options = array_merge(array('object_type' => $verb),
188 if (!array_key_exists('uri', $options)) {
189 $options['uri'] = $rsvp->uri;
192 $eventNotice = $event->getNotice();
194 if (!empty($eventNotice)) {
195 $options['reply_to'] = $eventNotice->id;
198 $saved = Notice::saveNew($profile->id,
200 array_key_exists('source', $options) ?
201 $options['source'] : 'web',
207 function codeFor($verb)
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 throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
243 $notice = Notice::staticGet('uri', $this->uri);
244 if (empty($notice)) {
245 throw new ServerException(sprintf(_m('RSVP %s does not correspond to a notice in the database.'),$this->id));
250 static function fromNotice($notice)
252 return RSVP::staticGet('uri', $notice->uri);
255 static function forEvent($event)
257 $keypart = sprintf('rsvp:for-event:%s', $event->id);
259 $idstr = self::cacheGet($keypart);
261 if ($idstr !== false) {
262 $ids = explode(',', $idstr);
269 $rsvp->selectAdd('id');
271 $rsvp->event_id = $event->id;
274 while ($rsvp->fetch()) {
278 self::cacheSet($keypart, implode(',', $ids));
281 $rsvps = array(RSVP::POSITIVE => array(),
282 RSVP::NEGATIVE => array(),
283 RSVP::POSSIBLE => array());
285 foreach ($ids as $id) {
286 $rsvp = RSVP::staticGet('id', $id);
288 $verb = self::verbFor($rsvp->response);
289 $rsvps[$verb][] = $rsvp;
296 function getProfile()
298 $profile = Profile::staticGet('id', $this->profile_id);
299 if (empty($profile)) {
300 throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
307 $event = Happening::staticGet('id', $this->event_id);
309 throw new Exception(sprintf(_m('No event with ID %s.'),$this->event_id));
316 $event = Happening::staticGet('id', $this->event_id);
318 return self::toHTML($this->getProfile(),
325 $event = Happening::staticGet('id', $this->event_id);
327 return self::toString($this->getProfile(),
332 static function toHTML($profile, $event, $response)
338 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
341 $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>");
344 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
347 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
353 $eventTitle = _m('an unknown event');
355 $notice = $event->getNotice();
356 $eventUrl = $notice->bestUrl();
357 $eventTitle = $event->title;
361 htmlspecialchars($profile->profileurl),
362 htmlspecialchars($profile->getBestName()),
363 htmlspecialchars($eventUrl),
364 htmlspecialchars($eventTitle));
367 static function toString($profile, $event, $response)
373 $fmt = _m('%1$s is attending %2$s.');
376 $fmt = _m('%1$s is not attending %2$s.');
379 $fmt = _m('%1$s might attend %2$s.');
382 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
387 $eventTitle = _m('an unknown event');
389 $notice = $event->getNotice();
390 $eventTitle = $event->title;
394 $profile->getBestName(),
400 self::blow('rsvp:for-event:%s', $event->id);