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 * Set up our tables (event and rsvp)
55 * @return boolean hook value; true means continue processing, false means stop.
57 function onCheckSchema()
59 $schema = Schema::get();
61 $schema->ensureTable('happening', Happening::schemaDef());
62 $schema->ensureTable('rsvp', RSVP::schemaDef());
68 * Load related modules when needed
70 * @param string $cls Name of the class to be loaded
72 * @return boolean hook value; true means continue processing, false means stop.
74 function onAutoload($cls)
76 $dir = dirname(__FILE__);
80 case 'NeweventAction':
82 case 'CancelrsvpAction':
83 case 'ShoweventAction':
84 case 'ShowrsvpAction':
85 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
89 case 'CancelRSVPForm':
90 include_once $dir . '/'.strtolower($cls).'.php';
94 include_once $dir . '/'.$cls.'.php';
102 * Map URLs to actions
104 * @param Net_URL_Mapper $m path-to-action mapper
106 * @return boolean hook value; true means continue processing, false means stop.
108 function onRouterInitialized($m)
110 $m->connect('main/event/new',
111 array('action' => 'newevent'));
112 $m->connect('main/event/rsvp',
113 array('action' => 'newrsvp'));
114 $m->connect('main/event/rsvp/cancel',
115 array('action' => 'cancelrsvp'));
116 $m->connect('event/:id',
117 array('action' => 'showevent'),
118 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
119 $m->connect('rsvp/:id',
120 array('action' => 'showrsvp'),
121 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
125 function onPluginVersion(&$versions)
127 $versions[] = array('name' => 'Event',
128 'version' => STATUSNET_VERSION,
129 'author' => 'Evan Prodromou',
130 'homepage' => 'http://status.net/wiki/Plugin:Event',
132 // TRANS: Plugin description.
133 _m('Event invitations and RSVPs.'));
137 function appTitle() {
138 // TRANS: Title for event application.
139 return _m('TITLE','Event');
147 return array(Happening::OBJECT_TYPE,
154 * Given a parsed ActivityStreams activity, save it into a notice
155 * and other data structures.
157 * @param Activity $activity
158 * @param Profile $actor
159 * @param array $options=array()
161 * @return Notice the resulting notice
163 function saveNoticeFromActivity($activity, $actor, $options=array())
165 if (count($activity->objects) != 1) {
166 throw new Exception(_('Too many activity objects.'));
169 $happeningObj = $activity->objects[0];
171 if ($happeningObj->type != Happening::OBJECT_TYPE) {
172 // TRANS: Exception thrown when event plugin comes across a non-event type object.
173 throw new Exception(_m('Wrong type for object.'));
178 switch ($activity->verb) {
179 case ActivityVerb::POST:
180 $notice = Happening::saveNew($actor,
183 $happeningObj->title,
185 $happeningObj->summary,
191 $happening = Happening::staticGet('uri', $happeningObj->id);
192 if (empty($happening)) {
193 // FIXME: save the event
194 // TRANS: Exception thrown when trying to RSVP for an unknown event.
195 throw new Exception(_m('RSVP for unknown event.'));
197 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
200 // TRANS: Exception thrown when event plugin comes across a undefined verb.
201 throw new Exception(_m('Unknown verb for events.'));
208 * Turn a Notice into an activity object
210 * @param Notice $notice
212 * @return ActivityObject
214 function activityObjectFromNotice($notice)
218 switch ($notice->object_type) {
219 case Happening::OBJECT_TYPE:
220 $happening = Happening::fromNotice($notice);
225 $rsvp = RSVP::fromNotice($notice);
226 $happening = $rsvp->getEvent();
230 if (empty($happening)) {
231 // TRANS: Exception thrown when event plugin comes across a unknown object type.
232 throw new Exception(_m('Unknown object type.'));
235 $notice = $happening->getNotice();
237 if (empty($notice)) {
238 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
239 throw new Exception(_m('Unknown event notice.'));
242 $obj = new ActivityObject();
244 $obj->id = $happening->uri;
245 $obj->type = Happening::OBJECT_TYPE;
246 $obj->title = $happening->title;
247 $obj->summary = $happening->description;
248 $obj->link = $notice->bestUrl();
250 // XXX: how to get this stuff into JSON?!
252 $obj->extra[] = array('dtstart',
253 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
254 common_date_iso8601($happening->start_time));
256 $obj->extra[] = array('dtend',
257 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
258 common_date_iso8601($happening->end_time));
260 // XXX: probably need other stuff here
266 * Change the verb on RSVP notices
268 * @param Notice $notice
270 * @return ActivityObject
272 function onEndNoticeAsActivity($notice, &$act) {
273 switch ($notice->object_type) {
277 $act->verb = $notice->object_type;
284 * Custom HTML output for our notices
286 * @param Notice $notice
287 * @param HTMLOutputter $out
289 function showNotice($notice, $out)
291 switch ($notice->object_type) {
292 case Happening::OBJECT_TYPE:
293 $this->showEventNotice($notice, $out);
298 $this->showRSVPNotice($notice, $out);
302 // @fixme we have to start the name/avatar and open this div
303 $out->elementStart('div', array('class' => 'event-info entry-content')); // EVENT-INFO.ENTRY-CONTENT IN
305 $profile = $notice->getProfile();
306 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
309 array('src' => ($avatar) ?
310 $avatar->displayUrl() :
311 Avatar::defaultImage(AVATAR_MINI_SIZE),
312 'class' => 'avatar photo bookmark-avatar',
313 'width' => AVATAR_MINI_SIZE,
314 'height' => AVATAR_MINI_SIZE,
315 'alt' => $profile->getBestName()));
317 $out->raw(' '); // avoid for AJAX XML compatibility
319 $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
321 array('class' => 'url',
322 'href' => $profile->profileurl,
323 'title' => $profile->getBestName()),
325 $out->elementEnd('span');
328 function showRSVPNotice($notice, $out)
330 $rsvp = RSVP::fromNotice($notice);
333 $out->element('p', null, _('Deleted.'));
337 $out->elementStart('div', 'rsvp');
338 $out->raw($rsvp->asHTML());
339 $out->elementEnd('div');
343 function showEventNotice($notice, $out)
345 $profile = $notice->getProfile();
346 $event = Happening::fromNotice($notice);
349 $out->element('p', null, _('Deleted.'));
353 $out->elementStart('div', 'vevent event'); // VEVENT IN
355 $out->elementStart('h3'); // VEVENT/H3 IN
357 if (!empty($event->url)) {
359 array('href' => $event->url,
360 'class' => 'event-title entry-title summary'),
363 $out->text($event->title);
366 $out->elementEnd('h3'); // VEVENT/H3 OUT
368 $startDate = strftime("%x", strtotime($event->start_time));
369 $startTime = strftime("%R", strtotime($event->start_time));
371 $endDate = strftime("%x", strtotime($event->end_time));
372 $endTime = strftime("%R", strtotime($event->end_time));
374 // FIXME: better dates
376 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
378 // TRANS: Field label for event description.
379 $out->element('strong', null, _m('Time:'));
381 $out->element('abbr', array('class' => 'dtstart',
382 'title' => common_date_iso8601($event->start_time)),
383 $startDate . ' ' . $startTime);
385 if ($startDate == $endDate) {
386 $out->element('span', array('class' => 'dtend',
387 'title' => common_date_iso8601($event->end_time)),
390 $out->element('span', array('class' => 'dtend',
391 'title' => common_date_iso8601($event->end_time)),
392 $endDate . ' ' . $endTime);
395 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
397 if (!empty($event->location)) {
398 $out->elementStart('div', 'event-location');
399 // TRANS: Field label for event description.
400 $out->element('strong', null, _m('Location:'));
401 $out->element('span', 'location', $event->location);
402 $out->elementEnd('div');
405 if (!empty($event->description)) {
406 $out->elementStart('div', 'event-description');
407 // TRANS: Field label for event description.
408 $out->element('strong', null, _m('Description:'));
409 $out->element('span', 'description', $event->description);
410 $out->elementEnd('div');
413 $rsvps = $event->getRSVPs();
415 $out->elementStart('div', 'event-rsvps');
416 // TRANS: Field label for event description.
417 $out->element('strong', null, _m('Attending:'));
418 $out->element('span', 'event-rsvps',
419 // TRANS: RSVP counts.
420 // TRANS: %1$d, %2$d and %3$d are numbers of RSVPs.
421 sprintf(_m('Yes: %1$d No: %2$d Maybe: %3$d'),
422 count($rsvps[RSVP::POSITIVE]),
423 count($rsvps[RSVP::NEGATIVE]),
424 count($rsvps[RSVP::POSSIBLE])));
425 $out->elementEnd('div');
427 $user = common_current_user();
430 $rsvp = $event->getRSVP($user->getProfile());
433 $form = new RSVPForm($event, $out);
435 $form = new CancelRSVPForm($rsvp, $out);
441 $out->elementEnd('div'); // vevent out
447 * @param HTMLOutputter $out
450 function entryForm($out)
452 return new EventForm($out);
456 * When a notice is deleted, clean up related tables.
458 * @param Notice $notice
460 function deleteRelated($notice)
462 switch ($notice->object_type) {
463 case Happening::OBJECT_TYPE:
464 common_log(LOG_DEBUG, "Deleting event from notice...");
465 $happening = Happening::fromNotice($notice);
466 $happening->delete();
471 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
472 $rsvp = RSVP::fromNotice($notice);
473 common_log(LOG_DEBUG, "to delete: $rsvp->id");
477 common_log(LOG_DEBUG, "Not deleting related, wtf...");
481 function onEndShowScripts($action)
483 $action->inlineScript('$(document).ready(function() { $("#event-startdate").datepicker(); $("#event-enddate").datepicker(); });');
486 function onEndShowStyles($action)
488 $action->cssLink($this->path('event.css'));
492 function onStartShowThreadedNoticeTail($nli, $notice, &$children)
494 // Filter out any poll responses
495 if ($notice->object_type == Happening::OBJECT_TYPE) {
496 $children = array_filter($children, array($this, 'isNotRSVP'));
501 function isNotRSVP($notice)
503 return (!in_array($notice->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)));