]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/classes/RSVP.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[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)
228     {
229         return RSVP::getKV('uri', $notice->uri);
230     }
231
232     static function forEvent($event)
233     {
234         $keypart = sprintf('rsvp:for-event:%s', $event->id);
235
236         $idstr = self::cacheGet($keypart);
237
238         if ($idstr !== false) {
239             $ids = explode(',', $idstr);
240         } else {
241             $ids = array();
242
243             $rsvp = new RSVP();
244
245             $rsvp->selectAdd();
246             $rsvp->selectAdd('id');
247
248             $rsvp->event_id = $event->id;
249
250             if ($rsvp->find()) {
251                 while ($rsvp->fetch()) {
252                     $ids[] = $rsvp->id;
253                 }
254             }
255             self::cacheSet($keypart, implode(',', $ids));
256         }
257
258         $rsvps = array(RSVP::POSITIVE => array(),
259                        RSVP::NEGATIVE => array(),
260                        RSVP::POSSIBLE => array());
261
262         foreach ($ids as $id) {
263             $rsvp = RSVP::getKV('id', $id);
264             if (!empty($rsvp)) {
265                 $verb = self::verbFor($rsvp->response);
266                 $rsvps[$verb][] = $rsvp;
267             }
268         }
269
270         return $rsvps;
271     }
272
273     function getProfile()
274     {
275         $profile = Profile::getKV('id', $this->profile_id);
276         if (empty($profile)) {
277             // TRANS: Exception thrown when requesting a non-existing profile.
278             // TRANS: %s is the ID of the non-existing profile.
279             throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
280         }
281         return $profile;
282     }
283
284     function getEvent()
285     {
286         $event = Happening::getKV('id', $this->event_id);
287         if (empty($event)) {
288             // TRANS: Exception thrown when requesting a non-existing event.
289             // TRANS: %s is the ID of the non-existing event.
290             throw new Exception(sprintf(_m('No event with ID %s.'),$this->event_id));
291         }
292         return $event;
293     }
294
295     function asHTML()
296     {
297         $event = Happening::getKV('id', $this->event_id);
298
299         return self::toHTML($this->getProfile(),
300                             $event,
301                             $this->response);
302     }
303
304     function asString()
305     {
306         $event = Happening::getKV('id', $this->event_id);
307
308         return self::toString($this->getProfile(),
309                               $event,
310                               $this->response);
311     }
312
313     static function toHTML($profile, $event, $response)
314     {
315         $fmt = null;
316
317         switch ($response) {
318         case 'Y':
319             // TRANS: HTML version of an RSVP ("please respond") status for a user.
320             // TRANS: %1$s is a profile URL, %2$s a profile name,
321             // TRANS: %3$s is an event URL, %4$s an event title.
322             $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
323             break;
324         case 'N':
325             // TRANS: HTML version of an RSVP ("please respond") status for a user.
326             // TRANS: %1$s is a profile URL, %2$s a profile name,
327             // TRANS: %3$s is an event URL, %4$s an event title.
328             $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>");
329             break;
330         case '?':
331             // TRANS: HTML version of an RSVP ("please respond") status for a user.
332             // TRANS: %1$s is a profile URL, %2$s a profile name,
333             // TRANS: %3$s is an event URL, %4$s an event title.
334             $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
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
343         if (empty($event)) {
344             $eventUrl = '#';
345             // TRANS: Used as event title when not event title is available.
346             // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
347             $eventTitle = _m('an unknown event');
348         } else {
349             $notice = $event->getNotice();
350             $eventUrl = $notice->bestUrl();
351             $eventTitle = $event->title;
352         }
353
354         return sprintf($fmt,
355                        htmlspecialchars($profile->profileurl),
356                        htmlspecialchars($profile->getBestName()),
357                        htmlspecialchars($eventUrl),
358                        htmlspecialchars($eventTitle));
359     }
360
361     static function toString($profile, $event, $response)
362     {
363         $fmt = null;
364
365         switch ($response) {
366         case 'Y':
367             // TRANS: Plain text version of an RSVP ("please respond") status for a user.
368             // TRANS: %1$s is a profile name, %2$s is an event title.
369             $fmt = _m('%1$s is attending %2$s.');
370             break;
371         case 'N':
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 not attending %2$s.');
375             break;
376         case '?':
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 might attend %2$s.');
380             break;
381         default:
382             // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
383             // TRANS: %s is the non-existing response code.
384             throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
385             break;
386         }
387
388         if (empty($event)) {
389             // TRANS: Used as event title when not event title is available.
390             // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
391             $eventTitle = _m('an unknown event');
392         } else {
393             $notice = $event->getNotice();
394             $eventTitle = $event->title;
395         }
396
397         return sprintf($fmt,
398                        $profile->getBestName(),
399                        $eventTitle);
400     }
401
402     function delete()
403     {
404         self::blow('rsvp:for-event:%s', $event->id);
405         parent::delete();
406     }
407 }