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());
73 * @param URLMapper $m path-to-action mapper
75 * @return boolean hook value; true means continue processing, false means stop.
77 public function onRouterInitialized(URLMapper $m)
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'));
94 $m->connect(':nickname/events',
95 array('action' => 'events'),
96 array('nickname' => Nickname::DISPLAY_FMT));
100 function onPluginVersion(array &$versions)
102 $versions[] = array('name' => 'Event',
103 'version' => GNUSOCIAL_VERSION,
104 'author' => 'Evan Prodromou',
105 'homepage' => 'http://status.net/wiki/Plugin:Event',
107 // TRANS: Plugin description.
108 _m('Event invitations and RSVPs.'));
112 function appTitle() {
113 // TRANS: Title for event application.
114 return _m('TITLE','Event');
122 return array(Happening::OBJECT_TYPE,
129 * Given a parsed ActivityStreams activity, save it into a notice
130 * and other data structures.
132 * @param Activity $activity
133 * @param Profile $actor
134 * @param array $options=array()
136 * @return Notice the resulting notice
138 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
140 if (count($activity->objects) != 1) {
141 // TRANS: Exception thrown when there are too many activity objects.
142 throw new Exception(_m('Too many activity objects.'));
145 $happeningObj = $activity->objects[0];
147 if ($happeningObj->type != Happening::OBJECT_TYPE) {
148 // TRANS: Exception thrown when event plugin comes across a non-event type object.
149 throw new Exception(_m('Wrong type for object.'));
152 $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
153 if($dtstart->length == 0) {
154 // TRANS: Exception thrown when has no start date
155 throw new Exception(_m('No start date for event.'));
158 $dtend = $happeningObj->element->getElementsByTagName('dtend');
159 if($dtend->length == 0) {
160 // TRANS: Exception thrown when has no end date
161 throw new Exception(_m('No end date for event.'));
164 // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
165 $start_time = new DateTime($dtstart->item(0)->nodeValue);
166 $start_time->setTimezone(new DateTimeZone('UTC'));
167 $start_time = $start_time->format('Y-m-d H:i:s');
168 $end_time = new DateTime($dtend->item(0)->nodeValue);
169 $end_time->setTimezone(new DateTimeZone('UTC'));
170 $end_time = $end_time->format('Y-m-d H:i:s');
172 // location is optional
174 $location_object = $happeningObj->element->getElementsByTagName('location');
175 if($location_object->length > 0) {
176 $location = $location_object->item(0)->nodeValue;
181 $url_object = $happeningObj->element->getElementsByTagName('url');
182 if($url_object->length > 0) {
183 $url = $url_object->item(0)->nodeValue;
188 switch ($activity->verb) {
189 case ActivityVerb::POST:
190 // FIXME: get startTime, endTime, location and URL
191 $notice = Happening::saveNew($actor,
194 $happeningObj->title,
196 $happeningObj->summary,
203 $happening = Happening::getKV('uri', $happeningObj->id);
204 if (empty($happening)) {
205 // FIXME: save the event
206 // TRANS: Exception thrown when trying to RSVP for an unknown event.
207 throw new Exception(_m('RSVP for unknown event.'));
209 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
212 // TRANS: Exception thrown when event plugin comes across a undefined verb.
213 throw new Exception(_m('Unknown verb for events.'));
220 * Turn a Notice into an activity object
222 * @param Notice $notice
224 * @return ActivityObject
226 function activityObjectFromNotice(Notice $notice)
230 switch ($notice->object_type) {
231 case Happening::OBJECT_TYPE:
232 $happening = Happening::fromNotice($notice);
237 $rsvp = RSVP::fromNotice($notice);
238 $happening = $rsvp->getEvent();
242 if (empty($happening)) {
243 // TRANS: Exception thrown when event plugin comes across a unknown object type.
244 throw new Exception(_m('Unknown object type.'));
247 $notice = $happening->getNotice();
249 if (empty($notice)) {
250 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
251 throw new Exception(_m('Unknown event notice.'));
254 $obj = new ActivityObject();
256 $obj->id = $happening->uri;
257 $obj->type = Happening::OBJECT_TYPE;
258 $obj->title = $happening->title;
259 $obj->summary = $happening->description;
260 $obj->link = $notice->getUrl();
262 // XXX: how to get this stuff into JSON?!
264 $obj->extra[] = array('dtstart',
265 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
266 common_date_iso8601($happening->start_time));
268 $obj->extra[] = array('dtend',
269 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
270 common_date_iso8601($happening->end_time));
272 $obj->extra[] = array('location', false, $happening->location);
273 $obj->extra[] = array('url', false, $happening->url);
275 // XXX: probably need other stuff here
281 * Change the verb on RSVP notices
283 * @param Notice $notice
285 * @return ActivityObject
287 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
288 switch ($stored->object_type) {
292 $act->verb = $stored->object_type;
301 * @param HTMLOutputter $out
304 function entryForm($out)
306 return new EventForm($out);
310 * When a notice is deleted, clean up related tables.
312 * @param Notice $notice
314 function deleteRelated(Notice $notice)
316 switch ($notice->object_type) {
317 case Happening::OBJECT_TYPE:
318 common_log(LOG_DEBUG, "Deleting event from notice...");
319 $happening = Happening::fromNotice($notice);
320 $happening->delete();
325 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
326 $rsvp = RSVP::fromNotice($notice);
327 common_log(LOG_DEBUG, "to delete: $rsvp->id");
331 common_log(LOG_DEBUG, "Not deleting related, wtf...");
335 function onEndShowScripts($action)
337 $action->script($this->path('js/event.js'));
340 function onEndShowStyles($action)
342 $action->cssLink($this->path('css/event.css'));
346 function onStartAddNoticeReply($nli, $parent, $child)
348 // Filter out any poll responses
349 if (($parent->object_type == Happening::OBJECT_TYPE) &&
350 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
356 protected function showNoticeItemNotice(NoticeListItem $nli)
362 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
364 switch ($stored->object_type) {
365 case Happening::OBJECT_TYPE:
366 $this->showEvent($stored, $out, $scoped);
371 $this->showRSVP($stored, $out, $scoped);
376 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
378 $profile = $stored->getProfile();
379 $event = Happening::fromNotice($stored);
381 if (!$event instanceof Happening) {
382 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
383 $out->element('p', null, _m('Deleted.'));
387 $out->elementStart('div', 'h-event');
389 $out->elementStart('h3', 'p-summary p-name');
392 $out->element('a', array('href' => $event->getUrl()), $event->title);
393 } catch (InvalidUrlException $e) {
394 $out->text($event->title);
397 $out->elementEnd('h3');
399 $now = new DateTime();
400 $startDate = new DateTime($event->start_time);
401 $endDate = new DateTime($event->end_time);
402 $userTz = new DateTimeZone(common_timezone());
404 // Localize the time for the observer
405 $now->setTimeZone($userTz);
406 $startDate->setTimezone($userTz);
407 $endDate->setTimezone($userTz);
409 $thisYear = $now->format('Y');
410 $startYear = $startDate->format('Y');
411 $endYear = $endDate->format('Y');
413 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
415 if ($startYear != $thisYear || $endYear != $thisYear) {
416 $dateFmt .= 'Y,'; // append year if we need to think about years
419 $startDateStr = $startDate->format($dateFmt);
420 $endDateStr = $endDate->format($dateFmt);
424 $startTimeStr = $startDate->format($timeFmt);
425 $endTimeStr = $endDate->format("{$timeFmt} (T)");
427 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
429 // TRANS: Field label for event description.
430 $out->element('strong', null, _m('Time:'));
432 $out->element('time', array('class' => 'dt-start',
433 'datetime' => common_date_iso8601($event->start_time)),
434 $startDateStr . ' ' . $startTimeStr);
436 $out->element('time', array('class' => 'dt-end',
437 'datetime' => common_date_iso8601($event->end_time)),
438 $startDateStr != $endDateStr
439 ? "$endDateStr $endTimeStr"
442 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
444 if (!empty($event->location)) {
445 $out->elementStart('div', 'event-location');
446 // TRANS: Field label for event description.
447 $out->element('strong', null, _m('Location:'));
448 $out->element('span', 'p-location', $event->location);
449 $out->elementEnd('div');
452 if (!empty($event->description)) {
453 $out->elementStart('div', 'event-description');
454 // TRANS: Field label for event description.
455 $out->element('strong', null, _m('Description:'));
456 $out->element('div', 'p-description', $event->description);
457 $out->elementEnd('div');
460 $rsvps = $event->getRSVPs();
462 $out->elementStart('div', 'event-rsvps');
464 // TRANS: Field label for event description.
465 $out->element('strong', null, _m('Attending:'));
466 $out->elementStart('ul', 'attending-list');
468 foreach ($rsvps as $verb => $responses) {
469 $out->elementStart('li', 'rsvp-list');
472 $out->text(_('Yes:'));
475 $out->text(_('No:'));
478 $out->text(_('Maybe:'));
482 foreach ($responses as $response) {
483 $ids[] = $response->profile_id;
485 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
486 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
489 $out->elementEnd('li');
492 $out->elementEnd('ul');
493 $out->elementEnd('div');
495 if ($scoped instanceof Profile) {
496 $rsvp = $event->getRSVP($scoped);
499 $form = new RSVPForm($event, $out);
501 $form = new CancelRSVPForm($rsvp, $out);
506 $out->elementEnd('div');
509 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
511 $rsvp = RSVP::fromNotice($stored);
514 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
515 $out->element('p', null, _m('Deleted.'));
519 $out->elementStart('div', 'rsvp');
520 $out->raw($rsvp->asHTML());
521 $out->elementEnd('div');
524 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
526 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
527 // TRANS: Menu item in sample plugin.
529 // TRANS: Menu item title in sample plugin.
530 _m('A list of your events'), false, 'nav_timeline_events');