]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/RSVP.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Event / 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
46 class RSVP extends Managed_DataObject
47 {
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';
51
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
59
60     /**
61      * Get an instance by key
62      *
63      * @param string $k Key to use to lookup (usually 'id' for this class)
64      * @param mixed  $v Value to lookup
65      *
66      * @return RSVP object found, or null for no hits
67      *
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('RSVP', $k, $v);
72     }
73
74     /**
75      * Get an instance by compound key
76      *
77      * @param array $kv array of key-value mappings
78      *
79      * @return Bookmark object found, or null for no hits
80      *
81      */
82
83     function pkeyGet($kv)
84     {
85         return Memcached_DataObject::pkeyGet('RSVP', $kv);
86     }
87
88     /**
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.
92      *
93      * @return array
94      */
95     function _allCacheKeys() {
96         $keys = parent::_allCacheKeys();
97         $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id,
98                                                     'event_id' => $this->event_id));
99         return $keys;
100     }
101
102     /**
103      * The One True Thingy that must be defined and declared.
104      */
105     public static function schemaDef()
106     {
107         return array(
108             'description' => 'Plan to attend event',
109             'fields' => array(
110                 'id' => array('type' => 'char',
111                               'length' => 36,
112                               'not null' => true,
113                               'description' => 'UUID'),
114                 'uri' => array('type' => 'varchar',
115                                'length' => 255,
116                                'not null' => true),
117                 'profile_id' => array('type' => 'int'),
118                 'event_id' => array('type' => 'char',
119                               'length' => 36,
120                               'not null' => true,
121                               'description' => 'UUID'),
122                 'response' => array('type' => 'char',
123                                   'length' => '1',
124                                   'description' => 'Y, N, or ? for three-state yes, no, maybe'),
125                 'created' => array('type' => 'datetime',
126                                    'not null' => true),
127             ),
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'),
132             ),
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')),
136         );
137     }
138
139     function saveNew($profile, $event, $verb, $options=array())
140     {
141         common_debug("RSVP::saveNew({$profile->id}, {$event->id}, '$verb', 'some options');");
142
143         if (array_key_exists('uri', $options)) {
144             $other = RSVP::staticGet('uri', $options['uri']);
145             if (!empty($other)) {
146                 throw new ClientException(_('RSVP already exists.'));
147             }
148         }
149
150         $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
151                                      'event_id' => $event->id));
152
153         if (!empty($other)) {
154             throw new ClientException(_('RSVP already exists.'));
155         }
156
157         $rsvp = new RSVP();
158
159         $rsvp->id          = UUID::gen();
160         $rsvp->profile_id  = $profile->id;
161         $rsvp->event_id    = $event->id;
162         $rsvp->response      = self::codeFor($verb);
163
164         common_debug("Got value {$rsvp->response} for verb {$verb}");
165
166         if (array_key_exists('created', $options)) {
167             $rsvp->created = $options['created'];
168         } else {
169             $rsvp->created = common_sql_now();
170         }
171
172         if (array_key_exists('uri', $options)) {
173             $rsvp->uri = $options['uri'];
174         } else {
175             $rsvp->uri = common_local_url('showrsvp',
176                                         array('id' => $rsvp->id));
177         }
178
179         $rsvp->insert();
180
181         // XXX: come up with something sexier
182
183         $content = $rsvp->asString();
184         
185         $rendered = $rsvp->asHTML();
186
187         $options = array_merge(array('object_type' => $verb),
188                                $options);
189
190         if (!array_key_exists('uri', $options)) {
191             $options['uri'] = $rsvp->uri;
192         }
193
194         $eventNotice = $event->getNotice();
195
196         if (!empty($eventNotice)) {
197             $options['reply_to'] = $eventNotice->id;
198         }
199
200         $saved = Notice::saveNew($profile->id,
201                                  $content,
202                                  array_key_exists('source', $options) ?
203                                  $options['source'] : 'web',
204                                  $options);
205
206         return $saved;
207     }
208
209     function codeFor($verb)
210     {
211         switch ($verb) {
212         case RSVP::POSITIVE:
213             return 'Y';
214             break;
215         case RSVP::NEGATIVE:
216             return 'N';
217             break;
218         case RSVP::POSSIBLE:
219             return '?';
220             break;
221         default:
222             throw new Exception("Unknown verb {$verb}");
223         }
224     }
225
226     static function verbFor($code)
227     {
228         switch ($code) {
229         case 'Y':
230             return RSVP::POSITIVE;
231             break;
232         case 'N':
233             return RSVP::NEGATIVE;
234             break;
235         case '?':
236             return RSVP::POSSIBLE;
237             break;
238         default:
239             throw new Exception("Unknown code {$code}");
240         }
241     }
242
243     function getNotice()
244     {
245         $notice = Notice::staticGet('uri', $this->uri);
246         if (empty($notice)) {
247             throw new ServerException("RSVP {$this->id} does not correspond to a notice in the DB.");
248         }
249         return $notice;
250     }
251
252     static function fromNotice($notice)
253     {
254         return RSVP::staticGet('uri', $notice->uri);
255     }
256
257     static function forEvent($event)
258     {
259         $rsvps = array(RSVP::POSITIVE => array(),
260                        RSVP::NEGATIVE => array(),
261                        RSVP::POSSIBLE => array());
262
263         $rsvp = new RSVP();
264
265         $rsvp->event_id = $event->id;
266
267         if ($rsvp->find()) {
268             while ($rsvp->fetch()) {
269                 $verb = self::verbFor($rsvp->response);
270                 $rsvps[$verb][] = clone($rsvp);
271             }
272         }
273
274         return $rsvps;
275     }
276
277     function getProfile()
278     {
279         $profile = Profile::staticGet('id', $this->profile_id);
280         if (empty($profile)) {
281             throw new Exception("No profile with ID {$this->profile_id}");
282         }
283         return $profile;
284     }
285
286     function getEvent()
287     {
288         $event = Happening::staticGet('id', $this->event_id);
289         if (empty($event)) {
290             throw new Exception("No event with ID {$this->event_id}");
291         }
292         return $event;
293     }
294
295     function asHTML()
296     {
297         return self::toHTML($this->getProfile(),
298                             $this->getEvent(),
299                             $this->response);
300     }
301
302     function asString()
303     {
304         return self::toString($this->getProfile(),
305                               $this->getEvent(),
306                               $this->response);
307     }
308
309     static function toHTML($profile, $event, $response)
310     {
311         $fmt = null;
312
313         $notice = $event->getNotice();
314
315         switch ($response) {
316         case 'Y':
317             $fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> is attending <a href='%3s'>%4s</a>.</span>");
318             break;
319         case 'N':
320             $fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> is not attending <a href='%3s'>%4s</a>.</span>");
321             break;
322         case '?':
323             $fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> might attend <a href='%3s'>%4s</a>.</span>");
324             break;
325         default:
326             throw new Exception("Unknown response code {$response}");
327             break;
328         }
329
330         return sprintf($fmt,
331                        htmlspecialchars($profile->profileurl),
332                        htmlspecialchars($profile->getBestName()),
333                        htmlspecialchars($notice->bestUrl()),
334                        htmlspecialchars($event->title));
335     }
336
337     static function toString($profile, $event, $response)
338     {
339         $fmt = null;
340
341         $notice = $event->getNotice();
342
343         switch ($response) {
344         case 'Y':
345             $fmt = _("%1s is attending %2s.");
346             break;
347         case 'N':
348             $fmt = _("%1s is not attending %2s.");
349             break;
350         case '?':
351             $fmt = _("%1s might attend %2s.>");
352             break;
353         default:
354             throw new Exception("Unknown response code {$response}");
355             break;
356         }
357
358         return sprintf($fmt,
359                        $profile->getBestName(),
360                        $event->title);
361     }
362 }