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