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