]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/classes/RSVP.php
Merge remote-tracking branch 'upstream/master'
[quix0rs-gnu-social.git] / plugins / Event / classes / RSVP.php
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
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Data class for event RSVPs
36  *
37  * @category Event
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      Managed_DataObject
44  */
45 class RSVP extends Managed_DataObject
46 {
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';
50
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
58
59     /**
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.
63      *
64      * @return array
65      */
66     function _allCacheKeys() {
67         $keys = parent::_allCacheKeys();
68         $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id,
69                                                     'event_id' => $this->event_id));
70         return $keys;
71     }
72
73     /**
74      * The One True Thingy that must be defined and declared.
75      */
76     public static function schemaDef()
77     {
78         return array(
79             'description' => 'Plan to attend event',
80             'fields' => array(
81                 'id' => array('type' => 'char',
82                               'length' => 36,
83                               'not null' => true,
84                               'description' => 'UUID'),
85                 'uri' => array('type' => 'varchar',
86                                'length' => 255,
87                                'not null' => true),
88                 'profile_id' => array('type' => 'int'),
89                 'event_id' => array('type' => 'char',
90                               'length' => 36,
91                               'not null' => true,
92                               'description' => 'UUID'),
93                 'response' => array('type' => 'char',
94                                   'length' => '1',
95                                   'description' => 'Y, N, or ? for three-state yes, no, maybe'),
96                 'created' => array('type' => 'datetime',
97                                    'not null' => true),
98             ),
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'),
103             ),
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')),
107         );
108     }
109
110     function saveNew($profile, $event, $verb, $options=array())
111     {
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.'));
117             }
118         }
119
120         $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
121                                      'event_id' => $event->id));
122
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.'));
126         }
127
128         $rsvp = new RSVP();
129
130         $rsvp->id          = UUID::gen();
131         $rsvp->profile_id  = $profile->id;
132         $rsvp->event_id    = $event->id;
133         $rsvp->response      = self::codeFor($verb);
134
135         if (array_key_exists('created', $options)) {
136             $rsvp->created = $options['created'];
137         } else {
138             $rsvp->created = common_sql_now();
139         }
140
141         if (array_key_exists('uri', $options)) {
142             $rsvp->uri = $options['uri'];
143         } else {
144             $rsvp->uri = common_local_url('showrsvp',
145                                         array('id' => $rsvp->id));
146         }
147
148         $rsvp->insert();
149
150         self::blow('rsvp:for-event:%s', $event->id);
151
152         // XXX: come up with something sexier
153
154         $content = $rsvp->asString();
155
156         $rendered = $rsvp->asHTML();
157
158         $options = array_merge(array('object_type' => $verb),
159                                $options);
160
161         if (!array_key_exists('uri', $options)) {
162             $options['uri'] = $rsvp->uri;
163         }
164
165         $eventNotice = $event->getNotice();
166
167         if (!empty($eventNotice)) {
168             $options['reply_to'] = $eventNotice->id;
169         }
170
171         $saved = Notice::saveNew($profile->id,
172                                  $content,
173                                  array_key_exists('source', $options) ?
174                                  $options['source'] : 'web',
175                                  $options);
176
177         return $saved;
178     }
179
180     function codeFor($verb)
181     {
182         switch ($verb) {
183         case RSVP::POSITIVE:
184             return 'Y';
185             break;
186         case RSVP::NEGATIVE:
187             return 'N';
188             break;
189         case RSVP::POSSIBLE:
190             return '?';
191             break;
192         default:
193             // TRANS: Exception thrown when requesting an undefined verb for RSVP.
194             throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
195         }
196     }
197
198     static function verbFor($code)
199     {
200         switch ($code) {
201         case 'Y':
202             return RSVP::POSITIVE;
203             break;
204         case 'N':
205             return RSVP::NEGATIVE;
206             break;
207         case '?':
208             return RSVP::POSSIBLE;
209             break;
210         default:
211             // TRANS: Exception thrown when requesting an undefined code for RSVP.
212             throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
213         }
214     }
215
216     function getNotice()
217     {
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));
223         }
224         return $notice;
225     }
226
227     static function fromNotice(Notice $notice)
228     {
229         $rsvp = new RSVP();
230         $rsvp->uri = $notice->uri;
231         if (!$rsvp->find(true)) {
232             throw new NoResultException($rsvp);
233         }
234         return $rsvp;
235     }
236
237     static function forEvent(Happening $event)
238     {
239         $keypart = sprintf('rsvp:for-event:%s', $event->id);
240
241         $idstr = self::cacheGet($keypart);
242
243         if ($idstr !== false) {
244             $ids = explode(',', $idstr);
245         } else {
246             $ids = array();
247
248             $rsvp = new RSVP();
249
250             $rsvp->selectAdd();
251             $rsvp->selectAdd('id');
252
253             $rsvp->event_id = $event->id;
254
255             if ($rsvp->find()) {
256                 while ($rsvp->fetch()) {
257                     $ids[] = $rsvp->id;
258                 }
259             }
260             self::cacheSet($keypart, implode(',', $ids));
261         }
262
263         $rsvps = array(RSVP::POSITIVE => array(),
264                        RSVP::NEGATIVE => array(),
265                        RSVP::POSSIBLE => array());
266
267         foreach ($ids as $id) {
268             $rsvp = RSVP::getKV('id', $id);
269             if (!empty($rsvp)) {
270                 $verb = self::verbFor($rsvp->response);
271                 $rsvps[$verb][] = $rsvp;
272             }
273         }
274
275         return $rsvps;
276     }
277
278     function getProfile()
279     {
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));
285         }
286         return $profile;
287     }
288
289     function getEvent()
290     {
291         $event = Happening::getKV('id', $this->event_id);
292         if (empty($event)) {
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));
296         }
297         return $event;
298     }
299
300     function asHTML()
301     {
302         $event = Happening::getKV('id', $this->event_id);
303
304         return self::toHTML($this->getProfile(),
305                             $event,
306                             $this->response);
307     }
308
309     function asString()
310     {
311         $event = Happening::getKV('id', $this->event_id);
312
313         return self::toString($this->getProfile(),
314                               $event,
315                               $this->response);
316     }
317
318     static function toHTML($profile, $event, $response)
319     {
320         $fmt = null;
321
322         switch ($response) {
323         case 'Y':
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>");
328             break;
329         case 'N':
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>");
334             break;
335         case '?':
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>");
340             break;
341         default:
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));
345             break;
346         }
347
348         if (empty($event)) {
349             $eventUrl = '#';
350             // TRANS: Used as event title when not event title is available.
351             // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
352             $eventTitle = _m('an unknown event');
353         } else {
354             $notice = $event->getNotice();
355             $eventUrl = $notice->getUrl();
356             $eventTitle = $event->title;
357         }
358
359         return sprintf($fmt,
360                        htmlspecialchars($profile->profileurl),
361                        htmlspecialchars($profile->getBestName()),
362                        htmlspecialchars($eventUrl),
363                        htmlspecialchars($eventTitle));
364     }
365
366     static function toString($profile, $event, $response)
367     {
368         $fmt = null;
369
370         switch ($response) {
371         case 'Y':
372             // TRANS: Plain text version of an RSVP ("please respond") status for a user.
373             // TRANS: %1$s is a profile name, %2$s is an event title.
374             $fmt = _m('%1$s is attending %2$s.');
375             break;
376         case 'N':
377             // TRANS: Plain text version of an RSVP ("please respond") status for a user.
378             // TRANS: %1$s is a profile name, %2$s is an event title.
379             $fmt = _m('%1$s is not attending %2$s.');
380             break;
381         case '?':
382             // TRANS: Plain text version of an RSVP ("please respond") status for a user.
383             // TRANS: %1$s is a profile name, %2$s is an event title.
384             $fmt = _m('%1$s might attend %2$s.');
385             break;
386         default:
387             // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
388             // TRANS: %s is the non-existing response code.
389             throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
390             break;
391         }
392
393         if (empty($event)) {
394             // TRANS: Used as event title when not event title is available.
395             // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
396             $eventTitle = _m('an unknown event');
397         } else {
398             $notice = $event->getNotice();
399             $eventTitle = $event->title;
400         }
401
402         return sprintf($fmt,
403                        $profile->getBestName(),
404                        $eventTitle);
405     }
406
407     function delete($useWhere=false)
408     {
409         self::blow('rsvp:for-event:%s', $this->id);
410         return parent::delete($useWhere);
411     }
412 }