]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
Merge remote-tracking branch 'gitorious/1.0.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Event / EventPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Microapp plugin for event invitations and RSVPs
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Event
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Event plugin
39  *
40  * @category  Sample
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class EventPlugin extends MicroappPlugin
48 {
49     /**
50      * Set up our tables (event and rsvp)
51      *
52      * @see Schema
53      * @see ColumnDef
54      *
55      * @return boolean hook value; true means continue processing, false means stop.
56      */
57     function onCheckSchema()
58     {
59         $schema = Schema::get();
60
61         $schema->ensureTable('happening', Happening::schemaDef());
62         $schema->ensureTable('rsvp', RSVP::schemaDef());
63
64         return true;
65     }
66
67     /**
68      * Load related modules when needed
69      *
70      * @param string $cls Name of the class to be loaded
71      *
72      * @return boolean hook value; true means continue processing, false means stop.
73      */
74     function onAutoload($cls)
75     {
76         $dir = dirname(__FILE__);
77
78         switch ($cls)
79         {
80         case 'NeweventAction':
81         case 'NewrsvpAction':
82         case 'CancelrsvpAction':
83         case 'ShoweventAction':
84         case 'ShowrsvpAction':
85             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
86             return false;
87         case 'EventForm':
88         case 'RSVPForm':
89         case 'CancelRSVPForm':
90             include_once $dir . '/'.strtolower($cls).'.php';
91             break;
92         case 'Happening':
93         case 'RSVP':
94             include_once $dir . '/'.$cls.'.php';
95             return false;
96         default:
97             return true;
98         }
99     }
100
101     /**
102      * Map URLs to actions
103      *
104      * @param Net_URL_Mapper $m path-to-action mapper
105      *
106      * @return boolean hook value; true means continue processing, false means stop.
107      */
108     function onRouterInitialized($m)
109     {
110         $m->connect('main/event/new',
111                     array('action' => 'newevent'));
112         $m->connect('main/event/rsvp',
113                     array('action' => 'newrsvp'));
114         $m->connect('main/event/rsvp/cancel',
115                     array('action' => 'cancelrsvp'));
116         $m->connect('event/:id',
117                     array('action' => 'showevent'),
118                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
119         $m->connect('rsvp/:id',
120                     array('action' => 'showrsvp'),
121                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
122         return true;
123     }
124
125     function onPluginVersion(&$versions)
126     {
127         $versions[] = array('name' => 'Event',
128                             'version' => STATUSNET_VERSION,
129                             'author' => 'Evan Prodromou',
130                             'homepage' => 'http://status.net/wiki/Plugin:Event',
131                             'description' =>
132                             // TRANS: Plugin description.
133                             _m('Event invitations and RSVPs.'));
134         return true;
135     }
136
137     function appTitle() {
138         // TRANS: Title for event application.
139         return _m('TITLE','Event');
140     }
141
142     function tag() {
143         return 'event';
144     }
145
146     function types() {
147         return array(Happening::OBJECT_TYPE,
148                      RSVP::POSITIVE,
149                      RSVP::NEGATIVE,
150                      RSVP::POSSIBLE);
151     }
152
153     /**
154      * Given a parsed ActivityStreams activity, save it into a notice
155      * and other data structures.
156      *
157      * @param Activity $activity
158      * @param Profile $actor
159      * @param array $options=array()
160      *
161      * @return Notice the resulting notice
162      */
163     function saveNoticeFromActivity($activity, $actor, $options=array())
164     {
165         if (count($activity->objects) != 1) {
166             throw new Exception(_('Too many activity objects.'));
167         }
168
169         $happeningObj = $activity->objects[0];
170
171         if ($happeningObj->type != Happening::OBJECT_TYPE) {
172             // TRANS: Exception thrown when event plugin comes across a non-event type object.
173             throw new Exception(_m('Wrong type for object.'));
174         }
175
176         $notice = null;
177
178         switch ($activity->verb) {
179         case ActivityVerb::POST:
180             $notice = Happening::saveNew($actor,
181                                      $start_time,
182                                      $end_time,
183                                      $happeningObj->title,
184                                      null,
185                                      $happeningObj->summary,
186                                      $options);
187             break;
188         case RSVP::POSITIVE:
189         case RSVP::NEGATIVE:
190         case RSVP::POSSIBLE:
191             $happening = Happening::staticGet('uri', $happeningObj->id);
192             if (empty($happening)) {
193                 // FIXME: save the event
194                 // TRANS: Exception thrown when trying to RSVP for an unknown event.
195                 throw new Exception(_m('RSVP for unknown event.'));
196             }
197             $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
198             break;
199         default:
200             // TRANS: Exception thrown when event plugin comes across a undefined verb.
201             throw new Exception(_m('Unknown verb for events.'));
202         }
203
204         return $notice;
205     }
206
207     /**
208      * Turn a Notice into an activity object
209      *
210      * @param Notice $notice
211      *
212      * @return ActivityObject
213      */
214     function activityObjectFromNotice($notice)
215     {
216         $happening = null;
217
218         switch ($notice->object_type) {
219         case Happening::OBJECT_TYPE:
220             $happening = Happening::fromNotice($notice);
221             break;
222         case RSVP::POSITIVE:
223         case RSVP::NEGATIVE:
224         case RSVP::POSSIBLE:
225             $rsvp  = RSVP::fromNotice($notice);
226             $happening = $rsvp->getEvent();
227             break;
228         }
229
230         if (empty($happening)) {
231             // TRANS: Exception thrown when event plugin comes across a unknown object type.
232             throw new Exception(_m('Unknown object type.'));
233         }
234
235         $notice = $happening->getNotice();
236
237         if (empty($notice)) {
238             // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
239             throw new Exception(_m('Unknown event notice.'));
240         }
241
242         $obj = new ActivityObject();
243
244         $obj->id      = $happening->uri;
245         $obj->type    = Happening::OBJECT_TYPE;
246         $obj->title   = $happening->title;
247         $obj->summary = $happening->description;
248         $obj->link    = $notice->bestUrl();
249
250         // XXX: how to get this stuff into JSON?!
251
252         $obj->extra[] = array('dtstart',
253                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
254                               common_date_iso8601($happening->start_time));
255
256         $obj->extra[] = array('dtend',
257                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
258                               common_date_iso8601($happening->end_time));
259
260         // XXX: probably need other stuff here
261
262         return $obj;
263     }
264
265     /**
266      * Change the verb on RSVP notices
267      *
268      * @param Notice $notice
269      *
270      * @return ActivityObject
271      */
272     function onEndNoticeAsActivity($notice, &$act) {
273         switch ($notice->object_type) {
274         case RSVP::POSITIVE:
275         case RSVP::NEGATIVE:
276         case RSVP::POSSIBLE:
277             $act->verb = $notice->object_type;
278             break;
279         }
280         return true;
281     }
282
283     /**
284      * Custom HTML output for our notices
285      *
286      * @param Notice $notice
287      * @param HTMLOutputter $out
288      */
289     function showNotice($notice, $out)
290     {
291         switch ($notice->object_type) {
292         case Happening::OBJECT_TYPE:
293             $this->showEventNotice($notice, $out);
294             break;
295         case RSVP::POSITIVE:
296         case RSVP::NEGATIVE:
297         case RSVP::POSSIBLE:
298             $this->showRSVPNotice($notice, $out);
299             break;
300         }
301
302         // @fixme we have to start the name/avatar and open this div
303         $out->elementStart('div', array('class' => 'event-info entry-content')); // EVENT-INFO.ENTRY-CONTENT IN
304
305         $profile = $notice->getProfile();
306         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
307
308         $out->element('img',
309                       array('src' => ($avatar) ?
310                             $avatar->displayUrl() :
311                             Avatar::defaultImage(AVATAR_MINI_SIZE),
312                             'class' => 'avatar photo bookmark-avatar',
313                             'width' => AVATAR_MINI_SIZE,
314                             'height' => AVATAR_MINI_SIZE,
315                             'alt' => $profile->getBestName()));
316
317         $out->raw('&#160;'); // avoid &nbsp; for AJAX XML compatibility
318
319         $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
320         $out->element('a',
321                       array('class' => 'url',
322                             'href' => $profile->profileurl,
323                             'title' => $profile->getBestName()),
324                       $profile->nickname);
325         $out->elementEnd('span');
326     }
327
328     function showRSVPNotice($notice, $out)
329     {
330         $rsvp = RSVP::fromNotice($notice);
331
332         if (empty($rsvp)) {
333             $out->element('p', null, _('Deleted.'));
334             return;
335         }
336
337         $out->elementStart('div', 'rsvp');
338         $out->raw($rsvp->asHTML());
339         $out->elementEnd('div');
340         return;
341     }
342
343     function showEventNotice($notice, $out)
344     {
345         $profile = $notice->getProfile();
346         $event   = Happening::fromNotice($notice);
347
348         if (empty($event)) {
349             $out->element('p', null, _('Deleted.'));
350             return;
351         }
352
353         $out->elementStart('div', 'vevent event'); // VEVENT IN
354
355         $out->elementStart('h3');  // VEVENT/H3 IN
356
357         if (!empty($event->url)) {
358             $out->element('a',
359                           array('href' => $event->url,
360                                 'class' => 'event-title entry-title summary'),
361                           $event->title);
362         } else {
363             $out->text($event->title);
364         }
365
366         $out->elementEnd('h3'); // VEVENT/H3 OUT
367
368         $startDate = strftime("%x", strtotime($event->start_time));
369         $startTime = strftime("%R", strtotime($event->start_time));
370
371         $endDate = strftime("%x", strtotime($event->end_time));
372         $endTime = strftime("%R", strtotime($event->end_time));
373
374         // FIXME: better dates
375
376         $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
377
378         // TRANS: Field label for event description.
379         $out->element('strong', null, _m('Time:'));
380
381         $out->element('abbr', array('class' => 'dtstart',
382                                     'title' => common_date_iso8601($event->start_time)),
383                       $startDate . ' ' . $startTime);
384         $out->text(' - ');
385         if ($startDate == $endDate) {
386             $out->element('span', array('class' => 'dtend',
387                                         'title' => common_date_iso8601($event->end_time)),
388                           $endTime);
389         } else {
390             $out->element('span', array('class' => 'dtend',
391                                         'title' => common_date_iso8601($event->end_time)),
392                           $endDate . ' ' . $endTime);
393         }
394
395         $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
396
397         if (!empty($event->location)) {
398             $out->elementStart('div', 'event-location');
399             // TRANS: Field label for event description.
400             $out->element('strong', null, _m('Location:'));
401             $out->element('span', 'location', $event->location);
402             $out->elementEnd('div');
403         }
404
405         if (!empty($event->description)) {
406             $out->elementStart('div', 'event-description');
407             // TRANS: Field label for event description.
408             $out->element('strong', null, _m('Description:'));
409             $out->element('span', 'description', $event->description);
410             $out->elementEnd('div');
411         }
412
413         $rsvps = $event->getRSVPs();
414
415         $out->elementStart('div', 'event-rsvps');
416         // TRANS: Field label for event description.
417         $out->element('strong', null, _m('Attending:'));
418         $out->element('span', 'event-rsvps',
419                       // TRANS: RSVP counts.
420                       // TRANS: %1$d, %2$d and %3$d are numbers of RSVPs.
421                       sprintf(_m('Yes: %1$d No: %2$d Maybe: %3$d'),
422                               count($rsvps[RSVP::POSITIVE]),
423                               count($rsvps[RSVP::NEGATIVE]),
424                               count($rsvps[RSVP::POSSIBLE])));
425         $out->elementEnd('div');
426
427         $user = common_current_user();
428
429         if (!empty($user)) {
430             $rsvp = $event->getRSVP($user->getProfile());
431
432             if (empty($rsvp)) {
433                 $form = new RSVPForm($event, $out);
434             } else {
435                 $form = new CancelRSVPForm($rsvp, $out);
436             }
437
438             $form->show();
439         }
440
441         $out->elementEnd('div'); // vevent out
442     }
443
444     /**
445      * Form for our app
446      *
447      * @param HTMLOutputter $out
448      * @return Widget
449      */
450     function entryForm($out)
451     {
452         return new EventForm($out);
453     }
454
455     /**
456      * When a notice is deleted, clean up related tables.
457      *
458      * @param Notice $notice
459      */
460     function deleteRelated($notice)
461     {
462         switch ($notice->object_type) {
463         case Happening::OBJECT_TYPE:
464             common_log(LOG_DEBUG, "Deleting event from notice...");
465             $happening = Happening::fromNotice($notice);
466             $happening->delete();
467             break;
468         case RSVP::POSITIVE:
469         case RSVP::NEGATIVE:
470         case RSVP::POSSIBLE:
471             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
472             $rsvp = RSVP::fromNotice($notice);
473             common_log(LOG_DEBUG, "to delete: $rsvp->id");
474             $rsvp->delete();
475             break;
476         default:
477             common_log(LOG_DEBUG, "Not deleting related, wtf...");
478         }
479     }
480
481     function onEndShowScripts($action)
482     {
483         $action->inlineScript('$(document).ready(function() { $("#startdate").datepicker(); $("#enddate").datepicker(); });');
484     }
485
486     function onEndShowStyles($action)
487     {
488         $action->cssLink($this->path('event.css'));
489         return true;
490     }
491 }