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'));
96 function onPluginVersion(array &$versions)
98 $versions[] = array('name' => 'Event',
99 'version' => GNUSOCIAL_VERSION,
100 'author' => 'Evan Prodromou',
101 'homepage' => 'http://status.net/wiki/Plugin:Event',
103 // TRANS: Plugin description.
104 _m('Event invitations and RSVPs.'));
108 function appTitle() {
109 // TRANS: Title for event application.
110 return _m('TITLE','Event');
118 return array(Happening::OBJECT_TYPE,
125 * Given a parsed ActivityStreams activity, save it into a notice
126 * and other data structures.
128 * @param Activity $activity
129 * @param Profile $actor
130 * @param array $options=array()
132 * @return Notice the resulting notice
134 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
136 if (count($activity->objects) != 1) {
137 // TRANS: Exception thrown when there are too many activity objects.
138 throw new Exception(_m('Too many activity objects.'));
141 $happeningObj = $activity->objects[0];
143 if ($happeningObj->type != Happening::OBJECT_TYPE) {
144 // TRANS: Exception thrown when event plugin comes across a non-event type object.
145 throw new Exception(_m('Wrong type for object.'));
150 switch ($activity->verb) {
151 case ActivityVerb::POST:
152 // FIXME: get startTime, endTime, location and URL
153 $notice = Happening::saveNew($actor,
156 $happeningObj->title,
158 $happeningObj->summary,
165 $happening = Happening::getKV('uri', $happeningObj->id);
166 if (empty($happening)) {
167 // FIXME: save the event
168 // TRANS: Exception thrown when trying to RSVP for an unknown event.
169 throw new Exception(_m('RSVP for unknown event.'));
171 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
174 // TRANS: Exception thrown when event plugin comes across a undefined verb.
175 throw new Exception(_m('Unknown verb for events.'));
182 * Turn a Notice into an activity object
184 * @param Notice $notice
186 * @return ActivityObject
188 function activityObjectFromNotice(Notice $notice)
192 switch ($notice->object_type) {
193 case Happening::OBJECT_TYPE:
194 $happening = Happening::fromNotice($notice);
199 $rsvp = RSVP::fromNotice($notice);
200 $happening = $rsvp->getEvent();
204 if (empty($happening)) {
205 // TRANS: Exception thrown when event plugin comes across a unknown object type.
206 throw new Exception(_m('Unknown object type.'));
209 $notice = $happening->getNotice();
211 if (empty($notice)) {
212 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
213 throw new Exception(_m('Unknown event notice.'));
216 $obj = new ActivityObject();
218 $obj->id = $happening->uri;
219 $obj->type = Happening::OBJECT_TYPE;
220 $obj->title = $happening->title;
221 $obj->summary = $happening->description;
222 $obj->link = $notice->getUrl();
224 // XXX: how to get this stuff into JSON?!
226 $obj->extra[] = array('dtstart',
227 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
228 common_date_iso8601($happening->start_time));
230 $obj->extra[] = array('dtend',
231 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
232 common_date_iso8601($happening->end_time));
234 // FIXME: add location
237 // XXX: probably need other stuff here
243 * Change the verb on RSVP notices
245 * @param Notice $notice
247 * @return ActivityObject
249 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
250 switch ($stored->object_type) {
254 $act->verb = $stored->object_type;
263 * @param HTMLOutputter $out
266 function entryForm($out)
268 return new EventForm($out);
272 * When a notice is deleted, clean up related tables.
274 * @param Notice $notice
276 function deleteRelated(Notice $notice)
278 switch ($notice->object_type) {
279 case Happening::OBJECT_TYPE:
280 common_log(LOG_DEBUG, "Deleting event from notice...");
281 $happening = Happening::fromNotice($notice);
282 $happening->delete();
287 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
288 $rsvp = RSVP::fromNotice($notice);
289 common_log(LOG_DEBUG, "to delete: $rsvp->id");
293 common_log(LOG_DEBUG, "Not deleting related, wtf...");
297 function onEndShowScripts($action)
299 $action->script($this->path('js/event.js'));
302 function onEndShowStyles($action)
304 $action->cssLink($this->path('css/event.css'));
308 function onStartAddNoticeReply($nli, $parent, $child)
310 // Filter out any poll responses
311 if (($parent->object_type == Happening::OBJECT_TYPE) &&
312 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
318 protected function showNoticeItemNotice(NoticeListItem $nli)
324 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
326 switch ($stored->object_type) {
327 case Happening::OBJECT_TYPE:
328 $this->showEvent($stored, $out, $scoped);
333 $this->showRSVP($stored, $out, $scoped);
338 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
340 $profile = $stored->getProfile();
341 $event = Happening::fromNotice($stored);
343 if (!$event instanceof Happening) {
344 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
345 $out->element('p', null, _m('Deleted.'));
349 $out->elementStart('div', 'h-event');
351 $out->elementStart('h3', 'p-summary p-name');
354 $out->element('a', array('href' => $event->getUrl()), $event->title);
355 } catch (InvalidUrlException $e) {
356 $out->text($event->title);
359 $out->elementEnd('h3');
361 $now = new DateTime();
362 $startDate = new DateTime($event->start_time);
363 $endDate = new DateTime($event->end_time);
364 $userTz = new DateTimeZone(common_timezone());
366 // Localize the time for the observer
367 $now->setTimeZone($userTz);
368 $startDate->setTimezone($userTz);
369 $endDate->setTimezone($userTz);
371 $thisYear = $now->format('Y');
372 $startYear = $startDate->format('Y');
373 $endYear = $endDate->format('Y');
375 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
377 if ($startYear != $thisYear || $endYear != $thisYear) {
378 $dateFmt .= 'Y,'; // append year if we need to think about years
381 $startDateStr = $startDate->format($dateFmt);
382 $endDateStr = $endDate->format($dateFmt);
386 $startTimeStr = $startDate->format($timeFmt);
387 $endTimeStr = $endDate->format("{$timeFmt} (T)");
389 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
391 // TRANS: Field label for event description.
392 $out->element('strong', null, _m('Time:'));
394 $out->element('time', array('class' => 'dt-start',
395 'datetime' => common_date_iso8601($event->start_time)),
396 $startDateStr . ' ' . $startTimeStr);
398 $out->element('time', array('class' => 'dt-end',
399 'datetime' => common_date_iso8601($event->end_time)),
400 $startDateStr != $endDateStr
401 ? "$endDateStr $endTimeStr"
404 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
406 if (!empty($event->location)) {
407 $out->elementStart('div', 'event-location');
408 // TRANS: Field label for event description.
409 $out->element('strong', null, _m('Location:'));
410 $out->element('span', 'p-location', $event->location);
411 $out->elementEnd('div');
414 if (!empty($event->description)) {
415 $out->elementStart('div', 'event-description');
416 // TRANS: Field label for event description.
417 $out->element('strong', null, _m('Description:'));
418 $out->element('div', 'p-description', $event->description);
419 $out->elementEnd('div');
422 $rsvps = $event->getRSVPs();
424 $out->elementStart('div', 'event-rsvps');
426 // TRANS: Field label for event description.
427 $out->element('strong', null, _m('Attending:'));
428 $out->elementStart('ul', 'attending-list');
430 foreach ($rsvps as $verb => $responses) {
431 $out->elementStart('li', 'rsvp-list');
434 $out->text(_('Yes:'));
437 $out->text(_('No:'));
440 $out->text(_('Maybe:'));
444 foreach ($responses as $response) {
445 $ids[] = $response->profile_id;
447 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
448 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
451 $out->elementEnd('li');
454 $out->elementEnd('ul');
455 $out->elementEnd('div');
457 if ($scoped instanceof Profile) {
458 $rsvp = $event->getRSVP($scoped);
461 $form = new RSVPForm($event, $out);
463 $form = new CancelRSVPForm($rsvp, $out);
468 $out->elementEnd('div');
471 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
473 $rsvp = RSVP::fromNotice($stored);
476 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
477 $out->element('p', null, _m('Deleted.'));
481 $out->elementStart('div', 'rsvp');
482 $out->raw($rsvp->asHTML());
483 $out->elementEnd('div');