3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Microapp plugin for event invitations and RSVPs
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
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/
47 class EventPlugin extends MicroAppPlugin
50 var $oldSaveNew = true;
53 * Set up our tables (event and rsvp)
58 * @return boolean hook value; true means continue processing, false means stop.
60 function onCheckSchema()
62 $schema = Schema::get();
64 $schema->ensureTable('happening', Happening::schemaDef());
65 $schema->ensureTable('rsvp', RSVP::schemaDef());
70 public function onBeforePluginCheckSchema()
72 RSVP::beforeSchemaUpdate();
79 * @param URLMapper $m path-to-action mapper
81 * @return boolean hook value; true means continue processing, false means stop.
83 public function onRouterInitialized(URLMapper $m)
85 $m->connect('main/event/new',
86 array('action' => 'newevent'));
87 $m->connect('main/event/rsvp',
88 array('action' => 'newrsvp'));
89 $m->connect('main/event/rsvp/cancel',
90 array('action' => 'cancelrsvp'));
91 $m->connect('event/:id',
92 array('action' => 'showevent'),
93 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
94 $m->connect('rsvp/:id',
95 array('action' => 'showrsvp'),
96 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
97 $m->connect('main/event/updatetimes',
98 array('action' => 'timelist'));
100 $m->connect(':nickname/events',
101 array('action' => 'events'),
102 array('nickname' => Nickname::DISPLAY_FMT));
106 function onPluginVersion(array &$versions)
108 $versions[] = array('name' => 'Event',
109 'version' => GNUSOCIAL_VERSION,
110 'author' => 'Evan Prodromou',
111 'homepage' => 'http://status.net/wiki/Plugin:Event',
113 // TRANS: Plugin description.
114 _m('Event invitations and RSVPs.'));
118 function appTitle() {
119 // TRANS: Title for event application.
120 return _m('TITLE','Event');
128 return array(Happening::OBJECT_TYPE,
135 * Given a parsed ActivityStreams activity, save it into a notice
136 * and other data structures.
138 * @param Activity $activity
139 * @param Profile $actor
140 * @param array $options=array()
142 * @return Notice the resulting notice
144 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
146 if (count($activity->objects) != 1) {
147 // TRANS: Exception thrown when there are too many activity objects.
148 throw new Exception(_m('Too many activity objects.'));
151 $happeningObj = $activity->objects[0];
153 if ($happeningObj->type != Happening::OBJECT_TYPE) {
154 // TRANS: Exception thrown when event plugin comes across a non-event type object.
155 throw new Exception(_m('Wrong type for object.'));
158 $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
159 if($dtstart->length == 0) {
160 // TRANS: Exception thrown when has no start date
161 throw new Exception(_m('No start date for event.'));
164 $dtend = $happeningObj->element->getElementsByTagName('dtend');
165 if($dtend->length == 0) {
166 // TRANS: Exception thrown when has no end date
167 throw new Exception(_m('No end date for event.'));
170 // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
171 $start_time = new DateTime($dtstart->item(0)->nodeValue);
172 $start_time->setTimezone(new DateTimeZone('UTC'));
173 $start_time = $start_time->format('Y-m-d H:i:s');
174 $end_time = new DateTime($dtend->item(0)->nodeValue);
175 $end_time->setTimezone(new DateTimeZone('UTC'));
176 $end_time = $end_time->format('Y-m-d H:i:s');
178 // location is optional
180 $location_object = $happeningObj->element->getElementsByTagName('location');
181 if($location_object->length > 0) {
182 $location = $location_object->item(0)->nodeValue;
187 $url_object = $happeningObj->element->getElementsByTagName('url');
188 if($url_object->length > 0) {
189 $url = $url_object->item(0)->nodeValue;
194 switch ($activity->verb) {
195 case ActivityVerb::POST:
196 // FIXME: get startTime, endTime, location and URL
197 $notice = Happening::saveNew($actor,
200 $happeningObj->title,
202 $happeningObj->summary,
209 $happening = Happening::getKV('uri', $happeningObj->id);
210 if (empty($happening)) {
211 // FIXME: save the event
212 // TRANS: Exception thrown when trying to RSVP for an unknown event.
213 throw new Exception(_m('RSVP for unknown event.'));
215 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
218 // TRANS: Exception thrown when event plugin comes across a undefined verb.
219 throw new Exception(_m('Unknown verb for events.'));
226 * Turn a Notice into an activity object
228 * @param Notice $notice
230 * @return ActivityObject
232 function activityObjectFromNotice(Notice $notice)
236 switch ($notice->object_type) {
237 case Happening::OBJECT_TYPE:
238 $happening = Happening::fromNotice($notice);
243 $rsvp = RSVP::fromNotice($notice);
244 $happening = $rsvp->getEvent();
248 if (empty($happening)) {
249 // TRANS: Exception thrown when event plugin comes across a unknown object type.
250 throw new Exception(_m('Unknown object type.'));
253 $notice = $happening->getNotice();
255 if (empty($notice)) {
256 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
257 throw new Exception(_m('Unknown event notice.'));
260 $obj = new ActivityObject();
262 $obj->id = $happening->uri;
263 $obj->type = Happening::OBJECT_TYPE;
264 $obj->title = $happening->title;
265 $obj->summary = $happening->description;
266 $obj->link = $notice->getUrl();
268 // XXX: how to get this stuff into JSON?!
270 $obj->extra[] = array('dtstart',
271 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
272 common_date_iso8601($happening->start_time));
274 $obj->extra[] = array('dtend',
275 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
276 common_date_iso8601($happening->end_time));
278 $obj->extra[] = array('location', false, $happening->location);
279 $obj->extra[] = array('url', false, $happening->url);
281 // XXX: probably need other stuff here
287 * Change the verb on RSVP notices
289 * @param Notice $notice
291 * @return ActivityObject
293 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
294 switch ($stored->object_type) {
298 $act->verb = $stored->object_type;
307 * @param HTMLOutputter $out
310 function entryForm($out)
312 return new EventForm($out);
316 * When a notice is deleted, clean up related tables.
318 * @param Notice $notice
320 function deleteRelated(Notice $notice)
322 switch ($notice->object_type) {
323 case Happening::OBJECT_TYPE:
324 common_log(LOG_DEBUG, "Deleting event from notice...");
325 $happening = Happening::fromNotice($notice);
326 $happening->delete();
331 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
332 $rsvp = RSVP::fromNotice($notice);
333 common_log(LOG_DEBUG, "to delete: $rsvp->id");
337 common_log(LOG_DEBUG, "Not deleting related, wtf...");
341 function onEndShowScripts($action)
343 $action->script($this->path('js/event.js'));
346 function onEndShowStyles($action)
348 $action->cssLink($this->path('css/event.css'));
352 function onStartAddNoticeReply($nli, $parent, $child)
354 // Filter out any poll responses
355 if (($parent->object_type == Happening::OBJECT_TYPE) &&
356 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
362 protected function showNoticeItemNotice(NoticeListItem $nli)
368 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
370 switch ($stored->object_type) {
371 case Happening::OBJECT_TYPE:
372 $this->showEvent($stored, $out, $scoped);
377 $this->showRSVP($stored, $out, $scoped);
382 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
384 $profile = $stored->getProfile();
385 $event = Happening::fromNotice($stored);
387 if (!$event instanceof Happening) {
388 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
389 $out->element('p', null, _m('Deleted.'));
393 $out->elementStart('div', 'h-event');
395 $out->elementStart('h3', 'p-summary p-name');
398 $out->element('a', array('href' => $event->getUrl()), $event->title);
399 } catch (InvalidUrlException $e) {
400 $out->text($event->title);
403 $out->elementEnd('h3');
405 $now = new DateTime();
406 $startDate = new DateTime($event->start_time);
407 $endDate = new DateTime($event->end_time);
408 $userTz = new DateTimeZone(common_timezone());
410 // Localize the time for the observer
411 $now->setTimeZone($userTz);
412 $startDate->setTimezone($userTz);
413 $endDate->setTimezone($userTz);
415 $thisYear = $now->format('Y');
416 $startYear = $startDate->format('Y');
417 $endYear = $endDate->format('Y');
419 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
421 if ($startYear != $thisYear || $endYear != $thisYear) {
422 $dateFmt .= 'Y,'; // append year if we need to think about years
425 $startDateStr = $startDate->format($dateFmt);
426 $endDateStr = $endDate->format($dateFmt);
430 $startTimeStr = $startDate->format($timeFmt);
431 $endTimeStr = $endDate->format("{$timeFmt} (T)");
433 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
435 // TRANS: Field label for event description.
436 $out->element('strong', null, _m('Time:'));
438 $out->element('time', array('class' => 'dt-start',
439 'datetime' => common_date_iso8601($event->start_time)),
440 $startDateStr . ' ' . $startTimeStr);
442 $out->element('time', array('class' => 'dt-end',
443 'datetime' => common_date_iso8601($event->end_time)),
444 $startDateStr != $endDateStr
445 ? "$endDateStr $endTimeStr"
448 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
450 if (!empty($event->location)) {
451 $out->elementStart('div', 'event-location');
452 // TRANS: Field label for event description.
453 $out->element('strong', null, _m('Location:'));
454 $out->element('span', 'p-location', $event->location);
455 $out->elementEnd('div');
458 if (!empty($event->description)) {
459 $out->elementStart('div', 'event-description');
460 // TRANS: Field label for event description.
461 $out->element('strong', null, _m('Description:'));
462 $out->element('div', 'p-description', $event->description);
463 $out->elementEnd('div');
466 $rsvps = $event->getRSVPs();
468 $out->elementStart('div', 'event-rsvps');
470 // TRANS: Field label for event description.
471 $out->element('strong', null, _m('Attending:'));
472 $out->elementStart('ul', 'attending-list');
474 foreach ($rsvps as $verb => $responses) {
475 $out->elementStart('li', 'rsvp-list');
478 $out->text(_('Yes:'));
481 $out->text(_('No:'));
484 $out->text(_('Maybe:'));
488 foreach ($responses as $response) {
489 $ids[] = $response->profile_id;
491 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
492 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
495 $out->elementEnd('li');
498 $out->elementEnd('ul');
499 $out->elementEnd('div');
501 if ($scoped instanceof Profile) {
502 $rsvp = $event->getRSVP($scoped);
505 $form = new RSVPForm($event, $out);
507 $form = new CancelRSVPForm($rsvp, $out);
512 $out->elementEnd('div');
515 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
517 $rsvp = RSVP::fromNotice($stored);
520 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
521 $out->element('p', null, _m('Deleted.'));
525 $out->elementStart('div', 'rsvp');
526 $out->raw($rsvp->asHTML());
527 $out->elementEnd('div');
530 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
532 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
533 // TRANS: Menu item in sample plugin.
535 // TRANS: Menu item title in sample plugin.
536 _m('A list of your events'), false, 'nav_timeline_events');