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