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