]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
Merge commit '5c4a7a10' into nightly
[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
94         $m->connect(':nickname/events',
95                     array('action' => 'events'),
96                     array('nickname' => Nickname::DISPLAY_FMT));
97         return true;
98     }
99
100     function onPluginVersion(array &$versions)
101     {
102         $versions[] = array('name' => 'Event',
103                             'version' => GNUSOCIAL_VERSION,
104                             'author' => 'Evan Prodromou',
105                             'homepage' => 'http://status.net/wiki/Plugin:Event',
106                             'description' =>
107                             // TRANS: Plugin description.
108                             _m('Event invitations and RSVPs.'));
109         return true;
110     }
111
112     function appTitle() {
113         // TRANS: Title for event application.
114         return _m('TITLE','Event');
115     }
116
117     function tag() {
118         return 'event';
119     }
120
121     function types() {
122         return array(Happening::OBJECT_TYPE,
123                      RSVP::POSITIVE,
124                      RSVP::NEGATIVE,
125                      RSVP::POSSIBLE);
126     }
127
128     /**
129      * Given a parsed ActivityStreams activity, save it into a notice
130      * and other data structures.
131      *
132      * @param Activity $activity
133      * @param Profile $actor
134      * @param array $options=array()
135      *
136      * @return Notice the resulting notice
137      */
138     function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
139     {
140         if (count($activity->objects) != 1) {
141             // TRANS: Exception thrown when there are too many activity objects.
142             throw new Exception(_m('Too many activity objects.'));
143         }
144
145         $happeningObj = $activity->objects[0];
146
147         if ($happeningObj->type != Happening::OBJECT_TYPE) {
148             // TRANS: Exception thrown when event plugin comes across a non-event type object.
149             throw new Exception(_m('Wrong type for object.'));
150         }
151
152         $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
153         if($dtstart->length == 0) {
154             // TRANS: Exception thrown when has no start date
155             throw new Exception(_m('No start date for event.'));
156         }
157
158         $dtend = $happeningObj->element->getElementsByTagName('dtend');
159         if($dtend->length == 0) {
160             // TRANS: Exception thrown when has no end date
161             throw new Exception(_m('No end date for event.'));
162         }
163
164         // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
165         $start_time = new DateTime($dtstart->item(0)->nodeValue);
166         $start_time->setTimezone(new DateTimeZone('UTC'));
167         $start_time = $start_time->format('Y-m-d H:i:s');
168         $end_time = new DateTime($dtend->item(0)->nodeValue);
169         $end_time->setTimezone(new DateTimeZone('UTC'));
170         $end_time = $end_time->format('Y-m-d H:i:s');
171
172         // location is optional
173         $location = null;
174         $location_object = $happeningObj->element->getElementsByTagName('location');
175         if($location_object->length > 0) {
176             $location = $location_object->item(0)->nodeValue;
177         }
178
179         // url is optional
180         $url = null;
181         $url_object = $happeningObj->element->getElementsByTagName('url');
182         if($url_object->length > 0) {
183             $url = $url_object->item(0)->nodeValue;
184         }
185
186         $notice = null;
187
188         switch ($activity->verb) {
189         case ActivityVerb::POST:
190                 // FIXME: get startTime, endTime, location and URL
191             $notice = Happening::saveNew($actor,
192                                          $start_time,
193                                          $end_time,
194                                          $happeningObj->title,
195                                          $location,
196                                          $happeningObj->summary,
197                                          $url,
198                                          $options);
199             break;
200         case RSVP::POSITIVE:
201         case RSVP::NEGATIVE:
202         case RSVP::POSSIBLE:
203             $happening = Happening::getKV('uri', $happeningObj->id);
204             if (empty($happening)) {
205                 // FIXME: save the event
206                 // TRANS: Exception thrown when trying to RSVP for an unknown event.
207                 throw new Exception(_m('RSVP for unknown event.'));
208             }
209             $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
210             break;
211         default:
212             // TRANS: Exception thrown when event plugin comes across a undefined verb.
213             throw new Exception(_m('Unknown verb for events.'));
214         }
215
216         return $notice;
217     }
218
219     /**
220      * Turn a Notice into an activity object
221      *
222      * @param Notice $notice
223      *
224      * @return ActivityObject
225      */
226     function activityObjectFromNotice(Notice $notice)
227     {
228         $happening = null;
229
230         switch ($notice->object_type) {
231         case Happening::OBJECT_TYPE:
232             $happening = Happening::fromNotice($notice);
233             break;
234         case RSVP::POSITIVE:
235         case RSVP::NEGATIVE:
236         case RSVP::POSSIBLE:
237             $rsvp  = RSVP::fromNotice($notice);
238             $happening = $rsvp->getEvent();
239             break;
240         }
241
242         if (empty($happening)) {
243             // TRANS: Exception thrown when event plugin comes across a unknown object type.
244             throw new Exception(_m('Unknown object type.'));
245         }
246
247         $notice = $happening->getNotice();
248
249         if (empty($notice)) {
250             // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
251             throw new Exception(_m('Unknown event notice.'));
252         }
253
254         $obj = new ActivityObject();
255
256         $obj->id      = $happening->uri;
257         $obj->type    = Happening::OBJECT_TYPE;
258         $obj->title   = $happening->title;
259         $obj->summary = $happening->description;
260         $obj->link    = $notice->getUrl();
261
262         // XXX: how to get this stuff into JSON?!
263
264         $obj->extra[] = array('dtstart',
265                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
266                               common_date_iso8601($happening->start_time));
267
268         $obj->extra[] = array('dtend',
269                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
270                               common_date_iso8601($happening->end_time));
271
272         $obj->extra[] = array('location', false, $happening->location);
273         $obj->extra[] = array('url', false, $happening->url);
274
275         // XXX: probably need other stuff here
276
277         return $obj;
278     }
279
280     /**
281      * Change the verb on RSVP notices
282      *
283      * @param Notice $notice
284      *
285      * @return ActivityObject
286      */
287     protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
288         switch ($stored->object_type) {
289         case RSVP::POSITIVE:
290         case RSVP::NEGATIVE:
291         case RSVP::POSSIBLE:
292             $act->verb = $stored->object_type;
293             break;
294         }
295         return true;
296     }
297
298     /**
299      * Form for our app
300      *
301      * @param HTMLOutputter $out
302      * @return Widget
303      */
304     function entryForm($out)
305     {
306         return new EventForm($out);
307     }
308
309     /**
310      * When a notice is deleted, clean up related tables.
311      *
312      * @param Notice $notice
313      */
314     function deleteRelated(Notice $notice)
315     {
316         switch ($notice->object_type) {
317         case Happening::OBJECT_TYPE:
318             common_log(LOG_DEBUG, "Deleting event from notice...");
319             $happening = Happening::fromNotice($notice);
320             $happening->delete();
321             break;
322         case RSVP::POSITIVE:
323         case RSVP::NEGATIVE:
324         case RSVP::POSSIBLE:
325             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
326             $rsvp = RSVP::fromNotice($notice);
327             common_log(LOG_DEBUG, "to delete: $rsvp->id");
328             $rsvp->delete();
329             break;
330         default:
331             common_log(LOG_DEBUG, "Not deleting related, wtf...");
332         }
333     }
334
335     function onEndShowScripts($action)
336     {
337         $action->script($this->path('js/event.js'));
338     }
339
340     function onEndShowStyles($action)
341     {
342         $action->cssLink($this->path('css/event.css'));
343         return true;
344     }
345
346     function onStartAddNoticeReply($nli, $parent, $child)
347     {
348         // Filter out any poll responses
349         if (($parent->object_type == Happening::OBJECT_TYPE) &&
350             in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
351             return false;
352         }
353         return true;
354     }
355
356     protected function showNoticeItemNotice(NoticeListItem $nli)
357     {
358         $nli->showAuthor();
359         $nli->showContent();
360     }
361
362     protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
363     {
364         switch ($stored->object_type) {
365         case Happening::OBJECT_TYPE:
366             $this->showEvent($stored, $out, $scoped);
367             break;
368         case RSVP::POSITIVE:
369         case RSVP::NEGATIVE:
370         case RSVP::POSSIBLE:
371             $this->showRSVP($stored, $out, $scoped);
372             break;
373         }
374     }
375
376     protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
377     {
378         $profile = $stored->getProfile();
379         $event   = Happening::fromNotice($stored);
380
381         if (!$event instanceof Happening) {
382             // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
383             $out->element('p', null, _m('Deleted.'));
384             return;
385         }
386
387         $out->elementStart('div', 'h-event');
388
389         $out->elementStart('h3', 'p-summary p-name');
390
391         try {
392             $out->element('a', array('href' => $event->getUrl()), $event->title);
393         } catch (InvalidUrlException $e) {
394             $out->text($event->title);
395         }
396
397         $out->elementEnd('h3');
398
399         $now       = new DateTime();
400         $startDate = new DateTime($event->start_time);
401         $endDate   = new DateTime($event->end_time);
402         $userTz    = new DateTimeZone(common_timezone());
403
404         // Localize the time for the observer
405         $now->setTimeZone($userTz);
406         $startDate->setTimezone($userTz);
407         $endDate->setTimezone($userTz);
408
409         $thisYear  = $now->format('Y');
410         $startYear = $startDate->format('Y');
411         $endYear   = $endDate->format('Y');
412
413         $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
414
415         if ($startYear != $thisYear || $endYear != $thisYear) {
416             $dateFmt .= 'Y,'; // append year if we need to think about years
417         }
418
419         $startDateStr = $startDate->format($dateFmt);
420         $endDateStr = $endDate->format($dateFmt);
421
422         $timeFmt = 'g:ia';
423
424         $startTimeStr = $startDate->format($timeFmt);
425         $endTimeStr = $endDate->format("{$timeFmt} (T)");
426
427         $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
428
429         // TRANS: Field label for event description.
430         $out->element('strong', null, _m('Time:'));
431
432         $out->element('time', array('class' => 'dt-start',
433                                     'datetime' => common_date_iso8601($event->start_time)),
434                       $startDateStr . ' ' . $startTimeStr);
435         $out->text(' – ');
436         $out->element('time', array('class' => 'dt-end',
437                                     'datetime' => common_date_iso8601($event->end_time)),
438                       $startDateStr != $endDateStr
439                                     ? "$endDateStr $endTimeStr"
440                                     :  $endTimeStr);
441
442         $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
443
444         if (!empty($event->location)) {
445             $out->elementStart('div', 'event-location');
446             // TRANS: Field label for event description.
447             $out->element('strong', null, _m('Location:'));
448             $out->element('span', 'p-location', $event->location);
449             $out->elementEnd('div');
450         }
451
452         if (!empty($event->description)) {
453             $out->elementStart('div', 'event-description');
454             // TRANS: Field label for event description.
455             $out->element('strong', null, _m('Description:'));
456             $out->element('div', 'p-description', $event->description);
457             $out->elementEnd('div');
458         }
459
460         $rsvps = $event->getRSVPs();
461
462         $out->elementStart('div', 'event-rsvps');
463
464         // TRANS: Field label for event description.
465         $out->element('strong', null, _m('Attending:'));
466         $out->elementStart('ul', 'attending-list');
467
468         foreach ($rsvps as $verb => $responses) {
469             $out->elementStart('li', 'rsvp-list');
470             switch ($verb) {
471             case RSVP::POSITIVE:
472                 $out->text(_('Yes:'));
473                 break;
474             case RSVP::NEGATIVE:
475                 $out->text(_('No:'));
476                 break;
477             case RSVP::POSSIBLE:
478                 $out->text(_('Maybe:'));
479                 break;
480             }
481             $ids = array();
482             foreach ($responses as $response) {
483                 $ids[] = $response->profile_id;
484             }
485             $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
486             $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
487             $minilist->show();
488
489             $out->elementEnd('li');
490         }
491
492         $out->elementEnd('ul');
493         $out->elementEnd('div');
494
495         if ($scoped instanceof Profile) {
496             $rsvp = $event->getRSVP($scoped);
497
498             if (empty($rsvp)) {
499                 $form = new RSVPForm($event, $out);
500             } else {
501                 $form = new CancelRSVPForm($rsvp, $out);
502             }
503
504             $form->show();
505         }
506         $out->elementEnd('div');
507     }
508
509     protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
510     {
511         $rsvp = RSVP::fromNotice($stored);
512
513         if (empty($rsvp)) {
514             // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
515             $out->element('p', null, _m('Deleted.'));
516             return;
517         }
518
519         $out->elementStart('div', 'rsvp');
520         $out->raw($rsvp->asHTML());
521         $out->elementEnd('div');
522     }
523
524     function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
525     {
526         $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
527                           // TRANS: Menu item in sample plugin.
528                           _m('Happenings'),
529                           // TRANS: Menu item title in sample plugin.
530                           _m('A list of your events'), false, 'nav_timeline_events');
531         return true;
532     }
533 }