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.
109 function onRouterInitialized($m)
111 $m->connect('main/event/new',
112 array('action' => 'newevent'));
113 $m->connect('main/event/rsvp',
114 array('action' => 'newrsvp'));
115 $m->connect('main/event/rsvp/cancel',
116 array('action' => 'cancelrsvp'));
117 $m->connect('event/:id',
118 array('action' => 'showevent'),
119 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
120 $m->connect('rsvp/:id',
121 array('action' => 'showrsvp'),
122 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
126 function onPluginVersion(&$versions)
128 $versions[] = array('name' => 'Event',
129 'version' => STATUSNET_VERSION,
130 'author' => 'Evan Prodromou',
131 'homepage' => 'http://status.net/wiki/Plugin:Event',
133 _m('Event invitations and RSVPs.'));
137 function appTitle() {
146 return array(Happening::OBJECT_TYPE,
153 * Given a parsed ActivityStreams activity, save it into a notice
154 * and other data structures.
156 * @param Activity $activity
157 * @param Profile $actor
158 * @param array $options=array()
160 * @return Notice the resulting notice
162 function saveNoticeFromActivity($activity, $actor, $options=array())
164 if (count($activity->objects) != 1) {
165 throw new Exception('Too many activity objects.');
168 $happeningObj = $activity->objects[0];
170 if ($happeningObj->type != Happening::OBJECT_TYPE) {
171 throw new Exception(_m('Wrong type for object.'));
176 switch ($activity->verb) {
177 case ActivityVerb::POST:
178 $notice = Happening::saveNew($actor,
181 $happeningObj->title,
183 $happeningObj->summary,
189 $happening = Happening::staticGet('uri', $happeningObj->id);
190 if (empty($happening)) {
191 // FIXME: save the event
192 throw new Exception(_m('RSVP for unknown event.'));
194 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
197 throw new Exception(_m('Unknown verb for events'));
204 * Turn a Notice into an activity object
206 * @param Notice $notice
208 * @return ActivityObject
211 function activityObjectFromNotice($notice)
215 switch ($notice->object_type) {
216 case Happening::OBJECT_TYPE:
217 $happening = Happening::fromNotice($notice);
222 $rsvp = RSVP::fromNotice($notice);
223 $happening = $rsvp->getEvent();
227 if (empty($happening)) {
228 throw new Exception(_m('Unknown object type.'));
231 $notice = $happening->getNotice();
233 if (empty($notice)) {
234 throw new Exception(_m('Unknown event notice.'));
237 $obj = new ActivityObject();
239 $obj->id = $happening->uri;
240 $obj->type = Happening::OBJECT_TYPE;
241 $obj->title = $happening->title;
242 $obj->summary = $happening->description;
243 $obj->link = $notice->bestUrl();
245 // XXX: how to get this stuff into JSON?!
247 $obj->extra[] = array('dtstart',
248 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
249 common_date_iso8601($happening->start_time));
251 $obj->extra[] = array('dtend',
252 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
253 common_date_iso8601($happening->end_time));
255 // XXX: probably need other stuff here
261 * Change the verb on RSVP notices
263 * @param Notice $notice
265 * @return ActivityObject
268 function onEndNoticeAsActivity($notice, &$act) {
269 switch ($notice->object_type) {
273 $act->verb = $notice->object_type;
280 * Custom HTML output for our notices
282 * @param Notice $notice
283 * @param HTMLOutputter $out
286 function showNotice($notice, $out)
288 switch ($notice->object_type) {
289 case Happening::OBJECT_TYPE:
290 $this->showEventNotice($notice, $out);
295 $this->showRSVPNotice($notice, $out);
299 // @fixme we have to start the name/avatar and open this div
300 $out->elementStart('div', array('class' => 'event-info entry-content')); // EVENT-INFO.ENTRY-CONTENT IN
302 $profile = $notice->getProfile();
303 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
306 array('src' => ($avatar) ?
307 $avatar->displayUrl() :
308 Avatar::defaultImage(AVATAR_MINI_SIZE),
309 'class' => 'avatar photo bookmark-avatar',
310 'width' => AVATAR_MINI_SIZE,
311 'height' => AVATAR_MINI_SIZE,
312 'alt' => $profile->getBestName()));
314 $out->raw(' '); // avoid for AJAX XML compatibility
316 $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
318 array('class' => 'url',
319 'href' => $profile->profileurl,
320 'title' => $profile->getBestName()),
322 $out->elementEnd('span');
325 function showRSVPNotice($notice, $out)
327 $rsvp = RSVP::fromNotice($notice);
330 $out->element('p', null, _('Deleted.'));
333 $out->elementStart('div', 'rsvp');
334 $out->raw($rsvp->asHTML());
335 $out->elementEnd('div');
339 function showEventNotice($notice, $out)
341 $profile = $notice->getProfile();
342 $event = Happening::fromNotice($notice);
345 $out->element('p', null, _('Deleted.'));
348 $out->elementStart('div', 'vevent event'); // VEVENT IN
350 $out->elementStart('h3'); // VEVENT/H3 IN
352 if (!empty($event->url)) {
354 array('href' => $event->url,
355 'class' => 'event-title entry-title summary'),
358 $out->text($event->title);
361 $out->elementEnd('h3'); // VEVENT/H3 OUT
363 $startDate = strftime("%x", strtotime($event->start_time));
364 $startTime = strftime("%R", strtotime($event->start_time));
366 $endDate = strftime("%x", strtotime($event->end_time));
367 $endTime = strftime("%R", strtotime($event->end_time));
369 // FIXME: better dates
371 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
373 $out->element('strong', null, _m('Time:'));
375 $out->element('abbr', array('class' => 'dtstart',
376 'title' => common_date_iso8601($event->start_time)),
377 $startDate . ' ' . $startTime);
379 if ($startDate == $endDate) {
380 $out->element('span', array('class' => 'dtend',
381 'title' => common_date_iso8601($event->end_time)),
384 $out->element('span', array('class' => 'dtend',
385 'title' => common_date_iso8601($event->end_time)),
386 $endDate . ' ' . $endTime);
389 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
391 if (!empty($event->location)) {
392 $out->elementStart('div', 'event-location');
393 $out->element('strong', null, _m('Location:'));
394 $out->element('span', 'location', $event->location);
395 $out->elementEnd('div');
398 if (!empty($event->description)) {
399 $out->elementStart('div', 'event-description');
400 $out->element('strong', null, _m('Description:'));
401 $out->element('span', 'description', $event->description);
402 $out->elementEnd('div');
405 $rsvps = $event->getRSVPs();
407 $out->elementStart('div', 'event-rsvps');
408 $out->element('strong', null, _m('Attending:'));
409 $out->element('span', 'event-rsvps',
410 // TRANS: RSVP counts.
411 // TRANS: %1$d, %2$d and %3$d are numbers of RSVPs.
412 sprintf(_m('Yes: %1$d No: %2$d Maybe: %3$d'),
413 count($rsvps[RSVP::POSITIVE]),
414 count($rsvps[RSVP::NEGATIVE]),
415 count($rsvps[RSVP::POSSIBLE])));
416 $out->elementEnd('div');
418 $user = common_current_user();
421 $rsvp = $event->getRSVP($user->getProfile());
424 $form = new RSVPForm($event, $out);
426 $form = new CancelRSVPForm($rsvp, $out);
432 $out->elementEnd('div'); // vevent out
438 * @param HTMLOutputter $out
442 function entryForm($out)
444 return new EventForm($out);
448 * When a notice is deleted, clean up related tables.
450 * @param Notice $notice
453 function deleteRelated($notice)
455 switch ($notice->object_type) {
456 case Happening::OBJECT_TYPE:
457 common_log(LOG_DEBUG, "Deleting event from notice...");
458 $happening = Happening::fromNotice($notice);
459 $happening->delete();
464 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
465 $rsvp = RSVP::fromNotice($notice);
466 common_log(LOG_DEBUG, "to delete: $rsvp->id");
470 common_log(LOG_DEBUG, "Not deleting related, wtf...");
474 function onEndShowScripts($action)
476 $action->inlineScript('$(document).ready(function() { $("#startdate").datepicker(); $("#enddate").datepicker(); });');
479 function onEndShowStyles($action)
481 $action->cssLink($this->path('event.css'));