]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
56081b1a8ea71dd0106f5577df0aaaae91411ac9
[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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Event plugin
35  *
36  * @category  Event
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2011 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43 class EventPlugin extends ActivityVerbHandlerPlugin
44 {
45     /**
46      * Set up our tables (event and rsvp)
47      *
48      * @see Schema
49      * @see ColumnDef
50      *
51      * @return boolean hook value; true means continue processing, false means stop.
52      */
53     function onCheckSchema()
54     {
55         $schema = Schema::get();
56
57         $schema->ensureTable('happening', Happening::schemaDef());
58         $schema->ensureTable('rsvp', RSVP::schemaDef());
59
60         return true;
61     }
62
63     public function onBeforePluginCheckSchema()
64     {
65         RSVP::beforeSchemaUpdate();
66         return true;
67     }
68
69     /**
70      * Map URLs to actions
71      *
72      * @param URLMapper $m path-to-action mapper
73      *
74      * @return boolean hook value; true means continue processing, false means stop.
75      */
76     public function onRouterInitialized(URLMapper $m)
77     {
78         $m->connect('main/event/new',
79                     array('action' => 'newevent'));
80         $m->connect('main/event/rsvp',
81                     array('action' => 'newrsvp'));
82         $m->connect('main/event/rsvp/cancel',
83                     array('action' => 'cancelrsvp'));
84         $m->connect('event/:id',
85                     array('action' => 'showevent'),
86                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
87         $m->connect('rsvp/:id',
88                     array('action' => 'showrsvp'),
89                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
90         $m->connect('main/event/updatetimes',
91                     array('action' => 'timelist'));
92
93         $m->connect(':nickname/events',
94                     array('action' => 'events'),
95                     array('nickname' => Nickname::DISPLAY_FMT));
96         return true;
97     }
98
99     function onPluginVersion(array &$versions)
100     {
101         $versions[] = array('name' => 'Event',
102                             'version' => GNUSOCIAL_VERSION,
103                             'author' => 'Evan Prodromou',
104                             'homepage' => 'http://status.net/wiki/Plugin:Event',
105                             'description' =>
106                             // TRANS: Plugin description.
107                             _m('Event invitations and RSVPs.'));
108         return true;
109     }
110
111     function appTitle() {
112         // TRANS: Title for event application.
113         return _m('TITLE','Event');
114     }
115
116     function tag() {
117         return 'event';
118     }
119
120     function types() {
121         return array(Happening::OBJECT_TYPE);
122     }
123
124     function verbs() {
125         return array(ActivityVerb::POST,
126                      RSVP::POSITIVE,
127                      RSVP::NEGATIVE,
128                      RSVP::POSSIBLE);
129     }
130
131     public function newFormAction() {
132         // such as 'newbookmark' or 'newevent' route
133         return 'new'.$this->tag();
134     }
135
136     function onStartShowEntryForms(&$tabs)
137     {
138         $tabs[$this->tag()] = array('title' => $this->appTitle(),
139                                     'href'  => common_local_url($this->newFormAction()),
140                                    );
141         return true;
142     }
143
144     function onStartMakeEntryForm($tag, $out, &$form)
145     {
146         if ($tag == $this->tag()) {
147             $form = $this->entryForm($out);
148             return false;
149         }
150
151         return true;
152     }
153
154     protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
155     {
156         return $verb;
157     }
158
159     protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
160     {
161         return true;
162     }
163
164     protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
165     {
166         throw new ServerException('Event does not handle doActionPost yet', 501);
167     }
168
169     protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
170     {
171         return new RSVPForm(Happening::fromStored($target), $action);
172     }
173
174     protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
175     {
176         switch (true) {
177         case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
178             return Happening::saveActivityObject($act, $stored);
179             break;
180
181         case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
182             return RSVP::saveActivityObject($act, $stored);
183             break;
184         }
185         return null;
186     }
187
188     function activityObjectFromNotice(Notice $stored)
189     {
190         $happening = null;
191
192         switch (true) {
193         case ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST)) &&
194                 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
195             $happening = Happening::fromStored($stored);
196             break;
197         // FIXME: Why are these object_type??
198         case ActivityUtils::compareTypes($stored->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
199             $rsvp  = RSVP::fromNotice($stored);
200             $happening = $rsvp->getEvent();
201             break;
202         default:
203             // TRANS: Exception thrown when event plugin comes across a unknown object type.
204             throw new Exception(_m('Unknown object type.'));
205         }
206
207         $obj = new ActivityObject();
208
209         $obj->id      = $happening->getUri();
210         $obj->type    = Happening::OBJECT_TYPE;
211         $obj->title   = $happening->title;
212         $obj->summary = $happening->description;
213         $obj->link    = $happening->getStored()->getUrl();
214
215         $obj->extra[] = array('dtstart',
216                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
217                               common_date_iso8601($happening->start_time));
218         $obj->extra[] = array('dtend',
219                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
220                               common_date_iso8601($happening->end_time));
221         $obj->extra[] = array('location', false, $happening->location);
222         $obj->extra[] = array('url', false, $happening->url);
223
224         return $obj;
225     }
226
227     /**
228      * Change the verb on RSVP notices
229      *
230      * @param Notice $notice
231      *
232      * @return ActivityObject
233      */
234     protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
235         switch (true) {
236         // FIXME: Why are these object_type??
237         case ActivityUtils::compareTypes($stored->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
238             $act->verb = $stored->object_type;
239             break;
240         }
241         return true;
242     }
243
244     /**
245      * Form for our app
246      *
247      * @param HTMLOutputter $out
248      * @return Widget
249      */
250     function entryForm($out)
251     {
252         return new EventForm($out);
253     }
254
255     /**
256      * When a notice is deleted, clean up related tables.
257      *
258      * @param Notice $notice
259      */
260     function deleteRelated(Notice $notice)
261     {
262         switch ($notice->object_type) {
263         case Happening::OBJECT_TYPE:
264             common_log(LOG_DEBUG, "Deleting event from notice...");
265             try {
266                 $happening = Happening::fromStored($notice);
267                 $happening->delete();
268             } catch (NoResultException $e) {
269                 // already gone
270             }
271             break;
272         case RSVP::POSITIVE:
273         case RSVP::NEGATIVE:
274         case RSVP::POSSIBLE:
275             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
276             $rsvp = RSVP::fromNotice($notice);
277             common_log(LOG_DEBUG, "to delete: $rsvp->id");
278             $rsvp->delete();
279             break;
280         default:
281             common_log(LOG_DEBUG, "Not deleting related, wtf...");
282         }
283     }
284
285     function onEndShowScripts($action)
286     {
287         $action->script($this->path('js/event.js'));
288     }
289
290     function onEndShowStyles($action)
291     {
292         $action->cssLink($this->path('css/event.css'));
293         return true;
294     }
295
296     function onStartAddNoticeReply($nli, $parent, $child)
297     {
298         if (($parent->object_type == Happening::OBJECT_TYPE) &&
299             in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
300             return false;
301         }
302         return true;
303     }
304
305     protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
306     {
307         switch (true) {
308         case ActivityUtils::compareTypes($stored->verb, array(ActivityVerb::POST)) &&
309                 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
310             $this->showEvent($stored, $out, $scoped);
311             break;
312         case ActivityUtils::compareVerbs($stored->verb, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
313             $this->showRSVP($stored, $out, $scoped);
314             break;
315         default:
316             throw new ServerException('This is not an Event notice');
317         }
318         return true;
319     }
320
321     protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
322     {
323         common_debug('shownotice'.$stored->getID());
324         $profile = $stored->getProfile();
325         $event   = Happening::fromStored($stored);
326
327         $out->elementStart('div', 'h-event');
328
329         $out->elementStart('h3', 'p-summary p-name');
330
331         try {
332             $out->element('a', array('href' => $event->getUrl()), $event->title);
333         } catch (InvalidUrlException $e) {
334             $out->text($event->title);
335         }
336
337         $out->elementEnd('h3');
338
339         $now       = new DateTime();
340         $startDate = new DateTime($event->start_time);
341         $endDate   = new DateTime($event->end_time);
342         $userTz    = new DateTimeZone(common_timezone());
343
344         // Localize the time for the observer
345         $now->setTimeZone($userTz);
346         $startDate->setTimezone($userTz);
347         $endDate->setTimezone($userTz);
348
349         $thisYear  = $now->format('Y');
350         $startYear = $startDate->format('Y');
351         $endYear   = $endDate->format('Y');
352
353         $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
354
355         if ($startYear != $thisYear || $endYear != $thisYear) {
356             $dateFmt .= 'Y,'; // append year if we need to think about years
357         }
358
359         $startDateStr = $startDate->format($dateFmt);
360         $endDateStr = $endDate->format($dateFmt);
361
362         $timeFmt = 'g:ia';
363
364         $startTimeStr = $startDate->format($timeFmt);
365         $endTimeStr = $endDate->format("{$timeFmt} (T)");
366
367         $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
368
369         // TRANS: Field label for event description.
370         $out->element('strong', null, _m('Time:'));
371
372         $out->element('time', array('class' => 'dt-start',
373                                     'datetime' => common_date_iso8601($event->start_time)),
374                       $startDateStr . ' ' . $startTimeStr);
375         $out->text(' – ');
376         $out->element('time', array('class' => 'dt-end',
377                                     'datetime' => common_date_iso8601($event->end_time)),
378                       $startDateStr != $endDateStr
379                                     ? "$endDateStr $endTimeStr"
380                                     :  $endTimeStr);
381
382         $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
383
384         if (!empty($event->location)) {
385             $out->elementStart('div', 'event-location');
386             // TRANS: Field label for event description.
387             $out->element('strong', null, _m('Location:'));
388             $out->element('span', 'p-location', $event->location);
389             $out->elementEnd('div');
390         }
391
392         if (!empty($event->description)) {
393             $out->elementStart('div', 'event-description');
394             // TRANS: Field label for event description.
395             $out->element('strong', null, _m('Description:'));
396             $out->element('div', 'p-description', $event->description);
397             $out->elementEnd('div');
398         }
399
400         $rsvps = $event->getRSVPs();
401
402         $out->elementStart('div', 'event-rsvps');
403
404         // TRANS: Field label for event description.
405         $out->element('strong', null, _m('Attending:'));
406         $out->elementStart('ul', 'attending-list');
407
408         foreach ($rsvps as $verb => $responses) {
409             $out->elementStart('li', 'rsvp-list');
410             switch ($verb) {
411             case RSVP::POSITIVE:
412                 $out->text(_('Yes:'));
413                 break;
414             case RSVP::NEGATIVE:
415                 $out->text(_('No:'));
416                 break;
417             case RSVP::POSSIBLE:
418                 $out->text(_('Maybe:'));
419                 break;
420             }
421             $ids = array();
422             foreach ($responses as $response) {
423                 $ids[] = $response->profile_id;
424             }
425             $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
426             $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
427             $minilist->show();
428
429             $out->elementEnd('li');
430         }
431
432         $out->elementEnd('ul');
433         $out->elementEnd('div');
434
435         if ($scoped instanceof Profile) {
436             $rsvp = $event->getRSVP($scoped);
437
438             if (empty($rsvp)) {
439                 $form = new RSVPForm($event, $out);
440             } else {
441                 $form = new CancelRSVPForm($rsvp, $out);
442             }
443
444             $form->show();
445         }
446         $out->elementEnd('div');
447     }
448
449     protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
450     {
451         $rsvp = RSVP::fromNotice($stored);
452
453         if (empty($rsvp)) {
454             // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
455             $out->element('p', null, _m('Deleted.'));
456             return;
457         }
458
459         $out->elementStart('div', 'rsvp');
460         $out->raw($rsvp->asHTML());
461         $out->elementEnd('div');
462     }
463
464     function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
465     {
466         $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
467                           // TRANS: Menu item in sample plugin.
468                           _m('Happenings'),
469                           // TRANS: Menu item title in sample plugin.
470                           _m('A list of your events'), false, 'nav_timeline_events');
471         return true;
472     }
473 }