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