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.'));
148 $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
149 if($dtstart->length == 0) {
150 // TRANS: Exception thrown when has no start date
151 throw new Exception(_m('No start date for event.'));
154 $dtend = $happeningObj->element->getElementsByTagName('dtend');
155 if($dtend->length == 0) {
156 // TRANS: Exception thrown when has no end date
157 throw new Exception(_m('No end date for event.'));
162 switch ($activity->verb) {
163 case ActivityVerb::POST:
164 // FIXME: get startTime, endTime, location and URL
165 $notice = Happening::saveNew($actor,
166 $dtstart->item(0)->nodeValue,
167 $dtend->item(0)->nodeValue,
168 $happeningObj->title,
170 $happeningObj->summary,
177 $happening = Happening::getKV('uri', $happeningObj->id);
178 if (empty($happening)) {
179 // FIXME: save the event
180 // TRANS: Exception thrown when trying to RSVP for an unknown event.
181 throw new Exception(_m('RSVP for unknown event.'));
183 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
186 // TRANS: Exception thrown when event plugin comes across a undefined verb.
187 throw new Exception(_m('Unknown verb for events.'));
194 * Turn a Notice into an activity object
196 * @param Notice $notice
198 * @return ActivityObject
200 function activityObjectFromNotice(Notice $notice)
204 switch ($notice->object_type) {
205 case Happening::OBJECT_TYPE:
206 $happening = Happening::fromNotice($notice);
211 $rsvp = RSVP::fromNotice($notice);
212 $happening = $rsvp->getEvent();
216 if (empty($happening)) {
217 // TRANS: Exception thrown when event plugin comes across a unknown object type.
218 throw new Exception(_m('Unknown object type.'));
221 $notice = $happening->getNotice();
223 if (empty($notice)) {
224 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
225 throw new Exception(_m('Unknown event notice.'));
228 $obj = new ActivityObject();
230 $obj->id = $happening->uri;
231 $obj->type = Happening::OBJECT_TYPE;
232 $obj->title = $happening->title;
233 $obj->summary = $happening->description;
234 $obj->link = $notice->getUrl();
236 // XXX: how to get this stuff into JSON?!
238 $obj->extra[] = array('dtstart',
239 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
240 common_date_iso8601($happening->start_time));
242 $obj->extra[] = array('dtend',
243 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
244 common_date_iso8601($happening->end_time));
246 // FIXME: add location
249 // XXX: probably need other stuff here
255 * Change the verb on RSVP notices
257 * @param Notice $notice
259 * @return ActivityObject
261 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
262 switch ($stored->object_type) {
266 $act->verb = $stored->object_type;
275 * @param HTMLOutputter $out
278 function entryForm($out)
280 return new EventForm($out);
284 * When a notice is deleted, clean up related tables.
286 * @param Notice $notice
288 function deleteRelated(Notice $notice)
290 switch ($notice->object_type) {
291 case Happening::OBJECT_TYPE:
292 common_log(LOG_DEBUG, "Deleting event from notice...");
293 $happening = Happening::fromNotice($notice);
294 $happening->delete();
299 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
300 $rsvp = RSVP::fromNotice($notice);
301 common_log(LOG_DEBUG, "to delete: $rsvp->id");
305 common_log(LOG_DEBUG, "Not deleting related, wtf...");
309 function onEndShowScripts($action)
311 $action->script($this->path('js/event.js'));
314 function onEndShowStyles($action)
316 $action->cssLink($this->path('css/event.css'));
320 function onStartAddNoticeReply($nli, $parent, $child)
322 // Filter out any poll responses
323 if (($parent->object_type == Happening::OBJECT_TYPE) &&
324 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
330 protected function showNoticeItemNotice(NoticeListItem $nli)
336 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
338 switch ($stored->object_type) {
339 case Happening::OBJECT_TYPE:
340 $this->showEvent($stored, $out, $scoped);
345 $this->showRSVP($stored, $out, $scoped);
350 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
352 $profile = $stored->getProfile();
353 $event = Happening::fromNotice($stored);
355 if (!$event instanceof Happening) {
356 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
357 $out->element('p', null, _m('Deleted.'));
361 $out->elementStart('div', 'h-event');
363 $out->elementStart('h3', 'p-summary p-name');
366 $out->element('a', array('href' => $event->getUrl()), $event->title);
367 } catch (InvalidUrlException $e) {
368 $out->text($event->title);
371 $out->elementEnd('h3');
373 $now = new DateTime();
374 $startDate = new DateTime($event->start_time);
375 $endDate = new DateTime($event->end_time);
376 $userTz = new DateTimeZone(common_timezone());
378 // Localize the time for the observer
379 $now->setTimeZone($userTz);
380 $startDate->setTimezone($userTz);
381 $endDate->setTimezone($userTz);
383 $thisYear = $now->format('Y');
384 $startYear = $startDate->format('Y');
385 $endYear = $endDate->format('Y');
387 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
389 if ($startYear != $thisYear || $endYear != $thisYear) {
390 $dateFmt .= 'Y,'; // append year if we need to think about years
393 $startDateStr = $startDate->format($dateFmt);
394 $endDateStr = $endDate->format($dateFmt);
398 $startTimeStr = $startDate->format($timeFmt);
399 $endTimeStr = $endDate->format("{$timeFmt} (T)");
401 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
403 // TRANS: Field label for event description.
404 $out->element('strong', null, _m('Time:'));
406 $out->element('time', array('class' => 'dt-start',
407 'datetime' => common_date_iso8601($event->start_time)),
408 $startDateStr . ' ' . $startTimeStr);
410 $out->element('time', array('class' => 'dt-end',
411 'datetime' => common_date_iso8601($event->end_time)),
412 $startDateStr != $endDateStr
413 ? "$endDateStr $endTimeStr"
416 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
418 if (!empty($event->location)) {
419 $out->elementStart('div', 'event-location');
420 // TRANS: Field label for event description.
421 $out->element('strong', null, _m('Location:'));
422 $out->element('span', 'p-location', $event->location);
423 $out->elementEnd('div');
426 if (!empty($event->description)) {
427 $out->elementStart('div', 'event-description');
428 // TRANS: Field label for event description.
429 $out->element('strong', null, _m('Description:'));
430 $out->element('div', 'p-description', $event->description);
431 $out->elementEnd('div');
434 $rsvps = $event->getRSVPs();
436 $out->elementStart('div', 'event-rsvps');
438 // TRANS: Field label for event description.
439 $out->element('strong', null, _m('Attending:'));
440 $out->elementStart('ul', 'attending-list');
442 foreach ($rsvps as $verb => $responses) {
443 $out->elementStart('li', 'rsvp-list');
446 $out->text(_('Yes:'));
449 $out->text(_('No:'));
452 $out->text(_('Maybe:'));
456 foreach ($responses as $response) {
457 $ids[] = $response->profile_id;
459 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
460 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
463 $out->elementEnd('li');
466 $out->elementEnd('ul');
467 $out->elementEnd('div');
469 if ($scoped instanceof Profile) {
470 $rsvp = $event->getRSVP($scoped);
473 $form = new RSVPForm($event, $out);
475 $form = new CancelRSVPForm($rsvp, $out);
480 $out->elementEnd('div');
483 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
485 $rsvp = RSVP::fromNotice($stored);
488 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
489 $out->element('p', null, _m('Deleted.'));
493 $out->elementStart('div', 'rsvp');
494 $out->raw($rsvp->asHTML());
495 $out->elementEnd('div');