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';
91 case 'CancelRSVPForm':
92 include_once $dir . '/'.strtolower($cls).'.php';
96 include_once $dir . '/'.$cls.'.php';
104 * Map URLs to actions
106 * @param Net_URL_Mapper $m path-to-action mapper
108 * @return boolean hook value; true means continue processing, false means stop.
110 function onRouterInitialized($m)
112 $m->connect('main/event/new',
113 array('action' => 'newevent'));
114 $m->connect('main/event/rsvp',
115 array('action' => 'newrsvp'));
116 $m->connect('main/event/rsvp/cancel',
117 array('action' => 'cancelrsvp'));
118 $m->connect('event/:id',
119 array('action' => 'showevent'),
120 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
121 $m->connect('rsvp/:id',
122 array('action' => 'showrsvp'),
123 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
127 function onPluginVersion(&$versions)
129 $versions[] = array('name' => 'Event',
130 'version' => STATUSNET_VERSION,
131 'author' => 'Evan Prodromou',
132 'homepage' => 'http://status.net/wiki/Plugin:Event',
134 // TRANS: Plugin description.
135 _m('Event invitations and RSVPs.'));
139 function appTitle() {
140 // TRANS: Title for event application.
141 return _m('TITLE','Event');
149 return array(Happening::OBJECT_TYPE,
156 * Given a parsed ActivityStreams activity, save it into a notice
157 * and other data structures.
159 * @param Activity $activity
160 * @param Profile $actor
161 * @param array $options=array()
163 * @return Notice the resulting notice
165 function saveNoticeFromActivity($activity, $actor, $options=array())
167 if (count($activity->objects) != 1) {
168 // TRANS: Exception thrown when there are too many activity objects.
169 throw new Exception(_m('Too many activity objects.'));
172 $happeningObj = $activity->objects[0];
174 if ($happeningObj->type != Happening::OBJECT_TYPE) {
175 // TRANS: Exception thrown when event plugin comes across a non-event type object.
176 throw new Exception(_m('Wrong type for object.'));
181 switch ($activity->verb) {
182 case ActivityVerb::POST:
183 // FIXME: get startTime, endTime, location and URL
184 $notice = Happening::saveNew($actor,
187 $happeningObj->title,
189 $happeningObj->summary,
196 $happening = Happening::staticGet('uri', $happeningObj->id);
197 if (empty($happening)) {
198 // FIXME: save the event
199 // TRANS: Exception thrown when trying to RSVP for an unknown event.
200 throw new Exception(_m('RSVP for unknown event.'));
202 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
205 // TRANS: Exception thrown when event plugin comes across a undefined verb.
206 throw new Exception(_m('Unknown verb for events.'));
213 * Turn a Notice into an activity object
215 * @param Notice $notice
217 * @return ActivityObject
219 function activityObjectFromNotice($notice)
223 switch ($notice->object_type) {
224 case Happening::OBJECT_TYPE:
225 $happening = Happening::fromNotice($notice);
230 $rsvp = RSVP::fromNotice($notice);
231 $happening = $rsvp->getEvent();
235 if (empty($happening)) {
236 // TRANS: Exception thrown when event plugin comes across a unknown object type.
237 throw new Exception(_m('Unknown object type.'));
240 $notice = $happening->getNotice();
242 if (empty($notice)) {
243 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
244 throw new Exception(_m('Unknown event notice.'));
247 $obj = new ActivityObject();
249 $obj->id = $happening->uri;
250 $obj->type = Happening::OBJECT_TYPE;
251 $obj->title = $happening->title;
252 $obj->summary = $happening->description;
253 $obj->link = $notice->bestUrl();
255 // XXX: how to get this stuff into JSON?!
257 $obj->extra[] = array('dtstart',
258 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
259 common_date_iso8601($happening->start_time));
261 $obj->extra[] = array('dtend',
262 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
263 common_date_iso8601($happening->end_time));
265 // FIXME: add location
268 // XXX: probably need other stuff here
274 * Change the verb on RSVP notices
276 * @param Notice $notice
278 * @return ActivityObject
280 function onEndNoticeAsActivity($notice, &$act) {
281 switch ($notice->object_type) {
285 $act->verb = $notice->object_type;
291 function adaptNoticeListItem($nli)
293 $notice = $nli->notice;
295 switch ($notice->object_type) {
296 case Happening::OBJECT_TYPE:
297 return new EventListItem($nli);
302 return new RSVPListItem($nli);
312 * @param HTMLOutputter $out
315 function entryForm($out)
317 return new EventForm($out);
321 * When a notice is deleted, clean up related tables.
323 * @param Notice $notice
325 function deleteRelated($notice)
327 switch ($notice->object_type) {
328 case Happening::OBJECT_TYPE:
329 common_log(LOG_DEBUG, "Deleting event from notice...");
330 $happening = Happening::fromNotice($notice);
331 $happening->delete();
336 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
337 $rsvp = RSVP::fromNotice($notice);
338 common_log(LOG_DEBUG, "to delete: $rsvp->id");
342 common_log(LOG_DEBUG, "Not deleting related, wtf...");
346 function onEndShowScripts($action)
348 $action->inlineScript('$(document).ready(function() { $("#event-startdate").datepicker(); $("#event-enddate").datepicker(); });');
351 function onEndShowStyles($action)
353 $action->cssLink($this->path('event.css'));
357 function onStartAddNoticeReply($nli, $parent, $child)
359 // Filter out any poll responses
360 if (($parent->object_type == Happening::OBJECT_TYPE) &&
361 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {