]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
make event urls federate
[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  Event
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     var $oldSaveNew = true;
51
52     /**
53      * Set up our tables (event and rsvp)
54      *
55      * @see Schema
56      * @see ColumnDef
57      *
58      * @return boolean hook value; true means continue processing, false means stop.
59      */
60     function onCheckSchema()
61     {
62         $schema = Schema::get();
63
64         $schema->ensureTable('happening', Happening::schemaDef());
65         $schema->ensureTable('rsvp', RSVP::schemaDef());
66
67         return true;
68     }
69
70     /**
71      * Map URLs to actions
72      *
73      * @param URLMapper $m path-to-action mapper
74      *
75      * @return boolean hook value; true means continue processing, false means stop.
76      */
77     public function onRouterInitialized(URLMapper $m)
78     {
79         $m->connect('main/event/new',
80                     array('action' => 'newevent'));
81         $m->connect('main/event/rsvp',
82                     array('action' => 'newrsvp'));
83         $m->connect('main/event/rsvp/cancel',
84                     array('action' => 'cancelrsvp'));
85         $m->connect('event/:id',
86                     array('action' => 'showevent'),
87                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
88         $m->connect('rsvp/:id',
89                     array('action' => 'showrsvp'),
90                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
91         $m->connect('main/event/updatetimes',
92                     array('action' => 'timelist'));
93         return true;
94     }
95
96     function onPluginVersion(array &$versions)
97     {
98         $versions[] = array('name' => 'Event',
99                             'version' => GNUSOCIAL_VERSION,
100                             'author' => 'Evan Prodromou',
101                             'homepage' => 'http://status.net/wiki/Plugin:Event',
102                             'description' =>
103                             // TRANS: Plugin description.
104                             _m('Event invitations and RSVPs.'));
105         return true;
106     }
107
108     function appTitle() {
109         // TRANS: Title for event application.
110         return _m('TITLE','Event');
111     }
112
113     function tag() {
114         return 'event';
115     }
116
117     function types() {
118         return array(Happening::OBJECT_TYPE,
119                      RSVP::POSITIVE,
120                      RSVP::NEGATIVE,
121                      RSVP::POSSIBLE);
122     }
123
124     /**
125      * Given a parsed ActivityStreams activity, save it into a notice
126      * and other data structures.
127      *
128      * @param Activity $activity
129      * @param Profile $actor
130      * @param array $options=array()
131      *
132      * @return Notice the resulting notice
133      */
134     function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
135     {
136         if (count($activity->objects) != 1) {
137             // TRANS: Exception thrown when there are too many activity objects.
138             throw new Exception(_m('Too many activity objects.'));
139         }
140
141         $happeningObj = $activity->objects[0];
142
143         if ($happeningObj->type != Happening::OBJECT_TYPE) {
144             // TRANS: Exception thrown when event plugin comes across a non-event type object.
145             throw new Exception(_m('Wrong type for object.'));
146         }
147
148         $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
149         if($dtstart->length == 0) {
150             // TRANS: Exception thrown when has no start date
151             throw new Exception(_m('No start date for event.'));
152         }
153
154         $dtend = $happeningObj->element->getElementsByTagName('dtend');
155         if($dtend->length == 0) {
156             // TRANS: Exception thrown when has no end date
157             throw new Exception(_m('No end date for event.'));
158         }
159
160         // location is optional
161         $location = null;
162         $location_object = $happeningObj->element->getElementsByTagName('location');
163         if($location_object->length > 0) {
164             $location = $location_object->item(0)->nodeValue;
165         }
166
167         // url is optional
168         $url = null;
169         $url_object = $happeningObj->element->getElementsByTagName('url');
170         if($url_object->length > 0) {
171             $url = $url_object->item(0)->nodeValue;
172         }
173
174         $notice = null;
175
176         switch ($activity->verb) {
177         case ActivityVerb::POST:
178                 // FIXME: get startTime, endTime, location and URL
179             $notice = Happening::saveNew($actor,
180                                          $dtstart->item(0)->nodeValue,
181                                          $dtend->item(0)->nodeValue,
182                                          $happeningObj->title,
183                                          $location,
184                                          $happeningObj->summary,
185                                          $url,
186                                          $options);
187             break;
188         case RSVP::POSITIVE:
189         case RSVP::NEGATIVE:
190         case RSVP::POSSIBLE:
191             $happening = Happening::getKV('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 $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->getUrl();
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         $obj->extra[] = array('location', false, $happening->location);
261         $obj->extra[] = array('url', false, $happening->url);
262
263         // XXX: probably need other stuff here
264
265         return $obj;
266     }
267
268     /**
269      * Change the verb on RSVP notices
270      *
271      * @param Notice $notice
272      *
273      * @return ActivityObject
274      */
275     protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
276         switch ($stored->object_type) {
277         case RSVP::POSITIVE:
278         case RSVP::NEGATIVE:
279         case RSVP::POSSIBLE:
280             $act->verb = $stored->object_type;
281             break;
282         }
283         return true;
284     }
285
286     /**
287      * Form for our app
288      *
289      * @param HTMLOutputter $out
290      * @return Widget
291      */
292     function entryForm($out)
293     {
294         return new EventForm($out);
295     }
296
297     /**
298      * When a notice is deleted, clean up related tables.
299      *
300      * @param Notice $notice
301      */
302     function deleteRelated(Notice $notice)
303     {
304         switch ($notice->object_type) {
305         case Happening::OBJECT_TYPE:
306             common_log(LOG_DEBUG, "Deleting event from notice...");
307             $happening = Happening::fromNotice($notice);
308             $happening->delete();
309             break;
310         case RSVP::POSITIVE:
311         case RSVP::NEGATIVE:
312         case RSVP::POSSIBLE:
313             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
314             $rsvp = RSVP::fromNotice($notice);
315             common_log(LOG_DEBUG, "to delete: $rsvp->id");
316             $rsvp->delete();
317             break;
318         default:
319             common_log(LOG_DEBUG, "Not deleting related, wtf...");
320         }
321     }
322
323     function onEndShowScripts($action)
324     {
325         $action->script($this->path('js/event.js'));
326     }
327
328     function onEndShowStyles($action)
329     {
330         $action->cssLink($this->path('css/event.css'));
331         return true;
332     }
333
334     function onStartAddNoticeReply($nli, $parent, $child)
335     {
336         // Filter out any poll responses
337         if (($parent->object_type == Happening::OBJECT_TYPE) &&
338             in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
339             return false;
340         }
341         return true;
342     }
343
344     protected function showNoticeItemNotice(NoticeListItem $nli)
345     {
346         $nli->showAuthor();
347         $nli->showContent();
348     }
349
350     protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
351     {
352         switch ($stored->object_type) {
353         case Happening::OBJECT_TYPE:
354             $this->showEvent($stored, $out, $scoped);
355             break;
356         case RSVP::POSITIVE:
357         case RSVP::NEGATIVE:
358         case RSVP::POSSIBLE:
359             $this->showRSVP($stored, $out, $scoped);
360             break;
361         }
362     }
363
364     protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
365     {
366         $profile = $stored->getProfile();
367         $event   = Happening::fromNotice($stored);
368
369         if (!$event instanceof Happening) {
370             // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
371             $out->element('p', null, _m('Deleted.'));
372             return;
373         }
374
375         $out->elementStart('div', 'h-event');
376
377         $out->elementStart('h3', 'p-summary p-name');
378
379         try {
380             $out->element('a', array('href' => $event->getUrl()), $event->title);
381         } catch (InvalidUrlException $e) {
382             $out->text($event->title);
383         }
384
385         $out->elementEnd('h3');
386
387         $now       = new DateTime();
388         $startDate = new DateTime($event->start_time);
389         $endDate   = new DateTime($event->end_time);
390         $userTz    = new DateTimeZone(common_timezone());
391
392         // Localize the time for the observer
393         $now->setTimeZone($userTz);
394         $startDate->setTimezone($userTz);
395         $endDate->setTimezone($userTz);
396
397         $thisYear  = $now->format('Y');
398         $startYear = $startDate->format('Y');
399         $endYear   = $endDate->format('Y');
400
401         $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
402
403         if ($startYear != $thisYear || $endYear != $thisYear) {
404             $dateFmt .= 'Y,'; // append year if we need to think about years
405         }
406
407         $startDateStr = $startDate->format($dateFmt);
408         $endDateStr = $endDate->format($dateFmt);
409
410         $timeFmt = 'g:ia';
411
412         $startTimeStr = $startDate->format($timeFmt);
413         $endTimeStr = $endDate->format("{$timeFmt} (T)");
414
415         $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
416
417         // TRANS: Field label for event description.
418         $out->element('strong', null, _m('Time:'));
419
420         $out->element('time', array('class' => 'dt-start',
421                                     'datetime' => common_date_iso8601($event->start_time)),
422                       $startDateStr . ' ' . $startTimeStr);
423         $out->text(' – ');
424         $out->element('time', array('class' => 'dt-end',
425                                     'datetime' => common_date_iso8601($event->end_time)),
426                       $startDateStr != $endDateStr
427                                     ? "$endDateStr $endTimeStr"
428                                     :  $endTimeStr);
429
430         $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
431
432         if (!empty($event->location)) {
433             $out->elementStart('div', 'event-location');
434             // TRANS: Field label for event description.
435             $out->element('strong', null, _m('Location:'));
436             $out->element('span', 'p-location', $event->location);
437             $out->elementEnd('div');
438         }
439
440         if (!empty($event->description)) {
441             $out->elementStart('div', 'event-description');
442             // TRANS: Field label for event description.
443             $out->element('strong', null, _m('Description:'));
444             $out->element('div', 'p-description', $event->description);
445             $out->elementEnd('div');
446         }
447
448         $rsvps = $event->getRSVPs();
449
450         $out->elementStart('div', 'event-rsvps');
451
452         // TRANS: Field label for event description.
453         $out->element('strong', null, _m('Attending:'));
454         $out->elementStart('ul', 'attending-list');
455
456         foreach ($rsvps as $verb => $responses) {
457             $out->elementStart('li', 'rsvp-list');
458             switch ($verb) {
459             case RSVP::POSITIVE:
460                 $out->text(_('Yes:'));
461                 break;
462             case RSVP::NEGATIVE:
463                 $out->text(_('No:'));
464                 break;
465             case RSVP::POSSIBLE:
466                 $out->text(_('Maybe:'));
467                 break;
468             }
469             $ids = array();
470             foreach ($responses as $response) {
471                 $ids[] = $response->profile_id;
472             }
473             $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
474             $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
475             $minilist->show();
476
477             $out->elementEnd('li');
478         }
479
480         $out->elementEnd('ul');
481         $out->elementEnd('div');
482
483         if ($scoped instanceof Profile) {
484             $rsvp = $event->getRSVP($scoped);
485
486             if (empty($rsvp)) {
487                 $form = new RSVPForm($event, $out);
488             } else {
489                 $form = new CancelRSVPForm($rsvp, $out);
490             }
491
492             $form->show();
493         }
494         $out->elementEnd('div');
495     }
496
497     protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
498     {
499         $rsvp = RSVP::fromNotice($stored);
500
501         if (empty($rsvp)) {
502             // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
503             $out->element('p', null, _m('Deleted.'));
504             return;
505         }
506
507         $out->elementStart('div', 'rsvp');
508         $out->raw($rsvp->asHTML());
509         $out->elementEnd('div');
510     }
511 }